data-structure-typed 1.34.7 → 1.34.9
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/.eslintrc.js +1 -0
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +0 -0
- package/README.md +8 -8
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +314 -457
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +64 -90
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +12 -31
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +157 -244
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +20 -44
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.js +80 -122
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +168 -239
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +351 -585
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +152 -282
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +47 -88
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +91 -180
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.js +85 -247
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +87 -128
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/pair.js.map +1 -1
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-map.js.map +1 -1
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/hash/tree-set.js.map +1 -1
- package/dist/data-structures/heap/heap.js +56 -80
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +57 -70
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.js +63 -84
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/queue.js +57 -157
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/tree.js +33 -46
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/utils/utils.js +22 -107
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +124 -123
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
- package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
- package/lib/data-structures/binary-tree/avl-tree.js +35 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +86 -89
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +40 -42
- package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
- package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
- package/lib/data-structures/graph/abstract-graph.js +86 -86
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.js +3 -3
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +37 -42
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +6 -4
- package/lib/interfaces/bst.d.ts +10 -9
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-binary-tree.js +0 -1
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +6 -6
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
- package/src/data-structures/binary-tree/avl-tree.ts +43 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +101 -96
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
- package/src/data-structures/graph/abstract-graph.ts +114 -109
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +37 -50
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +6 -4
- package/src/interfaces/bst.ts +9 -9
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -4
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/integration/avl-tree.test.ts +24 -24
- package/test/integration/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +28 -28
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -5
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -6,15 +6,18 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import {BST, BSTNode} from './bst';
|
|
9
|
-
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult,
|
|
9
|
+
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types';
|
|
10
10
|
import {IAVLTree, IAVLTreeNode} from '../../interfaces';
|
|
11
11
|
|
|
12
12
|
export class AVLTreeNode<V = any, NEIGHBOR extends AVLTreeNode<V, NEIGHBOR> = AVLTreeNodeNested<V>>
|
|
13
13
|
extends BSTNode<V, NEIGHBOR>
|
|
14
14
|
implements IAVLTreeNode<V, NEIGHBOR>
|
|
15
15
|
{
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
height: number;
|
|
17
|
+
|
|
18
|
+
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
19
|
+
super(key, val);
|
|
20
|
+
this.height = 0;
|
|
18
21
|
}
|
|
19
22
|
}
|
|
20
23
|
|
|
@@ -30,27 +33,53 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
/**
|
|
33
|
-
* The function
|
|
34
|
-
* @param {
|
|
36
|
+
* The `swapLocation` function swaps the location of two nodes in a binary tree.
|
|
37
|
+
* @param {N} srcNode - The source node that you want to swap with the destination node.
|
|
38
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
39
|
+
* be swapped to.
|
|
40
|
+
* @returns The `destNode` is being returned.
|
|
41
|
+
*/
|
|
42
|
+
override swapLocation(srcNode: N, destNode: N): N {
|
|
43
|
+
const {key, val, height} = destNode;
|
|
44
|
+
const tempNode = this.createNode(key, val);
|
|
45
|
+
|
|
46
|
+
if (tempNode) {
|
|
47
|
+
tempNode.height = height;
|
|
48
|
+
|
|
49
|
+
destNode.key = srcNode.key;
|
|
50
|
+
destNode.val = srcNode.val;
|
|
51
|
+
destNode.height = srcNode.height;
|
|
52
|
+
|
|
53
|
+
srcNode.key = tempNode.key;
|
|
54
|
+
srcNode.val = tempNode.val;
|
|
55
|
+
srcNode.height = tempNode.height;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return destNode;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The function creates a new AVL tree node with the given key and value.
|
|
63
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
35
64
|
* identify each node in the tree.
|
|
36
65
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
37
66
|
* that will be stored in the node.
|
|
38
|
-
* @returns a new AVLTreeNode object with the specified
|
|
67
|
+
* @returns a new AVLTreeNode object with the specified key and value.
|
|
39
68
|
*/
|
|
40
|
-
override createNode(
|
|
41
|
-
return new AVLTreeNode<N['val'], N>(
|
|
69
|
+
override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
70
|
+
return new AVLTreeNode<N['val'], N>(key, val) as N;
|
|
42
71
|
}
|
|
43
72
|
|
|
44
73
|
/**
|
|
45
74
|
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
46
|
-
* @param {
|
|
75
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier of the binary tree node that we want to add.
|
|
47
76
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
48
77
|
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
49
78
|
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
50
79
|
*/
|
|
51
|
-
override add(
|
|
80
|
+
override add(key: BinaryTreeNodeKey, val?: N['val']): N | null | undefined {
|
|
52
81
|
// TODO support node as a param
|
|
53
|
-
const inserted = super.add(
|
|
82
|
+
const inserted = super.add(key, val);
|
|
54
83
|
if (inserted) this._balancePath(inserted);
|
|
55
84
|
return inserted;
|
|
56
85
|
}
|
|
@@ -58,12 +87,12 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
58
87
|
/**
|
|
59
88
|
* The function overrides the remove method of a binary tree and performs additional operations to balance the tree after
|
|
60
89
|
* deletion.
|
|
61
|
-
* @param {
|
|
90
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter represents the identifier of the binary tree node that needs to be
|
|
62
91
|
* removed.
|
|
63
92
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
64
93
|
*/
|
|
65
|
-
override remove(
|
|
66
|
-
const deletedResults = super.remove(
|
|
94
|
+
override remove(key: BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
|
|
95
|
+
const deletedResults = super.remove(key);
|
|
67
96
|
for (const {needBalanced} of deletedResults) {
|
|
68
97
|
if (needBalanced) {
|
|
69
98
|
this._balancePath(needBalanced);
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {
|
|
9
|
+
import type {BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions} from '../../types';
|
|
10
10
|
import {AbstractBinaryTree, AbstractBinaryTreeNode} from './abstract-binary-tree';
|
|
11
11
|
import {IBinaryTree, IBinaryTreeNode} from '../../interfaces';
|
|
12
12
|
|
|
@@ -14,8 +14,8 @@ export class BinaryTreeNode<V = any, NEIGHBOR extends BinaryTreeNode<V, NEIGHBOR
|
|
|
14
14
|
extends AbstractBinaryTreeNode<V, NEIGHBOR>
|
|
15
15
|
implements IBinaryTreeNode<V, NEIGHBOR>
|
|
16
16
|
{
|
|
17
|
-
constructor(
|
|
18
|
-
super(
|
|
17
|
+
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
18
|
+
super(key, val);
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -35,13 +35,13 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* The function creates a new binary tree node with an optional value.
|
|
38
|
-
* @param {
|
|
39
|
-
* `
|
|
38
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is of type
|
|
39
|
+
* `BinaryTreeNodeKey`, which represents the unique identifier for each node in the binary tree.
|
|
40
40
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
41
41
|
* stored in the node.
|
|
42
|
-
* @returns a new instance of a BinaryTreeNode with the specified
|
|
42
|
+
* @returns a new instance of a BinaryTreeNode with the specified key and value.
|
|
43
43
|
*/
|
|
44
|
-
createNode(
|
|
45
|
-
return new BinaryTreeNode<N['val'], N>(
|
|
44
|
+
createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
45
|
+
return new BinaryTreeNode<N['val'], N>(key, val) as N;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -5,7 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type {
|
|
9
|
+
BinaryTreeNodeKey,
|
|
10
|
+
BinaryTreeNodePropertyName,
|
|
11
|
+
BSTComparator,
|
|
12
|
+
BSTNodeNested,
|
|
13
|
+
BSTOptions
|
|
14
|
+
} from '../../types';
|
|
9
15
|
import {CP, LoopType} from '../../types';
|
|
10
16
|
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
11
17
|
import {IBST, IBSTNode} from '../../interfaces';
|
|
@@ -14,8 +20,8 @@ export class BSTNode<V = any, NEIGHBOR extends BSTNode<V, NEIGHBOR> = BSTNodeNes
|
|
|
14
20
|
extends BinaryTreeNode<V, NEIGHBOR>
|
|
15
21
|
implements IBSTNode<V, NEIGHBOR>
|
|
16
22
|
{
|
|
17
|
-
constructor(
|
|
18
|
-
super(
|
|
23
|
+
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
24
|
+
super(key, val);
|
|
19
25
|
}
|
|
20
26
|
}
|
|
21
27
|
|
|
@@ -35,35 +41,35 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
/**
|
|
38
|
-
* The function creates a new binary search tree node with the given
|
|
39
|
-
* @param {
|
|
44
|
+
* The function creates a new binary search tree node with the given key and value.
|
|
45
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
40
46
|
* identify each node in the binary tree.
|
|
41
47
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
42
48
|
* that will be stored in the node.
|
|
43
|
-
* @returns a new instance of the BSTNode class with the specified
|
|
49
|
+
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
44
50
|
*/
|
|
45
|
-
override createNode(
|
|
46
|
-
return new BSTNode<N['val'], N>(
|
|
51
|
+
override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
52
|
+
return new BSTNode<N['val'], N>(key, val) as N;
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
/**
|
|
50
56
|
* The `add` function adds a new node to a binary search tree, either by creating a new node or by updating an existing
|
|
51
57
|
* node with the same ID.
|
|
52
|
-
* @param {
|
|
58
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N`
|
|
53
59
|
* (which represents a binary tree node) or `null`.
|
|
54
60
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
55
61
|
* being added to the binary search tree.
|
|
56
62
|
* @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
|
|
57
63
|
*/
|
|
58
|
-
override add(
|
|
64
|
+
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
|
59
65
|
// TODO support node as a param
|
|
60
66
|
let inserted: N | null = null;
|
|
61
67
|
let newNode: N | null = null;
|
|
62
|
-
if (
|
|
63
|
-
newNode =
|
|
64
|
-
} else if (typeof
|
|
65
|
-
newNode = this.createNode(
|
|
66
|
-
} else if (
|
|
68
|
+
if (keyOrNode instanceof BSTNode) {
|
|
69
|
+
newNode = keyOrNode;
|
|
70
|
+
} else if (typeof keyOrNode === 'number') {
|
|
71
|
+
newNode = this.createNode(keyOrNode, val);
|
|
72
|
+
} else if (keyOrNode === null) {
|
|
67
73
|
newNode = null;
|
|
68
74
|
}
|
|
69
75
|
if (this.root === null) {
|
|
@@ -75,14 +81,14 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
75
81
|
let traversing = true;
|
|
76
82
|
while (traversing) {
|
|
77
83
|
if (cur !== null && newNode !== null) {
|
|
78
|
-
if (this._compare(cur.
|
|
84
|
+
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
79
85
|
if (newNode) {
|
|
80
86
|
cur.val = newNode.val;
|
|
81
87
|
}
|
|
82
88
|
//Duplicates are not accepted.
|
|
83
89
|
traversing = false;
|
|
84
90
|
inserted = cur;
|
|
85
|
-
} else if (this._compare(cur.
|
|
91
|
+
} else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
86
92
|
// Traverse left of the node
|
|
87
93
|
if (cur.left === undefined) {
|
|
88
94
|
if (newNode) {
|
|
@@ -97,7 +103,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
97
103
|
//Traverse the left of the current node
|
|
98
104
|
if (cur.left) cur = cur.left;
|
|
99
105
|
}
|
|
100
|
-
} else if (this._compare(cur.
|
|
106
|
+
} else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
101
107
|
// Traverse right of the node
|
|
102
108
|
if (cur.right === undefined) {
|
|
103
109
|
if (newNode) {
|
|
@@ -124,48 +130,50 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
124
130
|
/**
|
|
125
131
|
* The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
|
|
126
132
|
* manner.
|
|
127
|
-
* @param {[
|
|
128
|
-
* `
|
|
133
|
+
* @param {[BinaryTreeNodeKey | N , N['val']][]} keysOrNodes - The `keysOrNodes` parameter in the `addMany` function is an array of
|
|
134
|
+
* `BinaryTreeNodeKey` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
|
|
129
135
|
* to the binary search tree.
|
|
130
136
|
* @param {N['val'][]} data - The values of tree nodes
|
|
131
137
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
132
138
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
133
139
|
*/
|
|
134
140
|
override addMany(
|
|
135
|
-
|
|
141
|
+
keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
|
|
136
142
|
data?: N['val'][],
|
|
137
143
|
isBalanceAdd = false
|
|
138
144
|
): (N | null | undefined)[] {
|
|
139
|
-
function hasNoNull(arr: (
|
|
145
|
+
function hasNoNull(arr: (BinaryTreeNodeKey | null)[] | (N | null)[]): arr is BinaryTreeNodeKey[] | N[] {
|
|
140
146
|
return arr.indexOf(null) === -1;
|
|
141
147
|
}
|
|
142
|
-
if (!isBalanceAdd || !hasNoNull(
|
|
143
|
-
return super.addMany(
|
|
148
|
+
if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
|
|
149
|
+
return super.addMany(keysOrNodes, data);
|
|
144
150
|
}
|
|
145
151
|
const inserted: (N | null | undefined)[] = [];
|
|
146
|
-
const combinedArr: [
|
|
152
|
+
const combinedArr: [BinaryTreeNodeKey | N, N['val']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
|
|
147
153
|
let sorted = [];
|
|
148
|
-
function isNodeOrNullTuple(arr: [
|
|
149
|
-
for (const [
|
|
154
|
+
function isNodeOrNullTuple(arr: [BinaryTreeNodeKey | N, N['val']][]): arr is [N, N['val']][] {
|
|
155
|
+
for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
|
|
150
156
|
return false;
|
|
151
157
|
}
|
|
152
|
-
function
|
|
153
|
-
|
|
158
|
+
function isBinaryTreeKeyOrNullTuple(
|
|
159
|
+
arr: [BinaryTreeNodeKey | N, N['val']][]
|
|
160
|
+
): arr is [BinaryTreeNodeKey, N['val']][] {
|
|
161
|
+
for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
|
|
154
162
|
return false;
|
|
155
163
|
}
|
|
156
|
-
let
|
|
164
|
+
let sortedKeysOrNodes: (number | N | null)[] = [],
|
|
157
165
|
sortedData: (N['val'] | undefined)[] | undefined = [];
|
|
158
166
|
|
|
159
167
|
if (isNodeOrNullTuple(combinedArr)) {
|
|
160
|
-
sorted = combinedArr.sort((a, b) => a[0].
|
|
161
|
-
} else if (
|
|
168
|
+
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
169
|
+
} else if (isBinaryTreeKeyOrNullTuple(combinedArr)) {
|
|
162
170
|
sorted = combinedArr.sort((a, b) => a[0] - b[0]);
|
|
163
171
|
} else {
|
|
164
|
-
throw new Error('Invalid input
|
|
172
|
+
throw new Error('Invalid input keysOrNodes');
|
|
165
173
|
}
|
|
166
|
-
|
|
174
|
+
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
167
175
|
sortedData = sorted.map(([, val]) => val);
|
|
168
|
-
const recursive = (arr: (
|
|
176
|
+
const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: N['val'][]) => {
|
|
169
177
|
if (arr.length === 0) return;
|
|
170
178
|
|
|
171
179
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
@@ -183,7 +191,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
183
191
|
const [l, r] = popped;
|
|
184
192
|
if (l <= r) {
|
|
185
193
|
const m = l + Math.floor((r - l) / 2);
|
|
186
|
-
const newNode = this.add(
|
|
194
|
+
const newNode = this.add(sortedKeysOrNodes[m], sortedData?.[m]);
|
|
187
195
|
inserted.push(newNode);
|
|
188
196
|
stack.push([m + 1, r]);
|
|
189
197
|
stack.push([l, m - 1]);
|
|
@@ -192,7 +200,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
192
200
|
}
|
|
193
201
|
};
|
|
194
202
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
195
|
-
recursive(
|
|
203
|
+
recursive(sortedKeysOrNodes, sortedData);
|
|
196
204
|
} else {
|
|
197
205
|
iterative();
|
|
198
206
|
}
|
|
@@ -202,45 +210,44 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
202
210
|
|
|
203
211
|
/**
|
|
204
212
|
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
205
|
-
* @param {
|
|
213
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
206
214
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
207
215
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
208
|
-
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'
|
|
209
|
-
* @returns The method is returning either a
|
|
216
|
+
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
|
|
217
|
+
* @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
|
|
210
218
|
*/
|
|
211
|
-
override get(nodeProperty:
|
|
212
|
-
propertyName = propertyName ?? 'id';
|
|
219
|
+
override get(nodeProperty: BinaryTreeNodeKey | N, propertyName: BinaryTreeNodePropertyName = 'key'): N | null {
|
|
213
220
|
return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
|
|
214
221
|
}
|
|
215
222
|
|
|
216
223
|
/**
|
|
217
|
-
* The function returns the
|
|
218
|
-
* leftmost node if the comparison is greater than, and the
|
|
219
|
-
* @returns The method `lastKey()` returns the
|
|
220
|
-
* the values at index 0 and 1 is less than, otherwise it returns the
|
|
221
|
-
* equal, it returns the
|
|
224
|
+
* The function returns the key of the rightmost node if the comparison between two values is less than, the key of the
|
|
225
|
+
* leftmost node if the comparison is greater than, and the key of the rightmost node otherwise.
|
|
226
|
+
* @returns The method `lastKey()` returns the key of the rightmost node in the binary tree if the comparison between
|
|
227
|
+
* the values at index 0 and 1 is less than, otherwise it returns the key of the leftmost node. If the comparison is
|
|
228
|
+
* equal, it returns the key of the rightmost node. If there are no nodes in the tree, it returns 0.
|
|
222
229
|
*/
|
|
223
|
-
lastKey():
|
|
224
|
-
if (this._compare(0, 1) === CP.lt) return this.getRightMost()?.
|
|
225
|
-
else if (this._compare(0, 1) === CP.gt) return this.getLeftMost()?.
|
|
226
|
-
else return this.getRightMost()?.
|
|
230
|
+
lastKey(): BinaryTreeNodeKey {
|
|
231
|
+
if (this._compare(0, 1) === CP.lt) return this.getRightMost()?.key ?? 0;
|
|
232
|
+
else if (this._compare(0, 1) === CP.gt) return this.getLeftMost()?.key ?? 0;
|
|
233
|
+
else return this.getRightMost()?.key ?? 0;
|
|
227
234
|
}
|
|
228
235
|
|
|
229
236
|
/**
|
|
230
237
|
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
231
|
-
* @param {
|
|
238
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
|
|
232
239
|
* `N` type. It represents the property of the binary tree node that you want to compare with.
|
|
233
240
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
234
|
-
* specifies the property name to use for comparison. If not provided, it defaults to `'
|
|
241
|
+
* specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
|
|
235
242
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
236
243
|
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
237
244
|
* is set to `true`, the function will return an array with only one node (if
|
|
238
245
|
* @returns an array of nodes (type N).
|
|
239
246
|
*/
|
|
240
247
|
override getNodes(
|
|
241
|
-
nodeProperty:
|
|
242
|
-
propertyName: BinaryTreeNodePropertyName = '
|
|
243
|
-
onlyOne
|
|
248
|
+
nodeProperty: BinaryTreeNodeKey | N,
|
|
249
|
+
propertyName: BinaryTreeNodePropertyName = 'key',
|
|
250
|
+
onlyOne = false
|
|
244
251
|
): N[] {
|
|
245
252
|
if (!this.root) return [];
|
|
246
253
|
const result: N[] = [];
|
|
@@ -250,9 +257,9 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
250
257
|
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
|
|
251
258
|
|
|
252
259
|
if (!cur.left && !cur.right) return;
|
|
253
|
-
if (propertyName === '
|
|
254
|
-
if (this._compare(cur.
|
|
255
|
-
if (this._compare(cur.
|
|
260
|
+
if (propertyName === 'key') {
|
|
261
|
+
if (this._compare(cur.key, nodeProperty as number) === CP.gt) cur.left && _traverse(cur.left);
|
|
262
|
+
if (this._compare(cur.key, nodeProperty as number) === CP.lt) cur.right && _traverse(cur.right);
|
|
256
263
|
} else {
|
|
257
264
|
cur.left && _traverse(cur.left);
|
|
258
265
|
cur.right && _traverse(cur.right);
|
|
@@ -266,9 +273,9 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
266
273
|
const cur = queue.shift();
|
|
267
274
|
if (cur) {
|
|
268
275
|
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result;
|
|
269
|
-
if (propertyName === '
|
|
270
|
-
if (this._compare(cur.
|
|
271
|
-
if (this._compare(cur.
|
|
276
|
+
if (propertyName === 'key') {
|
|
277
|
+
if (this._compare(cur.key, nodeProperty as number) === CP.gt) cur.left && queue.push(cur.left);
|
|
278
|
+
if (this._compare(cur.key, nodeProperty as number) === CP.lt) cur.right && queue.push(cur.right);
|
|
272
279
|
} else {
|
|
273
280
|
cur.left && queue.push(cur.left);
|
|
274
281
|
cur.right && queue.push(cur.right);
|
|
@@ -284,26 +291,25 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
284
291
|
/**
|
|
285
292
|
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
|
|
286
293
|
* less than a given node.
|
|
287
|
-
* @param {N |
|
|
294
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
288
295
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
289
|
-
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'
|
|
296
|
+
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'key'`.
|
|
290
297
|
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
|
|
291
298
|
* binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
|
|
292
299
|
*/
|
|
293
|
-
lesserSum(beginNode: N |
|
|
294
|
-
|
|
295
|
-
if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'id');
|
|
300
|
+
lesserSum(beginNode: N | BinaryTreeNodeKey | null, propertyName: BinaryTreeNodePropertyName = 'key'): number {
|
|
301
|
+
if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'key');
|
|
296
302
|
if (!beginNode) return 0;
|
|
297
303
|
if (!this.root) return 0;
|
|
298
|
-
const
|
|
304
|
+
const key = beginNode.key;
|
|
299
305
|
const getSumByPropertyName = (cur: N) => {
|
|
300
306
|
let needSum: number;
|
|
301
307
|
switch (propertyName) {
|
|
302
|
-
case '
|
|
303
|
-
needSum = cur.
|
|
308
|
+
case 'key':
|
|
309
|
+
needSum = cur.key;
|
|
304
310
|
break;
|
|
305
311
|
default:
|
|
306
|
-
needSum = cur.
|
|
312
|
+
needSum = cur.key;
|
|
307
313
|
break;
|
|
308
314
|
}
|
|
309
315
|
return needSum;
|
|
@@ -313,7 +319,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
313
319
|
|
|
314
320
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
315
321
|
const _traverse = (cur: N): void => {
|
|
316
|
-
const compared = this._compare(cur.
|
|
322
|
+
const compared = this._compare(cur.key, key);
|
|
317
323
|
if (compared === CP.eq) {
|
|
318
324
|
if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
|
|
319
325
|
return;
|
|
@@ -334,7 +340,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
334
340
|
while (queue.length > 0) {
|
|
335
341
|
const cur = queue.shift();
|
|
336
342
|
if (cur) {
|
|
337
|
-
const compared = this._compare(cur.
|
|
343
|
+
const compared = this._compare(cur.key, key);
|
|
338
344
|
if (compared === CP.eq) {
|
|
339
345
|
if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
|
|
340
346
|
return sum;
|
|
@@ -358,44 +364,43 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
358
364
|
/**
|
|
359
365
|
* The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
360
366
|
* have a greater value than a given node.
|
|
361
|
-
* @param {N |
|
|
362
|
-
* `
|
|
367
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type),
|
|
368
|
+
* `BinaryTreeNodeKey`, or `null`. It represents the node in the binary tree to which the delta value will be added.
|
|
363
369
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
364
370
|
* each greater node should be increased.
|
|
365
371
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
366
372
|
* specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
|
|
367
|
-
* '
|
|
373
|
+
* 'key'.
|
|
368
374
|
* @returns a boolean value.
|
|
369
375
|
*/
|
|
370
376
|
allGreaterNodesAdd(
|
|
371
|
-
node: N |
|
|
377
|
+
node: N | BinaryTreeNodeKey | null,
|
|
372
378
|
delta: number,
|
|
373
|
-
propertyName
|
|
379
|
+
propertyName: BinaryTreeNodePropertyName = 'key'
|
|
374
380
|
): boolean {
|
|
375
|
-
|
|
376
|
-
if (typeof node === 'number') node = this.get(node, 'id');
|
|
381
|
+
if (typeof node === 'number') node = this.get(node, 'key');
|
|
377
382
|
if (!node) return false;
|
|
378
|
-
const
|
|
383
|
+
const key = node.key;
|
|
379
384
|
if (!this.root) return false;
|
|
380
385
|
|
|
381
386
|
const _sumByPropertyName = (cur: N) => {
|
|
382
387
|
switch (propertyName) {
|
|
383
|
-
case '
|
|
384
|
-
cur.
|
|
388
|
+
case 'key':
|
|
389
|
+
cur.key += delta;
|
|
385
390
|
break;
|
|
386
391
|
default:
|
|
387
|
-
cur.
|
|
392
|
+
cur.key += delta;
|
|
388
393
|
break;
|
|
389
394
|
}
|
|
390
395
|
};
|
|
391
396
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
392
397
|
const _traverse = (cur: N) => {
|
|
393
|
-
const compared = this._compare(cur.
|
|
398
|
+
const compared = this._compare(cur.key, key);
|
|
394
399
|
if (compared === CP.gt) _sumByPropertyName(cur);
|
|
395
400
|
|
|
396
401
|
if (!cur.left && !cur.right) return;
|
|
397
|
-
if (cur.left && this._compare(cur.left.
|
|
398
|
-
if (cur.right && this._compare(cur.right.
|
|
402
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt) _traverse(cur.left);
|
|
403
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt) _traverse(cur.right);
|
|
399
404
|
};
|
|
400
405
|
|
|
401
406
|
_traverse(this.root);
|
|
@@ -405,11 +410,11 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
405
410
|
while (queue.length > 0) {
|
|
406
411
|
const cur = queue.shift();
|
|
407
412
|
if (cur) {
|
|
408
|
-
const compared = this._compare(cur.
|
|
413
|
+
const compared = this._compare(cur.key, key);
|
|
409
414
|
if (compared === CP.gt) _sumByPropertyName(cur);
|
|
410
415
|
|
|
411
|
-
if (cur.left && this._compare(cur.left.
|
|
412
|
-
if (cur.right && this._compare(cur.right.
|
|
416
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt) queue.push(cur.left);
|
|
417
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt) queue.push(cur.right);
|
|
413
418
|
}
|
|
414
419
|
}
|
|
415
420
|
return true;
|
|
@@ -432,7 +437,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
432
437
|
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
433
438
|
*/
|
|
434
439
|
perfectlyBalance(): boolean {
|
|
435
|
-
const sorted = this.
|
|
440
|
+
const sorted = this.dfs('in', 'node'),
|
|
436
441
|
n = sorted.length;
|
|
437
442
|
this.clear();
|
|
438
443
|
|
|
@@ -442,7 +447,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
442
447
|
if (l > r) return;
|
|
443
448
|
const m = l + Math.floor((r - l) / 2);
|
|
444
449
|
const midNode = sorted[m];
|
|
445
|
-
this.add(midNode.
|
|
450
|
+
this.add(midNode.key, midNode.val);
|
|
446
451
|
buildBalanceBST(l, m - 1);
|
|
447
452
|
buildBalanceBST(m + 1, r);
|
|
448
453
|
};
|
|
@@ -458,7 +463,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
458
463
|
if (l <= r) {
|
|
459
464
|
const m = l + Math.floor((r - l) / 2);
|
|
460
465
|
const midNode = sorted[m];
|
|
461
|
-
this.add(midNode.
|
|
466
|
+
this.add(midNode.key, midNode.val);
|
|
462
467
|
stack.push([m + 1, r]);
|
|
463
468
|
stack.push([l, m - 1]);
|
|
464
469
|
}
|
|
@@ -521,12 +526,12 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
521
526
|
/**
|
|
522
527
|
* The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
|
|
523
528
|
* greater than, less than, or equal to the second ID.
|
|
524
|
-
* @param {
|
|
525
|
-
* @param {
|
|
529
|
+
* @param {BinaryTreeNodeKey} a - "a" is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
|
|
530
|
+
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code refers to a BinaryTreeNodeKey.
|
|
526
531
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
|
|
527
532
|
* than), or CP.eq (equal).
|
|
528
533
|
*/
|
|
529
|
-
protected _compare(a:
|
|
534
|
+
protected _compare(a: BinaryTreeNodeKey, b: BinaryTreeNodeKey): CP {
|
|
530
535
|
const compared = this._comparator(a, b);
|
|
531
536
|
if (compared > 0) return CP.gt;
|
|
532
537
|
else if (compared < 0) return CP.lt;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types';
|
|
2
2
|
import {IRBTree, IRBTreeNode} from '../../interfaces';
|
|
3
3
|
import {BST, BSTNode} from './bst';
|
|
4
4
|
|
|
@@ -8,8 +8,8 @@ export class RBTreeNode<V = any, NEIGHBOR extends RBTreeNode<V, NEIGHBOR> = RBTr
|
|
|
8
8
|
{
|
|
9
9
|
private _color: RBColor;
|
|
10
10
|
|
|
11
|
-
constructor(
|
|
12
|
-
super(
|
|
11
|
+
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
12
|
+
super(key, val);
|
|
13
13
|
this._color = RBColor.RED;
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -27,12 +27,12 @@ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<
|
|
|
27
27
|
super(options);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
override createNode(
|
|
31
|
-
return new RBTreeNode(
|
|
30
|
+
override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
31
|
+
return new RBTreeNode(key, val) as N;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
// override add(
|
|
35
|
-
// const inserted = super.add(
|
|
34
|
+
// override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
|
35
|
+
// const inserted = super.add(keyOrNode, val);
|
|
36
36
|
// if (inserted) this._fixInsertViolation(inserted);
|
|
37
37
|
// return inserted;
|
|
38
38
|
// }
|
|
@@ -205,8 +205,8 @@ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<
|
|
|
205
205
|
// node.right = null;
|
|
206
206
|
// }
|
|
207
207
|
//
|
|
208
|
-
// override remove(
|
|
209
|
-
// const node = this.get(
|
|
208
|
+
// override remove(nodeOrKey: BinaryTreeNodeKey | N): BinaryTreeDeletedResult<N>[] {
|
|
209
|
+
// const node = this.get(nodeOrKey);
|
|
210
210
|
// const result: BinaryTreeDeletedResult<N>[] = [{deleted: undefined, needBalanced: null}];
|
|
211
211
|
// if (!node) return result; // Node does not exist
|
|
212
212
|
//
|