heap-typed 1.49.8 → 1.50.0
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/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/data-structures/binary-tree/bst.js +131 -46
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/data-structures/hash/hash-map.d.ts +16 -12
- package/dist/data-structures/hash/hash-map.js +36 -15
- package/dist/types/data-structures/hash/hash-map.d.ts +2 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +0 -10
- package/src/data-structures/binary-tree/binary-tree.ts +104 -141
- package/src/data-structures/binary-tree/bst.ts +153 -49
- package/src/data-structures/binary-tree/rb-tree.ts +1 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +2 -12
- package/src/data-structures/hash/hash-map.ts +42 -16
- package/src/types/data-structures/hash/hash-map.ts +2 -1
|
@@ -56,13 +56,6 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
56
56
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
57
57
|
*/
|
|
58
58
|
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
|
|
59
|
-
/**
|
|
60
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
61
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
62
|
-
* data type.
|
|
63
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
64
|
-
*/
|
|
65
|
-
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
66
59
|
/**
|
|
67
60
|
* Time Complexity: O(log n)
|
|
68
61
|
* Space Complexity: O(1)
|
|
@@ -70,15 +70,6 @@ class AVLTree extends bst_1.BST {
|
|
|
70
70
|
isNode(keyOrNodeOrEntry) {
|
|
71
71
|
return keyOrNodeOrEntry instanceof AVLTreeNode;
|
|
72
72
|
}
|
|
73
|
-
/**
|
|
74
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
75
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
76
|
-
* data type.
|
|
77
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
78
|
-
*/
|
|
79
|
-
isNotNodeInstance(potentialKey) {
|
|
80
|
-
return !(potentialKey instanceof AVLTreeNode);
|
|
81
|
-
}
|
|
82
73
|
/**
|
|
83
74
|
* Time Complexity: O(log n)
|
|
84
75
|
* Space Complexity: O(1)
|
|
@@ -135,13 +135,6 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
135
135
|
* @returns a boolean value.
|
|
136
136
|
*/
|
|
137
137
|
isNodeOrNull(node: KeyOrNodeOrEntry<K, V, N>): node is N | null;
|
|
138
|
-
/**
|
|
139
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
140
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
141
|
-
* data type.
|
|
142
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
143
|
-
*/
|
|
144
|
-
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
145
138
|
/**
|
|
146
139
|
* Time Complexity O(log n) - O(n)
|
|
147
140
|
* Space Complexity O(1)
|
|
@@ -344,7 +337,7 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
344
337
|
*
|
|
345
338
|
* The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
|
|
346
339
|
* structure, with the option to reverse the order of the nodes.
|
|
347
|
-
* @param {K | N | null | undefined}
|
|
340
|
+
* @param {K | N | null | undefined} beginNode - The `beginRoot` parameter represents the
|
|
348
341
|
* starting node from which you want to find the path to the root. It can be of type `K`, `N`,
|
|
349
342
|
* `null`, or `undefined`.
|
|
350
343
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
@@ -352,7 +345,7 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
352
345
|
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
|
353
346
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
354
347
|
*/
|
|
355
|
-
getPathToRoot(
|
|
348
|
+
getPathToRoot(beginNode: KeyOrNodeOrEntry<K, V, N>, isReverse?: boolean): N[];
|
|
356
349
|
/**
|
|
357
350
|
* Time Complexity: O(log n)
|
|
358
351
|
* Space Complexity: O(1)
|
|
@@ -409,30 +402,20 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
409
402
|
* @returns a boolean value.
|
|
410
403
|
*/
|
|
411
404
|
isBST(beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): boolean;
|
|
412
|
-
/**
|
|
413
|
-
* Time complexity: O(n)
|
|
414
|
-
* Space complexity: O(log n)
|
|
415
|
-
*/
|
|
416
|
-
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
417
|
-
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
|
|
418
|
-
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
419
405
|
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
420
|
-
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?:
|
|
421
|
-
dfs<C extends BTNCallback<N | null | undefined>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
406
|
+
dfs<C extends BTNCallback<N | null>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
422
407
|
/**
|
|
423
408
|
* Time complexity: O(n)
|
|
424
409
|
* Space complexity: O(n)
|
|
425
410
|
*/
|
|
426
411
|
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
427
|
-
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?:
|
|
428
|
-
bfs<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
412
|
+
bfs<C extends BTNCallback<N | null>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
429
413
|
/**
|
|
430
414
|
* Time complexity: O(n)
|
|
431
415
|
* Space complexity: O(n)
|
|
432
416
|
*/
|
|
433
417
|
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
|
|
434
|
-
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?:
|
|
435
|
-
listLevels<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
|
|
418
|
+
listLevels<C extends BTNCallback<N | null>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
|
|
436
419
|
/**
|
|
437
420
|
* Time Complexity: O(log n)
|
|
438
421
|
* Space Complexity: O(1)
|
|
@@ -150,7 +150,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
150
150
|
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
151
151
|
node = keyOrNodeOrEntry;
|
|
152
152
|
}
|
|
153
|
-
else if (this.
|
|
153
|
+
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
154
154
|
node = this.createNode(keyOrNodeOrEntry, value);
|
|
155
155
|
}
|
|
156
156
|
else {
|
|
@@ -241,15 +241,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
241
241
|
isNodeOrNull(node) {
|
|
242
242
|
return this.isRealNode(node) || node === null;
|
|
243
243
|
}
|
|
244
|
-
/**
|
|
245
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
246
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
247
|
-
* data type.
|
|
248
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
249
|
-
*/
|
|
250
|
-
isNotNodeInstance(potentialKey) {
|
|
251
|
-
return !(potentialKey instanceof BinaryTreeNode);
|
|
252
|
-
}
|
|
253
244
|
/**
|
|
254
245
|
* Time Complexity O(log n) - O(n)
|
|
255
246
|
* Space Complexity O(1)
|
|
@@ -811,7 +802,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
811
802
|
*
|
|
812
803
|
* The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
|
|
813
804
|
* structure, with the option to reverse the order of the nodes.
|
|
814
|
-
* @param {K | N | null | undefined}
|
|
805
|
+
* @param {K | N | null | undefined} beginNode - The `beginRoot` parameter represents the
|
|
815
806
|
* starting node from which you want to find the path to the root. It can be of type `K`, `N`,
|
|
816
807
|
* `null`, or `undefined`.
|
|
817
808
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
@@ -819,19 +810,19 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
819
810
|
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
|
820
811
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
821
812
|
*/
|
|
822
|
-
getPathToRoot(
|
|
813
|
+
getPathToRoot(beginNode, isReverse = true) {
|
|
823
814
|
// TODO to support get path through passing key
|
|
824
815
|
const result = [];
|
|
825
|
-
|
|
826
|
-
if (!
|
|
816
|
+
beginNode = this.ensureNode(beginNode);
|
|
817
|
+
if (!beginNode)
|
|
827
818
|
return result;
|
|
828
|
-
while (
|
|
819
|
+
while (beginNode.parent) {
|
|
829
820
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
830
821
|
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
831
|
-
result.push(
|
|
832
|
-
|
|
822
|
+
result.push(beginNode);
|
|
823
|
+
beginNode = beginNode.parent;
|
|
833
824
|
}
|
|
834
|
-
result.push(
|
|
825
|
+
result.push(beginNode);
|
|
835
826
|
return isReverse ? result.reverse() : result;
|
|
836
827
|
}
|
|
837
828
|
/**
|
|
@@ -974,68 +965,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
974
965
|
return isStandardBST || isInverseBST;
|
|
975
966
|
}
|
|
976
967
|
}
|
|
977
|
-
/**
|
|
978
|
-
* Time complexity: O(n)
|
|
979
|
-
* Space complexity: O(log n)
|
|
980
|
-
*
|
|
981
|
-
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
982
|
-
* node, either recursively or iteratively.
|
|
983
|
-
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
984
|
-
* the subtree traversal. It takes a single parameter, which is the current node being traversed, and
|
|
985
|
-
* returns a value of any type.
|
|
986
|
-
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
987
|
-
* starting node or key from which the subtree traversal should begin. It can be of type `K`,
|
|
988
|
-
* `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
|
|
989
|
-
* the default value.
|
|
990
|
-
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
991
|
-
* performed on the subtree. It can have two possible values:
|
|
992
|
-
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
993
|
-
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
994
|
-
* traversal will include null values, otherwise it will skip them.
|
|
995
|
-
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
996
|
-
* the `callback` function on each node in the subtree. The type of the array nodes is determined
|
|
997
|
-
* by the return type of the `callback` function.
|
|
998
|
-
*/
|
|
999
|
-
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1000
|
-
beginRoot = this.ensureNode(beginRoot);
|
|
1001
|
-
const ans = [];
|
|
1002
|
-
if (!beginRoot)
|
|
1003
|
-
return ans;
|
|
1004
|
-
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
1005
|
-
const _traverse = (cur) => {
|
|
1006
|
-
if (cur !== undefined) {
|
|
1007
|
-
ans.push(callback(cur));
|
|
1008
|
-
if (includeNull) {
|
|
1009
|
-
cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
|
|
1010
|
-
cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
|
|
1011
|
-
}
|
|
1012
|
-
else {
|
|
1013
|
-
cur && cur.left && _traverse(cur.left);
|
|
1014
|
-
cur && cur.right && _traverse(cur.right);
|
|
1015
|
-
}
|
|
1016
|
-
}
|
|
1017
|
-
};
|
|
1018
|
-
_traverse(beginRoot);
|
|
1019
|
-
}
|
|
1020
|
-
else {
|
|
1021
|
-
const stack = [beginRoot];
|
|
1022
|
-
while (stack.length > 0) {
|
|
1023
|
-
const cur = stack.pop();
|
|
1024
|
-
if (cur !== undefined) {
|
|
1025
|
-
ans.push(callback(cur));
|
|
1026
|
-
if (includeNull) {
|
|
1027
|
-
cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
|
|
1028
|
-
cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
|
|
1029
|
-
}
|
|
1030
|
-
else {
|
|
1031
|
-
cur && cur.right && stack.push(cur.right);
|
|
1032
|
-
cur && cur.left && stack.push(cur.left);
|
|
1033
|
-
}
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
return ans;
|
|
1038
|
-
}
|
|
1039
968
|
/**
|
|
1040
969
|
* Time complexity: O(n)
|
|
1041
970
|
* Space complexity: O(n)
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
|
-
import { BSTVariant, CP, IterationType } from '../../types';
|
|
9
|
+
import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, N> {
|
|
@@ -100,13 +100,6 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
100
100
|
* @returns either a node object (N) or undefined.
|
|
101
101
|
*/
|
|
102
102
|
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | undefined;
|
|
103
|
-
/**
|
|
104
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
105
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
106
|
-
* data type.
|
|
107
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
108
|
-
*/
|
|
109
|
-
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
110
103
|
/**
|
|
111
104
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
112
105
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
@@ -157,25 +150,6 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
157
150
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
158
151
|
*/
|
|
159
152
|
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
160
|
-
/**
|
|
161
|
-
* Time Complexity: O(n log n)
|
|
162
|
-
* Space Complexity: O(n)
|
|
163
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
164
|
-
*/
|
|
165
|
-
/**
|
|
166
|
-
* Time Complexity: O(n log n)
|
|
167
|
-
* Space Complexity: O(n)
|
|
168
|
-
*
|
|
169
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
170
|
-
* leftmost node if the comparison result is greater than.
|
|
171
|
-
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
172
|
-
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
173
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
174
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
175
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
176
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
177
|
-
*/
|
|
178
|
-
lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, N>): K | undefined;
|
|
179
153
|
/**
|
|
180
154
|
* Time Complexity: O(log n)
|
|
181
155
|
* Space Complexity: O(1)
|
|
@@ -225,6 +199,94 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
225
199
|
* @returns The method returns an array of nodes (`N[]`).
|
|
226
200
|
*/
|
|
227
201
|
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N[];
|
|
202
|
+
/**
|
|
203
|
+
* Time complexity: O(n)
|
|
204
|
+
* Space complexity: O(n)
|
|
205
|
+
*/
|
|
206
|
+
/**
|
|
207
|
+
* Time complexity: O(n)
|
|
208
|
+
* Space complexity: O(n)
|
|
209
|
+
*
|
|
210
|
+
* The function overrides the depth-first search method and returns an array of the return types of
|
|
211
|
+
* the callback function.
|
|
212
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
213
|
+
* during the depth-first search traversal. It is an optional parameter and if not provided, a
|
|
214
|
+
* default callback function will be used.
|
|
215
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
|
|
216
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
217
|
+
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
|
|
218
|
+
* Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
|
|
219
|
+
* value is provided, the DFS traversal will start from the root of the tree.
|
|
220
|
+
* @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
|
|
221
|
+
* iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
222
|
+
* following values:
|
|
223
|
+
* @returns The method is returning an array of the return type of the callback function.
|
|
224
|
+
*/
|
|
225
|
+
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[];
|
|
226
|
+
/**
|
|
227
|
+
* Time complexity: O(n)
|
|
228
|
+
* Space complexity: O(n)
|
|
229
|
+
*/
|
|
230
|
+
/**
|
|
231
|
+
* Time complexity: O(n)
|
|
232
|
+
* Space complexity: O(n)
|
|
233
|
+
*
|
|
234
|
+
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
235
|
+
* the callback function.
|
|
236
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
237
|
+
* visited during the breadth-first search traversal. It is an optional parameter and if not
|
|
238
|
+
* provided, a default callback function will be used.
|
|
239
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
|
|
240
|
+
* traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
|
|
241
|
+
* the tree is used as the starting point.
|
|
242
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
243
|
+
* be performed during the breadth-first search (BFS) traversal. It determines the order in which the
|
|
244
|
+
* nodes are visited.
|
|
245
|
+
* @returns The method is returning an array of the return type of the callback function.
|
|
246
|
+
*/
|
|
247
|
+
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[];
|
|
248
|
+
/**
|
|
249
|
+
* Time complexity: O(n)
|
|
250
|
+
* Space complexity: O(n)
|
|
251
|
+
*/
|
|
252
|
+
/**
|
|
253
|
+
* Time complexity: O(n)
|
|
254
|
+
* Space complexity: O(n)
|
|
255
|
+
*
|
|
256
|
+
* The function overrides the listLevels method and returns an array of arrays containing the return
|
|
257
|
+
* type of the callback function for each level of the tree.
|
|
258
|
+
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
259
|
+
* `BTNCallback<N>`. It represents a callback function that will be called for each node in the tree
|
|
260
|
+
* during the level listing process.
|
|
261
|
+
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
|
|
262
|
+
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
|
|
263
|
+
* provided, the root of the binary tree is used as the starting point.
|
|
264
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
265
|
+
* be performed on the tree. It determines the order in which the nodes are visited during the
|
|
266
|
+
* iteration.
|
|
267
|
+
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
268
|
+
* function.
|
|
269
|
+
*/
|
|
270
|
+
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[][];
|
|
271
|
+
/**
|
|
272
|
+
* Time Complexity: O(n log n)
|
|
273
|
+
* Space Complexity: O(n)
|
|
274
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
275
|
+
*/
|
|
276
|
+
/**
|
|
277
|
+
* Time Complexity: O(n log n)
|
|
278
|
+
* Space Complexity: O(n)
|
|
279
|
+
*
|
|
280
|
+
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
281
|
+
* leftmost node if the comparison result is greater than.
|
|
282
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
283
|
+
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
284
|
+
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
285
|
+
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
286
|
+
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
287
|
+
* rightmost node otherwise. If no node is found, it returns 0.
|
|
288
|
+
*/
|
|
289
|
+
lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, N>): K | undefined;
|
|
228
290
|
/**
|
|
229
291
|
* Time Complexity: O(log n)
|
|
230
292
|
* Space Complexity: O(log n)
|
|
@@ -127,7 +127,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
127
127
|
node = this.createNode(key, value);
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
|
-
else if (this.
|
|
130
|
+
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
131
131
|
node = this.createNode(keyOrNodeOrEntry, value);
|
|
132
132
|
}
|
|
133
133
|
else {
|
|
@@ -167,15 +167,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
167
167
|
}
|
|
168
168
|
return res;
|
|
169
169
|
}
|
|
170
|
-
/**
|
|
171
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
172
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
173
|
-
* data type.
|
|
174
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
175
|
-
*/
|
|
176
|
-
isNotNodeInstance(potentialKey) {
|
|
177
|
-
return !(potentialKey instanceof BSTNode);
|
|
178
|
-
}
|
|
179
170
|
/**
|
|
180
171
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
181
172
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
@@ -343,42 +334,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
343
334
|
}
|
|
344
335
|
return inserted;
|
|
345
336
|
}
|
|
346
|
-
/**
|
|
347
|
-
* Time Complexity: O(n log n)
|
|
348
|
-
* Space Complexity: O(n)
|
|
349
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
350
|
-
*/
|
|
351
|
-
/**
|
|
352
|
-
* Time Complexity: O(n log n)
|
|
353
|
-
* Space Complexity: O(n)
|
|
354
|
-
*
|
|
355
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
356
|
-
* leftmost node if the comparison result is greater than.
|
|
357
|
-
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
358
|
-
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
359
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
360
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
361
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
362
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
363
|
-
*/
|
|
364
|
-
lastKey(beginRoot = this.root) {
|
|
365
|
-
let current = this.ensureNode(beginRoot);
|
|
366
|
-
if (!current)
|
|
367
|
-
return undefined;
|
|
368
|
-
if (this._variant === types_1.BSTVariant.STANDARD) {
|
|
369
|
-
// For BSTVariant.MIN, find the rightmost node
|
|
370
|
-
while (current.right !== undefined) {
|
|
371
|
-
current = current.right;
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
else {
|
|
375
|
-
// For BSTVariant.MAX, find the leftmost node
|
|
376
|
-
while (current.left !== undefined) {
|
|
377
|
-
current = current.left;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
return current.key;
|
|
381
|
-
}
|
|
382
337
|
/**
|
|
383
338
|
* Time Complexity: O(log n)
|
|
384
339
|
* Space Complexity: O(1)
|
|
@@ -513,6 +468,136 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
513
468
|
}
|
|
514
469
|
return ans;
|
|
515
470
|
}
|
|
471
|
+
// /**
|
|
472
|
+
// * The function overrides the subTreeTraverse method and returns the result of calling the super
|
|
473
|
+
// * method with the provided arguments.
|
|
474
|
+
// * @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
475
|
+
// * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
|
|
476
|
+
// * the tree. The return type of the callback function can be any type.
|
|
477
|
+
// * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
|
|
478
|
+
// * can be either a key, a node, or an entry.
|
|
479
|
+
// * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
480
|
+
// * be performed during the traversal of the subtree. It can have one of the following values:
|
|
481
|
+
// * @returns The method is returning an array of the return type of the callback function.
|
|
482
|
+
// */
|
|
483
|
+
// override subTreeTraverse<C extends BTNCallback<N>>(
|
|
484
|
+
// callback: C = this._defaultOneParamCallback as C,
|
|
485
|
+
// beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
486
|
+
// iterationType = this.iterationType
|
|
487
|
+
// ): ReturnType<C>[] {
|
|
488
|
+
// return super.subTreeTraverse(callback, beginRoot, iterationType, false);
|
|
489
|
+
// }
|
|
490
|
+
/**
|
|
491
|
+
* Time complexity: O(n)
|
|
492
|
+
* Space complexity: O(n)
|
|
493
|
+
*/
|
|
494
|
+
/**
|
|
495
|
+
* Time complexity: O(n)
|
|
496
|
+
* Space complexity: O(n)
|
|
497
|
+
*
|
|
498
|
+
* The function overrides the depth-first search method and returns an array of the return types of
|
|
499
|
+
* the callback function.
|
|
500
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
501
|
+
* during the depth-first search traversal. It is an optional parameter and if not provided, a
|
|
502
|
+
* default callback function will be used.
|
|
503
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
|
|
504
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
505
|
+
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
|
|
506
|
+
* Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
|
|
507
|
+
* value is provided, the DFS traversal will start from the root of the tree.
|
|
508
|
+
* @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
|
|
509
|
+
* iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
510
|
+
* following values:
|
|
511
|
+
* @returns The method is returning an array of the return type of the callback function.
|
|
512
|
+
*/
|
|
513
|
+
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
|
|
514
|
+
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Time complexity: O(n)
|
|
518
|
+
* Space complexity: O(n)
|
|
519
|
+
*/
|
|
520
|
+
/**
|
|
521
|
+
* Time complexity: O(n)
|
|
522
|
+
* Space complexity: O(n)
|
|
523
|
+
*
|
|
524
|
+
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
525
|
+
* the callback function.
|
|
526
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
527
|
+
* visited during the breadth-first search traversal. It is an optional parameter and if not
|
|
528
|
+
* provided, a default callback function will be used.
|
|
529
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
|
|
530
|
+
* traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
|
|
531
|
+
* the tree is used as the starting point.
|
|
532
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
533
|
+
* be performed during the breadth-first search (BFS) traversal. It determines the order in which the
|
|
534
|
+
* nodes are visited.
|
|
535
|
+
* @returns The method is returning an array of the return type of the callback function.
|
|
536
|
+
*/
|
|
537
|
+
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
538
|
+
return super.bfs(callback, beginRoot, iterationType, false);
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Time complexity: O(n)
|
|
542
|
+
* Space complexity: O(n)
|
|
543
|
+
*/
|
|
544
|
+
/**
|
|
545
|
+
* Time complexity: O(n)
|
|
546
|
+
* Space complexity: O(n)
|
|
547
|
+
*
|
|
548
|
+
* The function overrides the listLevels method and returns an array of arrays containing the return
|
|
549
|
+
* type of the callback function for each level of the tree.
|
|
550
|
+
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
551
|
+
* `BTNCallback<N>`. It represents a callback function that will be called for each node in the tree
|
|
552
|
+
* during the level listing process.
|
|
553
|
+
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
|
|
554
|
+
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
|
|
555
|
+
* provided, the root of the binary tree is used as the starting point.
|
|
556
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
557
|
+
* be performed on the tree. It determines the order in which the nodes are visited during the
|
|
558
|
+
* iteration.
|
|
559
|
+
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
560
|
+
* function.
|
|
561
|
+
*/
|
|
562
|
+
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
563
|
+
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Time Complexity: O(n log n)
|
|
567
|
+
* Space Complexity: O(n)
|
|
568
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
569
|
+
*/
|
|
570
|
+
/**
|
|
571
|
+
* Time Complexity: O(n log n)
|
|
572
|
+
* Space Complexity: O(n)
|
|
573
|
+
*
|
|
574
|
+
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
575
|
+
* leftmost node if the comparison result is greater than.
|
|
576
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
577
|
+
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
578
|
+
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
579
|
+
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
580
|
+
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
581
|
+
* rightmost node otherwise. If no node is found, it returns 0.
|
|
582
|
+
*/
|
|
583
|
+
lastKey(beginRoot = this.root) {
|
|
584
|
+
let current = this.ensureNode(beginRoot);
|
|
585
|
+
if (!current)
|
|
586
|
+
return undefined;
|
|
587
|
+
if (this._variant === types_1.BSTVariant.STANDARD) {
|
|
588
|
+
// For BSTVariant.MIN, find the rightmost node
|
|
589
|
+
while (current.right !== undefined) {
|
|
590
|
+
current = current.right;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
else {
|
|
594
|
+
// For BSTVariant.MAX, find the leftmost node
|
|
595
|
+
while (current.left !== undefined) {
|
|
596
|
+
current = current.left;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return current.key;
|
|
600
|
+
}
|
|
516
601
|
/**
|
|
517
602
|
* Time Complexity: O(log n)
|
|
518
603
|
* Space Complexity: O(log n)
|
|
@@ -79,13 +79,6 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
79
79
|
* Space Complexity: O(1)
|
|
80
80
|
*/
|
|
81
81
|
isRealNode(node: N | undefined): node is N;
|
|
82
|
-
/**
|
|
83
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
84
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
85
|
-
* data type.
|
|
86
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
87
|
-
*/
|
|
88
|
-
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
89
82
|
/**
|
|
90
83
|
* Time Complexity: O(log n)
|
|
91
84
|
* Space Complexity: O(1)
|
|
@@ -100,7 +100,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
100
100
|
node = this.createNode(key, value, types_1.RBTNColor.RED);
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
-
else if (this.
|
|
103
|
+
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
104
104
|
node = this.createNode(keyOrNodeOrEntry, value, types_1.RBTNColor.RED);
|
|
105
105
|
}
|
|
106
106
|
else {
|
|
@@ -126,15 +126,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
126
126
|
return false;
|
|
127
127
|
return node instanceof RedBlackTreeNode;
|
|
128
128
|
}
|
|
129
|
-
/**
|
|
130
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
131
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
132
|
-
* data type.
|
|
133
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
134
|
-
*/
|
|
135
|
-
isNotNodeInstance(potentialKey) {
|
|
136
|
-
return !(potentialKey instanceof RedBlackTreeNode);
|
|
137
|
-
}
|
|
138
129
|
/**
|
|
139
130
|
* Time Complexity: O(log n)
|
|
140
131
|
* Space Complexity: O(1)
|
|
@@ -60,13 +60,6 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
60
60
|
* class.
|
|
61
61
|
*/
|
|
62
62
|
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
|
|
63
|
-
/**
|
|
64
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
65
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
66
|
-
* data type.
|
|
67
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
68
|
-
*/
|
|
69
|
-
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
70
63
|
/**
|
|
71
64
|
* Time Complexity: O(log n)
|
|
72
65
|
* Space Complexity: O(1)
|
|
@@ -33,7 +33,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
33
33
|
// TODO the _count is not accurate after nodes count modified
|
|
34
34
|
get count() {
|
|
35
35
|
let sum = 0;
|
|
36
|
-
this.
|
|
36
|
+
this.dfs(node => (sum += node.count));
|
|
37
37
|
return sum;
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
@@ -79,7 +79,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
79
79
|
node = this.createNode(key, value, count);
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
-
else if (this.
|
|
82
|
+
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
83
83
|
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
84
84
|
}
|
|
85
85
|
else {
|
|
@@ -96,15 +96,6 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
96
96
|
isNode(keyOrNodeOrEntry) {
|
|
97
97
|
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
98
98
|
}
|
|
99
|
-
/**
|
|
100
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
101
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
102
|
-
* data type.
|
|
103
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
104
|
-
*/
|
|
105
|
-
isNotNodeInstance(potentialKey) {
|
|
106
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
107
|
-
}
|
|
108
99
|
/**
|
|
109
100
|
* Time Complexity: O(log n)
|
|
110
101
|
* Space Complexity: O(1)
|