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
|
@@ -24,11 +24,38 @@ export class RedBlackTreeNode<
|
|
|
24
24
|
V = any,
|
|
25
25
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
|
|
26
26
|
> extends BSTNode<K, V, NODE> {
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
29
|
+
* color.
|
|
30
|
+
* @param {K} key - The key parameter is of type K and represents the key of the node in the
|
|
31
|
+
* Red-Black Tree.
|
|
32
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
33
|
+
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
34
|
+
* creating a new instance of the Red-Black Tree Node.
|
|
35
|
+
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
36
|
+
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`.
|
|
37
|
+
*/
|
|
29
38
|
constructor(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK) {
|
|
30
39
|
super(key, value);
|
|
31
|
-
this.
|
|
40
|
+
this._color = color;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected _color: RBTNColor;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The function returns the color value of a variable.
|
|
47
|
+
* @returns The color value stored in the protected variable `_color`.
|
|
48
|
+
*/
|
|
49
|
+
get color(): RBTNColor {
|
|
50
|
+
return this._color;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The function sets the color property to the specified value.
|
|
55
|
+
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
56
|
+
*/
|
|
57
|
+
set color(value: RBTNColor) {
|
|
58
|
+
this._color = value;
|
|
32
59
|
}
|
|
33
60
|
}
|
|
34
61
|
|
|
@@ -47,8 +74,6 @@ export class RedBlackTree<
|
|
|
47
74
|
>
|
|
48
75
|
extends BST<K, V, NODE, TREE>
|
|
49
76
|
implements IBinaryTree<K, V, NODE, TREE> {
|
|
50
|
-
Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
51
|
-
|
|
52
77
|
/**
|
|
53
78
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
54
79
|
* initializes the tree with optional nodes and options.
|
|
@@ -63,18 +88,36 @@ export class RedBlackTree<
|
|
|
63
88
|
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
|
|
64
89
|
super([], options);
|
|
65
90
|
|
|
66
|
-
this._root = this.
|
|
91
|
+
this._root = this._Sentinel;
|
|
67
92
|
if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
|
|
68
93
|
}
|
|
69
94
|
|
|
95
|
+
protected _Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The function returns the value of the `_Sentinel` property.
|
|
99
|
+
* @returns The method is returning the value of the `_Sentinel` property.
|
|
100
|
+
*/
|
|
101
|
+
get Sentinel(): NODE {
|
|
102
|
+
return this._Sentinel;
|
|
103
|
+
}
|
|
104
|
+
|
|
70
105
|
protected _root: NODE;
|
|
71
106
|
|
|
107
|
+
/**
|
|
108
|
+
* The function returns the root node.
|
|
109
|
+
* @returns The root node of the data structure.
|
|
110
|
+
*/
|
|
72
111
|
get root(): NODE {
|
|
73
112
|
return this._root;
|
|
74
113
|
}
|
|
75
114
|
|
|
76
115
|
protected _size: number = 0;
|
|
77
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The function returns the size of an object.
|
|
119
|
+
* @returns The size of the object, which is a number.
|
|
120
|
+
*/
|
|
78
121
|
get size(): number {
|
|
79
122
|
return this._size;
|
|
80
123
|
}
|
|
@@ -149,8 +192,14 @@ export class RedBlackTree<
|
|
|
149
192
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
150
193
|
}
|
|
151
194
|
|
|
195
|
+
/**
|
|
196
|
+
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
197
|
+
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
198
|
+
* it can either be of type `NODE` or `undefined`.
|
|
199
|
+
* @returns a boolean value.
|
|
200
|
+
*/
|
|
152
201
|
override isRealNode(node: NODE | undefined): node is NODE {
|
|
153
|
-
if (node === this.
|
|
202
|
+
if (node === this._Sentinel || node === undefined) return false;
|
|
154
203
|
return node instanceof RedBlackTreeNode;
|
|
155
204
|
}
|
|
156
205
|
|
|
@@ -176,13 +225,13 @@ export class RedBlackTree<
|
|
|
176
225
|
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
177
226
|
if (newNode === undefined) return false;
|
|
178
227
|
|
|
179
|
-
newNode.left = this.
|
|
180
|
-
newNode.right = this.
|
|
228
|
+
newNode.left = this._Sentinel;
|
|
229
|
+
newNode.right = this._Sentinel;
|
|
181
230
|
|
|
182
231
|
let y: NODE | undefined = undefined;
|
|
183
232
|
let x: NODE | undefined = this.root;
|
|
184
233
|
|
|
185
|
-
while (x !== this.
|
|
234
|
+
while (x !== this._Sentinel) {
|
|
186
235
|
y = x;
|
|
187
236
|
if (x) {
|
|
188
237
|
if (newNode.key < x.key) {
|
|
@@ -250,9 +299,9 @@ export class RedBlackTree<
|
|
|
250
299
|
const ans: BinaryTreeDeleteResult<NODE>[] = [];
|
|
251
300
|
if (identifier === null) return ans;
|
|
252
301
|
const helper = (node: NODE | undefined): void => {
|
|
253
|
-
let z: NODE = this.
|
|
302
|
+
let z: NODE = this._Sentinel;
|
|
254
303
|
let x: NODE | undefined, y: NODE;
|
|
255
|
-
while (node !== this.
|
|
304
|
+
while (node !== this._Sentinel) {
|
|
256
305
|
if (node && callback(node) === identifier) {
|
|
257
306
|
z = node;
|
|
258
307
|
}
|
|
@@ -264,17 +313,17 @@ export class RedBlackTree<
|
|
|
264
313
|
}
|
|
265
314
|
}
|
|
266
315
|
|
|
267
|
-
if (z === this.
|
|
316
|
+
if (z === this._Sentinel) {
|
|
268
317
|
this._size--;
|
|
269
318
|
return;
|
|
270
319
|
}
|
|
271
320
|
|
|
272
321
|
y = z;
|
|
273
322
|
let yOriginalColor: number = y.color;
|
|
274
|
-
if (z.left === this.
|
|
323
|
+
if (z.left === this._Sentinel) {
|
|
275
324
|
x = z.right;
|
|
276
325
|
this._rbTransplant(z, z.right!);
|
|
277
|
-
} else if (z.right === this.
|
|
326
|
+
} else if (z.right === this._Sentinel) {
|
|
278
327
|
x = z.left;
|
|
279
328
|
this._rbTransplant(z, z.left!);
|
|
280
329
|
} else {
|
|
@@ -346,8 +395,14 @@ export class RedBlackTree<
|
|
|
346
395
|
* Space Complexity: O(1)
|
|
347
396
|
*/
|
|
348
397
|
|
|
398
|
+
/**
|
|
399
|
+
* Time Complexity: O(1)
|
|
400
|
+
* Space Complexity: O(1)
|
|
401
|
+
*
|
|
402
|
+
* The "clear" function sets the root node to the sentinel node and resets the size to 0.
|
|
403
|
+
*/
|
|
349
404
|
override clear() {
|
|
350
|
-
this._root = this.
|
|
405
|
+
this._root = this._Sentinel;
|
|
351
406
|
this._size = 0;
|
|
352
407
|
}
|
|
353
408
|
|
|
@@ -379,6 +434,12 @@ export class RedBlackTree<
|
|
|
379
434
|
return y!;
|
|
380
435
|
}
|
|
381
436
|
|
|
437
|
+
/**
|
|
438
|
+
* The function sets the root node of a tree structure and updates the parent property of the new
|
|
439
|
+
* root node.
|
|
440
|
+
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
|
|
441
|
+
* structure.
|
|
442
|
+
*/
|
|
382
443
|
protected override _setRoot(v: NODE) {
|
|
383
444
|
if (v) {
|
|
384
445
|
v.parent = undefined;
|
|
@@ -402,7 +463,7 @@ export class RedBlackTree<
|
|
|
402
463
|
if (x.right) {
|
|
403
464
|
const y: NODE = x.right;
|
|
404
465
|
x.right = y.left;
|
|
405
|
-
if (y.left !== this.
|
|
466
|
+
if (y.left !== this._Sentinel) {
|
|
406
467
|
if (y.left) y.left.parent = x;
|
|
407
468
|
}
|
|
408
469
|
y.parent = x.parent;
|
|
@@ -435,7 +496,7 @@ export class RedBlackTree<
|
|
|
435
496
|
if (x.left) {
|
|
436
497
|
const y: NODE = x.left;
|
|
437
498
|
x.left = y.right;
|
|
438
|
-
if (y.right !== this.
|
|
499
|
+
if (y.right !== this._Sentinel) {
|
|
439
500
|
if (y.right) y.right.parent = x;
|
|
440
501
|
}
|
|
441
502
|
y.parent = x.parent;
|
|
@@ -466,12 +527,14 @@ export class RedBlackTree<
|
|
|
466
527
|
*/
|
|
467
528
|
protected _fixInsert(k: NODE): void {
|
|
468
529
|
let u: NODE | undefined;
|
|
469
|
-
while (k.parent && k.parent.color ===
|
|
530
|
+
while (k.parent && k.parent.color === RBTNColor.RED) {
|
|
470
531
|
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
471
532
|
u = k.parent.parent.left;
|
|
472
|
-
|
|
473
|
-
|
|
533
|
+
|
|
534
|
+
if (u && u.color === RBTNColor.RED) {
|
|
535
|
+
// Delay color flip
|
|
474
536
|
k.parent.color = RBTNColor.BLACK;
|
|
537
|
+
u.color = RBTNColor.BLACK;
|
|
475
538
|
k.parent.parent.color = RBTNColor.RED;
|
|
476
539
|
k = k.parent.parent;
|
|
477
540
|
} else {
|
|
@@ -480,16 +543,20 @@ export class RedBlackTree<
|
|
|
480
543
|
this._rightRotate(k);
|
|
481
544
|
}
|
|
482
545
|
|
|
483
|
-
|
|
484
|
-
k.parent!.
|
|
546
|
+
// Check color before rotation
|
|
547
|
+
if (k.parent!.color === RBTNColor.RED) {
|
|
548
|
+
k.parent!.color = RBTNColor.BLACK;
|
|
549
|
+
k.parent!.parent!.color = RBTNColor.RED;
|
|
550
|
+
}
|
|
485
551
|
this._leftRotate(k.parent!.parent!);
|
|
486
552
|
}
|
|
487
553
|
} else {
|
|
488
|
-
u = k.parent
|
|
554
|
+
u = k.parent!.parent!.right;
|
|
489
555
|
|
|
490
|
-
if (u && u.color ===
|
|
491
|
-
|
|
556
|
+
if (u && u.color === RBTNColor.RED) {
|
|
557
|
+
// Delay color flip
|
|
492
558
|
k.parent.color = RBTNColor.BLACK;
|
|
559
|
+
u.color = RBTNColor.BLACK;
|
|
493
560
|
k.parent.parent!.color = RBTNColor.RED;
|
|
494
561
|
k = k.parent.parent!;
|
|
495
562
|
} else {
|
|
@@ -498,11 +565,15 @@ export class RedBlackTree<
|
|
|
498
565
|
this._leftRotate(k);
|
|
499
566
|
}
|
|
500
567
|
|
|
501
|
-
|
|
502
|
-
k.parent!.
|
|
568
|
+
// Check color before rotation
|
|
569
|
+
if (k.parent!.color === RBTNColor.RED) {
|
|
570
|
+
k.parent!.color = RBTNColor.BLACK;
|
|
571
|
+
k.parent!.parent!.color = RBTNColor.RED;
|
|
572
|
+
}
|
|
503
573
|
this._rightRotate(k.parent!.parent!);
|
|
504
574
|
}
|
|
505
575
|
}
|
|
576
|
+
|
|
506
577
|
if (k === this.root) {
|
|
507
578
|
break;
|
|
508
579
|
}
|
|
@@ -9,18 +9,136 @@
|
|
|
9
9
|
import type { SegmentTreeNodeVal } from '../../types';
|
|
10
10
|
|
|
11
11
|
export class SegmentTreeNode {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The constructor initializes the properties of a SegmentTreeNode object.
|
|
14
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment covered
|
|
15
|
+
* by this node in a segment tree.
|
|
16
|
+
* @param {number} end - The `end` parameter represents the end index of the segment covered by this
|
|
17
|
+
* node in a segment tree.
|
|
18
|
+
* @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
|
|
19
|
+
* the segment tree node.
|
|
20
|
+
* @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
|
|
21
|
+
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
|
|
22
|
+
*/
|
|
19
23
|
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined) {
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
+
this._start = start;
|
|
25
|
+
this._end = end;
|
|
26
|
+
this._sum = sum;
|
|
27
|
+
this._value = value || undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected _start = 0;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The function returns the value of the protected variable _start.
|
|
34
|
+
* @returns The start value, which is of type number.
|
|
35
|
+
*/
|
|
36
|
+
get start(): number {
|
|
37
|
+
return this._start;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The above function sets the value of the "start" property.
|
|
42
|
+
* @param {number} value - The value parameter is of type number.
|
|
43
|
+
*/
|
|
44
|
+
set start(value: number) {
|
|
45
|
+
this._start = value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected _end = 0;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The function returns the value of the protected variable `_end`.
|
|
52
|
+
* @returns The value of the protected property `_end`.
|
|
53
|
+
*/
|
|
54
|
+
get end(): number {
|
|
55
|
+
return this._end;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The above function sets the value of the "end" property.
|
|
60
|
+
* @param {number} value - The value parameter is a number that represents the new value for the end
|
|
61
|
+
* property.
|
|
62
|
+
*/
|
|
63
|
+
set end(value: number) {
|
|
64
|
+
this._end = value;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected _value: SegmentTreeNodeVal | undefined = undefined;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The function returns the value of a segment tree node.
|
|
71
|
+
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
|
|
72
|
+
*/
|
|
73
|
+
get value(): SegmentTreeNodeVal | undefined {
|
|
74
|
+
return this._value;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The function sets the value of a segment tree node.
|
|
79
|
+
* @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
|
|
80
|
+
* `SegmentTreeNodeVal` or `undefined`.
|
|
81
|
+
*/
|
|
82
|
+
set value(value: SegmentTreeNodeVal | undefined) {
|
|
83
|
+
this._value = value;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected _sum = 0;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The function returns the value of the sum property.
|
|
90
|
+
* @returns The method is returning the value of the variable `_sum`.
|
|
91
|
+
*/
|
|
92
|
+
get sum(): number {
|
|
93
|
+
return this._sum;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The above function sets the value of the sum property.
|
|
98
|
+
* @param {number} value - The parameter "value" is of type "number".
|
|
99
|
+
*/
|
|
100
|
+
set sum(value: number) {
|
|
101
|
+
this._sum = value;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
protected _left: SegmentTreeNode | undefined = undefined;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The function returns the left child of a segment tree node.
|
|
108
|
+
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
|
|
109
|
+
* `SegmentTreeNode` or `undefined`.
|
|
110
|
+
*/
|
|
111
|
+
get left(): SegmentTreeNode | undefined {
|
|
112
|
+
return this._left;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The function sets the value of the left property of a SegmentTreeNode object.
|
|
117
|
+
* @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
|
|
118
|
+
* undefined.
|
|
119
|
+
*/
|
|
120
|
+
set left(value: SegmentTreeNode | undefined) {
|
|
121
|
+
this._left = value;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
protected _right: SegmentTreeNode | undefined = undefined;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The function returns the right child of a segment tree node.
|
|
128
|
+
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
|
|
129
|
+
*/
|
|
130
|
+
get right(): SegmentTreeNode | undefined {
|
|
131
|
+
return this._right;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The function sets the right child of a segment tree node.
|
|
136
|
+
* @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
|
|
137
|
+
* undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
|
|
138
|
+
* value.
|
|
139
|
+
*/
|
|
140
|
+
set right(value: SegmentTreeNode | undefined) {
|
|
141
|
+
this._right = value;
|
|
24
142
|
}
|
|
25
143
|
}
|
|
26
144
|
|
|
@@ -51,24 +169,40 @@ export class SegmentTree {
|
|
|
51
169
|
|
|
52
170
|
protected _values: number[] = [];
|
|
53
171
|
|
|
172
|
+
/**
|
|
173
|
+
* The function returns an array of numbers.
|
|
174
|
+
* @returns An array of numbers is being returned.
|
|
175
|
+
*/
|
|
54
176
|
get values(): number[] {
|
|
55
177
|
return this._values;
|
|
56
178
|
}
|
|
57
179
|
|
|
58
180
|
protected _start = 0;
|
|
59
181
|
|
|
182
|
+
/**
|
|
183
|
+
* The function returns the value of the protected variable _start.
|
|
184
|
+
* @returns The start value, which is of type number.
|
|
185
|
+
*/
|
|
60
186
|
get start(): number {
|
|
61
187
|
return this._start;
|
|
62
188
|
}
|
|
63
189
|
|
|
64
190
|
protected _end: number;
|
|
65
191
|
|
|
192
|
+
/**
|
|
193
|
+
* The function returns the value of the protected variable `_end`.
|
|
194
|
+
* @returns The value of the protected property `_end`.
|
|
195
|
+
*/
|
|
66
196
|
get end(): number {
|
|
67
197
|
return this._end;
|
|
68
198
|
}
|
|
69
199
|
|
|
70
200
|
protected _root: SegmentTreeNode | undefined;
|
|
71
201
|
|
|
202
|
+
/**
|
|
203
|
+
* The function returns the root node of a segment tree.
|
|
204
|
+
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
|
|
205
|
+
*/
|
|
72
206
|
get root(): SegmentTreeNode | undefined {
|
|
73
207
|
return this._root;
|
|
74
208
|
}
|