data-structure-typed 1.41.0 → 1.41.2
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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +9 -6
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +29 -10
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +20 -4
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +109 -44
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +9 -6
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +28 -10
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +20 -4
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +110 -45
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +9 -9
- 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 +73 -25
- package/src/data-structures/binary-tree/bst.ts +7 -4
- package/src/data-structures/binary-tree/rb-tree.ts +121 -68
- 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/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
- package/test/integration/avl-tree.test.ts +3 -3
- package/test/integration/bst.test.ts +7 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +11 -11
- package/test/unit/data-structures/binary-tree/bst.test.ts +22 -20
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +214 -21
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +8 -8
- package/test/utils/big-o.js +10 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.41.
|
|
3
|
+
"version": "1.41.2",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -26,16 +26,16 @@
|
|
|
26
26
|
"format:src": "prettier --write 'src/**/*.{js,ts}'",
|
|
27
27
|
"format:test": "prettier --write 'test/**/*.{js,ts}'",
|
|
28
28
|
"format": "npm run format:src && npm run format:test",
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
29
|
+
"reformat:src": "npm run lint:src && npm run format:src",
|
|
30
|
+
"reformat:test": "npm run lint:test && npm run format:test",
|
|
31
|
+
"reformat": "npm run reformat:src && npm run reformat:test",
|
|
32
32
|
"update:subs": "npm i avl-tree-typed binary-tree-typed bst-typed heap-typed --save-dev",
|
|
33
33
|
"install:all-subs": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed queue-typed --save-dev",
|
|
34
34
|
"test": "jest",
|
|
35
35
|
"check:deps": "dependency-cruiser src",
|
|
36
36
|
"changelog": "auto-changelog",
|
|
37
37
|
"coverage:badge": "istanbul-badges-readme",
|
|
38
|
-
"ci": "env && git fetch --tags && npm run lint && npm run build && npm run update:subs && npm run test && npm run changelog",
|
|
38
|
+
"ci": "env && git fetch --tags && npm run check && npm run lint && npm run build && npm run update:subs && npm run test && npm run changelog",
|
|
39
39
|
"copy:to-subs": "sh scripts/copy_to_all_subs.sh",
|
|
40
40
|
"publish:subs": "npm run copy:to-subs && sh scripts/publish_all_subs.sh",
|
|
41
41
|
"publish:docs": "sh scripts/publish_docs.sh",
|
|
@@ -61,17 +61,17 @@
|
|
|
61
61
|
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
62
62
|
"@typescript-eslint/parser": "^6.7.4",
|
|
63
63
|
"auto-changelog": "^2.4.0",
|
|
64
|
-
"avl-tree-typed": "^1.
|
|
64
|
+
"avl-tree-typed": "^1.41.1",
|
|
65
65
|
"benchmark": "^2.1.4",
|
|
66
|
-
"binary-tree-typed": "^1.
|
|
67
|
-
"bst-typed": "^1.
|
|
66
|
+
"binary-tree-typed": "^1.41.1",
|
|
67
|
+
"bst-typed": "^1.41.1",
|
|
68
68
|
"dependency-cruiser": "^14.1.0",
|
|
69
69
|
"eslint": "^8.50.0",
|
|
70
70
|
"eslint-config-prettier": "^9.0.0",
|
|
71
71
|
"eslint-import-resolver-alias": "^1.1.2",
|
|
72
72
|
"eslint-import-resolver-typescript": "^3.6.1",
|
|
73
73
|
"eslint-plugin-import": "^2.28.1",
|
|
74
|
-
"heap-typed": "^1.
|
|
74
|
+
"heap-typed": "^1.41.1",
|
|
75
75
|
"istanbul-badges-readme": "^1.8.5",
|
|
76
76
|
"jest": "^29.7.0",
|
|
77
77
|
"prettier": "^3.0.3",
|
|
@@ -21,7 +21,8 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|
|
21
21
|
|
|
22
22
|
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
|
23
23
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
24
|
+
implements IBinaryTree<V, N>
|
|
25
|
+
{
|
|
25
26
|
/**
|
|
26
27
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
27
28
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -160,7 +161,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
160
161
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
161
162
|
switch (
|
|
162
163
|
this._balanceFactor(A) // second O(1)
|
|
163
|
-
|
|
164
|
+
) {
|
|
164
165
|
case -2:
|
|
165
166
|
if (A && A.left) {
|
|
166
167
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
|
|
|
17
17
|
* @param - - `frequency`: The default frequency value. It is optional and has a default
|
|
18
18
|
* value of 0.
|
|
19
19
|
*/
|
|
20
|
-
constructor({frequency = 0, max}: {
|
|
20
|
+
constructor({frequency = 0, max}: {frequency?: number; max: number}) {
|
|
21
21
|
this._freq = frequency;
|
|
22
22
|
this._max = max;
|
|
23
23
|
this._freqMap = {0: 0};
|
|
@@ -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) {
|
|
@@ -558,21 +559,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
558
559
|
has<C extends BTNCallback<N, BTNKey>>(
|
|
559
560
|
identifier: BTNKey,
|
|
560
561
|
callback?: C,
|
|
561
|
-
beginRoot?: N,
|
|
562
|
+
beginRoot?: N | null,
|
|
562
563
|
iterationType?: IterationType
|
|
563
564
|
): boolean;
|
|
564
565
|
|
|
565
566
|
has<C extends BTNCallback<N, N>>(
|
|
566
567
|
identifier: N | null,
|
|
567
568
|
callback?: C,
|
|
568
|
-
beginRoot?: N,
|
|
569
|
+
beginRoot?: N | null,
|
|
569
570
|
iterationType?: IterationType
|
|
570
571
|
): boolean;
|
|
571
572
|
|
|
572
573
|
has<C extends BTNCallback<N>>(
|
|
573
574
|
identifier: ReturnType<C> | null,
|
|
574
575
|
callback: C,
|
|
575
|
-
beginRoot?: N,
|
|
576
|
+
beginRoot?: N | null,
|
|
576
577
|
iterationType?: IterationType
|
|
577
578
|
): boolean;
|
|
578
579
|
|
|
@@ -600,28 +601,28 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
600
601
|
iterationType = this.iterationType
|
|
601
602
|
): boolean {
|
|
602
603
|
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
603
|
-
|
|
604
|
+
|
|
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
|
-
beginRoot?: N,
|
|
611
|
+
beginRoot?: N | null,
|
|
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
|
-
beginRoot?: N,
|
|
618
|
+
beginRoot?: N | null,
|
|
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
|
-
beginRoot?: N,
|
|
625
|
+
beginRoot?: N | null,
|
|
625
626
|
iterationType?: IterationType
|
|
626
627
|
): N | null;
|
|
627
628
|
|
|
@@ -640,17 +641,64 @@ 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,
|
|
647
648
|
iterationType = this.iterationType
|
|
648
649
|
): N | null {
|
|
649
650
|
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
|
650
|
-
|
|
651
|
+
|
|
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 | null,
|
|
659
|
+
iterationType?: IterationType
|
|
660
|
+
): V | undefined;
|
|
661
|
+
|
|
662
|
+
get<C extends BTNCallback<N, N>>(
|
|
663
|
+
identifier: N | null,
|
|
664
|
+
callback?: C,
|
|
665
|
+
beginRoot?: N | null,
|
|
666
|
+
iterationType?: IterationType
|
|
667
|
+
): V | undefined;
|
|
668
|
+
|
|
669
|
+
get<C extends BTNCallback<N>>(
|
|
670
|
+
identifier: ReturnType<C>,
|
|
671
|
+
callback: C,
|
|
672
|
+
beginRoot?: N | null,
|
|
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
|
+
|
|
699
|
+
return this.getNode(identifier, callback, beginRoot, iterationType)?.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();
|
|
@@ -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 {
|
|
@@ -1055,7 +1103,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1055
1103
|
* @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
|
|
1056
1104
|
* if there is no successor, or `undefined` if the input `x` is `undefined`.
|
|
1057
1105
|
*/
|
|
1058
|
-
getSuccessor(x: N): N | null | undefined{
|
|
1106
|
+
getSuccessor(x: N): N | null | undefined {
|
|
1059
1107
|
if (x.right) {
|
|
1060
1108
|
return this.getLeftMost(x.right);
|
|
1061
1109
|
}
|
|
@@ -1178,7 +1226,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1178
1226
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1179
1227
|
* binary tree nodes in a specific order.
|
|
1180
1228
|
*/
|
|
1181
|
-
*
|
|
1229
|
+
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1182
1230
|
if (!node) {
|
|
1183
1231
|
return;
|
|
1184
1232
|
}
|
|
@@ -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.
|
|
@@ -153,7 +154,9 @@ 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, V][] = keysOrNodes.map(
|
|
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
162
|
function isNodeOrNullTuple(arr: [BTNKey | N, V][]): arr is [N, V][] {
|
|
@@ -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;
|