data-structure-typed 1.39.4 → 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 +3 -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 +96 -96
- package/dist/cjs/data-structures/graph/abstract-graph.js +64 -64
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +68 -68
- package/dist/cjs/data-structures/graph/directed-graph.js +48 -48
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +13 -13
- package/dist/cjs/data-structures/graph/map-graph.js +15 -15
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +42 -42
- package/dist/cjs/data-structures/graph/undirected-graph.js +32 -32
- 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.d.ts +1 -1
- package/dist/cjs/data-structures/queue/queue.js +4 -4
- 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 +3 -3
- 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 +96 -96
- package/dist/mjs/data-structures/graph/abstract-graph.js +66 -66
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +68 -68
- package/dist/mjs/data-structures/graph/directed-graph.js +48 -48
- package/dist/mjs/data-structures/graph/map-graph.d.ts +13 -13
- package/dist/mjs/data-structures/graph/map-graph.js +15 -15
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +42 -42
- package/dist/mjs/data-structures/graph/undirected-graph.js +32 -32
- 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.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +4 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/interfaces/graph.d.ts +3 -3
- 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 +156 -154
- package/src/data-structures/graph/directed-graph.ts +99 -94
- package/src/data-structures/graph/map-graph.ts +22 -25
- package/src/data-structures/graph/undirected-graph.ts +62 -60
- 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 +2 -2
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +3 -3
- 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 +38 -23
- package/test/unit/data-structures/graph/map-graph.test.ts +23 -23
- package/test/unit/data-structures/graph/undirected-graph.test.ts +10 -2
- 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/unit/data-structures/queue/queue.test.ts +8 -8
- package/test/utils/big-o.ts +7 -7
|
@@ -11,8 +11,8 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
|
11
11
|
const bst_1 = require("./bst");
|
|
12
12
|
class AVLTreeNode extends bst_1.BSTNode {
|
|
13
13
|
height;
|
|
14
|
-
constructor(key,
|
|
15
|
-
super(key,
|
|
14
|
+
constructor(key, value) {
|
|
15
|
+
super(key, value);
|
|
16
16
|
this.height = 0;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -31,26 +31,26 @@ class AVLTree extends bst_1.BST {
|
|
|
31
31
|
* The function creates a new AVL tree node with the specified key and value.
|
|
32
32
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
33
33
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
34
|
-
* @param [
|
|
35
|
-
* type `V`, which means it can be any value that is assignable to the `
|
|
34
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
35
|
+
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
36
36
|
* node type `N`.
|
|
37
37
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
38
38
|
*/
|
|
39
|
-
createNode(key,
|
|
40
|
-
return new AVLTreeNode(key,
|
|
39
|
+
createNode(key, value) {
|
|
40
|
+
return new AVLTreeNode(key, value);
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
44
44
|
* a new node.
|
|
45
45
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
46
46
|
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
47
|
-
* @param [
|
|
47
|
+
* @param [value] - The `value` parameter is the value that you want to assign to the new node that you
|
|
48
48
|
* are adding to the binary search tree.
|
|
49
49
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
50
50
|
*/
|
|
51
|
-
add(keyOrNode,
|
|
51
|
+
add(keyOrNode, value) {
|
|
52
52
|
// TODO support node as a param
|
|
53
|
-
const inserted = super.add(keyOrNode,
|
|
53
|
+
const inserted = super.add(keyOrNode, value);
|
|
54
54
|
if (inserted)
|
|
55
55
|
this._balancePath(inserted);
|
|
56
56
|
return inserted;
|
|
@@ -87,15 +87,15 @@ class AVLTree extends bst_1.BST {
|
|
|
87
87
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
88
88
|
*/
|
|
89
89
|
_swap(srcNode, destNode) {
|
|
90
|
-
const { key,
|
|
91
|
-
const tempNode = this.createNode(key,
|
|
90
|
+
const { key, value, height } = destNode;
|
|
91
|
+
const tempNode = this.createNode(key, value);
|
|
92
92
|
if (tempNode) {
|
|
93
93
|
tempNode.height = height;
|
|
94
94
|
destNode.key = srcNode.key;
|
|
95
|
-
destNode.
|
|
95
|
+
destNode.value = srcNode.value;
|
|
96
96
|
destNode.height = srcNode.height;
|
|
97
97
|
srcNode.key = tempNode.key;
|
|
98
|
-
srcNode.
|
|
98
|
+
srcNode.value = tempNode.value;
|
|
99
99
|
srcNode.height = tempNode.height;
|
|
100
100
|
}
|
|
101
101
|
return destNode;
|
|
@@ -21,7 +21,7 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
21
21
|
/**
|
|
22
22
|
* The value stored in the node.
|
|
23
23
|
*/
|
|
24
|
-
|
|
24
|
+
value: V | undefined;
|
|
25
25
|
/**
|
|
26
26
|
* The parent node of the current node.
|
|
27
27
|
*/
|
|
@@ -29,9 +29,9 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
29
29
|
/**
|
|
30
30
|
* Creates a new instance of BinaryTreeNode.
|
|
31
31
|
* @param {BTNKey} key - The key associated with the node.
|
|
32
|
-
* @param {V}
|
|
32
|
+
* @param {V} value - The value stored in the node.
|
|
33
33
|
*/
|
|
34
|
-
constructor(key: BTNKey,
|
|
34
|
+
constructor(key: BTNKey, value?: V);
|
|
35
35
|
private _left;
|
|
36
36
|
/**
|
|
37
37
|
* Get the left child node.
|
|
@@ -91,10 +91,10 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
91
91
|
/**
|
|
92
92
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
93
93
|
* @param {BTNKey} key - The key for the new node.
|
|
94
|
-
* @param {V}
|
|
94
|
+
* @param {V} value - The value for the new node.
|
|
95
95
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
96
96
|
*/
|
|
97
|
-
createNode(key: BTNKey,
|
|
97
|
+
createNode(key: BTNKey, value?: V): N;
|
|
98
98
|
/**
|
|
99
99
|
* Clear the binary tree, removing all nodes.
|
|
100
100
|
*/
|
|
@@ -107,10 +107,10 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
107
107
|
/**
|
|
108
108
|
* Add a node with the given key and value to the binary tree.
|
|
109
109
|
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
110
|
-
* @param {V}
|
|
110
|
+
* @param {V} value - The value for the new node (optional).
|
|
111
111
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
112
112
|
*/
|
|
113
|
-
add(keyOrNode: BTNKey | N | null,
|
|
113
|
+
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined;
|
|
114
114
|
/**
|
|
115
115
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
116
116
|
* values, and adds them to the binary tree.
|
|
@@ -24,7 +24,7 @@ class BinaryTreeNode {
|
|
|
24
24
|
/**
|
|
25
25
|
* The value stored in the node.
|
|
26
26
|
*/
|
|
27
|
-
|
|
27
|
+
value;
|
|
28
28
|
/**
|
|
29
29
|
* The parent node of the current node.
|
|
30
30
|
*/
|
|
@@ -32,11 +32,11 @@ class BinaryTreeNode {
|
|
|
32
32
|
/**
|
|
33
33
|
* Creates a new instance of BinaryTreeNode.
|
|
34
34
|
* @param {BTNKey} key - The key associated with the node.
|
|
35
|
-
* @param {V}
|
|
35
|
+
* @param {V} value - The value stored in the node.
|
|
36
36
|
*/
|
|
37
|
-
constructor(key,
|
|
37
|
+
constructor(key, value) {
|
|
38
38
|
this.key = key;
|
|
39
|
-
this.
|
|
39
|
+
this.value = value;
|
|
40
40
|
}
|
|
41
41
|
_left;
|
|
42
42
|
/**
|
|
@@ -137,11 +137,11 @@ class BinaryTree {
|
|
|
137
137
|
/**
|
|
138
138
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
139
139
|
* @param {BTNKey} key - The key for the new node.
|
|
140
|
-
* @param {V}
|
|
140
|
+
* @param {V} value - The value for the new node.
|
|
141
141
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
142
142
|
*/
|
|
143
|
-
createNode(key,
|
|
144
|
-
return new BinaryTreeNode(key,
|
|
143
|
+
createNode(key, value) {
|
|
144
|
+
return new BinaryTreeNode(key, value);
|
|
145
145
|
}
|
|
146
146
|
/**
|
|
147
147
|
* Clear the binary tree, removing all nodes.
|
|
@@ -160,10 +160,10 @@ class BinaryTree {
|
|
|
160
160
|
/**
|
|
161
161
|
* Add a node with the given key and value to the binary tree.
|
|
162
162
|
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
163
|
-
* @param {V}
|
|
163
|
+
* @param {V} value - The value for the new node (optional).
|
|
164
164
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
165
165
|
*/
|
|
166
|
-
add(keyOrNode,
|
|
166
|
+
add(keyOrNode, value) {
|
|
167
167
|
const _bfs = (root, newNode) => {
|
|
168
168
|
const queue = new queue_1.Queue([root]);
|
|
169
169
|
while (queue.size > 0) {
|
|
@@ -189,7 +189,7 @@ class BinaryTree {
|
|
|
189
189
|
needInsert = null;
|
|
190
190
|
}
|
|
191
191
|
else if (typeof keyOrNode === 'number') {
|
|
192
|
-
needInsert = this.createNode(keyOrNode,
|
|
192
|
+
needInsert = this.createNode(keyOrNode, value);
|
|
193
193
|
}
|
|
194
194
|
else if (keyOrNode instanceof BinaryTreeNode) {
|
|
195
195
|
needInsert = keyOrNode;
|
|
@@ -201,7 +201,7 @@ class BinaryTree {
|
|
|
201
201
|
const existNode = key !== undefined ? this.get(key, (node) => node.key) : undefined;
|
|
202
202
|
if (this.root) {
|
|
203
203
|
if (existNode) {
|
|
204
|
-
existNode.
|
|
204
|
+
existNode.value = value;
|
|
205
205
|
inserted = existNode;
|
|
206
206
|
}
|
|
207
207
|
else {
|
|
@@ -234,13 +234,13 @@ class BinaryTree {
|
|
|
234
234
|
// TODO not sure addMany not be run multi times
|
|
235
235
|
return keysOrNodes.map((keyOrNode, i) => {
|
|
236
236
|
if (keyOrNode instanceof BinaryTreeNode) {
|
|
237
|
-
return this.add(keyOrNode.key, keyOrNode.
|
|
237
|
+
return this.add(keyOrNode.key, keyOrNode.value);
|
|
238
238
|
}
|
|
239
239
|
if (keyOrNode === null) {
|
|
240
240
|
return this.add(null);
|
|
241
241
|
}
|
|
242
|
-
const
|
|
243
|
-
return this.add(keyOrNode,
|
|
242
|
+
const value = values?.[i];
|
|
243
|
+
return this.add(keyOrNode, value);
|
|
244
244
|
});
|
|
245
245
|
}
|
|
246
246
|
/**
|
|
@@ -1077,13 +1077,13 @@ class BinaryTree {
|
|
|
1077
1077
|
* @returns {N} - The destination node after the swap.
|
|
1078
1078
|
*/
|
|
1079
1079
|
_swap(srcNode, destNode) {
|
|
1080
|
-
const { key,
|
|
1081
|
-
const tempNode = this.createNode(key,
|
|
1080
|
+
const { key, value } = destNode;
|
|
1081
|
+
const tempNode = this.createNode(key, value);
|
|
1082
1082
|
if (tempNode) {
|
|
1083
1083
|
destNode.key = srcNode.key;
|
|
1084
|
-
destNode.
|
|
1084
|
+
destNode.value = srcNode.value;
|
|
1085
1085
|
srcNode.key = tempNode.key;
|
|
1086
|
-
srcNode.
|
|
1086
|
+
srcNode.value = tempNode.value;
|
|
1087
1087
|
}
|
|
1088
1088
|
return destNode;
|
|
1089
1089
|
}
|
|
@@ -10,7 +10,7 @@ import { CP, IterationType } from '../../types';
|
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
13
|
-
constructor(key: BTNKey,
|
|
13
|
+
constructor(key: BTNKey, value?: V);
|
|
14
14
|
}
|
|
15
15
|
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
16
16
|
/**
|
|
@@ -24,26 +24,26 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
24
24
|
* The function creates a new binary search tree node with the given key and value.
|
|
25
25
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
26
26
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
27
|
-
* @param [
|
|
27
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
|
|
28
28
|
* represents the value associated with the node in a binary search tree.
|
|
29
29
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
30
30
|
*/
|
|
31
|
-
createNode(key: BTNKey,
|
|
31
|
+
createNode(key: BTNKey, value?: V): N;
|
|
32
32
|
/**
|
|
33
33
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
34
34
|
* into the tree.
|
|
35
35
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
36
36
|
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
37
|
-
* @param [
|
|
37
|
+
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
|
38
38
|
* binary search tree.
|
|
39
39
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
40
40
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
41
41
|
*/
|
|
42
|
-
add(keyOrNode: BTNKey | N | null,
|
|
42
|
+
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined;
|
|
43
43
|
/**
|
|
44
44
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
45
45
|
* maintaining balance.
|
|
46
|
-
* @param {[BTNKey | N, N['
|
|
46
|
+
* @param {[BTNKey | N, N['value']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
47
47
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
48
48
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
49
49
|
* `null
|
|
@@ -5,8 +5,8 @@ const types_1 = require("../../types");
|
|
|
5
5
|
const binary_tree_1 = require("./binary-tree");
|
|
6
6
|
const queue_1 = require("../queue");
|
|
7
7
|
class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
8
|
-
constructor(key,
|
|
9
|
-
super(key,
|
|
8
|
+
constructor(key, value) {
|
|
9
|
+
super(key, value);
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.BSTNode = BSTNode;
|
|
@@ -30,24 +30,24 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
30
30
|
* The function creates a new binary search tree node with the given key and value.
|
|
31
31
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
32
32
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
33
|
-
* @param [
|
|
33
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
|
|
34
34
|
* represents the value associated with the node in a binary search tree.
|
|
35
35
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
36
36
|
*/
|
|
37
|
-
createNode(key,
|
|
38
|
-
return new BSTNode(key,
|
|
37
|
+
createNode(key, value) {
|
|
38
|
+
return new BSTNode(key, value);
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
42
42
|
* into the tree.
|
|
43
43
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
44
44
|
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
45
|
-
* @param [
|
|
45
|
+
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
|
46
46
|
* binary search tree.
|
|
47
47
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
48
48
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
49
49
|
*/
|
|
50
|
-
add(keyOrNode,
|
|
50
|
+
add(keyOrNode, value) {
|
|
51
51
|
// TODO support node as a parameter
|
|
52
52
|
let inserted = null;
|
|
53
53
|
let newNode = null;
|
|
@@ -55,7 +55,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
55
55
|
newNode = keyOrNode;
|
|
56
56
|
}
|
|
57
57
|
else if (typeof keyOrNode === 'number') {
|
|
58
|
-
newNode = this.createNode(keyOrNode,
|
|
58
|
+
newNode = this.createNode(keyOrNode, value);
|
|
59
59
|
}
|
|
60
60
|
else if (keyOrNode === null) {
|
|
61
61
|
newNode = null;
|
|
@@ -72,7 +72,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
72
72
|
if (cur !== null && newNode !== null) {
|
|
73
73
|
if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
|
|
74
74
|
if (newNode) {
|
|
75
|
-
cur.
|
|
75
|
+
cur.value = newNode.value;
|
|
76
76
|
}
|
|
77
77
|
//Duplicates are not accepted.
|
|
78
78
|
traversing = false;
|
|
@@ -125,7 +125,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
125
125
|
/**
|
|
126
126
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
127
127
|
* maintaining balance.
|
|
128
|
-
* @param {[BTNKey | N, N['
|
|
128
|
+
* @param {[BTNKey | N, N['value']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
129
129
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
130
130
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
131
131
|
* `null
|
|
@@ -169,7 +169,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
169
169
|
throw new Error('Invalid input keysOrNodes');
|
|
170
170
|
}
|
|
171
171
|
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
172
|
-
sortedData = sorted.map(([,
|
|
172
|
+
sortedData = sorted.map(([, value]) => value);
|
|
173
173
|
const recursive = (arr, data) => {
|
|
174
174
|
if (arr.length === 0)
|
|
175
175
|
return;
|
|
@@ -411,7 +411,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
411
411
|
return;
|
|
412
412
|
const m = l + Math.floor((r - l) / 2);
|
|
413
413
|
const midNode = sorted[m];
|
|
414
|
-
this.add(midNode.key, midNode.
|
|
414
|
+
this.add(midNode.key, midNode.value);
|
|
415
415
|
buildBalanceBST(l, m - 1);
|
|
416
416
|
buildBalanceBST(m + 1, r);
|
|
417
417
|
};
|
|
@@ -427,7 +427,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
427
427
|
if (l <= r) {
|
|
428
428
|
const m = l + Math.floor((r - l) / 2);
|
|
429
429
|
const midNode = sorted[m];
|
|
430
|
-
this.add(midNode.key, midNode.
|
|
430
|
+
this.add(midNode.key, midNode.value);
|
|
431
431
|
stack.push([m + 1, r]);
|
|
432
432
|
stack.push([l, m - 1]);
|
|
433
433
|
}
|
|
@@ -2,12 +2,12 @@ import { BTNKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
|
|
|
2
2
|
import { IBinaryTree } from '../../interfaces';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
4
|
export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
5
|
-
constructor(key: BTNKey,
|
|
5
|
+
constructor(key: BTNKey, value?: V);
|
|
6
6
|
private _color;
|
|
7
7
|
get color(): RBColor;
|
|
8
8
|
set color(value: RBColor);
|
|
9
9
|
}
|
|
10
10
|
export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
11
11
|
constructor(options?: RBTreeOptions);
|
|
12
|
-
createNode(key: BTNKey,
|
|
12
|
+
createNode(key: BTNKey, value?: V): N;
|
|
13
13
|
}
|
|
@@ -4,8 +4,8 @@ exports.RBTree = exports.RBTreeNode = void 0;
|
|
|
4
4
|
const types_1 = require("../../types");
|
|
5
5
|
const bst_1 = require("./bst");
|
|
6
6
|
class RBTreeNode extends bst_1.BSTNode {
|
|
7
|
-
constructor(key,
|
|
8
|
-
super(key,
|
|
7
|
+
constructor(key, value) {
|
|
8
|
+
super(key, value);
|
|
9
9
|
this._color = types_1.RBColor.RED;
|
|
10
10
|
}
|
|
11
11
|
_color;
|
|
@@ -21,8 +21,8 @@ class RBTree extends bst_1.BST {
|
|
|
21
21
|
constructor(options) {
|
|
22
22
|
super(options);
|
|
23
23
|
}
|
|
24
|
-
createNode(key,
|
|
25
|
-
return new RBTreeNode(key,
|
|
24
|
+
createNode(key, value) {
|
|
25
|
+
return new RBTreeNode(key, value);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
exports.RBTree = RBTree;
|
|
@@ -7,16 +7,16 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { SegmentTreeNodeVal } from '../../types';
|
|
9
9
|
export declare class SegmentTreeNode {
|
|
10
|
-
constructor(start: number, end: number, sum: number,
|
|
10
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | null);
|
|
11
11
|
private _start;
|
|
12
12
|
get start(): number;
|
|
13
13
|
set start(v: number);
|
|
14
14
|
private _end;
|
|
15
15
|
get end(): number;
|
|
16
16
|
set end(v: number);
|
|
17
|
-
private
|
|
18
|
-
get
|
|
19
|
-
set
|
|
17
|
+
private _value;
|
|
18
|
+
get value(): SegmentTreeNodeVal | null;
|
|
19
|
+
set value(v: SegmentTreeNodeVal | null);
|
|
20
20
|
private _sum;
|
|
21
21
|
get sum(): number;
|
|
22
22
|
set sum(v: number);
|
|
@@ -62,12 +62,12 @@ export declare class SegmentTree {
|
|
|
62
62
|
* updated.
|
|
63
63
|
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
64
64
|
* the `SegmentTreeNode` at the specified `index`.
|
|
65
|
-
* @param {SegmentTreeNodeVal} [
|
|
65
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
66
66
|
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
67
|
-
* cur.
|
|
67
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
68
68
|
* @returns The function does not return anything.
|
|
69
69
|
*/
|
|
70
|
-
updateNode(index: number, sum: number,
|
|
70
|
+
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal): void;
|
|
71
71
|
/**
|
|
72
72
|
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
73
73
|
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.SegmentTree = exports.SegmentTreeNode = void 0;
|
|
11
11
|
class SegmentTreeNode {
|
|
12
|
-
constructor(start, end, sum,
|
|
12
|
+
constructor(start, end, sum, value) {
|
|
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
|
_start = 0;
|
|
19
19
|
get start() {
|
|
@@ -29,12 +29,12 @@ class SegmentTreeNode {
|
|
|
29
29
|
set end(v) {
|
|
30
30
|
this._end = v;
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
get
|
|
34
|
-
return this.
|
|
32
|
+
_value = null;
|
|
33
|
+
get value() {
|
|
34
|
+
return this._value;
|
|
35
35
|
}
|
|
36
|
-
set
|
|
37
|
-
this.
|
|
36
|
+
set value(v) {
|
|
37
|
+
this._value = v;
|
|
38
38
|
}
|
|
39
39
|
_sum = 0;
|
|
40
40
|
get sum() {
|
|
@@ -128,39 +128,39 @@ class SegmentTree {
|
|
|
128
128
|
* updated.
|
|
129
129
|
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
130
130
|
* the `SegmentTreeNode` at the specified `index`.
|
|
131
|
-
* @param {SegmentTreeNodeVal} [
|
|
131
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
132
132
|
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
133
|
-
* cur.
|
|
133
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
134
134
|
* @returns The function does not return anything.
|
|
135
135
|
*/
|
|
136
|
-
updateNode(index, sum,
|
|
136
|
+
updateNode(index, sum, value) {
|
|
137
137
|
const root = this.root || null;
|
|
138
138
|
if (!root) {
|
|
139
139
|
return;
|
|
140
140
|
}
|
|
141
|
-
const dfs = (cur, index, sum,
|
|
141
|
+
const dfs = (cur, index, sum, value) => {
|
|
142
142
|
if (cur.start === cur.end && cur.start === index) {
|
|
143
143
|
cur.sum = sum;
|
|
144
|
-
if (
|
|
145
|
-
cur.
|
|
144
|
+
if (value !== undefined)
|
|
145
|
+
cur.value = value;
|
|
146
146
|
return;
|
|
147
147
|
}
|
|
148
148
|
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
149
149
|
if (index <= mid) {
|
|
150
150
|
if (cur.left) {
|
|
151
|
-
dfs(cur.left, index, sum,
|
|
151
|
+
dfs(cur.left, index, sum, value);
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
154
|
else {
|
|
155
155
|
if (cur.right) {
|
|
156
|
-
dfs(cur.right, index, sum,
|
|
156
|
+
dfs(cur.right, index, sum, value);
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
if (cur.left && cur.right) {
|
|
160
160
|
cur.sum = cur.left.sum + cur.right.sum;
|
|
161
161
|
}
|
|
162
162
|
};
|
|
163
|
-
dfs(root, index, sum,
|
|
163
|
+
dfs(root, index, sum, value);
|
|
164
164
|
}
|
|
165
165
|
/**
|
|
166
166
|
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
@@ -15,13 +15,13 @@ export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N>
|
|
|
15
15
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
16
16
|
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
17
17
|
* of the binary tree node.
|
|
18
|
-
* @param {V} [
|
|
18
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
19
19
|
* tree node. If no value is provided, it will be `undefined`.
|
|
20
20
|
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
21
21
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
22
22
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
23
23
|
*/
|
|
24
|
-
constructor(key: BTNKey,
|
|
24
|
+
constructor(key: BTNKey, value?: V, count?: number);
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
@@ -40,26 +40,26 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
40
40
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
41
41
|
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
42
42
|
* distinguish one node from another in the tree.
|
|
43
|
-
* @param {N}
|
|
43
|
+
* @param {N} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
|
44
44
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
45
45
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
46
46
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
47
47
|
*/
|
|
48
|
-
createNode(key: BTNKey,
|
|
48
|
+
createNode(key: BTNKey, value?: V, count?: number): N;
|
|
49
49
|
/**
|
|
50
50
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
51
51
|
* exists, and balancing the tree if necessary.
|
|
52
52
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
53
53
|
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
54
54
|
* node to be added), or `null` (which represents a null node).
|
|
55
|
-
* @param [
|
|
55
|
+
* @param [value] - The `value` parameter represents the value associated with the key that is being
|
|
56
56
|
* added to the binary tree.
|
|
57
57
|
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
58
58
|
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
59
59
|
* count is specified, the default count will be 1.
|
|
60
60
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
61
61
|
*/
|
|
62
|
-
add(keyOrNode: BTNKey | N | null,
|
|
62
|
+
add(keyOrNode: BTNKey | N | null, value?: V, count?: number): N | null | undefined;
|
|
63
63
|
/**
|
|
64
64
|
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
65
65
|
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
|