deque-typed 1.52.6 → 1.52.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants/index.d.ts +4 -0
- package/dist/constants/index.js +8 -0
- package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-element-base.js +10 -1
- package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/data-structures/binary-tree/binary-tree.js +668 -597
- package/dist/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/data-structures/binary-tree/bst.js +115 -113
- package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.js +7 -4
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +4 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/heap/heap.js +3 -3
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.js +4 -2
- package/dist/data-structures/queue/deque.d.ts +3 -3
- package/dist/data-structures/queue/deque.js +29 -29
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/stack/stack.d.ts +2 -2
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/package.json +2 -2
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +697 -725
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +2 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
|
@@ -37,7 +37,7 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
|
37
37
|
class RedBlackTree extends bst_1.BST {
|
|
38
38
|
/**
|
|
39
39
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
40
|
-
* @param
|
|
40
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
|
|
41
41
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
42
42
|
* initialize the RBTree with the provided elements.
|
|
43
43
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -45,11 +45,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
45
45
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
46
46
|
* depend on the implementation
|
|
47
47
|
*/
|
|
48
|
-
constructor(
|
|
48
|
+
constructor(keysOrNodesOrEntriesOrRaws = [], options) {
|
|
49
49
|
super([], options);
|
|
50
50
|
this._root = this.NIL;
|
|
51
|
-
if (
|
|
52
|
-
this.addMany(
|
|
51
|
+
if (keysOrNodesOrEntriesOrRaws) {
|
|
52
|
+
this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
@@ -83,20 +83,20 @@ class RedBlackTree extends bst_1.BST {
|
|
|
83
83
|
* @returns a new instance of a RedBlackTree object.
|
|
84
84
|
*/
|
|
85
85
|
createTree(options) {
|
|
86
|
-
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
86
|
+
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
89
|
* Time Complexity: O(1)
|
|
90
90
|
* Space Complexity: O(1)
|
|
91
91
|
*
|
|
92
92
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
93
|
-
* @param {
|
|
94
|
-
* `
|
|
95
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
93
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
94
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
95
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
96
96
|
* an instance of the `RedBlackTreeNode` class.
|
|
97
97
|
*/
|
|
98
|
-
isNode(
|
|
99
|
-
return
|
|
98
|
+
isNode(keyOrNodeOrEntryOrRaw) {
|
|
99
|
+
return keyOrNodeOrEntryOrRaw instanceof RedBlackTreeNode;
|
|
100
100
|
}
|
|
101
101
|
// /**
|
|
102
102
|
// * Time Complexity: O(1)
|
|
@@ -109,27 +109,27 @@ class RedBlackTree extends bst_1.BST {
|
|
|
109
109
|
// *
|
|
110
110
|
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
|
|
111
111
|
// * valid, otherwise it returns undefined.
|
|
112
|
-
// * @param {BTNKeyOrNodeOrEntry<K, V, NODE>}
|
|
113
|
-
// * @param {V} [value] - The value associated with the key (if `
|
|
112
|
+
// * @param {BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRaw - The key, value, or entry to convert.
|
|
113
|
+
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRaw` is a key).
|
|
114
114
|
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
115
115
|
// */
|
|
116
|
-
// override keyValueOrEntryOrRawElementToNode(
|
|
116
|
+
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): NODE | undefined {
|
|
117
117
|
//
|
|
118
|
-
// if (
|
|
119
|
-
// if (this.isNode(
|
|
118
|
+
// if (keyOrNodeOrEntryOrRaw === null || keyOrNodeOrEntryOrRaw === undefined) return;
|
|
119
|
+
// if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
|
|
120
120
|
//
|
|
121
|
-
// if (this.
|
|
122
|
-
// const [key, entryValue] = this.
|
|
121
|
+
// if (this._toEntryFn) {
|
|
122
|
+
// const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
|
|
123
123
|
// if (this.isKey(key)) return this.createNode(key, entryValue ?? value, 'RED');
|
|
124
124
|
// }
|
|
125
125
|
//
|
|
126
|
-
// if (this.isEntry(
|
|
127
|
-
// const [key, value] =
|
|
126
|
+
// if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
127
|
+
// const [key, value] = keyOrNodeOrEntryOrRaw;
|
|
128
128
|
// if (key === undefined || key === null) return;
|
|
129
129
|
// else return this.createNode(key, value, 'RED');
|
|
130
130
|
// }
|
|
131
131
|
//
|
|
132
|
-
// if (this.isKey(
|
|
132
|
+
// if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.createNode(keyOrNodeOrEntryOrRaw, value, 'RED');
|
|
133
133
|
//
|
|
134
134
|
// return ;
|
|
135
135
|
// }
|
|
@@ -150,8 +150,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
150
150
|
*
|
|
151
151
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
152
152
|
* added.
|
|
153
|
-
* @param {
|
|
154
|
-
* `
|
|
153
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
154
|
+
* `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
155
155
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
156
156
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
157
157
|
* structure.
|
|
@@ -159,8 +159,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
159
159
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
160
160
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
161
161
|
*/
|
|
162
|
-
add(
|
|
163
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
162
|
+
add(keyOrNodeOrEntryOrRaw, value) {
|
|
163
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
|
|
164
164
|
if (!this.isRealNode(newNode))
|
|
165
165
|
return false;
|
|
166
166
|
const insertStatus = this._insert(newNode);
|
|
@@ -182,24 +182,25 @@ class RedBlackTree extends bst_1.BST {
|
|
|
182
182
|
* Time Complexity: O(log n)
|
|
183
183
|
* Space Complexity: O(1)
|
|
184
184
|
*
|
|
185
|
-
* The function overrides the delete method
|
|
186
|
-
*
|
|
187
|
-
* @param {
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* @
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
* that extends the `BTNCallback
|
|
195
|
-
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
185
|
+
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
186
|
+
* a given predicate and maintain the binary search tree properties.
|
|
187
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
188
|
+
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
189
|
+
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
190
|
+
* function that determines which node(s) should be deleted.
|
|
191
|
+
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<NODE>`
|
|
192
|
+
* objects. Each object in the array contains information about the deleted node and whether
|
|
193
|
+
* balancing is needed.
|
|
196
194
|
*/
|
|
197
|
-
delete(
|
|
198
|
-
if (
|
|
195
|
+
delete(predicate) {
|
|
196
|
+
if (predicate === null)
|
|
199
197
|
return [];
|
|
200
198
|
const results = [];
|
|
201
|
-
|
|
202
|
-
|
|
199
|
+
let nodeToDelete;
|
|
200
|
+
if (this._isPredicated(predicate))
|
|
201
|
+
nodeToDelete = this.getNode(predicate);
|
|
202
|
+
else
|
|
203
|
+
nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate);
|
|
203
204
|
if (!nodeToDelete) {
|
|
204
205
|
return results;
|
|
205
206
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { SegmentTreeNodeVal } from '../../types';
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* data-structure-typed
|
|
4
4
|
*
|
|
5
|
-
* @author
|
|
6
|
-
* @copyright Copyright (c) 2022
|
|
5
|
+
* @author Pablo Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
7
|
* @license MIT License
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BSTNKeyOrNode,
|
|
9
|
-
import { BTNEntry } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNKeyOrNodeOrEntry, BTNPredicate, IterationType, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions, BTNEntry } from '../../types';
|
|
10
9
|
import { IBinaryTree } from '../../interfaces';
|
|
11
10
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
12
11
|
export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
|
|
@@ -39,14 +38,14 @@ export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMa
|
|
|
39
38
|
export declare class TreeMultiMap<K = any, V = any, R = BTNEntry<K, V>, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>> extends RedBlackTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
40
39
|
/**
|
|
41
40
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
42
|
-
* @param
|
|
41
|
+
* @param keysOrNodesOrEntriesOrRaws - The parameter `keysOrNodesOrEntriesOrRaws` is an
|
|
43
42
|
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
|
|
44
43
|
* TreeMultiMap with initial data.
|
|
45
44
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
46
45
|
* behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
|
|
47
46
|
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
48
47
|
*/
|
|
49
|
-
constructor(
|
|
48
|
+
constructor(keysOrNodesOrEntriesOrRaws?: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>>, options?: TreeMultiMapOptions<K, V, R>);
|
|
50
49
|
protected _count: number;
|
|
51
50
|
/**
|
|
52
51
|
* The function calculates the sum of the count property of all nodes in a tree structure.
|
|
@@ -88,8 +87,8 @@ export declare class TreeMultiMap<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
88
87
|
/**
|
|
89
88
|
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
|
|
90
89
|
* node based on the input.
|
|
91
|
-
* @param {
|
|
92
|
-
* `
|
|
90
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
91
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
93
92
|
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
94
93
|
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
95
94
|
* an existing node.
|
|
@@ -97,23 +96,23 @@ export declare class TreeMultiMap<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
97
96
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
98
97
|
* @returns either a NODE object or undefined.
|
|
99
98
|
*/
|
|
100
|
-
keyValueOrEntryOrRawElementToNode(
|
|
99
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V, count?: number): NODE | undefined;
|
|
101
100
|
/**
|
|
102
101
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
103
|
-
* @param {
|
|
104
|
-
* `
|
|
105
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
102
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
103
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
104
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
106
105
|
* an instance of the `TreeMultiMapNode` class.
|
|
107
106
|
*/
|
|
108
|
-
isNode(
|
|
107
|
+
isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE;
|
|
109
108
|
/**
|
|
110
109
|
* Time Complexity: O(log n)
|
|
111
110
|
* Space Complexity: O(1)
|
|
112
111
|
*
|
|
113
112
|
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
114
113
|
* the count and returning a boolean indicating success.
|
|
115
|
-
* @param {
|
|
116
|
-
* `
|
|
114
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
|
|
115
|
+
* `keyOrNodeOrEntryOrRaw` parameter can accept one of the following types:
|
|
117
116
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
118
117
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
119
118
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
@@ -122,26 +121,24 @@ export declare class TreeMultiMap<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
122
121
|
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
123
122
|
* was successful, and false otherwise.
|
|
124
123
|
*/
|
|
125
|
-
add(
|
|
124
|
+
add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V, count?: number): boolean;
|
|
126
125
|
/**
|
|
127
126
|
* Time Complexity: O(log n)
|
|
128
127
|
* Space Complexity: O(1)
|
|
129
128
|
*
|
|
130
|
-
* The function `delete`
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* the
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
*/
|
|
144
|
-
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<NODE>[];
|
|
129
|
+
* The function `delete` in TypeScript overrides the deletion operation in a binary tree data
|
|
130
|
+
* structure, handling cases where nodes have children and maintaining balance in the tree.
|
|
131
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
132
|
+
* parameter in the `delete` method is used to specify the condition or key based on which a node
|
|
133
|
+
* should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
134
|
+
* function.
|
|
135
|
+
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
136
|
+
* boolean flag that determines whether to ignore the count of nodes when performing deletion. If
|
|
137
|
+
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
|
|
138
|
+
* `ignoreCount` is `false
|
|
139
|
+
* @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
|
|
140
|
+
*/
|
|
141
|
+
delete(predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>, ignoreCount?: boolean): BinaryTreeDeleteResult<NODE>[];
|
|
145
142
|
/**
|
|
146
143
|
* Time Complexity: O(1)
|
|
147
144
|
* Space Complexity: O(1)
|
|
@@ -40,18 +40,18 @@ exports.TreeMultiMapNode = TreeMultiMapNode;
|
|
|
40
40
|
class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
41
41
|
/**
|
|
42
42
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
43
|
-
* @param
|
|
43
|
+
* @param keysOrNodesOrEntriesOrRaws - The parameter `keysOrNodesOrEntriesOrRaws` is an
|
|
44
44
|
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
|
|
45
45
|
* TreeMultiMap with initial data.
|
|
46
46
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
47
47
|
* behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
|
|
48
48
|
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
49
49
|
*/
|
|
50
|
-
constructor(
|
|
50
|
+
constructor(keysOrNodesOrEntriesOrRaws = [], options) {
|
|
51
51
|
super([], options);
|
|
52
52
|
this._count = 0;
|
|
53
|
-
if (
|
|
54
|
-
this.addMany(
|
|
53
|
+
if (keysOrNodesOrEntriesOrRaws)
|
|
54
|
+
this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
55
55
|
}
|
|
56
56
|
// TODO the _count is not accurate after nodes count modified
|
|
57
57
|
/**
|
|
@@ -99,13 +99,13 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
99
99
|
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
100
100
|
*/
|
|
101
101
|
createTree(options) {
|
|
102
|
-
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType }, options));
|
|
102
|
+
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
105
|
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
|
|
106
106
|
* node based on the input.
|
|
107
|
-
* @param {
|
|
108
|
-
* `
|
|
107
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
108
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
109
109
|
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
110
110
|
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
111
111
|
* an existing node.
|
|
@@ -113,36 +113,36 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
113
113
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
114
114
|
* @returns either a NODE object or undefined.
|
|
115
115
|
*/
|
|
116
|
-
keyValueOrEntryOrRawElementToNode(
|
|
117
|
-
if (
|
|
116
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value, count = 1) {
|
|
117
|
+
if (keyOrNodeOrEntryOrRaw === undefined || keyOrNodeOrEntryOrRaw === null)
|
|
118
118
|
return;
|
|
119
|
-
if (this.isNode(
|
|
120
|
-
return
|
|
121
|
-
if (this.isEntry(
|
|
122
|
-
const [key, entryValue] =
|
|
119
|
+
if (this.isNode(keyOrNodeOrEntryOrRaw))
|
|
120
|
+
return keyOrNodeOrEntryOrRaw;
|
|
121
|
+
if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
122
|
+
const [key, entryValue] = keyOrNodeOrEntryOrRaw;
|
|
123
123
|
if (key === undefined || key === null)
|
|
124
124
|
return;
|
|
125
125
|
if (this.isKey(key))
|
|
126
126
|
return this.createNode(key, value !== null && value !== void 0 ? value : entryValue, 'BLACK', count);
|
|
127
127
|
}
|
|
128
|
-
if (this.
|
|
129
|
-
const [key, entryValue] = this.
|
|
128
|
+
if (this._toEntryFn) {
|
|
129
|
+
const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw);
|
|
130
130
|
if (this.isKey(key))
|
|
131
131
|
return this.createNode(key, value !== null && value !== void 0 ? value : entryValue, 'BLACK', count);
|
|
132
132
|
}
|
|
133
|
-
if (this.isKey(
|
|
134
|
-
return this.createNode(
|
|
133
|
+
if (this.isKey(keyOrNodeOrEntryOrRaw))
|
|
134
|
+
return this.createNode(keyOrNodeOrEntryOrRaw, value, 'BLACK', count);
|
|
135
135
|
return;
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
138
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
139
|
-
* @param {
|
|
140
|
-
* `
|
|
141
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
139
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
140
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
141
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
142
142
|
* an instance of the `TreeMultiMapNode` class.
|
|
143
143
|
*/
|
|
144
|
-
isNode(
|
|
145
|
-
return
|
|
144
|
+
isNode(keyOrNodeOrEntryOrRaw) {
|
|
145
|
+
return keyOrNodeOrEntryOrRaw instanceof TreeMultiMapNode;
|
|
146
146
|
}
|
|
147
147
|
/**
|
|
148
148
|
* Time Complexity: O(log n)
|
|
@@ -150,8 +150,8 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
150
150
|
*
|
|
151
151
|
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
152
152
|
* the count and returning a boolean indicating success.
|
|
153
|
-
* @param {
|
|
154
|
-
* `
|
|
153
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
|
|
154
|
+
* `keyOrNodeOrEntryOrRaw` parameter can accept one of the following types:
|
|
155
155
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
156
156
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
157
157
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
@@ -160,8 +160,8 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
160
160
|
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
161
161
|
* was successful, and false otherwise.
|
|
162
162
|
*/
|
|
163
|
-
add(
|
|
164
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
163
|
+
add(keyOrNodeOrEntryOrRaw, value, count = 1) {
|
|
164
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value, count);
|
|
165
165
|
const orgCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
166
166
|
const isSuccessAdded = super.add(newNode);
|
|
167
167
|
if (isSuccessAdded) {
|
|
@@ -176,26 +176,27 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
176
176
|
* Time Complexity: O(log n)
|
|
177
177
|
* Space Complexity: O(1)
|
|
178
178
|
*
|
|
179
|
-
* The function `delete`
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
* the
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
* it. If set to false, the count of the node will be decremented by 1 before deleting it.
|
|
191
|
-
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
179
|
+
* The function `delete` in TypeScript overrides the deletion operation in a binary tree data
|
|
180
|
+
* structure, handling cases where nodes have children and maintaining balance in the tree.
|
|
181
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
182
|
+
* parameter in the `delete` method is used to specify the condition or key based on which a node
|
|
183
|
+
* should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
184
|
+
* function.
|
|
185
|
+
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
186
|
+
* boolean flag that determines whether to ignore the count of nodes when performing deletion. If
|
|
187
|
+
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
|
|
188
|
+
* `ignoreCount` is `false
|
|
189
|
+
* @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
|
|
192
190
|
*/
|
|
193
|
-
delete(
|
|
194
|
-
if (
|
|
191
|
+
delete(predicate, ignoreCount = false) {
|
|
192
|
+
if (predicate === null)
|
|
195
193
|
return [];
|
|
196
194
|
const results = [];
|
|
197
|
-
|
|
198
|
-
|
|
195
|
+
let nodeToDelete;
|
|
196
|
+
if (this._isPredicated(predicate))
|
|
197
|
+
nodeToDelete = this.getNode(predicate);
|
|
198
|
+
else
|
|
199
|
+
nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate);
|
|
199
200
|
if (!nodeToDelete) {
|
|
200
201
|
return results;
|
|
201
202
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { DijkstraResult, EntryCallback, VertexKey } from '../../types';
|
|
@@ -461,7 +461,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
461
461
|
}
|
|
462
462
|
}
|
|
463
463
|
}
|
|
464
|
-
getMinDist
|
|
464
|
+
if (getMinDist)
|
|
465
465
|
distMap.forEach((d, v) => {
|
|
466
466
|
if (v !== srcVertex) {
|
|
467
467
|
if (d < minDist) {
|
|
@@ -471,7 +471,8 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
471
471
|
}
|
|
472
472
|
}
|
|
473
473
|
});
|
|
474
|
-
|
|
474
|
+
if (genPaths)
|
|
475
|
+
getPaths(minDest);
|
|
475
476
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
476
477
|
}
|
|
477
478
|
/**
|
|
@@ -644,7 +645,8 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
644
645
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
645
646
|
if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
|
|
646
647
|
distMap.set(d, sWeight + weight);
|
|
647
|
-
|
|
648
|
+
if (genPath)
|
|
649
|
+
preMap.set(d, s);
|
|
648
650
|
}
|
|
649
651
|
}
|
|
650
652
|
}
|
|
@@ -769,7 +771,8 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
769
771
|
visited.add(vertex);
|
|
770
772
|
currentPath.push(vertex.key);
|
|
771
773
|
for (const neighbor of this.getNeighbors(vertex)) {
|
|
772
|
-
|
|
774
|
+
if (neighbor)
|
|
775
|
+
dfs(neighbor, currentPath, visited);
|
|
773
776
|
}
|
|
774
777
|
visited.delete(vertex);
|
|
775
778
|
currentPath.pop();
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { VertexKey } from '../../types';
|
|
@@ -220,8 +220,10 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
220
220
|
if (v1 && v2) {
|
|
221
221
|
const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
|
|
222
222
|
const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
|
|
223
|
-
|
|
224
|
-
|
|
223
|
+
if (v1ToV2)
|
|
224
|
+
removed.push(v1ToV2);
|
|
225
|
+
if (v2ToV1)
|
|
226
|
+
removed.push(v2ToV1);
|
|
225
227
|
}
|
|
226
228
|
return removed;
|
|
227
229
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { VertexKey } from '../../types';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem, LinkedHashMapOptions } from '../../types';
|
|
@@ -88,7 +88,7 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
88
88
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
89
89
|
*/
|
|
90
90
|
isEmpty() {
|
|
91
|
-
return this.
|
|
91
|
+
return this._size === 0;
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
94
94
|
* The clear() function resets the state of an object by clearing its internal store, object map, and
|
|
@@ -573,7 +573,7 @@ class FibonacciHeap {
|
|
|
573
573
|
* @returns The top element or undefined if the heap is empty.
|
|
574
574
|
*/
|
|
575
575
|
pop() {
|
|
576
|
-
if (this.
|
|
576
|
+
if (this._size === 0)
|
|
577
577
|
return undefined;
|
|
578
578
|
const z = this.min;
|
|
579
579
|
if (z.child) {
|
|
@@ -706,7 +706,7 @@ class FibonacciHeap {
|
|
|
706
706
|
* @protected
|
|
707
707
|
*/
|
|
708
708
|
_consolidate() {
|
|
709
|
-
const A = new Array(this.
|
|
709
|
+
const A = new Array(this._size);
|
|
710
710
|
const elements = this.consumeLinkedList(this.root);
|
|
711
711
|
let x, y, d, t;
|
|
712
712
|
for (const node of elements) {
|
|
@@ -725,7 +725,7 @@ class FibonacciHeap {
|
|
|
725
725
|
}
|
|
726
726
|
A[d] = x;
|
|
727
727
|
}
|
|
728
|
-
for (let i = 0; i < this.
|
|
728
|
+
for (let i = 0; i < this._size; i++) {
|
|
729
729
|
if (A[i] && this.comparator(A[i].element, this.min.element) <= 0) {
|
|
730
730
|
this._min = A[i];
|
|
731
731
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { DoublyLinkedListOptions, ElementCallback } from '../../types';
|