linked-list-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/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/data-structures/binary-tree/binary-tree.js +17 -17
- package/dist/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/data-structures/binary-tree/bst.js +13 -13
- package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/data-structures/graph/abstract-graph.js +24 -24
- package/dist/data-structures/graph/directed-graph.d.ts +12 -12
- package/dist/data-structures/graph/directed-graph.js +15 -15
- package/dist/data-structures/graph/map-graph.d.ts +9 -9
- package/dist/data-structures/graph/map-graph.js +13 -13
- package/dist/data-structures/graph/undirected-graph.d.ts +11 -11
- package/dist/data-structures/graph/undirected-graph.js +14 -14
- package/dist/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/data-structures/hash/hash-table.js +8 -8
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/data-structures/linked-list/doubly-linked-list.js +54 -54
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/data-structures/linked-list/singly-linked-list.js +52 -52
- package/dist/data-structures/queue/queue.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/interfaces/graph.d.ts +2 -2
- package/package.json +2 -2
- 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
|
@@ -11,7 +11,7 @@ import { BTNCallback } from '../../types';
|
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
13
13
|
height: number;
|
|
14
|
-
constructor(key: BTNKey,
|
|
14
|
+
constructor(key: BTNKey, value?: V);
|
|
15
15
|
}
|
|
16
16
|
export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
17
17
|
/**
|
|
@@ -25,22 +25,22 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
25
25
|
* The function creates a new AVL tree node with the specified key and value.
|
|
26
26
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
27
27
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
28
|
-
* @param [
|
|
29
|
-
* type `V`, which means it can be any value that is assignable to the `
|
|
28
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
29
|
+
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
30
30
|
* node type `N`.
|
|
31
31
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
32
32
|
*/
|
|
33
|
-
createNode(key: BTNKey,
|
|
33
|
+
createNode(key: BTNKey, value?: V): N;
|
|
34
34
|
/**
|
|
35
35
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
36
36
|
* a new node.
|
|
37
37
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
38
38
|
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
39
|
-
* @param [
|
|
39
|
+
* @param [value] - The `value` parameter is the value that you want to assign to the new node that you
|
|
40
40
|
* are adding to the binary search tree.
|
|
41
41
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
42
42
|
*/
|
|
43
|
-
add(keyOrNode: BTNKey | N | null,
|
|
43
|
+
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined;
|
|
44
44
|
/**
|
|
45
45
|
* The function overrides the delete method of a binary tree and balances the tree after deleting a
|
|
46
46
|
* node if necessary.
|
|
@@ -10,8 +10,8 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
|
10
10
|
*/
|
|
11
11
|
const bst_1 = require("./bst");
|
|
12
12
|
class AVLTreeNode extends bst_1.BSTNode {
|
|
13
|
-
constructor(key,
|
|
14
|
-
super(key,
|
|
13
|
+
constructor(key, value) {
|
|
14
|
+
super(key, value);
|
|
15
15
|
this.height = 0;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
@@ -30,26 +30,26 @@ class AVLTree extends bst_1.BST {
|
|
|
30
30
|
* The function creates a new AVL tree node with the specified 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 [
|
|
34
|
-
* type `V`, which means it can be any value that is assignable to the `
|
|
33
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
34
|
+
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
35
35
|
* node type `N`.
|
|
36
36
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
37
37
|
*/
|
|
38
|
-
createNode(key,
|
|
39
|
-
return new AVLTreeNode(key,
|
|
38
|
+
createNode(key, value) {
|
|
39
|
+
return new AVLTreeNode(key, value);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
43
43
|
* a new node.
|
|
44
44
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
45
45
|
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
46
|
-
* @param [
|
|
46
|
+
* @param [value] - The `value` parameter is the value that you want to assign to the new node that you
|
|
47
47
|
* are adding to the binary search tree.
|
|
48
48
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
49
49
|
*/
|
|
50
|
-
add(keyOrNode,
|
|
50
|
+
add(keyOrNode, value) {
|
|
51
51
|
// TODO support node as a param
|
|
52
|
-
const inserted = super.add(keyOrNode,
|
|
52
|
+
const inserted = super.add(keyOrNode, value);
|
|
53
53
|
if (inserted)
|
|
54
54
|
this._balancePath(inserted);
|
|
55
55
|
return inserted;
|
|
@@ -86,15 +86,15 @@ class AVLTree extends bst_1.BST {
|
|
|
86
86
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
87
87
|
*/
|
|
88
88
|
_swap(srcNode, destNode) {
|
|
89
|
-
const { key,
|
|
90
|
-
const tempNode = this.createNode(key,
|
|
89
|
+
const { key, value, height } = destNode;
|
|
90
|
+
const tempNode = this.createNode(key, value);
|
|
91
91
|
if (tempNode) {
|
|
92
92
|
tempNode.height = height;
|
|
93
93
|
destNode.key = srcNode.key;
|
|
94
|
-
destNode.
|
|
94
|
+
destNode.value = srcNode.value;
|
|
95
95
|
destNode.height = srcNode.height;
|
|
96
96
|
srcNode.key = tempNode.key;
|
|
97
|
-
srcNode.
|
|
97
|
+
srcNode.value = tempNode.value;
|
|
98
98
|
srcNode.height = tempNode.height;
|
|
99
99
|
}
|
|
100
100
|
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.
|
|
@@ -20,11 +20,11 @@ class BinaryTreeNode {
|
|
|
20
20
|
/**
|
|
21
21
|
* Creates a new instance of BinaryTreeNode.
|
|
22
22
|
* @param {BTNKey} key - The key associated with the node.
|
|
23
|
-
* @param {V}
|
|
23
|
+
* @param {V} value - The value stored in the node.
|
|
24
24
|
*/
|
|
25
|
-
constructor(key,
|
|
25
|
+
constructor(key, value) {
|
|
26
26
|
this.key = key;
|
|
27
|
-
this.
|
|
27
|
+
this.value = value;
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
30
|
* Get the left child node.
|
|
@@ -123,11 +123,11 @@ class BinaryTree {
|
|
|
123
123
|
/**
|
|
124
124
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
125
125
|
* @param {BTNKey} key - The key for the new node.
|
|
126
|
-
* @param {V}
|
|
126
|
+
* @param {V} value - The value for the new node.
|
|
127
127
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
128
128
|
*/
|
|
129
|
-
createNode(key,
|
|
130
|
-
return new BinaryTreeNode(key,
|
|
129
|
+
createNode(key, value) {
|
|
130
|
+
return new BinaryTreeNode(key, value);
|
|
131
131
|
}
|
|
132
132
|
/**
|
|
133
133
|
* Clear the binary tree, removing all nodes.
|
|
@@ -146,10 +146,10 @@ class BinaryTree {
|
|
|
146
146
|
/**
|
|
147
147
|
* Add a node with the given key and value to the binary tree.
|
|
148
148
|
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
149
|
-
* @param {V}
|
|
149
|
+
* @param {V} value - The value for the new node (optional).
|
|
150
150
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
151
151
|
*/
|
|
152
|
-
add(keyOrNode,
|
|
152
|
+
add(keyOrNode, value) {
|
|
153
153
|
const _bfs = (root, newNode) => {
|
|
154
154
|
const queue = new queue_1.Queue([root]);
|
|
155
155
|
while (queue.size > 0) {
|
|
@@ -175,7 +175,7 @@ class BinaryTree {
|
|
|
175
175
|
needInsert = null;
|
|
176
176
|
}
|
|
177
177
|
else if (typeof keyOrNode === 'number') {
|
|
178
|
-
needInsert = this.createNode(keyOrNode,
|
|
178
|
+
needInsert = this.createNode(keyOrNode, value);
|
|
179
179
|
}
|
|
180
180
|
else if (keyOrNode instanceof BinaryTreeNode) {
|
|
181
181
|
needInsert = keyOrNode;
|
|
@@ -187,7 +187,7 @@ class BinaryTree {
|
|
|
187
187
|
const existNode = key !== undefined ? this.get(key, (node) => node.key) : undefined;
|
|
188
188
|
if (this.root) {
|
|
189
189
|
if (existNode) {
|
|
190
|
-
existNode.
|
|
190
|
+
existNode.value = value;
|
|
191
191
|
inserted = existNode;
|
|
192
192
|
}
|
|
193
193
|
else {
|
|
@@ -220,13 +220,13 @@ class BinaryTree {
|
|
|
220
220
|
// TODO not sure addMany not be run multi times
|
|
221
221
|
return keysOrNodes.map((keyOrNode, i) => {
|
|
222
222
|
if (keyOrNode instanceof BinaryTreeNode) {
|
|
223
|
-
return this.add(keyOrNode.key, keyOrNode.
|
|
223
|
+
return this.add(keyOrNode.key, keyOrNode.value);
|
|
224
224
|
}
|
|
225
225
|
if (keyOrNode === null) {
|
|
226
226
|
return this.add(null);
|
|
227
227
|
}
|
|
228
|
-
const
|
|
229
|
-
return this.add(keyOrNode,
|
|
228
|
+
const value = values === null || values === void 0 ? void 0 : values[i];
|
|
229
|
+
return this.add(keyOrNode, value);
|
|
230
230
|
});
|
|
231
231
|
}
|
|
232
232
|
/**
|
|
@@ -1065,13 +1065,13 @@ class BinaryTree {
|
|
|
1065
1065
|
* @returns {N} - The destination node after the swap.
|
|
1066
1066
|
*/
|
|
1067
1067
|
_swap(srcNode, destNode) {
|
|
1068
|
-
const { key,
|
|
1069
|
-
const tempNode = this.createNode(key,
|
|
1068
|
+
const { key, value } = destNode;
|
|
1069
|
+
const tempNode = this.createNode(key, value);
|
|
1070
1070
|
if (tempNode) {
|
|
1071
1071
|
destNode.key = srcNode.key;
|
|
1072
|
-
destNode.
|
|
1072
|
+
destNode.value = srcNode.value;
|
|
1073
1073
|
srcNode.key = tempNode.key;
|
|
1074
|
-
srcNode.
|
|
1074
|
+
srcNode.value = tempNode.value;
|
|
1075
1075
|
}
|
|
1076
1076
|
return destNode;
|
|
1077
1077
|
}
|
|
@@ -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;
|
|
@@ -31,24 +31,24 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
31
31
|
* The function creates a new binary search tree node with the given 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 [
|
|
34
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
|
|
35
35
|
* represents the value associated with the node in a binary search tree.
|
|
36
36
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
37
37
|
*/
|
|
38
|
-
createNode(key,
|
|
39
|
-
return new BSTNode(key,
|
|
38
|
+
createNode(key, value) {
|
|
39
|
+
return new BSTNode(key, value);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
43
43
|
* into the tree.
|
|
44
44
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
45
45
|
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
46
|
-
* @param [
|
|
46
|
+
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
|
47
47
|
* binary search tree.
|
|
48
48
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
49
49
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
50
50
|
*/
|
|
51
|
-
add(keyOrNode,
|
|
51
|
+
add(keyOrNode, value) {
|
|
52
52
|
// TODO support node as a parameter
|
|
53
53
|
let inserted = null;
|
|
54
54
|
let newNode = null;
|
|
@@ -56,7 +56,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
56
56
|
newNode = keyOrNode;
|
|
57
57
|
}
|
|
58
58
|
else if (typeof keyOrNode === 'number') {
|
|
59
|
-
newNode = this.createNode(keyOrNode,
|
|
59
|
+
newNode = this.createNode(keyOrNode, value);
|
|
60
60
|
}
|
|
61
61
|
else if (keyOrNode === null) {
|
|
62
62
|
newNode = null;
|
|
@@ -73,7 +73,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
73
73
|
if (cur !== null && newNode !== null) {
|
|
74
74
|
if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
|
|
75
75
|
if (newNode) {
|
|
76
|
-
cur.
|
|
76
|
+
cur.value = newNode.value;
|
|
77
77
|
}
|
|
78
78
|
//Duplicates are not accepted.
|
|
79
79
|
traversing = false;
|
|
@@ -126,7 +126,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
126
126
|
/**
|
|
127
127
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
128
128
|
* maintaining balance.
|
|
129
|
-
* @param {[BTNKey | N, N['
|
|
129
|
+
* @param {[BTNKey | N, N['value']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
130
130
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
131
131
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
132
132
|
* `null
|
|
@@ -170,7 +170,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
170
170
|
throw new Error('Invalid input keysOrNodes');
|
|
171
171
|
}
|
|
172
172
|
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
173
|
-
sortedData = sorted.map(([,
|
|
173
|
+
sortedData = sorted.map(([, value]) => value);
|
|
174
174
|
const recursive = (arr, data) => {
|
|
175
175
|
if (arr.length === 0)
|
|
176
176
|
return;
|
|
@@ -414,7 +414,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
414
414
|
return;
|
|
415
415
|
const m = l + Math.floor((r - l) / 2);
|
|
416
416
|
const midNode = sorted[m];
|
|
417
|
-
this.add(midNode.key, midNode.
|
|
417
|
+
this.add(midNode.key, midNode.value);
|
|
418
418
|
buildBalanceBST(l, m - 1);
|
|
419
419
|
buildBalanceBST(m + 1, r);
|
|
420
420
|
};
|
|
@@ -430,7 +430,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
430
430
|
if (l <= r) {
|
|
431
431
|
const m = l + Math.floor((r - l) / 2);
|
|
432
432
|
const midNode = sorted[m];
|
|
433
|
-
this.add(midNode.key, midNode.
|
|
433
|
+
this.add(midNode.key, midNode.value);
|
|
434
434
|
stack.push([m + 1, r]);
|
|
435
435
|
stack.push([l, m - 1]);
|
|
436
436
|
}
|
|
@@ -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
|
get color() {
|
|
@@ -20,8 +20,8 @@ class RBTree extends bst_1.BST {
|
|
|
20
20
|
constructor(options) {
|
|
21
21
|
super(options);
|
|
22
22
|
}
|
|
23
|
-
createNode(key,
|
|
24
|
-
return new RBTreeNode(key,
|
|
23
|
+
createNode(key, value) {
|
|
24
|
+
return new RBTreeNode(key, value);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
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,17 +9,17 @@
|
|
|
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 = 0;
|
|
14
14
|
this._end = 0;
|
|
15
|
-
this.
|
|
15
|
+
this._value = null;
|
|
16
16
|
this._sum = 0;
|
|
17
17
|
this._left = null;
|
|
18
18
|
this._right = null;
|
|
19
19
|
this._start = start;
|
|
20
20
|
this._end = end;
|
|
21
21
|
this._sum = sum;
|
|
22
|
-
this.
|
|
22
|
+
this._value = value || null;
|
|
23
23
|
}
|
|
24
24
|
get start() {
|
|
25
25
|
return this._start;
|
|
@@ -33,11 +33,11 @@ class SegmentTreeNode {
|
|
|
33
33
|
set end(v) {
|
|
34
34
|
this._end = v;
|
|
35
35
|
}
|
|
36
|
-
get
|
|
37
|
-
return this.
|
|
36
|
+
get value() {
|
|
37
|
+
return this._value;
|
|
38
38
|
}
|
|
39
|
-
set
|
|
40
|
-
this.
|
|
39
|
+
set value(v) {
|
|
40
|
+
this._value = v;
|
|
41
41
|
}
|
|
42
42
|
get sum() {
|
|
43
43
|
return this._sum;
|
|
@@ -126,39 +126,39 @@ class SegmentTree {
|
|
|
126
126
|
* updated.
|
|
127
127
|
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
128
128
|
* the `SegmentTreeNode` at the specified `index`.
|
|
129
|
-
* @param {SegmentTreeNodeVal} [
|
|
129
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
130
130
|
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
131
|
-
* cur.
|
|
131
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
132
132
|
* @returns The function does not return anything.
|
|
133
133
|
*/
|
|
134
|
-
updateNode(index, sum,
|
|
134
|
+
updateNode(index, sum, value) {
|
|
135
135
|
const root = this.root || null;
|
|
136
136
|
if (!root) {
|
|
137
137
|
return;
|
|
138
138
|
}
|
|
139
|
-
const dfs = (cur, index, sum,
|
|
139
|
+
const dfs = (cur, index, sum, value) => {
|
|
140
140
|
if (cur.start === cur.end && cur.start === index) {
|
|
141
141
|
cur.sum = sum;
|
|
142
|
-
if (
|
|
143
|
-
cur.
|
|
142
|
+
if (value !== undefined)
|
|
143
|
+
cur.value = value;
|
|
144
144
|
return;
|
|
145
145
|
}
|
|
146
146
|
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
147
147
|
if (index <= mid) {
|
|
148
148
|
if (cur.left) {
|
|
149
|
-
dfs(cur.left, index, sum,
|
|
149
|
+
dfs(cur.left, index, sum, value);
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
else {
|
|
153
153
|
if (cur.right) {
|
|
154
|
-
dfs(cur.right, index, sum,
|
|
154
|
+
dfs(cur.right, index, sum, value);
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
157
|
if (cur.left && cur.right) {
|
|
158
158
|
cur.sum = cur.left.sum + cur.right.sum;
|
|
159
159
|
}
|
|
160
160
|
};
|
|
161
|
-
dfs(root, index, sum,
|
|
161
|
+
dfs(root, index, sum, value);
|
|
162
162
|
}
|
|
163
163
|
/**
|
|
164
164
|
* 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
|