data-structure-typed 1.50.2 → 1.50.4
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/.github/ISSUE_TEMPLATE/bug_report.md +0 -13
- package/CHANGELOG.md +1 -1
- package/README.md +23 -21
- package/README_zh-CN.md +7 -7
- 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 -155
- package/dist/cjs/data-structures/base/iterable-base.d.ts +6 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +43 -10
- package/dist/cjs/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +50 -12
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +33 -1
- 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 +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/cjs/data-structures/binary-tree/bst.js +51 -20
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/cjs/data-structures/binary-tree/index.js +2 -1
- package/dist/cjs/data-structures/binary-tree/index.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +90 -24
- 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-multi-map.d.ts +200 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +400 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -0
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +0 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +105 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +126 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/cjs/data-structures/hash/hash-map.js +196 -62
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +29 -19
- package/dist/cjs/data-structures/heap/heap.js +29 -20
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +83 -25
- 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 +26 -3
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +34 -3
- 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 +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/cjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js +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 +95 -21
- package/dist/cjs/data-structures/queue/deque.js +100 -16
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +65 -45
- package/dist/cjs/data-structures/queue/queue.js +65 -45
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +36 -22
- package/dist/cjs/data-structures/stack/stack.js +36 -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 +100 -36
- package/dist/cjs/data-structures/trie/trie.js +115 -36
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
- package/dist/cjs/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/cjs/types/data-structures/binary-tree/index.js +2 -1
- package/dist/cjs/types/data-structures/binary-tree/index.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/cjs/types/data-structures/binary-tree/{tree-multimap.js → tree-multi-map.js} +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js.map +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +6 -0
- package/dist/mjs/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +43 -10
- package/dist/mjs/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +46 -9
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +34 -2
- 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 +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/mjs/data-structures/binary-tree/bst.js +51 -20
- package/dist/mjs/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/index.js +2 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +91 -25
- 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-multi-map.d.ts +200 -0
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +397 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +0 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +105 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +127 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/mjs/data-structures/hash/hash-map.js +204 -70
- package/dist/mjs/data-structures/heap/heap.d.ts +29 -19
- package/dist/mjs/data-structures/heap/heap.js +29 -20
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +86 -28
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +36 -5
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/mjs/data-structures/matrix/matrix.js +1 -1
- 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 +95 -21
- package/dist/mjs/data-structures/queue/deque.js +105 -21
- package/dist/mjs/data-structures/queue/queue.d.ts +65 -45
- package/dist/mjs/data-structures/queue/queue.js +65 -45
- package/dist/mjs/data-structures/stack/stack.d.ts +36 -22
- package/dist/mjs/data-structures/stack/stack.js +36 -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 +100 -36
- package/dist/mjs/data-structures/trie/trie.js +118 -39
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/mjs/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/mjs/types/data-structures/binary-tree/index.js +2 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.js +1 -0
- package/dist/umd/data-structure-typed.js +1876 -656
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +59 -58
- package/src/data-structures/base/iterable-base.ts +12 -0
- package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +59 -20
- package/src/data-structures/binary-tree/avl-tree.ts +37 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/binary-tree/bst.ts +51 -19
- package/src/data-structures/binary-tree/index.ts +2 -1
- package/src/data-structures/binary-tree/rb-tree.ts +99 -28
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
- package/src/data-structures/graph/abstract-graph.ts +0 -211
- package/src/data-structures/graph/directed-graph.ts +122 -0
- package/src/data-structures/graph/undirected-graph.ts +143 -19
- package/src/data-structures/hash/hash-map.ts +228 -76
- package/src/data-structures/heap/heap.ts +31 -20
- package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
- package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +1 -1
- 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 +118 -22
- package/src/data-structures/queue/queue.ts +68 -45
- package/src/data-structures/stack/stack.ts +39 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +131 -40
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
- package/src/types/data-structures/binary-tree/index.ts +2 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
- package/test/integration/index.html +2 -2
- package/test/performance/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/{tree-multimap.test.ts → avl-tree-multi-map.test.ts} +55 -55
- package/test/unit/data-structures/binary-tree/overall.test.ts +24 -25
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +706 -0
- package/test/unit/data-structures/graph/abstract-graph.test.ts +3 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +189 -253
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/queue/queue.test.ts +2 -0
- package/test/unit/unrestricted-interconversion.test.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +0 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.js.map +0 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
- /package/dist/mjs/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -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,7 +47,6 @@ 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.
|
|
@@ -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
|
}
|
|
@@ -118,8 +158,14 @@ export class RedBlackTree extends BST {
|
|
|
118
158
|
isNode(keyOrNodeOrEntry) {
|
|
119
159
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
120
160
|
}
|
|
161
|
+
/**
|
|
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.
|
|
166
|
+
*/
|
|
121
167
|
isRealNode(node) {
|
|
122
|
-
if (node === this.
|
|
168
|
+
if (node === this._Sentinel || node === undefined)
|
|
123
169
|
return false;
|
|
124
170
|
return node instanceof RedBlackTreeNode;
|
|
125
171
|
}
|
|
@@ -144,11 +190,11 @@ export class RedBlackTree extends BST {
|
|
|
144
190
|
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
145
191
|
if (newNode === undefined)
|
|
146
192
|
return false;
|
|
147
|
-
newNode.left = this.
|
|
148
|
-
newNode.right = this.
|
|
193
|
+
newNode.left = this._Sentinel;
|
|
194
|
+
newNode.right = this._Sentinel;
|
|
149
195
|
let y = undefined;
|
|
150
196
|
let x = this.root;
|
|
151
|
-
while (x !== this.
|
|
197
|
+
while (x !== this._Sentinel) {
|
|
152
198
|
y = x;
|
|
153
199
|
if (x) {
|
|
154
200
|
if (newNode.key < x.key) {
|
|
@@ -212,9 +258,9 @@ export class RedBlackTree extends BST {
|
|
|
212
258
|
if (identifier === null)
|
|
213
259
|
return ans;
|
|
214
260
|
const helper = (node) => {
|
|
215
|
-
let z = this.
|
|
261
|
+
let z = this._Sentinel;
|
|
216
262
|
let x, y;
|
|
217
|
-
while (node !== this.
|
|
263
|
+
while (node !== this._Sentinel) {
|
|
218
264
|
if (node && callback(node) === identifier) {
|
|
219
265
|
z = node;
|
|
220
266
|
}
|
|
@@ -225,17 +271,17 @@ export class RedBlackTree extends BST {
|
|
|
225
271
|
node = node?.left;
|
|
226
272
|
}
|
|
227
273
|
}
|
|
228
|
-
if (z === this.
|
|
274
|
+
if (z === this._Sentinel) {
|
|
229
275
|
this._size--;
|
|
230
276
|
return;
|
|
231
277
|
}
|
|
232
278
|
y = z;
|
|
233
279
|
let yOriginalColor = y.color;
|
|
234
|
-
if (z.left === this.
|
|
280
|
+
if (z.left === this._Sentinel) {
|
|
235
281
|
x = z.right;
|
|
236
282
|
this._rbTransplant(z, z.right);
|
|
237
283
|
}
|
|
238
|
-
else if (z.right === this.
|
|
284
|
+
else if (z.right === this._Sentinel) {
|
|
239
285
|
x = z.left;
|
|
240
286
|
this._rbTransplant(z, z.left);
|
|
241
287
|
}
|
|
@@ -300,8 +346,14 @@ export class RedBlackTree extends BST {
|
|
|
300
346
|
* Time Complexity: O(1)
|
|
301
347
|
* Space Complexity: O(1)
|
|
302
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
|
+
*/
|
|
303
355
|
clear() {
|
|
304
|
-
this._root = this.
|
|
356
|
+
this._root = this._Sentinel;
|
|
305
357
|
this._size = 0;
|
|
306
358
|
}
|
|
307
359
|
/**
|
|
@@ -328,6 +380,12 @@ export class RedBlackTree extends BST {
|
|
|
328
380
|
}
|
|
329
381
|
return y;
|
|
330
382
|
}
|
|
383
|
+
/**
|
|
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.
|
|
388
|
+
*/
|
|
331
389
|
_setRoot(v) {
|
|
332
390
|
if (v) {
|
|
333
391
|
v.parent = undefined;
|
|
@@ -349,7 +407,7 @@ export class RedBlackTree extends BST {
|
|
|
349
407
|
if (x.right) {
|
|
350
408
|
const y = x.right;
|
|
351
409
|
x.right = y.left;
|
|
352
|
-
if (y.left !== this.
|
|
410
|
+
if (y.left !== this._Sentinel) {
|
|
353
411
|
if (y.left)
|
|
354
412
|
y.left.parent = x;
|
|
355
413
|
}
|
|
@@ -383,7 +441,7 @@ export class RedBlackTree extends BST {
|
|
|
383
441
|
if (x.left) {
|
|
384
442
|
const y = x.left;
|
|
385
443
|
x.left = y.right;
|
|
386
|
-
if (y.right !== this.
|
|
444
|
+
if (y.right !== this._Sentinel) {
|
|
387
445
|
if (y.right)
|
|
388
446
|
y.right.parent = x;
|
|
389
447
|
}
|
|
@@ -415,12 +473,13 @@ export class RedBlackTree extends BST {
|
|
|
415
473
|
*/
|
|
416
474
|
_fixInsert(k) {
|
|
417
475
|
let u;
|
|
418
|
-
while (k.parent && k.parent.color ===
|
|
476
|
+
while (k.parent && k.parent.color === RBTNColor.RED) {
|
|
419
477
|
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
420
478
|
u = k.parent.parent.left;
|
|
421
|
-
if (u && u.color ===
|
|
422
|
-
|
|
479
|
+
if (u && u.color === RBTNColor.RED) {
|
|
480
|
+
// Delay color flip
|
|
423
481
|
k.parent.color = RBTNColor.BLACK;
|
|
482
|
+
u.color = RBTNColor.BLACK;
|
|
424
483
|
k.parent.parent.color = RBTNColor.RED;
|
|
425
484
|
k = k.parent.parent;
|
|
426
485
|
}
|
|
@@ -429,16 +488,20 @@ export class RedBlackTree extends BST {
|
|
|
429
488
|
k = k.parent;
|
|
430
489
|
this._rightRotate(k);
|
|
431
490
|
}
|
|
432
|
-
|
|
433
|
-
k.parent.
|
|
491
|
+
// Check color before rotation
|
|
492
|
+
if (k.parent.color === RBTNColor.RED) {
|
|
493
|
+
k.parent.color = RBTNColor.BLACK;
|
|
494
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
495
|
+
}
|
|
434
496
|
this._leftRotate(k.parent.parent);
|
|
435
497
|
}
|
|
436
498
|
}
|
|
437
499
|
else {
|
|
438
500
|
u = k.parent.parent.right;
|
|
439
|
-
if (u && u.color ===
|
|
440
|
-
|
|
501
|
+
if (u && u.color === RBTNColor.RED) {
|
|
502
|
+
// Delay color flip
|
|
441
503
|
k.parent.color = RBTNColor.BLACK;
|
|
504
|
+
u.color = RBTNColor.BLACK;
|
|
442
505
|
k.parent.parent.color = RBTNColor.RED;
|
|
443
506
|
k = k.parent.parent;
|
|
444
507
|
}
|
|
@@ -447,8 +510,11 @@ export class RedBlackTree extends BST {
|
|
|
447
510
|
k = k.parent;
|
|
448
511
|
this._leftRotate(k);
|
|
449
512
|
}
|
|
450
|
-
|
|
451
|
-
k.parent.
|
|
513
|
+
// Check color before rotation
|
|
514
|
+
if (k.parent.color === RBTNColor.RED) {
|
|
515
|
+
k.parent.color = RBTNColor.BLACK;
|
|
516
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
517
|
+
}
|
|
452
518
|
this._rightRotate(k.parent.parent);
|
|
453
519
|
}
|
|
454
520
|
}
|
|
@@ -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
|
|
@@ -6,17 +6,118 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export class SegmentTreeNode {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The constructor initializes the properties of a SegmentTreeNode object.
|
|
11
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment covered
|
|
12
|
+
* by this node in a segment tree.
|
|
13
|
+
* @param {number} end - The `end` parameter represents the end index of the segment covered by this
|
|
14
|
+
* node in a segment tree.
|
|
15
|
+
* @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
|
|
16
|
+
* the segment tree node.
|
|
17
|
+
* @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
|
|
18
|
+
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
|
|
19
|
+
*/
|
|
15
20
|
constructor(start, end, sum, value) {
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
21
|
+
this._start = start;
|
|
22
|
+
this._end = end;
|
|
23
|
+
this._sum = sum;
|
|
24
|
+
this._value = value || undefined;
|
|
25
|
+
}
|
|
26
|
+
_start = 0;
|
|
27
|
+
/**
|
|
28
|
+
* The function returns the value of the protected variable _start.
|
|
29
|
+
* @returns The start value, which is of type number.
|
|
30
|
+
*/
|
|
31
|
+
get start() {
|
|
32
|
+
return this._start;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The above function sets the value of the "start" property.
|
|
36
|
+
* @param {number} value - The value parameter is of type number.
|
|
37
|
+
*/
|
|
38
|
+
set start(value) {
|
|
39
|
+
this._start = value;
|
|
40
|
+
}
|
|
41
|
+
_end = 0;
|
|
42
|
+
/**
|
|
43
|
+
* The function returns the value of the protected variable `_end`.
|
|
44
|
+
* @returns The value of the protected property `_end`.
|
|
45
|
+
*/
|
|
46
|
+
get end() {
|
|
47
|
+
return this._end;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The above function sets the value of the "end" property.
|
|
51
|
+
* @param {number} value - The value parameter is a number that represents the new value for the end
|
|
52
|
+
* property.
|
|
53
|
+
*/
|
|
54
|
+
set end(value) {
|
|
55
|
+
this._end = value;
|
|
56
|
+
}
|
|
57
|
+
_value = undefined;
|
|
58
|
+
/**
|
|
59
|
+
* The function returns the value of a segment tree node.
|
|
60
|
+
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
|
|
61
|
+
*/
|
|
62
|
+
get value() {
|
|
63
|
+
return this._value;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The function sets the value of a segment tree node.
|
|
67
|
+
* @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
|
|
68
|
+
* `SegmentTreeNodeVal` or `undefined`.
|
|
69
|
+
*/
|
|
70
|
+
set value(value) {
|
|
71
|
+
this._value = value;
|
|
72
|
+
}
|
|
73
|
+
_sum = 0;
|
|
74
|
+
/**
|
|
75
|
+
* The function returns the value of the sum property.
|
|
76
|
+
* @returns The method is returning the value of the variable `_sum`.
|
|
77
|
+
*/
|
|
78
|
+
get sum() {
|
|
79
|
+
return this._sum;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The above function sets the value of the sum property.
|
|
83
|
+
* @param {number} value - The parameter "value" is of type "number".
|
|
84
|
+
*/
|
|
85
|
+
set sum(value) {
|
|
86
|
+
this._sum = value;
|
|
87
|
+
}
|
|
88
|
+
_left = undefined;
|
|
89
|
+
/**
|
|
90
|
+
* The function returns the left child of a segment tree node.
|
|
91
|
+
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
|
|
92
|
+
* `SegmentTreeNode` or `undefined`.
|
|
93
|
+
*/
|
|
94
|
+
get left() {
|
|
95
|
+
return this._left;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The function sets the value of the left property of a SegmentTreeNode object.
|
|
99
|
+
* @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
|
|
100
|
+
* undefined.
|
|
101
|
+
*/
|
|
102
|
+
set left(value) {
|
|
103
|
+
this._left = value;
|
|
104
|
+
}
|
|
105
|
+
_right = undefined;
|
|
106
|
+
/**
|
|
107
|
+
* The function returns the right child of a segment tree node.
|
|
108
|
+
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
|
|
109
|
+
*/
|
|
110
|
+
get right() {
|
|
111
|
+
return this._right;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The function sets the right child of a segment tree node.
|
|
115
|
+
* @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
|
|
116
|
+
* undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
|
|
117
|
+
* value.
|
|
118
|
+
*/
|
|
119
|
+
set right(value) {
|
|
120
|
+
this._right = value;
|
|
20
121
|
}
|
|
21
122
|
}
|
|
22
123
|
export class SegmentTree {
|
|
@@ -44,18 +145,34 @@ export class SegmentTree {
|
|
|
44
145
|
}
|
|
45
146
|
}
|
|
46
147
|
_values = [];
|
|
148
|
+
/**
|
|
149
|
+
* The function returns an array of numbers.
|
|
150
|
+
* @returns An array of numbers is being returned.
|
|
151
|
+
*/
|
|
47
152
|
get values() {
|
|
48
153
|
return this._values;
|
|
49
154
|
}
|
|
50
155
|
_start = 0;
|
|
156
|
+
/**
|
|
157
|
+
* The function returns the value of the protected variable _start.
|
|
158
|
+
* @returns The start value, which is of type number.
|
|
159
|
+
*/
|
|
51
160
|
get start() {
|
|
52
161
|
return this._start;
|
|
53
162
|
}
|
|
54
163
|
_end;
|
|
164
|
+
/**
|
|
165
|
+
* The function returns the value of the protected variable `_end`.
|
|
166
|
+
* @returns The value of the protected property `_end`.
|
|
167
|
+
*/
|
|
55
168
|
get end() {
|
|
56
169
|
return this._end;
|
|
57
170
|
}
|
|
58
171
|
_root;
|
|
172
|
+
/**
|
|
173
|
+
* The function returns the root node of a segment tree.
|
|
174
|
+
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
|
|
175
|
+
*/
|
|
59
176
|
get root() {
|
|
60
177
|
return this._root;
|
|
61
178
|
}
|