min-heap-typed 1.40.0 → 1.41.1
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/binary-tree.d.ts +14 -3
- package/dist/data-structures/binary-tree/binary-tree.js +52 -10
- package/dist/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +96 -9
- package/dist/data-structures/binary-tree/rb-tree.js +377 -12
- package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -7
- package/dist/types/data-structures/binary-tree/rb-tree.js +11 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +87 -17
- package/src/data-structures/binary-tree/bst.ts +10 -7
- package/src/data-structures/binary-tree/rb-tree.ts +396 -349
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
- package/src/data-structures/graph/abstract-graph.ts +11 -12
- package/src/data-structures/graph/directed-graph.ts +7 -6
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +1 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -4
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -108,7 +108,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
108
108
|
* @template N - The type of the binary tree's nodes.
|
|
109
109
|
*/
|
|
110
110
|
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
-
implements IBinaryTree<V, N>
|
|
111
|
+
implements IBinaryTree<V, N>
|
|
112
|
+
{
|
|
112
113
|
iterationType: IterationType = IterationType.ITERATIVE;
|
|
113
114
|
|
|
114
115
|
/**
|
|
@@ -201,7 +202,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
201
202
|
}
|
|
202
203
|
|
|
203
204
|
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
204
|
-
const existNode = key !== undefined ? this.
|
|
205
|
+
const existNode = key !== undefined ? this.getNode(key, (node: N) => node.key) : undefined;
|
|
205
206
|
|
|
206
207
|
if (this.root) {
|
|
207
208
|
if (existNode) {
|
|
@@ -290,7 +291,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
290
291
|
if (!this.root) return bstDeletedResult;
|
|
291
292
|
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
292
293
|
|
|
293
|
-
const curr = this.
|
|
294
|
+
const curr = this.getNode(identifier, callback);
|
|
294
295
|
if (!curr) return bstDeletedResult;
|
|
295
296
|
|
|
296
297
|
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
@@ -342,8 +343,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
342
343
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
343
344
|
*/
|
|
344
345
|
getDepth(distNode: BTNKey | N | null, beginRoot: BTNKey | N | null = this.root): number {
|
|
345
|
-
if (typeof distNode === 'number') distNode = this.
|
|
346
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
346
|
+
if (typeof distNode === 'number') distNode = this.getNode(distNode);
|
|
347
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
347
348
|
let depth = 0;
|
|
348
349
|
while (distNode?.parent) {
|
|
349
350
|
if (distNode === beginRoot) {
|
|
@@ -368,7 +369,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
368
369
|
* @returns the height of the binary tree.
|
|
369
370
|
*/
|
|
370
371
|
getHeight(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): number {
|
|
371
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
372
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
372
373
|
if (!beginRoot) return -1;
|
|
373
374
|
|
|
374
375
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -385,7 +386,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
385
386
|
return -1;
|
|
386
387
|
}
|
|
387
388
|
|
|
388
|
-
const stack: {
|
|
389
|
+
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
389
390
|
let maxHeight = 0;
|
|
390
391
|
|
|
391
392
|
while (stack.length > 0) {
|
|
@@ -604,21 +605,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
604
605
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
605
606
|
}
|
|
606
607
|
|
|
607
|
-
|
|
608
|
+
getNode<C extends BTNCallback<N, BTNKey>>(
|
|
608
609
|
identifier: BTNKey,
|
|
609
610
|
callback?: C,
|
|
610
611
|
beginRoot?: N,
|
|
611
612
|
iterationType?: IterationType
|
|
612
613
|
): N | null;
|
|
613
614
|
|
|
614
|
-
|
|
615
|
+
getNode<C extends BTNCallback<N, N>>(
|
|
615
616
|
identifier: N | null,
|
|
616
617
|
callback?: C,
|
|
617
618
|
beginRoot?: N,
|
|
618
619
|
iterationType?: IterationType
|
|
619
620
|
): N | null;
|
|
620
621
|
|
|
621
|
-
|
|
622
|
+
getNode<C extends BTNCallback<N>>(
|
|
622
623
|
identifier: ReturnType<C>,
|
|
623
624
|
callback: C,
|
|
624
625
|
beginRoot?: N,
|
|
@@ -640,7 +641,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
640
641
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
641
642
|
* @returns either the found node (of type N) or null if no node is found.
|
|
642
643
|
*/
|
|
643
|
-
|
|
644
|
+
getNode<C extends BTNCallback<N>>(
|
|
644
645
|
identifier: ReturnType<C> | null,
|
|
645
646
|
callback: C = ((node: N) => node.key) as C,
|
|
646
647
|
beginRoot = this.root,
|
|
@@ -651,6 +652,53 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
651
652
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
652
653
|
}
|
|
653
654
|
|
|
655
|
+
get<C extends BTNCallback<N, BTNKey>>(
|
|
656
|
+
identifier: BTNKey,
|
|
657
|
+
callback?: C,
|
|
658
|
+
beginRoot?: N,
|
|
659
|
+
iterationType?: IterationType
|
|
660
|
+
): V | undefined;
|
|
661
|
+
|
|
662
|
+
get<C extends BTNCallback<N, N>>(
|
|
663
|
+
identifier: N | null,
|
|
664
|
+
callback?: C,
|
|
665
|
+
beginRoot?: N,
|
|
666
|
+
iterationType?: IterationType
|
|
667
|
+
): V | undefined;
|
|
668
|
+
|
|
669
|
+
get<C extends BTNCallback<N>>(
|
|
670
|
+
identifier: ReturnType<C>,
|
|
671
|
+
callback: C,
|
|
672
|
+
beginRoot?: N,
|
|
673
|
+
iterationType?: IterationType
|
|
674
|
+
): V | undefined;
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* The function `get` returns the first node value in a binary tree that matches the given property or key.
|
|
678
|
+
* @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
|
|
679
|
+
* the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
|
|
680
|
+
* type.
|
|
681
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
682
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
683
|
+
* whether the node matches the criteria or not. The default callback function
|
|
684
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
685
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
686
|
+
* the root node from which the search should begin.
|
|
687
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
688
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
689
|
+
* @returns either the found value (of type V) or undefined if no node value is found.
|
|
690
|
+
*/
|
|
691
|
+
get<C extends BTNCallback<N>>(
|
|
692
|
+
identifier: ReturnType<C> | null,
|
|
693
|
+
callback: C = ((node: N) => node.key) as C,
|
|
694
|
+
beginRoot = this.root,
|
|
695
|
+
iterationType = this.iterationType
|
|
696
|
+
): V | undefined {
|
|
697
|
+
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
698
|
+
// TODO may support finding node by value equal
|
|
699
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0].value ?? undefined;
|
|
700
|
+
}
|
|
701
|
+
|
|
654
702
|
/**
|
|
655
703
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
656
704
|
* up to the root node, with the option to reverse the order of the nodes.
|
|
@@ -686,7 +734,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
686
734
|
* no leftmost node, it returns `null`.
|
|
687
735
|
*/
|
|
688
736
|
getLeftMost(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): N | null {
|
|
689
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
737
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
690
738
|
|
|
691
739
|
if (!beginRoot) return beginRoot;
|
|
692
740
|
|
|
@@ -812,7 +860,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
812
860
|
beginRoot: BTNKey | N | null = this.root,
|
|
813
861
|
iterationType = this.iterationType
|
|
814
862
|
): ReturnType<C>[] {
|
|
815
|
-
if (typeof beginRoot === 'number') beginRoot = this.
|
|
863
|
+
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
|
|
816
864
|
|
|
817
865
|
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
818
866
|
if (!beginRoot) return ans;
|
|
@@ -888,7 +936,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
888
936
|
_traverse(beginRoot);
|
|
889
937
|
} else {
|
|
890
938
|
// 0: visit, 1: print
|
|
891
|
-
const stack: {
|
|
939
|
+
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
892
940
|
|
|
893
941
|
while (stack.length > 0) {
|
|
894
942
|
const cur = stack.pop();
|
|
@@ -950,7 +998,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
950
998
|
if (iterationType === IterationType.RECURSIVE) {
|
|
951
999
|
const queue = new Queue<N>([beginRoot]);
|
|
952
1000
|
|
|
953
|
-
|
|
1001
|
+
const traverse = (level: number) => {
|
|
954
1002
|
if (queue.size === 0) return;
|
|
955
1003
|
|
|
956
1004
|
const current = queue.shift()!;
|
|
@@ -960,7 +1008,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
960
1008
|
if (current.right) queue.push(current.right);
|
|
961
1009
|
|
|
962
1010
|
traverse(level + 1);
|
|
963
|
-
}
|
|
1011
|
+
};
|
|
964
1012
|
|
|
965
1013
|
traverse(0);
|
|
966
1014
|
} else {
|
|
@@ -1048,6 +1096,26 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1048
1096
|
}
|
|
1049
1097
|
}
|
|
1050
1098
|
|
|
1099
|
+
/**
|
|
1100
|
+
* The function `getSuccessor` returns the next node in a binary tree given a node `x`, or `null` if
|
|
1101
|
+
* `x` is the last node.
|
|
1102
|
+
* @param {N} x - N - a node in a binary tree
|
|
1103
|
+
* @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
|
|
1104
|
+
* if there is no successor, or `undefined` if the input `x` is `undefined`.
|
|
1105
|
+
*/
|
|
1106
|
+
getSuccessor(x: N): N | null | undefined {
|
|
1107
|
+
if (x.right) {
|
|
1108
|
+
return this.getLeftMost(x.right);
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
let y: N | null | undefined = x.parent;
|
|
1112
|
+
while (y && y && x === y.right) {
|
|
1113
|
+
x = y;
|
|
1114
|
+
y = y.parent;
|
|
1115
|
+
}
|
|
1116
|
+
return y;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1051
1119
|
// --- start additional methods ---
|
|
1052
1120
|
|
|
1053
1121
|
/**
|
|
@@ -1158,7 +1226,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1158
1226
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1159
1227
|
* binary tree nodes in a specific order.
|
|
1160
1228
|
*/
|
|
1161
|
-
*
|
|
1229
|
+
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1162
1230
|
if (!node) {
|
|
1163
1231
|
return;
|
|
1164
1232
|
}
|
|
@@ -1180,10 +1248,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1180
1248
|
}
|
|
1181
1249
|
} else {
|
|
1182
1250
|
if (node.left) {
|
|
1251
|
+
// @ts-ignore
|
|
1183
1252
|
yield* this[Symbol.iterator](node.left);
|
|
1184
1253
|
}
|
|
1185
1254
|
yield node.key;
|
|
1186
1255
|
if (node.right) {
|
|
1256
|
+
// @ts-ignore
|
|
1187
1257
|
yield* this[Symbol.iterator](node.right);
|
|
1188
1258
|
}
|
|
1189
1259
|
}
|
|
@@ -19,7 +19,8 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
19
19
|
|
|
20
20
|
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
21
21
|
extends BinaryTree<V, N>
|
|
22
|
-
implements IBinaryTree<V, N>
|
|
22
|
+
implements IBinaryTree<V, N>
|
|
23
|
+
{
|
|
23
24
|
/**
|
|
24
25
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
25
26
|
* function.
|
|
@@ -127,7 +128,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
127
128
|
/**
|
|
128
129
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
129
130
|
* maintaining balance.
|
|
130
|
-
* @param {[BTNKey | N,
|
|
131
|
+
* @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
131
132
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
132
133
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
133
134
|
* `null
|
|
@@ -153,15 +154,17 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
153
154
|
return super.addMany(keysOrNodes, data);
|
|
154
155
|
}
|
|
155
156
|
const inserted: (N | null | undefined)[] = [];
|
|
156
|
-
const combinedArr: [BTNKey | N,
|
|
157
|
+
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
158
|
+
(value: BTNKey | N, index) => [value, data?.[index]] as [BTNKey | N, V]
|
|
159
|
+
);
|
|
157
160
|
let sorted = [];
|
|
158
161
|
|
|
159
|
-
function isNodeOrNullTuple(arr: [BTNKey | N,
|
|
162
|
+
function isNodeOrNullTuple(arr: [BTNKey | N, V][]): arr is [N, V][] {
|
|
160
163
|
for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
|
|
161
164
|
return false;
|
|
162
165
|
}
|
|
163
166
|
|
|
164
|
-
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N,
|
|
167
|
+
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N, V][]): arr is [BTNKey, V][] {
|
|
165
168
|
for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
|
|
166
169
|
return false;
|
|
167
170
|
}
|
|
@@ -231,7 +234,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
231
234
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
232
235
|
* matching node is found.
|
|
233
236
|
*/
|
|
234
|
-
override
|
|
237
|
+
override getNode<C extends BTNCallback<N>>(
|
|
235
238
|
identifier: ReturnType<C> | null,
|
|
236
239
|
callback: C = ((node: N) => node.key) as C,
|
|
237
240
|
beginRoot = this.root,
|
|
@@ -362,7 +365,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
362
365
|
targetNode: BTNKey | N | null = this.root,
|
|
363
366
|
iterationType = this.iterationType
|
|
364
367
|
): ReturnType<C>[] {
|
|
365
|
-
if (typeof targetNode === 'number') targetNode = this.
|
|
368
|
+
if (typeof targetNode === 'number') targetNode = this.getNode(targetNode);
|
|
366
369
|
const ans: ReturnType<BTNCallback<N>>[] = [];
|
|
367
370
|
if (!targetNode) return ans;
|
|
368
371
|
const targetKey = targetNode.key;
|