data-structure-typed 1.39.5 → 1.39.6
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 +2 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +17 -17
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/bst.js +13 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/cjs/data-structures/graph/abstract-graph.js +24 -24
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +12 -12
- package/dist/cjs/data-structures/graph/directed-graph.js +15 -15
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +9 -9
- package/dist/cjs/data-structures/graph/map-graph.js +13 -13
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +11 -11
- package/dist/cjs/data-structures/graph/undirected-graph.js +14 -14
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/cjs/data-structures/hash/hash-table.js +8 -8
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +54 -54
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +52 -52
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/interfaces/graph.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +18 -18
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/bst.js +13 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/mjs/data-structures/graph/abstract-graph.js +26 -26
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +12 -12
- package/dist/mjs/data-structures/graph/directed-graph.js +15 -15
- package/dist/mjs/data-structures/graph/map-graph.d.ts +9 -9
- package/dist/mjs/data-structures/graph/map-graph.js +13 -13
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +11 -11
- package/dist/mjs/data-structures/graph/undirected-graph.js +14 -14
- package/dist/mjs/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/mjs/data-structures/hash/hash-table.js +9 -9
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +55 -55
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +53 -53
- package/dist/mjs/data-structures/queue/queue.js +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/interfaces/graph.d.ts +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 +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +13 -13
- package/src/data-structures/binary-tree/binary-tree.ts +18 -18
- package/src/data-structures/binary-tree/bst.ts +16 -16
- package/src/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/data-structures/binary-tree/segment-tree.ts +15 -15
- package/src/data-structures/binary-tree/tree-multiset.ts +18 -18
- package/src/data-structures/graph/abstract-graph.ts +34 -34
- package/src/data-structures/graph/directed-graph.ts +16 -16
- package/src/data-structures/graph/map-graph.ts +13 -13
- package/src/data-structures/graph/undirected-graph.ts +15 -15
- package/src/data-structures/hash/hash-table.ts +9 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +61 -61
- package/src/data-structures/linked-list/singly-linked-list.ts +58 -58
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +2 -2
- package/test/integration/bst.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/bst.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -2
- package/test/unit/data-structures/graph/abstract-graph.test.ts +99 -2
- package/test/unit/data-structures/graph/directed-graph.test.ts +22 -12
- package/test/unit/data-structures/graph/undirected-graph.test.ts +8 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +15 -15
- package/test/utils/big-o.ts +7 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.39.
|
|
3
|
+
"version": "1.39.6",
|
|
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",
|
|
@@ -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.39.
|
|
64
|
+
"avl-tree-typed": "^1.39.5",
|
|
65
65
|
"benchmark": "^2.1.4",
|
|
66
|
-
"binary-tree-typed": "^1.39.
|
|
67
|
-
"bst-typed": "^1.39.
|
|
66
|
+
"binary-tree-typed": "^1.39.5",
|
|
67
|
+
"bst-typed": "^1.39.5",
|
|
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.39.
|
|
74
|
+
"heap-typed": "^1.39.5",
|
|
75
75
|
"istanbul-badges-readme": "^1.8.5",
|
|
76
76
|
"jest": "^29.7.0",
|
|
77
77
|
"prettier": "^3.0.3",
|
|
@@ -13,8 +13,8 @@ import {IBinaryTree} from '../../interfaces';
|
|
|
13
13
|
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
14
14
|
height: number;
|
|
15
15
|
|
|
16
|
-
constructor(key: BTNKey,
|
|
17
|
-
super(key,
|
|
16
|
+
constructor(key: BTNKey, value?: V) {
|
|
17
|
+
super(key, value);
|
|
18
18
|
this.height = 0;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -37,13 +37,13 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
37
37
|
* The function creates a new AVL tree node with the specified key and value.
|
|
38
38
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
39
39
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
40
|
-
* @param [
|
|
41
|
-
* type `V`, which means it can be any value that is assignable to the `
|
|
40
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
41
|
+
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
42
42
|
* node type `N`.
|
|
43
43
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
44
44
|
*/
|
|
45
|
-
override createNode(key: BTNKey,
|
|
46
|
-
return new AVLTreeNode<V, N>(key,
|
|
45
|
+
override createNode(key: BTNKey, value?: V): N {
|
|
46
|
+
return new AVLTreeNode<V, N>(key, value) as N;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
@@ -51,13 +51,13 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
51
51
|
* a new node.
|
|
52
52
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
53
53
|
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
54
|
-
* @param [
|
|
54
|
+
* @param [value] - The `value` parameter is the value that you want to assign to the new node that you
|
|
55
55
|
* are adding to the binary search tree.
|
|
56
56
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
57
57
|
*/
|
|
58
|
-
override add(keyOrNode: BTNKey | N | null,
|
|
58
|
+
override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
59
59
|
// TODO support node as a param
|
|
60
|
-
const inserted = super.add(keyOrNode,
|
|
60
|
+
const inserted = super.add(keyOrNode, value);
|
|
61
61
|
if (inserted) this._balancePath(inserted);
|
|
62
62
|
return inserted;
|
|
63
63
|
}
|
|
@@ -97,18 +97,18 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
97
97
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
98
98
|
*/
|
|
99
99
|
protected override _swap(srcNode: N, destNode: N): N {
|
|
100
|
-
const {key,
|
|
101
|
-
const tempNode = this.createNode(key,
|
|
100
|
+
const {key, value, height} = destNode;
|
|
101
|
+
const tempNode = this.createNode(key, value);
|
|
102
102
|
|
|
103
103
|
if (tempNode) {
|
|
104
104
|
tempNode.height = height;
|
|
105
105
|
|
|
106
106
|
destNode.key = srcNode.key;
|
|
107
|
-
destNode.
|
|
107
|
+
destNode.value = srcNode.value;
|
|
108
108
|
destNode.height = srcNode.height;
|
|
109
109
|
|
|
110
110
|
srcNode.key = tempNode.key;
|
|
111
|
-
srcNode.
|
|
111
|
+
srcNode.value = tempNode.value;
|
|
112
112
|
srcNode.height = tempNode.height;
|
|
113
113
|
}
|
|
114
114
|
|
|
@@ -26,7 +26,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
26
26
|
/**
|
|
27
27
|
* The value stored in the node.
|
|
28
28
|
*/
|
|
29
|
-
|
|
29
|
+
value: V | undefined;
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* The parent node of the current node.
|
|
@@ -36,11 +36,11 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
36
36
|
/**
|
|
37
37
|
* Creates a new instance of BinaryTreeNode.
|
|
38
38
|
* @param {BTNKey} key - The key associated with the node.
|
|
39
|
-
* @param {V}
|
|
39
|
+
* @param {V} value - The value stored in the node.
|
|
40
40
|
*/
|
|
41
|
-
constructor(key: BTNKey,
|
|
41
|
+
constructor(key: BTNKey, value?: V) {
|
|
42
42
|
this.key = key;
|
|
43
|
-
this.
|
|
43
|
+
this.value = value;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
private _left: N | null | undefined;
|
|
@@ -159,11 +159,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
159
159
|
/**
|
|
160
160
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
161
161
|
* @param {BTNKey} key - The key for the new node.
|
|
162
|
-
* @param {V}
|
|
162
|
+
* @param {V} value - The value for the new node.
|
|
163
163
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
164
164
|
*/
|
|
165
|
-
createNode(key: BTNKey,
|
|
166
|
-
return new BinaryTreeNode<V, N>(key,
|
|
165
|
+
createNode(key: BTNKey, value?: V): N {
|
|
166
|
+
return new BinaryTreeNode<V, N>(key, value) as N;
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
/**
|
|
@@ -185,10 +185,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
185
185
|
/**
|
|
186
186
|
* Add a node with the given key and value to the binary tree.
|
|
187
187
|
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
188
|
-
* @param {V}
|
|
188
|
+
* @param {V} value - The value for the new node (optional).
|
|
189
189
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
190
190
|
*/
|
|
191
|
-
add(keyOrNode: BTNKey | N | null,
|
|
191
|
+
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
192
192
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
193
193
|
const queue = new Queue<N | null>([root]);
|
|
194
194
|
while (queue.size > 0) {
|
|
@@ -209,7 +209,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
209
209
|
if (keyOrNode === null) {
|
|
210
210
|
needInsert = null;
|
|
211
211
|
} else if (typeof keyOrNode === 'number') {
|
|
212
|
-
needInsert = this.createNode(keyOrNode,
|
|
212
|
+
needInsert = this.createNode(keyOrNode, value);
|
|
213
213
|
} else if (keyOrNode instanceof BinaryTreeNode) {
|
|
214
214
|
needInsert = keyOrNode;
|
|
215
215
|
} else {
|
|
@@ -221,7 +221,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
221
221
|
|
|
222
222
|
if (this.root) {
|
|
223
223
|
if (existNode) {
|
|
224
|
-
existNode.
|
|
224
|
+
existNode.value = value;
|
|
225
225
|
inserted = existNode;
|
|
226
226
|
} else {
|
|
227
227
|
inserted = _bfs(this.root, needInsert);
|
|
@@ -252,15 +252,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
252
252
|
// TODO not sure addMany not be run multi times
|
|
253
253
|
return keysOrNodes.map((keyOrNode, i) => {
|
|
254
254
|
if (keyOrNode instanceof BinaryTreeNode) {
|
|
255
|
-
return this.add(keyOrNode.key, keyOrNode.
|
|
255
|
+
return this.add(keyOrNode.key, keyOrNode.value);
|
|
256
256
|
}
|
|
257
257
|
|
|
258
258
|
if (keyOrNode === null) {
|
|
259
259
|
return this.add(null);
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
-
const
|
|
263
|
-
return this.add(keyOrNode,
|
|
262
|
+
const value = values?.[i];
|
|
263
|
+
return this.add(keyOrNode, value);
|
|
264
264
|
});
|
|
265
265
|
}
|
|
266
266
|
|
|
@@ -1212,15 +1212,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1212
1212
|
* @returns {N} - The destination node after the swap.
|
|
1213
1213
|
*/
|
|
1214
1214
|
protected _swap(srcNode: N, destNode: N): N {
|
|
1215
|
-
const {key,
|
|
1216
|
-
const tempNode = this.createNode(key,
|
|
1215
|
+
const {key, value} = destNode;
|
|
1216
|
+
const tempNode = this.createNode(key, value);
|
|
1217
1217
|
|
|
1218
1218
|
if (tempNode) {
|
|
1219
1219
|
destNode.key = srcNode.key;
|
|
1220
|
-
destNode.
|
|
1220
|
+
destNode.value = srcNode.value;
|
|
1221
1221
|
|
|
1222
1222
|
srcNode.key = tempNode.key;
|
|
1223
|
-
srcNode.
|
|
1223
|
+
srcNode.value = tempNode.value;
|
|
1224
1224
|
}
|
|
1225
1225
|
|
|
1226
1226
|
return destNode;
|
|
@@ -12,8 +12,8 @@ import {IBinaryTree} from '../../interfaces';
|
|
|
12
12
|
import {Queue} from '../queue';
|
|
13
13
|
|
|
14
14
|
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
15
|
-
constructor(key: BTNKey,
|
|
16
|
-
super(key,
|
|
15
|
+
constructor(key: BTNKey, value?: V) {
|
|
16
|
+
super(key, value);
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
@@ -41,12 +41,12 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
41
41
|
* The function creates a new binary search tree node with the given key and value.
|
|
42
42
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
43
43
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
44
|
-
* @param [
|
|
44
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
|
|
45
45
|
* represents the value associated with the node in a binary search tree.
|
|
46
46
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
47
47
|
*/
|
|
48
|
-
override createNode(key: BTNKey,
|
|
49
|
-
return new BSTNode<V, N>(key,
|
|
48
|
+
override createNode(key: BTNKey, value?: V): N {
|
|
49
|
+
return new BSTNode<V, N>(key, value) as N;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
/**
|
|
@@ -54,19 +54,19 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
54
54
|
* into the tree.
|
|
55
55
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
56
56
|
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
57
|
-
* @param [
|
|
57
|
+
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
|
58
58
|
* binary search tree.
|
|
59
59
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
60
60
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
61
61
|
*/
|
|
62
|
-
override add(keyOrNode: BTNKey | N | null,
|
|
62
|
+
override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
63
63
|
// TODO support node as a parameter
|
|
64
64
|
let inserted: N | null = null;
|
|
65
65
|
let newNode: N | null = null;
|
|
66
66
|
if (keyOrNode instanceof BSTNode) {
|
|
67
67
|
newNode = keyOrNode;
|
|
68
68
|
} else if (typeof keyOrNode === 'number') {
|
|
69
|
-
newNode = this.createNode(keyOrNode,
|
|
69
|
+
newNode = this.createNode(keyOrNode, value);
|
|
70
70
|
} else if (keyOrNode === null) {
|
|
71
71
|
newNode = null;
|
|
72
72
|
}
|
|
@@ -81,7 +81,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
81
81
|
if (cur !== null && newNode !== null) {
|
|
82
82
|
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
83
83
|
if (newNode) {
|
|
84
|
-
cur.
|
|
84
|
+
cur.value = newNode.value;
|
|
85
85
|
}
|
|
86
86
|
//Duplicates are not accepted.
|
|
87
87
|
traversing = false;
|
|
@@ -128,7 +128,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
128
128
|
/**
|
|
129
129
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
130
130
|
* maintaining balance.
|
|
131
|
-
* @param {[BTNKey | N, N['
|
|
131
|
+
* @param {[BTNKey | N, N['value']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
132
132
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
133
133
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
134
134
|
* `null
|
|
@@ -154,15 +154,15 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
154
154
|
return super.addMany(keysOrNodes, data);
|
|
155
155
|
}
|
|
156
156
|
const inserted: (N | null | undefined)[] = [];
|
|
157
|
-
const combinedArr: [BTNKey | N, N['
|
|
157
|
+
const combinedArr: [BTNKey | N, N['value']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
|
|
158
158
|
let sorted = [];
|
|
159
159
|
|
|
160
|
-
function isNodeOrNullTuple(arr: [BTNKey | N, N['
|
|
160
|
+
function isNodeOrNullTuple(arr: [BTNKey | N, N['value']][]): arr is [N, N['value']][] {
|
|
161
161
|
for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
|
|
162
162
|
return false;
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N, N['
|
|
165
|
+
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N, N['value']][]): arr is [BTNKey, N['value']][] {
|
|
166
166
|
for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
|
|
167
167
|
return false;
|
|
168
168
|
}
|
|
@@ -178,7 +178,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
178
178
|
throw new Error('Invalid input keysOrNodes');
|
|
179
179
|
}
|
|
180
180
|
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
181
|
-
sortedData = sorted.map(([,
|
|
181
|
+
sortedData = sorted.map(([, value]) => value);
|
|
182
182
|
const recursive = (arr: (BTNKey | null | N)[], data?: (V | undefined)[]) => {
|
|
183
183
|
if (arr.length === 0) return;
|
|
184
184
|
|
|
@@ -426,7 +426,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
426
426
|
if (l > r) return;
|
|
427
427
|
const m = l + Math.floor((r - l) / 2);
|
|
428
428
|
const midNode = sorted[m];
|
|
429
|
-
this.add(midNode.key, midNode.
|
|
429
|
+
this.add(midNode.key, midNode.value);
|
|
430
430
|
buildBalanceBST(l, m - 1);
|
|
431
431
|
buildBalanceBST(m + 1, r);
|
|
432
432
|
};
|
|
@@ -442,7 +442,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
442
442
|
if (l <= r) {
|
|
443
443
|
const m = l + Math.floor((r - l) / 2);
|
|
444
444
|
const midNode = sorted[m];
|
|
445
|
-
this.add(midNode.key, midNode.
|
|
445
|
+
this.add(midNode.key, midNode.value);
|
|
446
446
|
stack.push([m + 1, r]);
|
|
447
447
|
stack.push([l, m - 1]);
|
|
448
448
|
}
|
|
@@ -3,8 +3,8 @@ import {IBinaryTree} from '../../interfaces';
|
|
|
3
3
|
import {BST, BSTNode} from './bst';
|
|
4
4
|
|
|
5
5
|
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
6
|
-
constructor(key: BTNKey,
|
|
7
|
-
super(key,
|
|
6
|
+
constructor(key: BTNKey, value?: V) {
|
|
7
|
+
super(key, value);
|
|
8
8
|
this._color = RBColor.RED;
|
|
9
9
|
}
|
|
10
10
|
|
|
@@ -27,12 +27,12 @@ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNest
|
|
|
27
27
|
super(options);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
override createNode(key: BTNKey,
|
|
31
|
-
return new RBTreeNode(key,
|
|
30
|
+
override createNode(key: BTNKey, value?: V): N {
|
|
31
|
+
return new RBTreeNode(key, value) as N;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
// override add(keyOrNode: BTNKey | N | null,
|
|
35
|
-
// const inserted = super.add(keyOrNode,
|
|
34
|
+
// override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
35
|
+
// const inserted = super.add(keyOrNode, value);
|
|
36
36
|
// if (inserted) this._fixInsertViolation(inserted);
|
|
37
37
|
// return inserted;
|
|
38
38
|
// }
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
import type {SegmentTreeNodeVal} from '../../types';
|
|
10
10
|
|
|
11
11
|
export class SegmentTreeNode {
|
|
12
|
-
constructor(start: number, end: number, sum: number,
|
|
12
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | null) {
|
|
13
13
|
this._start = start;
|
|
14
14
|
this._end = end;
|
|
15
15
|
this._sum = sum;
|
|
16
|
-
this.
|
|
16
|
+
this._value = value || null;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
private _start = 0;
|
|
@@ -35,14 +35,14 @@ export class SegmentTreeNode {
|
|
|
35
35
|
this._end = v;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
private
|
|
38
|
+
private _value: SegmentTreeNodeVal | null = null;
|
|
39
39
|
|
|
40
|
-
get
|
|
41
|
-
return this.
|
|
40
|
+
get value(): SegmentTreeNodeVal | null {
|
|
41
|
+
return this._value;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
set
|
|
45
|
-
this.
|
|
44
|
+
set value(v: SegmentTreeNodeVal | null) {
|
|
45
|
+
this._value = v;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
private _sum = 0;
|
|
@@ -154,30 +154,30 @@ export class SegmentTree {
|
|
|
154
154
|
* updated.
|
|
155
155
|
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
156
156
|
* the `SegmentTreeNode` at the specified `index`.
|
|
157
|
-
* @param {SegmentTreeNodeVal} [
|
|
157
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
158
158
|
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
159
|
-
* cur.
|
|
159
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
160
160
|
* @returns The function does not return anything.
|
|
161
161
|
*/
|
|
162
|
-
updateNode(index: number, sum: number,
|
|
162
|
+
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal) {
|
|
163
163
|
const root = this.root || null;
|
|
164
164
|
if (!root) {
|
|
165
165
|
return;
|
|
166
166
|
}
|
|
167
|
-
const dfs = (cur: SegmentTreeNode, index: number, sum: number,
|
|
167
|
+
const dfs = (cur: SegmentTreeNode, index: number, sum: number, value?: SegmentTreeNodeVal) => {
|
|
168
168
|
if (cur.start === cur.end && cur.start === index) {
|
|
169
169
|
cur.sum = sum;
|
|
170
|
-
if (
|
|
170
|
+
if (value !== undefined) cur.value = value;
|
|
171
171
|
return;
|
|
172
172
|
}
|
|
173
173
|
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
174
174
|
if (index <= mid) {
|
|
175
175
|
if (cur.left) {
|
|
176
|
-
dfs(cur.left, index, sum,
|
|
176
|
+
dfs(cur.left, index, sum, value);
|
|
177
177
|
}
|
|
178
178
|
} else {
|
|
179
179
|
if (cur.right) {
|
|
180
|
-
dfs(cur.right, index, sum,
|
|
180
|
+
dfs(cur.right, index, sum, value);
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
if (cur.left && cur.right) {
|
|
@@ -185,7 +185,7 @@ export class SegmentTree {
|
|
|
185
185
|
}
|
|
186
186
|
};
|
|
187
187
|
|
|
188
|
-
dfs(root, index, sum,
|
|
188
|
+
dfs(root, index, sum, value);
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
/**
|
|
@@ -20,14 +20,14 @@ export class TreeMultisetNode<
|
|
|
20
20
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
21
21
|
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
22
22
|
* of the binary tree node.
|
|
23
|
-
* @param {V} [
|
|
23
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
24
24
|
* tree node. If no value is provided, it will be `undefined`.
|
|
25
25
|
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
26
26
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
27
27
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
28
28
|
*/
|
|
29
|
-
constructor(key: BTNKey,
|
|
30
|
-
super(key,
|
|
29
|
+
constructor(key: BTNKey, value?: V, count = 1) {
|
|
30
|
+
super(key, value);
|
|
31
31
|
this.count = count;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -59,13 +59,13 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
59
59
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
60
60
|
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
61
61
|
* distinguish one node from another in the tree.
|
|
62
|
-
* @param {N}
|
|
62
|
+
* @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
|
63
63
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
64
64
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
65
65
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
66
66
|
*/
|
|
67
|
-
override createNode(key: BTNKey,
|
|
68
|
-
return new TreeMultisetNode(key,
|
|
67
|
+
override createNode(key: BTNKey, value?: V, count?: number): N {
|
|
68
|
+
return new TreeMultisetNode(key, value, count) as N;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
@@ -74,22 +74,22 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
74
74
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
75
75
|
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
76
76
|
* node to be added), or `null` (which represents a null node).
|
|
77
|
-
* @param [
|
|
77
|
+
* @param [value] - The `value` parameter represents the value associated with the key that is being
|
|
78
78
|
* added to the binary tree.
|
|
79
79
|
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
80
80
|
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
81
81
|
* count is specified, the default count will be 1.
|
|
82
82
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
83
83
|
*/
|
|
84
|
-
override add(keyOrNode: BTNKey | N | null,
|
|
84
|
+
override add(keyOrNode: BTNKey | N | null, value?: V, count = 1): N | null | undefined {
|
|
85
85
|
let inserted: N | null | undefined = undefined,
|
|
86
86
|
newNode: N | null;
|
|
87
87
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
88
|
-
newNode = this.createNode(keyOrNode.key, keyOrNode.
|
|
88
|
+
newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
|
|
89
89
|
} else if (keyOrNode === null) {
|
|
90
90
|
newNode = null;
|
|
91
91
|
} else {
|
|
92
|
-
newNode = this.createNode(keyOrNode,
|
|
92
|
+
newNode = this.createNode(keyOrNode, value, count);
|
|
93
93
|
}
|
|
94
94
|
if (!this.root) {
|
|
95
95
|
this._setRoot(newNode);
|
|
@@ -103,7 +103,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
103
103
|
if (cur) {
|
|
104
104
|
if (newNode) {
|
|
105
105
|
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
106
|
-
cur.
|
|
106
|
+
cur.value = newNode.value;
|
|
107
107
|
cur.count += newNode.count;
|
|
108
108
|
this._setCount(this.count + newNode.count);
|
|
109
109
|
traversing = false;
|
|
@@ -199,7 +199,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
199
199
|
const keyOrNode = keysOrNodes[i];
|
|
200
200
|
|
|
201
201
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
202
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.
|
|
202
|
+
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
203
203
|
continue;
|
|
204
204
|
}
|
|
205
205
|
|
|
@@ -233,7 +233,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
233
233
|
if (l > r) return;
|
|
234
234
|
const m = l + Math.floor((r - l) / 2);
|
|
235
235
|
const midNode = sorted[m];
|
|
236
|
-
this.add(midNode.key, midNode.
|
|
236
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
237
237
|
buildBalanceBST(l, m - 1);
|
|
238
238
|
buildBalanceBST(m + 1, r);
|
|
239
239
|
};
|
|
@@ -249,7 +249,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
249
249
|
if (l <= r) {
|
|
250
250
|
const m = l + Math.floor((r - l) / 2);
|
|
251
251
|
const midNode = sorted[m];
|
|
252
|
-
this.add(midNode.key, midNode.
|
|
252
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
253
253
|
stack.push([m + 1, r]);
|
|
254
254
|
stack.push([l, m - 1]);
|
|
255
255
|
}
|
|
@@ -351,18 +351,18 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
351
351
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
352
352
|
*/
|
|
353
353
|
protected override _swap(srcNode: N, destNode: N): N {
|
|
354
|
-
const {key,
|
|
355
|
-
const tempNode = this.createNode(key,
|
|
354
|
+
const {key, value, count, height} = destNode;
|
|
355
|
+
const tempNode = this.createNode(key, value, count);
|
|
356
356
|
if (tempNode) {
|
|
357
357
|
tempNode.height = height;
|
|
358
358
|
|
|
359
359
|
destNode.key = srcNode.key;
|
|
360
|
-
destNode.
|
|
360
|
+
destNode.value = srcNode.value;
|
|
361
361
|
destNode.count = srcNode.count;
|
|
362
362
|
destNode.height = srcNode.height;
|
|
363
363
|
|
|
364
364
|
srcNode.key = tempNode.key;
|
|
365
|
-
srcNode.
|
|
365
|
+
srcNode.value = tempNode.value;
|
|
366
366
|
srcNode.count = tempNode.count;
|
|
367
367
|
srcNode.height = tempNode.height;
|
|
368
368
|
}
|