data-structure-typed 1.50.1 → 1.50.3
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/README.md +30 -28
- package/README_zh-CN.md +1 -1
- package/SPECIFICATION.md +56 -0
- package/SPONSOR-zh-CN.md +90 -0
- package/SPONSOR.md +54 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +149 -161
- package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/cjs/data-structures/hash/hash-map.js +347 -78
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/cjs/data-structures/heap/heap.js +95 -26
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
- 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 +154 -106
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/cjs/data-structures/queue/deque.js +200 -62
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/cjs/data-structures/queue/queue.js +111 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/cjs/data-structures/stack/stack.js +58 -22
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/cjs/data-structures/tree/tree.js +77 -11
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/cjs/data-structures/trie/trie.js +153 -33
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/mjs/data-structures/hash/hash-map.js +353 -84
- package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
- package/dist/mjs/data-structures/heap/heap.js +95 -26
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
- package/dist/mjs/data-structures/queue/deque.js +205 -67
- package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
- package/dist/mjs/data-structures/queue/queue.js +111 -49
- package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
- package/dist/mjs/data-structures/stack/stack.js +58 -22
- package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/mjs/data-structures/tree/tree.js +80 -14
- package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
- package/dist/mjs/data-structures/trie/trie.js +156 -36
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +3121 -1583
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +61 -55
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +65 -5
- package/test/unit/data-structures/queue/queue.test.ts +24 -5
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
|
@@ -8,10 +8,35 @@
|
|
|
8
8
|
import { RBTNColor } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
export class RedBlackTreeNode extends BSTNode {
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
13
|
+
* color.
|
|
14
|
+
* @param {K} key - The key parameter is of type K and represents the key of the node in the
|
|
15
|
+
* Red-Black Tree.
|
|
16
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
17
|
+
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
18
|
+
* creating a new instance of the Red-Black Tree Node.
|
|
19
|
+
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
20
|
+
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`.
|
|
21
|
+
*/
|
|
12
22
|
constructor(key, value, color = RBTNColor.BLACK) {
|
|
13
23
|
super(key, value);
|
|
14
|
-
this.
|
|
24
|
+
this._color = color;
|
|
25
|
+
}
|
|
26
|
+
_color;
|
|
27
|
+
/**
|
|
28
|
+
* The function returns the color value of a variable.
|
|
29
|
+
* @returns The color value stored in the protected variable `_color`.
|
|
30
|
+
*/
|
|
31
|
+
get color() {
|
|
32
|
+
return this._color;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The function sets the color property to the specified value.
|
|
36
|
+
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
37
|
+
*/
|
|
38
|
+
set color(value) {
|
|
39
|
+
this._color = value;
|
|
15
40
|
}
|
|
16
41
|
}
|
|
17
42
|
/**
|
|
@@ -22,11 +47,10 @@ export class RedBlackTreeNode extends BSTNode {
|
|
|
22
47
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
23
48
|
*/
|
|
24
49
|
export class RedBlackTree extends BST {
|
|
25
|
-
Sentinel = new RedBlackTreeNode(NaN);
|
|
26
50
|
/**
|
|
27
51
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
28
52
|
* initializes the tree with optional nodes and options.
|
|
29
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
53
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
30
54
|
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
31
55
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
32
56
|
* nodes to the
|
|
@@ -36,15 +60,31 @@ export class RedBlackTree extends BST {
|
|
|
36
60
|
*/
|
|
37
61
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
38
62
|
super([], options);
|
|
39
|
-
this._root = this.
|
|
63
|
+
this._root = this._Sentinel;
|
|
40
64
|
if (keysOrNodesOrEntries)
|
|
41
65
|
super.addMany(keysOrNodesOrEntries);
|
|
42
66
|
}
|
|
67
|
+
_Sentinel = new RedBlackTreeNode(NaN);
|
|
68
|
+
/**
|
|
69
|
+
* The function returns the value of the `_Sentinel` property.
|
|
70
|
+
* @returns The method is returning the value of the `_Sentinel` property.
|
|
71
|
+
*/
|
|
72
|
+
get Sentinel() {
|
|
73
|
+
return this._Sentinel;
|
|
74
|
+
}
|
|
43
75
|
_root;
|
|
76
|
+
/**
|
|
77
|
+
* The function returns the root node.
|
|
78
|
+
* @returns The root node of the data structure.
|
|
79
|
+
*/
|
|
44
80
|
get root() {
|
|
45
81
|
return this._root;
|
|
46
82
|
}
|
|
47
83
|
_size = 0;
|
|
84
|
+
/**
|
|
85
|
+
* The function returns the size of an object.
|
|
86
|
+
* @returns The size of the object, which is a number.
|
|
87
|
+
*/
|
|
48
88
|
get size() {
|
|
49
89
|
return this._size;
|
|
50
90
|
}
|
|
@@ -77,14 +117,14 @@ export class RedBlackTree extends BST {
|
|
|
77
117
|
});
|
|
78
118
|
}
|
|
79
119
|
/**
|
|
80
|
-
* The function `
|
|
81
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
120
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
121
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
82
122
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
83
|
-
* `
|
|
123
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
84
124
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
85
|
-
* @returns a node of type
|
|
125
|
+
* @returns a node of type NODE or undefined.
|
|
86
126
|
*/
|
|
87
|
-
|
|
127
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
88
128
|
let node;
|
|
89
129
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
90
130
|
return;
|
|
@@ -111,7 +151,7 @@ export class RedBlackTree extends BST {
|
|
|
111
151
|
}
|
|
112
152
|
/**
|
|
113
153
|
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
114
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
154
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
115
155
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
116
156
|
* class.
|
|
117
157
|
*/
|
|
@@ -119,18 +159,20 @@ export class RedBlackTree extends BST {
|
|
|
119
159
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
120
160
|
}
|
|
121
161
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
162
|
+
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
163
|
+
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
164
|
+
* it can either be of type `NODE` or `undefined`.
|
|
165
|
+
* @returns a boolean value.
|
|
124
166
|
*/
|
|
125
167
|
isRealNode(node) {
|
|
126
|
-
if (node === this.
|
|
168
|
+
if (node === this._Sentinel || node === undefined)
|
|
127
169
|
return false;
|
|
128
170
|
return node instanceof RedBlackTreeNode;
|
|
129
171
|
}
|
|
130
172
|
/**
|
|
131
173
|
* Time Complexity: O(log n)
|
|
132
174
|
* Space Complexity: O(1)
|
|
133
|
-
*
|
|
175
|
+
* On average (where n is the number of nodes in the tree)
|
|
134
176
|
*/
|
|
135
177
|
/**
|
|
136
178
|
* Time Complexity: O(log n)
|
|
@@ -142,17 +184,17 @@ export class RedBlackTree extends BST {
|
|
|
142
184
|
* entry.
|
|
143
185
|
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
144
186
|
* being added to the binary search tree.
|
|
145
|
-
* @returns The method `add` returns either the newly added node (`
|
|
187
|
+
* @returns The method `add` returns either the newly added node (`NODE`) or `undefined`.
|
|
146
188
|
*/
|
|
147
189
|
add(keyOrNodeOrEntry, value) {
|
|
148
|
-
const newNode = this.
|
|
190
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
149
191
|
if (newNode === undefined)
|
|
150
192
|
return false;
|
|
151
|
-
newNode.left = this.
|
|
152
|
-
newNode.right = this.
|
|
193
|
+
newNode.left = this._Sentinel;
|
|
194
|
+
newNode.right = this._Sentinel;
|
|
153
195
|
let y = undefined;
|
|
154
196
|
let x = this.root;
|
|
155
|
-
while (x !== this.
|
|
197
|
+
while (x !== this._Sentinel) {
|
|
156
198
|
y = x;
|
|
157
199
|
if (x) {
|
|
158
200
|
if (newNode.key < x.key) {
|
|
@@ -195,7 +237,6 @@ export class RedBlackTree extends BST {
|
|
|
195
237
|
/**
|
|
196
238
|
* Time Complexity: O(log n)
|
|
197
239
|
* Space Complexity: O(1)
|
|
198
|
-
* on average (where n is the number of nodes in the tree)
|
|
199
240
|
*/
|
|
200
241
|
/**
|
|
201
242
|
* Time Complexity: O(log n)
|
|
@@ -207,19 +248,19 @@ export class RedBlackTree extends BST {
|
|
|
207
248
|
* that you want to use to identify the node that you want to delete from the binary tree. It can be
|
|
208
249
|
* of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
209
250
|
* you don't want to
|
|
210
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
251
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` and
|
|
211
252
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
212
253
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
213
|
-
* @returns an array of `BinaryTreeDeleteResult<
|
|
254
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
214
255
|
*/
|
|
215
256
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
216
257
|
const ans = [];
|
|
217
258
|
if (identifier === null)
|
|
218
259
|
return ans;
|
|
219
260
|
const helper = (node) => {
|
|
220
|
-
let z = this.
|
|
261
|
+
let z = this._Sentinel;
|
|
221
262
|
let x, y;
|
|
222
|
-
while (node !== this.
|
|
263
|
+
while (node !== this._Sentinel) {
|
|
223
264
|
if (node && callback(node) === identifier) {
|
|
224
265
|
z = node;
|
|
225
266
|
}
|
|
@@ -230,17 +271,17 @@ export class RedBlackTree extends BST {
|
|
|
230
271
|
node = node?.left;
|
|
231
272
|
}
|
|
232
273
|
}
|
|
233
|
-
if (z === this.
|
|
274
|
+
if (z === this._Sentinel) {
|
|
234
275
|
this._size--;
|
|
235
276
|
return;
|
|
236
277
|
}
|
|
237
278
|
y = z;
|
|
238
279
|
let yOriginalColor = y.color;
|
|
239
|
-
if (z.left === this.
|
|
280
|
+
if (z.left === this._Sentinel) {
|
|
240
281
|
x = z.right;
|
|
241
282
|
this._rbTransplant(z, z.right);
|
|
242
283
|
}
|
|
243
|
-
else if (z.right === this.
|
|
284
|
+
else if (z.right === this._Sentinel) {
|
|
244
285
|
x = z.left;
|
|
245
286
|
this._rbTransplant(z, z.left);
|
|
246
287
|
}
|
|
@@ -286,14 +327,14 @@ export class RedBlackTree extends BST {
|
|
|
286
327
|
* node that matches the other criteria
|
|
287
328
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
288
329
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
289
|
-
* function should take a single parameter of type `
|
|
290
|
-
* @param {K |
|
|
330
|
+
* function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
|
|
331
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
291
332
|
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
292
333
|
* provided, the search will start from the root of the binary tree.
|
|
293
334
|
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
294
335
|
* iteration to be performed when searching for nodes in the binary tree. It is used in the
|
|
295
336
|
* `getNodes` method, which is called within the `getNode` method.
|
|
296
|
-
* @returns a value of type `
|
|
337
|
+
* @returns a value of type `NODE`, `null`, or `undefined`.
|
|
297
338
|
*/
|
|
298
339
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
299
340
|
if (identifier instanceof RedBlackTreeNode)
|
|
@@ -301,6 +342,20 @@ export class RedBlackTree extends BST {
|
|
|
301
342
|
beginRoot = this.ensureNode(beginRoot);
|
|
302
343
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
303
344
|
}
|
|
345
|
+
/**
|
|
346
|
+
* Time Complexity: O(1)
|
|
347
|
+
* Space Complexity: O(1)
|
|
348
|
+
*/
|
|
349
|
+
/**
|
|
350
|
+
* Time Complexity: O(1)
|
|
351
|
+
* Space Complexity: O(1)
|
|
352
|
+
*
|
|
353
|
+
* The "clear" function sets the root node to the sentinel node and resets the size to 0.
|
|
354
|
+
*/
|
|
355
|
+
clear() {
|
|
356
|
+
this._root = this._Sentinel;
|
|
357
|
+
this._size = 0;
|
|
358
|
+
}
|
|
304
359
|
/**
|
|
305
360
|
* Time Complexity: O(log n)
|
|
306
361
|
* Space Complexity: O(1)
|
|
@@ -326,13 +381,11 @@ export class RedBlackTree extends BST {
|
|
|
326
381
|
return y;
|
|
327
382
|
}
|
|
328
383
|
/**
|
|
329
|
-
*
|
|
330
|
-
*
|
|
384
|
+
* The function sets the root node of a tree structure and updates the parent property of the new
|
|
385
|
+
* root node.
|
|
386
|
+
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
|
|
387
|
+
* structure.
|
|
331
388
|
*/
|
|
332
|
-
clear() {
|
|
333
|
-
this._root = this.Sentinel;
|
|
334
|
-
this._size = 0;
|
|
335
|
-
}
|
|
336
389
|
_setRoot(v) {
|
|
337
390
|
if (v) {
|
|
338
391
|
v.parent = undefined;
|
|
@@ -348,13 +401,13 @@ export class RedBlackTree extends BST {
|
|
|
348
401
|
* Space Complexity: O(1)
|
|
349
402
|
*
|
|
350
403
|
* The function performs a left rotation on a binary tree node.
|
|
351
|
-
* @param {RedBlackTreeNode} x - The parameter `x` is of type `
|
|
404
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `NODE`, which likely represents a node in a binary tree.
|
|
352
405
|
*/
|
|
353
406
|
_leftRotate(x) {
|
|
354
407
|
if (x.right) {
|
|
355
408
|
const y = x.right;
|
|
356
409
|
x.right = y.left;
|
|
357
|
-
if (y.left !== this.
|
|
410
|
+
if (y.left !== this._Sentinel) {
|
|
358
411
|
if (y.left)
|
|
359
412
|
y.left.parent = x;
|
|
360
413
|
}
|
|
@@ -388,7 +441,7 @@ export class RedBlackTree extends BST {
|
|
|
388
441
|
if (x.left) {
|
|
389
442
|
const y = x.left;
|
|
390
443
|
x.left = y.right;
|
|
391
|
-
if (y.right !== this.
|
|
444
|
+
if (y.right !== this._Sentinel) {
|
|
392
445
|
if (y.right)
|
|
393
446
|
y.right.parent = x;
|
|
394
447
|
}
|
|
@@ -406,6 +459,63 @@ export class RedBlackTree extends BST {
|
|
|
406
459
|
x.parent = y;
|
|
407
460
|
}
|
|
408
461
|
}
|
|
462
|
+
/**
|
|
463
|
+
* Time Complexity: O(log n)
|
|
464
|
+
* Space Complexity: O(1)
|
|
465
|
+
*/
|
|
466
|
+
/**
|
|
467
|
+
* Time Complexity: O(log n)
|
|
468
|
+
* Space Complexity: O(1)
|
|
469
|
+
*
|
|
470
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
471
|
+
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
472
|
+
* red-black tree.
|
|
473
|
+
*/
|
|
474
|
+
_fixInsert(k) {
|
|
475
|
+
let u;
|
|
476
|
+
while (k.parent && k.parent.color === 1) {
|
|
477
|
+
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
478
|
+
u = k.parent.parent.left;
|
|
479
|
+
if (u && u.color === 1) {
|
|
480
|
+
u.color = RBTNColor.BLACK;
|
|
481
|
+
k.parent.color = RBTNColor.BLACK;
|
|
482
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
483
|
+
k = k.parent.parent;
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
if (k === k.parent.left) {
|
|
487
|
+
k = k.parent;
|
|
488
|
+
this._rightRotate(k);
|
|
489
|
+
}
|
|
490
|
+
k.parent.color = RBTNColor.BLACK;
|
|
491
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
492
|
+
this._leftRotate(k.parent.parent);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
u = k.parent.parent.right;
|
|
497
|
+
if (u && u.color === 1) {
|
|
498
|
+
u.color = RBTNColor.BLACK;
|
|
499
|
+
k.parent.color = RBTNColor.BLACK;
|
|
500
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
501
|
+
k = k.parent.parent;
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
if (k === k.parent.right) {
|
|
505
|
+
k = k.parent;
|
|
506
|
+
this._leftRotate(k);
|
|
507
|
+
}
|
|
508
|
+
k.parent.color = RBTNColor.BLACK;
|
|
509
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
510
|
+
this._rightRotate(k.parent.parent);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
if (k === this.root) {
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
this.root.color = RBTNColor.BLACK;
|
|
518
|
+
}
|
|
409
519
|
/**
|
|
410
520
|
* Time Complexity: O(log n)
|
|
411
521
|
* Space Complexity: O(1)
|
|
@@ -505,68 +615,11 @@ export class RedBlackTree extends BST {
|
|
|
505
615
|
}
|
|
506
616
|
v.parent = u.parent;
|
|
507
617
|
}
|
|
508
|
-
/**
|
|
509
|
-
* Time Complexity: O(log n)
|
|
510
|
-
* Space Complexity: O(1)
|
|
511
|
-
*/
|
|
512
|
-
/**
|
|
513
|
-
* Time Complexity: O(log n)
|
|
514
|
-
* Space Complexity: O(1)
|
|
515
|
-
*
|
|
516
|
-
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
517
|
-
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
518
|
-
* red-black tree.
|
|
519
|
-
*/
|
|
520
|
-
_fixInsert(k) {
|
|
521
|
-
let u;
|
|
522
|
-
while (k.parent && k.parent.color === 1) {
|
|
523
|
-
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
524
|
-
u = k.parent.parent.left;
|
|
525
|
-
if (u && u.color === 1) {
|
|
526
|
-
u.color = RBTNColor.BLACK;
|
|
527
|
-
k.parent.color = RBTNColor.BLACK;
|
|
528
|
-
k.parent.parent.color = RBTNColor.RED;
|
|
529
|
-
k = k.parent.parent;
|
|
530
|
-
}
|
|
531
|
-
else {
|
|
532
|
-
if (k === k.parent.left) {
|
|
533
|
-
k = k.parent;
|
|
534
|
-
this._rightRotate(k);
|
|
535
|
-
}
|
|
536
|
-
k.parent.color = RBTNColor.BLACK;
|
|
537
|
-
k.parent.parent.color = RBTNColor.RED;
|
|
538
|
-
this._leftRotate(k.parent.parent);
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
else {
|
|
542
|
-
u = k.parent.parent.right;
|
|
543
|
-
if (u && u.color === 1) {
|
|
544
|
-
u.color = RBTNColor.BLACK;
|
|
545
|
-
k.parent.color = RBTNColor.BLACK;
|
|
546
|
-
k.parent.parent.color = RBTNColor.RED;
|
|
547
|
-
k = k.parent.parent;
|
|
548
|
-
}
|
|
549
|
-
else {
|
|
550
|
-
if (k === k.parent.right) {
|
|
551
|
-
k = k.parent;
|
|
552
|
-
this._leftRotate(k);
|
|
553
|
-
}
|
|
554
|
-
k.parent.color = RBTNColor.BLACK;
|
|
555
|
-
k.parent.parent.color = RBTNColor.RED;
|
|
556
|
-
this._rightRotate(k.parent.parent);
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
if (k === this.root) {
|
|
560
|
-
break;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
this.root.color = RBTNColor.BLACK;
|
|
564
|
-
}
|
|
565
618
|
/**
|
|
566
619
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
567
|
-
* @param {
|
|
568
|
-
* data structure. It is of type `
|
|
569
|
-
* @param {
|
|
620
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
621
|
+
* data structure. It is of type `NODE`, which is the type of the nodes in the data structure.
|
|
622
|
+
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
570
623
|
* data structure.
|
|
571
624
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
572
625
|
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
@@ -7,13 +7,90 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { SegmentTreeNodeVal } from '../../types';
|
|
9
9
|
export declare class SegmentTreeNode {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
/**
|
|
11
|
+
* The constructor initializes the properties of a SegmentTreeNode object.
|
|
12
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment covered
|
|
13
|
+
* by this node in a segment tree.
|
|
14
|
+
* @param {number} end - The `end` parameter represents the end index of the segment covered by this
|
|
15
|
+
* node in a segment tree.
|
|
16
|
+
* @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
|
|
17
|
+
* the segment tree node.
|
|
18
|
+
* @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
|
|
19
|
+
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
|
|
20
|
+
*/
|
|
16
21
|
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined);
|
|
22
|
+
protected _start: number;
|
|
23
|
+
/**
|
|
24
|
+
* The function returns the value of the protected variable _start.
|
|
25
|
+
* @returns The start value, which is of type number.
|
|
26
|
+
*/
|
|
27
|
+
get start(): number;
|
|
28
|
+
/**
|
|
29
|
+
* The above function sets the value of the "start" property.
|
|
30
|
+
* @param {number} value - The value parameter is of type number.
|
|
31
|
+
*/
|
|
32
|
+
set start(value: number);
|
|
33
|
+
protected _end: number;
|
|
34
|
+
/**
|
|
35
|
+
* The function returns the value of the protected variable `_end`.
|
|
36
|
+
* @returns The value of the protected property `_end`.
|
|
37
|
+
*/
|
|
38
|
+
get end(): number;
|
|
39
|
+
/**
|
|
40
|
+
* The above function sets the value of the "end" property.
|
|
41
|
+
* @param {number} value - The value parameter is a number that represents the new value for the end
|
|
42
|
+
* property.
|
|
43
|
+
*/
|
|
44
|
+
set end(value: number);
|
|
45
|
+
protected _value: SegmentTreeNodeVal | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* The function returns the value of a segment tree node.
|
|
48
|
+
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
|
|
49
|
+
*/
|
|
50
|
+
get value(): SegmentTreeNodeVal | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* The function sets the value of a segment tree node.
|
|
53
|
+
* @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
|
|
54
|
+
* `SegmentTreeNodeVal` or `undefined`.
|
|
55
|
+
*/
|
|
56
|
+
set value(value: SegmentTreeNodeVal | undefined);
|
|
57
|
+
protected _sum: number;
|
|
58
|
+
/**
|
|
59
|
+
* The function returns the value of the sum property.
|
|
60
|
+
* @returns The method is returning the value of the variable `_sum`.
|
|
61
|
+
*/
|
|
62
|
+
get sum(): number;
|
|
63
|
+
/**
|
|
64
|
+
* The above function sets the value of the sum property.
|
|
65
|
+
* @param {number} value - The parameter "value" is of type "number".
|
|
66
|
+
*/
|
|
67
|
+
set sum(value: number);
|
|
68
|
+
protected _left: SegmentTreeNode | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* The function returns the left child of a segment tree node.
|
|
71
|
+
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
|
|
72
|
+
* `SegmentTreeNode` or `undefined`.
|
|
73
|
+
*/
|
|
74
|
+
get left(): SegmentTreeNode | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* The function sets the value of the left property of a SegmentTreeNode object.
|
|
77
|
+
* @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
|
|
78
|
+
* undefined.
|
|
79
|
+
*/
|
|
80
|
+
set left(value: SegmentTreeNode | undefined);
|
|
81
|
+
protected _right: SegmentTreeNode | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* The function returns the right child of a segment tree node.
|
|
84
|
+
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
|
|
85
|
+
*/
|
|
86
|
+
get right(): SegmentTreeNode | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* The function sets the right child of a segment tree node.
|
|
89
|
+
* @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
|
|
90
|
+
* undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
|
|
91
|
+
* value.
|
|
92
|
+
*/
|
|
93
|
+
set right(value: SegmentTreeNode | undefined);
|
|
17
94
|
}
|
|
18
95
|
export declare class SegmentTree {
|
|
19
96
|
/**
|
|
@@ -27,12 +104,28 @@ export declare class SegmentTree {
|
|
|
27
104
|
*/
|
|
28
105
|
constructor(values: number[], start?: number, end?: number);
|
|
29
106
|
protected _values: number[];
|
|
107
|
+
/**
|
|
108
|
+
* The function returns an array of numbers.
|
|
109
|
+
* @returns An array of numbers is being returned.
|
|
110
|
+
*/
|
|
30
111
|
get values(): number[];
|
|
31
112
|
protected _start: number;
|
|
113
|
+
/**
|
|
114
|
+
* The function returns the value of the protected variable _start.
|
|
115
|
+
* @returns The start value, which is of type number.
|
|
116
|
+
*/
|
|
32
117
|
get start(): number;
|
|
33
118
|
protected _end: number;
|
|
119
|
+
/**
|
|
120
|
+
* The function returns the value of the protected variable `_end`.
|
|
121
|
+
* @returns The value of the protected property `_end`.
|
|
122
|
+
*/
|
|
34
123
|
get end(): number;
|
|
35
124
|
protected _root: SegmentTreeNode | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* The function returns the root node of a segment tree.
|
|
127
|
+
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
|
|
128
|
+
*/
|
|
36
129
|
get root(): SegmentTreeNode | undefined;
|
|
37
130
|
/**
|
|
38
131
|
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|