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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { CP, LoopType } from '../../types';
|
|
2
2
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
3
3
|
export class BSTNode extends BinaryTreeNode {
|
|
4
|
-
constructor(
|
|
5
|
-
super(
|
|
4
|
+
constructor(key, val) {
|
|
5
|
+
super(key, val);
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
export class BST extends BinaryTree {
|
|
@@ -21,36 +21,36 @@ export class BST extends BinaryTree {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
|
-
* The function creates a new binary search tree node with the given
|
|
25
|
-
* @param {
|
|
24
|
+
* The function creates a new binary search tree node with the given key and value.
|
|
25
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
26
26
|
* identify each node in the binary tree.
|
|
27
27
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
28
28
|
* that will be stored in the node.
|
|
29
|
-
* @returns a new instance of the BSTNode class with the specified
|
|
29
|
+
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
30
30
|
*/
|
|
31
|
-
createNode(
|
|
32
|
-
return new BSTNode(
|
|
31
|
+
createNode(key, val) {
|
|
32
|
+
return new BSTNode(key, val);
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* The `add` function adds a new node to a binary search tree, either by creating a new node or by updating an existing
|
|
36
36
|
* node with the same ID.
|
|
37
|
-
* @param {
|
|
37
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N`
|
|
38
38
|
* (which represents a binary tree node) or `null`.
|
|
39
39
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
40
40
|
* being added to the binary search tree.
|
|
41
41
|
* @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
|
|
42
42
|
*/
|
|
43
|
-
add(
|
|
43
|
+
add(keyOrNode, val) {
|
|
44
44
|
// TODO support node as a param
|
|
45
45
|
let inserted = null;
|
|
46
46
|
let newNode = null;
|
|
47
|
-
if (
|
|
48
|
-
newNode =
|
|
47
|
+
if (keyOrNode instanceof BSTNode) {
|
|
48
|
+
newNode = keyOrNode;
|
|
49
49
|
}
|
|
50
|
-
else if (typeof
|
|
51
|
-
newNode = this.createNode(
|
|
50
|
+
else if (typeof keyOrNode === 'number') {
|
|
51
|
+
newNode = this.createNode(keyOrNode, val);
|
|
52
52
|
}
|
|
53
|
-
else if (
|
|
53
|
+
else if (keyOrNode === null) {
|
|
54
54
|
newNode = null;
|
|
55
55
|
}
|
|
56
56
|
if (this.root === null) {
|
|
@@ -63,7 +63,7 @@ export class BST extends BinaryTree {
|
|
|
63
63
|
let traversing = true;
|
|
64
64
|
while (traversing) {
|
|
65
65
|
if (cur !== null && newNode !== null) {
|
|
66
|
-
if (this._compare(cur.
|
|
66
|
+
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
67
67
|
if (newNode) {
|
|
68
68
|
cur.val = newNode.val;
|
|
69
69
|
}
|
|
@@ -71,7 +71,7 @@ export class BST extends BinaryTree {
|
|
|
71
71
|
traversing = false;
|
|
72
72
|
inserted = cur;
|
|
73
73
|
}
|
|
74
|
-
else if (this._compare(cur.
|
|
74
|
+
else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
75
75
|
// Traverse left of the node
|
|
76
76
|
if (cur.left === undefined) {
|
|
77
77
|
if (newNode) {
|
|
@@ -89,7 +89,7 @@ export class BST extends BinaryTree {
|
|
|
89
89
|
cur = cur.left;
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
else if (this._compare(cur.
|
|
92
|
+
else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
93
93
|
// Traverse right of the node
|
|
94
94
|
if (cur.right === undefined) {
|
|
95
95
|
if (newNode) {
|
|
@@ -118,46 +118,46 @@ export class BST extends BinaryTree {
|
|
|
118
118
|
/**
|
|
119
119
|
* The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
|
|
120
120
|
* manner.
|
|
121
|
-
* @param {[
|
|
122
|
-
* `
|
|
121
|
+
* @param {[BinaryTreeNodeKey | N , N['val']][]} keysOrNodes - The `keysOrNodes` parameter in the `addMany` function is an array of
|
|
122
|
+
* `BinaryTreeNodeKey` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
|
|
123
123
|
* to the binary search tree.
|
|
124
124
|
* @param {N['val'][]} data - The values of tree nodes
|
|
125
125
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
126
126
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
127
127
|
*/
|
|
128
|
-
addMany(
|
|
128
|
+
addMany(keysOrNodes, data, isBalanceAdd = false) {
|
|
129
129
|
function hasNoNull(arr) {
|
|
130
130
|
return arr.indexOf(null) === -1;
|
|
131
131
|
}
|
|
132
|
-
if (!isBalanceAdd || !hasNoNull(
|
|
133
|
-
return super.addMany(
|
|
132
|
+
if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
|
|
133
|
+
return super.addMany(keysOrNodes, data);
|
|
134
134
|
}
|
|
135
135
|
const inserted = [];
|
|
136
|
-
const combinedArr =
|
|
136
|
+
const combinedArr = keysOrNodes.map((value, index) => [value, data === null || data === void 0 ? void 0 : data[index]]);
|
|
137
137
|
let sorted = [];
|
|
138
138
|
function isNodeOrNullTuple(arr) {
|
|
139
|
-
for (const [
|
|
140
|
-
if (
|
|
139
|
+
for (const [keyOrNode] of arr)
|
|
140
|
+
if (keyOrNode instanceof BSTNode)
|
|
141
141
|
return true;
|
|
142
142
|
return false;
|
|
143
143
|
}
|
|
144
|
-
function
|
|
145
|
-
for (const [
|
|
146
|
-
if (typeof
|
|
144
|
+
function isBinaryTreeKeyOrNullTuple(arr) {
|
|
145
|
+
for (const [keyOrNode] of arr)
|
|
146
|
+
if (typeof keyOrNode === 'number')
|
|
147
147
|
return true;
|
|
148
148
|
return false;
|
|
149
149
|
}
|
|
150
|
-
let
|
|
150
|
+
let sortedKeysOrNodes = [], sortedData = [];
|
|
151
151
|
if (isNodeOrNullTuple(combinedArr)) {
|
|
152
|
-
sorted = combinedArr.sort((a, b) => a[0].
|
|
152
|
+
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
153
153
|
}
|
|
154
|
-
else if (
|
|
154
|
+
else if (isBinaryTreeKeyOrNullTuple(combinedArr)) {
|
|
155
155
|
sorted = combinedArr.sort((a, b) => a[0] - b[0]);
|
|
156
156
|
}
|
|
157
157
|
else {
|
|
158
|
-
throw new Error('Invalid input
|
|
158
|
+
throw new Error('Invalid input keysOrNodes');
|
|
159
159
|
}
|
|
160
|
-
|
|
160
|
+
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
161
161
|
sortedData = sorted.map(([, val]) => val);
|
|
162
162
|
const recursive = (arr, data) => {
|
|
163
163
|
if (arr.length === 0)
|
|
@@ -177,7 +177,7 @@ export class BST extends BinaryTree {
|
|
|
177
177
|
const [l, r] = popped;
|
|
178
178
|
if (l <= r) {
|
|
179
179
|
const m = l + Math.floor((r - l) / 2);
|
|
180
|
-
const newNode = this.add(
|
|
180
|
+
const newNode = this.add(sortedKeysOrNodes[m], sortedData === null || sortedData === void 0 ? void 0 : sortedData[m]);
|
|
181
181
|
inserted.push(newNode);
|
|
182
182
|
stack.push([m + 1, r]);
|
|
183
183
|
stack.push([l, m - 1]);
|
|
@@ -186,7 +186,7 @@ export class BST extends BinaryTree {
|
|
|
186
186
|
}
|
|
187
187
|
};
|
|
188
188
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
189
|
-
recursive(
|
|
189
|
+
recursive(sortedKeysOrNodes, sortedData);
|
|
190
190
|
}
|
|
191
191
|
else {
|
|
192
192
|
iterative();
|
|
@@ -195,45 +195,44 @@ export class BST extends BinaryTree {
|
|
|
195
195
|
}
|
|
196
196
|
/**
|
|
197
197
|
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
198
|
-
* @param {
|
|
198
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
199
199
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
200
200
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
201
|
-
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'
|
|
202
|
-
* @returns The method is returning either a
|
|
201
|
+
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
|
|
202
|
+
* @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
|
|
203
203
|
*/
|
|
204
|
-
get(nodeProperty, propertyName) {
|
|
204
|
+
get(nodeProperty, propertyName = 'key') {
|
|
205
205
|
var _a;
|
|
206
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
207
206
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
208
207
|
}
|
|
209
208
|
/**
|
|
210
|
-
* The function returns the
|
|
211
|
-
* leftmost node if the comparison is greater than, and the
|
|
212
|
-
* @returns The method `lastKey()` returns the
|
|
213
|
-
* the values at index 0 and 1 is less than, otherwise it returns the
|
|
214
|
-
* equal, it returns the
|
|
209
|
+
* The function returns the key of the rightmost node if the comparison between two values is less than, the key of the
|
|
210
|
+
* leftmost node if the comparison is greater than, and the key of the rightmost node otherwise.
|
|
211
|
+
* @returns The method `lastKey()` returns the key of the rightmost node in the binary tree if the comparison between
|
|
212
|
+
* the values at index 0 and 1 is less than, otherwise it returns the key of the leftmost node. If the comparison is
|
|
213
|
+
* equal, it returns the key of the rightmost node. If there are no nodes in the tree, it returns 0.
|
|
215
214
|
*/
|
|
216
215
|
lastKey() {
|
|
217
216
|
var _a, _b, _c, _d, _e, _f;
|
|
218
217
|
if (this._compare(0, 1) === CP.lt)
|
|
219
|
-
return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.
|
|
218
|
+
return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
|
|
220
219
|
else if (this._compare(0, 1) === CP.gt)
|
|
221
|
-
return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.
|
|
220
|
+
return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : 0;
|
|
222
221
|
else
|
|
223
|
-
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.
|
|
222
|
+
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
|
|
224
223
|
}
|
|
225
224
|
/**
|
|
226
225
|
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
227
|
-
* @param {
|
|
226
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
|
|
228
227
|
* `N` type. It represents the property of the binary tree node that you want to compare with.
|
|
229
228
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
230
|
-
* specifies the property name to use for comparison. If not provided, it defaults to `'
|
|
229
|
+
* specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
|
|
231
230
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
232
231
|
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
233
232
|
* is set to `true`, the function will return an array with only one node (if
|
|
234
233
|
* @returns an array of nodes (type N).
|
|
235
234
|
*/
|
|
236
|
-
getNodes(nodeProperty, propertyName = '
|
|
235
|
+
getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
|
|
237
236
|
if (!this.root)
|
|
238
237
|
return [];
|
|
239
238
|
const result = [];
|
|
@@ -243,10 +242,10 @@ export class BST extends BinaryTree {
|
|
|
243
242
|
return;
|
|
244
243
|
if (!cur.left && !cur.right)
|
|
245
244
|
return;
|
|
246
|
-
if (propertyName === '
|
|
247
|
-
if (this._compare(cur.
|
|
245
|
+
if (propertyName === 'key') {
|
|
246
|
+
if (this._compare(cur.key, nodeProperty) === CP.gt)
|
|
248
247
|
cur.left && _traverse(cur.left);
|
|
249
|
-
if (this._compare(cur.
|
|
248
|
+
if (this._compare(cur.key, nodeProperty) === CP.lt)
|
|
250
249
|
cur.right && _traverse(cur.right);
|
|
251
250
|
}
|
|
252
251
|
else {
|
|
@@ -263,10 +262,10 @@ export class BST extends BinaryTree {
|
|
|
263
262
|
if (cur) {
|
|
264
263
|
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
265
264
|
return result;
|
|
266
|
-
if (propertyName === '
|
|
267
|
-
if (this._compare(cur.
|
|
265
|
+
if (propertyName === 'key') {
|
|
266
|
+
if (this._compare(cur.key, nodeProperty) === CP.gt)
|
|
268
267
|
cur.left && queue.push(cur.left);
|
|
269
|
-
if (this._compare(cur.
|
|
268
|
+
if (this._compare(cur.key, nodeProperty) === CP.lt)
|
|
270
269
|
cur.right && queue.push(cur.right);
|
|
271
270
|
}
|
|
272
271
|
else {
|
|
@@ -282,29 +281,28 @@ export class BST extends BinaryTree {
|
|
|
282
281
|
/**
|
|
283
282
|
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
|
|
284
283
|
* less than a given node.
|
|
285
|
-
* @param {N |
|
|
284
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
286
285
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
287
|
-
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'
|
|
286
|
+
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'key'`.
|
|
288
287
|
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
|
|
289
288
|
* binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
|
|
290
289
|
*/
|
|
291
|
-
lesserSum(beginNode, propertyName) {
|
|
292
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
290
|
+
lesserSum(beginNode, propertyName = 'key') {
|
|
293
291
|
if (typeof beginNode === 'number')
|
|
294
|
-
beginNode = this.get(beginNode, '
|
|
292
|
+
beginNode = this.get(beginNode, 'key');
|
|
295
293
|
if (!beginNode)
|
|
296
294
|
return 0;
|
|
297
295
|
if (!this.root)
|
|
298
296
|
return 0;
|
|
299
|
-
const
|
|
297
|
+
const key = beginNode.key;
|
|
300
298
|
const getSumByPropertyName = (cur) => {
|
|
301
299
|
let needSum;
|
|
302
300
|
switch (propertyName) {
|
|
303
|
-
case '
|
|
304
|
-
needSum = cur.
|
|
301
|
+
case 'key':
|
|
302
|
+
needSum = cur.key;
|
|
305
303
|
break;
|
|
306
304
|
default:
|
|
307
|
-
needSum = cur.
|
|
305
|
+
needSum = cur.key;
|
|
308
306
|
break;
|
|
309
307
|
}
|
|
310
308
|
return needSum;
|
|
@@ -312,7 +310,7 @@ export class BST extends BinaryTree {
|
|
|
312
310
|
let sum = 0;
|
|
313
311
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
314
312
|
const _traverse = (cur) => {
|
|
315
|
-
const compared = this._compare(cur.
|
|
313
|
+
const compared = this._compare(cur.key, key);
|
|
316
314
|
if (compared === CP.eq) {
|
|
317
315
|
if (cur.right)
|
|
318
316
|
sum += this.subTreeSum(cur.right, propertyName);
|
|
@@ -341,7 +339,7 @@ export class BST extends BinaryTree {
|
|
|
341
339
|
while (queue.length > 0) {
|
|
342
340
|
const cur = queue.shift();
|
|
343
341
|
if (cur) {
|
|
344
|
-
const compared = this._compare(cur.
|
|
342
|
+
const compared = this._compare(cur.key, key);
|
|
345
343
|
if (compared === CP.eq) {
|
|
346
344
|
if (cur.right)
|
|
347
345
|
sum += this.subTreeSum(cur.right, propertyName);
|
|
@@ -371,44 +369,43 @@ export class BST extends BinaryTree {
|
|
|
371
369
|
/**
|
|
372
370
|
* The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
373
371
|
* have a greater value than a given node.
|
|
374
|
-
* @param {N |
|
|
375
|
-
* `
|
|
372
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type),
|
|
373
|
+
* `BinaryTreeNodeKey`, or `null`. It represents the node in the binary tree to which the delta value will be added.
|
|
376
374
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
377
375
|
* each greater node should be increased.
|
|
378
376
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
379
377
|
* specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
|
|
380
|
-
* '
|
|
378
|
+
* 'key'.
|
|
381
379
|
* @returns a boolean value.
|
|
382
380
|
*/
|
|
383
|
-
allGreaterNodesAdd(node, delta, propertyName) {
|
|
384
|
-
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
381
|
+
allGreaterNodesAdd(node, delta, propertyName = 'key') {
|
|
385
382
|
if (typeof node === 'number')
|
|
386
|
-
node = this.get(node, '
|
|
383
|
+
node = this.get(node, 'key');
|
|
387
384
|
if (!node)
|
|
388
385
|
return false;
|
|
389
|
-
const
|
|
386
|
+
const key = node.key;
|
|
390
387
|
if (!this.root)
|
|
391
388
|
return false;
|
|
392
389
|
const _sumByPropertyName = (cur) => {
|
|
393
390
|
switch (propertyName) {
|
|
394
|
-
case '
|
|
395
|
-
cur.
|
|
391
|
+
case 'key':
|
|
392
|
+
cur.key += delta;
|
|
396
393
|
break;
|
|
397
394
|
default:
|
|
398
|
-
cur.
|
|
395
|
+
cur.key += delta;
|
|
399
396
|
break;
|
|
400
397
|
}
|
|
401
398
|
};
|
|
402
399
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
403
400
|
const _traverse = (cur) => {
|
|
404
|
-
const compared = this._compare(cur.
|
|
401
|
+
const compared = this._compare(cur.key, key);
|
|
405
402
|
if (compared === CP.gt)
|
|
406
403
|
_sumByPropertyName(cur);
|
|
407
404
|
if (!cur.left && !cur.right)
|
|
408
405
|
return;
|
|
409
|
-
if (cur.left && this._compare(cur.left.
|
|
406
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt)
|
|
410
407
|
_traverse(cur.left);
|
|
411
|
-
if (cur.right && this._compare(cur.right.
|
|
408
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt)
|
|
412
409
|
_traverse(cur.right);
|
|
413
410
|
};
|
|
414
411
|
_traverse(this.root);
|
|
@@ -419,12 +416,12 @@ export class BST extends BinaryTree {
|
|
|
419
416
|
while (queue.length > 0) {
|
|
420
417
|
const cur = queue.shift();
|
|
421
418
|
if (cur) {
|
|
422
|
-
const compared = this._compare(cur.
|
|
419
|
+
const compared = this._compare(cur.key, key);
|
|
423
420
|
if (compared === CP.gt)
|
|
424
421
|
_sumByPropertyName(cur);
|
|
425
|
-
if (cur.left && this._compare(cur.left.
|
|
422
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt)
|
|
426
423
|
queue.push(cur.left);
|
|
427
|
-
if (cur.right && this._compare(cur.right.
|
|
424
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt)
|
|
428
425
|
queue.push(cur.right);
|
|
429
426
|
}
|
|
430
427
|
}
|
|
@@ -446,7 +443,7 @@ export class BST extends BinaryTree {
|
|
|
446
443
|
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
447
444
|
*/
|
|
448
445
|
perfectlyBalance() {
|
|
449
|
-
const sorted = this.
|
|
446
|
+
const sorted = this.dfs('in', 'node'), n = sorted.length;
|
|
450
447
|
this.clear();
|
|
451
448
|
if (sorted.length < 1)
|
|
452
449
|
return false;
|
|
@@ -456,7 +453,7 @@ export class BST extends BinaryTree {
|
|
|
456
453
|
return;
|
|
457
454
|
const m = l + Math.floor((r - l) / 2);
|
|
458
455
|
const midNode = sorted[m];
|
|
459
|
-
this.add(midNode.
|
|
456
|
+
this.add(midNode.key, midNode.val);
|
|
460
457
|
buildBalanceBST(l, m - 1);
|
|
461
458
|
buildBalanceBST(m + 1, r);
|
|
462
459
|
};
|
|
@@ -472,7 +469,7 @@ export class BST extends BinaryTree {
|
|
|
472
469
|
if (l <= r) {
|
|
473
470
|
const m = l + Math.floor((r - l) / 2);
|
|
474
471
|
const midNode = sorted[m];
|
|
475
|
-
this.add(midNode.
|
|
472
|
+
this.add(midNode.key, midNode.val);
|
|
476
473
|
stack.push([m + 1, r]);
|
|
477
474
|
stack.push([l, m - 1]);
|
|
478
475
|
}
|
|
@@ -534,8 +531,8 @@ export class BST extends BinaryTree {
|
|
|
534
531
|
/**
|
|
535
532
|
* The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
|
|
536
533
|
* greater than, less than, or equal to the second ID.
|
|
537
|
-
* @param {
|
|
538
|
-
* @param {
|
|
534
|
+
* @param {BinaryTreeNodeKey} a - "a" is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
|
|
535
|
+
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code refers to a BinaryTreeNodeKey.
|
|
539
536
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
|
|
540
537
|
* than), or CP.eq (equal).
|
|
541
538
|
*/
|
|
@@ -1,13 +1,13 @@
|
|
|
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
|
export declare class RBTreeNode<V = any, NEIGHBOR extends RBTreeNode<V, NEIGHBOR> = RBTreeNodeNested<V>> extends BSTNode<V, NEIGHBOR> implements IRBTreeNode<V, NEIGHBOR> {
|
|
5
5
|
private _color;
|
|
6
|
-
constructor(
|
|
6
|
+
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
7
7
|
get color(): RBColor;
|
|
8
8
|
set color(value: RBColor);
|
|
9
9
|
}
|
|
10
10
|
export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
|
|
11
11
|
constructor(options?: RBTreeOptions);
|
|
12
|
-
createNode(
|
|
12
|
+
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
13
13
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { RBColor } from '../../types';
|
|
2
2
|
import { BST, BSTNode } from './bst';
|
|
3
3
|
export class RBTreeNode extends BSTNode {
|
|
4
|
-
constructor(
|
|
5
|
-
super(
|
|
4
|
+
constructor(key, val) {
|
|
5
|
+
super(key, val);
|
|
6
6
|
this._color = RBColor.RED;
|
|
7
7
|
}
|
|
8
8
|
get color() {
|
|
@@ -16,7 +16,7 @@ export class RBTree extends BST {
|
|
|
16
16
|
constructor(options) {
|
|
17
17
|
super(options);
|
|
18
18
|
}
|
|
19
|
-
createNode(
|
|
20
|
-
return new RBTreeNode(
|
|
19
|
+
createNode(key, val) {
|
|
20
|
+
return new RBTreeNode(key, val);
|
|
21
21
|
}
|
|
22
22
|
}
|