red-black-tree-typed 2.1.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +4017 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs-legacy/index.cjs +4019 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +4008 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm-legacy/index.mjs +4010 -0
- package/dist/esm-legacy/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +236 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +197 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +345 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +467 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +262 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +244 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +332 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +15 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/red-black-tree-typed.js +4012 -0
- package/dist/umd/red-black-tree-typed.js.map +1 -0
- package/dist/umd/red-black-tree-typed.min.js +13 -0
- package/dist/umd/red-black-tree-typed.min.js.map +1 -0
- package/package.json +43 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +106 -15
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +116 -12
- package/src/data-structures/binary-tree/avl-tree.ts +107 -16
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +103 -12
- package/src/data-structures/binary-tree/red-black-tree.ts +110 -20
- package/src/data-structures/binary-tree/tree-counter.ts +105 -14
- package/src/data-structures/binary-tree/tree-multi-map.ts +123 -12
- package/src/data-structures/graph/abstract-graph.ts +5 -5
- package/src/data-structures/graph/directed-graph.ts +5 -5
- package/src/data-structures/graph/undirected-graph.ts +5 -5
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +71 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -16
- package/dist/index.js +0 -32
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -390,7 +390,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
390
390
|
* @param [value] - The value for the new node (used if not in Map mode).
|
|
391
391
|
* @returns The newly created node.
|
|
392
392
|
*/
|
|
393
|
-
|
|
393
|
+
createNode(key: K, value?: V): BinaryTreeNode<K, V> {
|
|
394
394
|
return new BinaryTreeNode<K, V>(key, this._isMapMode ? undefined : value);
|
|
395
395
|
}
|
|
396
396
|
|
|
@@ -2019,10 +2019,10 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
2019
2019
|
if (key === undefined) return [undefined, undefined];
|
|
2020
2020
|
else if (key === null) return [null, undefined];
|
|
2021
2021
|
const finalValue = value ?? entryValue;
|
|
2022
|
-
return [this.
|
|
2022
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
2023
2023
|
}
|
|
2024
2024
|
|
|
2025
|
-
return [this.
|
|
2025
|
+
return [this.createNode(keyNodeOrEntry, value), value];
|
|
2026
2026
|
}
|
|
2027
2027
|
|
|
2028
2028
|
/**
|
|
@@ -2147,7 +2147,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
2147
2147
|
|
|
2148
2148
|
if (srcNode && destNode) {
|
|
2149
2149
|
const { key, value } = destNode;
|
|
2150
|
-
const tempNode = this.
|
|
2150
|
+
const tempNode = this.createNode(key, value); // Use a temp node to hold dest properties
|
|
2151
2151
|
|
|
2152
2152
|
if (tempNode) {
|
|
2153
2153
|
// Copy src to dest
|
|
@@ -16,12 +16,14 @@ import type {
|
|
|
16
16
|
CP,
|
|
17
17
|
DFSOrderPattern,
|
|
18
18
|
EntryCallback,
|
|
19
|
+
FamilyPosition,
|
|
19
20
|
IterationType,
|
|
20
21
|
NodeCallback,
|
|
21
22
|
NodePredicate,
|
|
22
|
-
OptNode
|
|
23
|
+
OptNode,
|
|
24
|
+
RBTNColor
|
|
23
25
|
} from '../../types';
|
|
24
|
-
import { BinaryTree
|
|
26
|
+
import { BinaryTree } from './binary-tree';
|
|
25
27
|
import { IBinaryTree } from '../../interfaces';
|
|
26
28
|
import { Queue } from '../queue';
|
|
27
29
|
import { isComparable } from '../../utils';
|
|
@@ -33,8 +35,10 @@ import { Range } from '../../common';
|
|
|
33
35
|
* @template K - The type of the key.
|
|
34
36
|
* @template V - The type of the value.
|
|
35
37
|
*/
|
|
36
|
-
export class BSTNode<K = any, V = any>
|
|
37
|
-
|
|
38
|
+
export class BSTNode<K = any, V = any> {
|
|
39
|
+
key: K;
|
|
40
|
+
value?: V;
|
|
41
|
+
parent?: BSTNode<K, V> = undefined;
|
|
38
42
|
|
|
39
43
|
/**
|
|
40
44
|
* Creates an instance of BSTNode.
|
|
@@ -44,10 +48,11 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
44
48
|
* @param [value] - The value associated with the key.
|
|
45
49
|
*/
|
|
46
50
|
constructor(key: K, value?: V) {
|
|
47
|
-
|
|
51
|
+
this.key = key;
|
|
52
|
+
this.value = value;
|
|
48
53
|
}
|
|
49
54
|
|
|
50
|
-
|
|
55
|
+
_left?: BSTNode<K, V> | null | undefined = undefined;
|
|
51
56
|
|
|
52
57
|
/**
|
|
53
58
|
* Gets the left child of the node.
|
|
@@ -55,7 +60,7 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
55
60
|
*
|
|
56
61
|
* @returns The left child.
|
|
57
62
|
*/
|
|
58
|
-
|
|
63
|
+
get left(): BSTNode<K, V> | null | undefined {
|
|
59
64
|
return this._left;
|
|
60
65
|
}
|
|
61
66
|
|
|
@@ -65,12 +70,12 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
65
70
|
*
|
|
66
71
|
* @param v - The node to set as the left child.
|
|
67
72
|
*/
|
|
68
|
-
|
|
73
|
+
set left(v: BSTNode<K, V> | null | undefined) {
|
|
69
74
|
if (v) v.parent = this;
|
|
70
75
|
this._left = v;
|
|
71
76
|
}
|
|
72
77
|
|
|
73
|
-
|
|
78
|
+
_right?: BSTNode<K, V> | null | undefined = undefined;
|
|
74
79
|
|
|
75
80
|
/**
|
|
76
81
|
* Gets the right child of the node.
|
|
@@ -78,7 +83,7 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
78
83
|
*
|
|
79
84
|
* @returns The right child.
|
|
80
85
|
*/
|
|
81
|
-
|
|
86
|
+
get right(): BSTNode<K, V> | null | undefined {
|
|
82
87
|
return this._right;
|
|
83
88
|
}
|
|
84
89
|
|
|
@@ -88,10 +93,96 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
88
93
|
*
|
|
89
94
|
* @param v - The node to set as the right child.
|
|
90
95
|
*/
|
|
91
|
-
|
|
96
|
+
set right(v: BSTNode<K, V> | null | undefined) {
|
|
92
97
|
if (v) v.parent = this;
|
|
93
98
|
this._right = v;
|
|
94
99
|
}
|
|
100
|
+
|
|
101
|
+
_height: number = 0;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
105
|
+
* @remarks Time O(1), Space O(1)
|
|
106
|
+
*
|
|
107
|
+
* @returns The height.
|
|
108
|
+
*/
|
|
109
|
+
get height(): number {
|
|
110
|
+
return this._height;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Sets the height of the node.
|
|
115
|
+
* @remarks Time O(1), Space O(1)
|
|
116
|
+
*
|
|
117
|
+
* @param value - The new height.
|
|
118
|
+
*/
|
|
119
|
+
set height(value: number) {
|
|
120
|
+
this._height = value;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
_color: RBTNColor = 'BLACK';
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
127
|
+
* @remarks Time O(1), Space O(1)
|
|
128
|
+
*
|
|
129
|
+
* @returns The node's color.
|
|
130
|
+
*/
|
|
131
|
+
get color(): RBTNColor {
|
|
132
|
+
return this._color;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Sets the color of the node.
|
|
137
|
+
* @remarks Time O(1), Space O(1)
|
|
138
|
+
*
|
|
139
|
+
* @param value - The new color.
|
|
140
|
+
*/
|
|
141
|
+
set color(value: RBTNColor) {
|
|
142
|
+
this._color = value;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
_count: number = 1;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
149
|
+
* @remarks Time O(1), Space O(1)
|
|
150
|
+
*
|
|
151
|
+
* @returns The subtree node count.
|
|
152
|
+
*/
|
|
153
|
+
get count(): number {
|
|
154
|
+
return this._count;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Sets the count of nodes in the subtree.
|
|
159
|
+
* @remarks Time O(1), Space O(1)
|
|
160
|
+
*
|
|
161
|
+
* @param value - The new count.
|
|
162
|
+
*/
|
|
163
|
+
set count(value: number) {
|
|
164
|
+
this._count = value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Gets the position of the node relative to its parent.
|
|
169
|
+
* @remarks Time O(1), Space O(1)
|
|
170
|
+
*
|
|
171
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
172
|
+
*/
|
|
173
|
+
get familyPosition(): FamilyPosition {
|
|
174
|
+
if (!this.parent) {
|
|
175
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (this.parent.left === this) {
|
|
179
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
180
|
+
} else if (this.parent.right === this) {
|
|
181
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return 'MAL_NODE';
|
|
185
|
+
}
|
|
95
186
|
}
|
|
96
187
|
|
|
97
188
|
/**
|
|
@@ -268,7 +359,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
268
359
|
* @param [value] - The value for the new node (used if not in Map mode).
|
|
269
360
|
* @returns The newly created BSTNode.
|
|
270
361
|
*/
|
|
271
|
-
override
|
|
362
|
+
override createNode(key: K, value?: V): BSTNode<K, V> {
|
|
272
363
|
return new BSTNode<K, V>(key, this._isMapMode ? undefined : value);
|
|
273
364
|
}
|
|
274
365
|
|
|
@@ -11,15 +11,18 @@ import type {
|
|
|
11
11
|
BinaryTreeOptions,
|
|
12
12
|
CRUD,
|
|
13
13
|
EntryCallback,
|
|
14
|
+
FamilyPosition,
|
|
14
15
|
OptNode,
|
|
15
16
|
RBTNColor,
|
|
16
17
|
RedBlackTreeOptions
|
|
17
18
|
} from '../../types';
|
|
18
|
-
import { BST
|
|
19
|
+
import { BST } from './bst';
|
|
19
20
|
import { IBinaryTree } from '../../interfaces';
|
|
20
21
|
|
|
21
|
-
export class RedBlackTreeNode<K = any, V = any>
|
|
22
|
-
|
|
22
|
+
export class RedBlackTreeNode<K = any, V = any> {
|
|
23
|
+
key: K;
|
|
24
|
+
value?: V;
|
|
25
|
+
parent?: RedBlackTreeNode<K, V> = undefined;
|
|
23
26
|
|
|
24
27
|
/**
|
|
25
28
|
* Create a Red-Black Tree and optionally bulk-insert items.
|
|
@@ -31,11 +34,12 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
31
34
|
*/
|
|
32
35
|
|
|
33
36
|
constructor(key: K, value?: V, color: RBTNColor = 'BLACK') {
|
|
34
|
-
|
|
35
|
-
this.
|
|
37
|
+
this.key = key;
|
|
38
|
+
this.value = value;
|
|
39
|
+
this.color = color;
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
|
|
42
|
+
_left?: RedBlackTreeNode<K, V> | null | undefined = undefined;
|
|
39
43
|
|
|
40
44
|
/**
|
|
41
45
|
* Get the left child pointer.
|
|
@@ -43,7 +47,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
43
47
|
* @returns Left child node, or null/undefined.
|
|
44
48
|
*/
|
|
45
49
|
|
|
46
|
-
|
|
50
|
+
get left(): RedBlackTreeNode<K, V> | null | undefined {
|
|
47
51
|
return this._left;
|
|
48
52
|
}
|
|
49
53
|
|
|
@@ -54,14 +58,14 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
54
58
|
* @returns void
|
|
55
59
|
*/
|
|
56
60
|
|
|
57
|
-
|
|
61
|
+
set left(v: RedBlackTreeNode<K, V> | null | undefined) {
|
|
58
62
|
if (v) {
|
|
59
63
|
v.parent = this;
|
|
60
64
|
}
|
|
61
65
|
this._left = v;
|
|
62
66
|
}
|
|
63
67
|
|
|
64
|
-
|
|
68
|
+
_right?: RedBlackTreeNode<K, V> | null | undefined = undefined;
|
|
65
69
|
|
|
66
70
|
/**
|
|
67
71
|
* Get the right child pointer.
|
|
@@ -69,7 +73,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
69
73
|
* @returns Right child node, or null/undefined.
|
|
70
74
|
*/
|
|
71
75
|
|
|
72
|
-
|
|
76
|
+
get right(): RedBlackTreeNode<K, V> | null | undefined {
|
|
73
77
|
return this._right;
|
|
74
78
|
}
|
|
75
79
|
|
|
@@ -80,21 +84,107 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
80
84
|
* @returns void
|
|
81
85
|
*/
|
|
82
86
|
|
|
83
|
-
|
|
87
|
+
set right(v: RedBlackTreeNode<K, V> | null | undefined) {
|
|
84
88
|
if (v) {
|
|
85
89
|
v.parent = this;
|
|
86
90
|
}
|
|
87
91
|
this._right = v;
|
|
88
92
|
}
|
|
93
|
+
|
|
94
|
+
_height: number = 0;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
98
|
+
* @remarks Time O(1), Space O(1)
|
|
99
|
+
*
|
|
100
|
+
* @returns The height.
|
|
101
|
+
*/
|
|
102
|
+
get height(): number {
|
|
103
|
+
return this._height;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Sets the height of the node.
|
|
108
|
+
* @remarks Time O(1), Space O(1)
|
|
109
|
+
*
|
|
110
|
+
* @param value - The new height.
|
|
111
|
+
*/
|
|
112
|
+
set height(value: number) {
|
|
113
|
+
this._height = value;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
_color: RBTNColor = 'BLACK';
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
120
|
+
* @remarks Time O(1), Space O(1)
|
|
121
|
+
*
|
|
122
|
+
* @returns The node's color.
|
|
123
|
+
*/
|
|
124
|
+
get color(): RBTNColor {
|
|
125
|
+
return this._color;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Sets the color of the node.
|
|
130
|
+
* @remarks Time O(1), Space O(1)
|
|
131
|
+
*
|
|
132
|
+
* @param value - The new color.
|
|
133
|
+
*/
|
|
134
|
+
set color(value: RBTNColor) {
|
|
135
|
+
this._color = value;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
_count: number = 1;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
142
|
+
* @remarks Time O(1), Space O(1)
|
|
143
|
+
*
|
|
144
|
+
* @returns The subtree node count.
|
|
145
|
+
*/
|
|
146
|
+
get count(): number {
|
|
147
|
+
return this._count;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Sets the count of nodes in the subtree.
|
|
152
|
+
* @remarks Time O(1), Space O(1)
|
|
153
|
+
*
|
|
154
|
+
* @param value - The new count.
|
|
155
|
+
*/
|
|
156
|
+
set count(value: number) {
|
|
157
|
+
this._count = value;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Gets the position of the node relative to its parent.
|
|
162
|
+
* @remarks Time O(1), Space O(1)
|
|
163
|
+
*
|
|
164
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
165
|
+
*/
|
|
166
|
+
get familyPosition(): FamilyPosition {
|
|
167
|
+
if (!this.parent) {
|
|
168
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (this.parent.left === this) {
|
|
172
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
173
|
+
} else if (this.parent.right === this) {
|
|
174
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return 'MAL_NODE';
|
|
178
|
+
}
|
|
89
179
|
}
|
|
90
180
|
|
|
91
181
|
/**
|
|
92
|
-
*
|
|
182
|
+
* Represents a Red-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
|
|
93
183
|
* @remarks Time O(1), Space O(1)
|
|
94
184
|
* @template K
|
|
95
185
|
* @template V
|
|
96
186
|
* @template R
|
|
97
|
-
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
|
|
187
|
+
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high, but the query efficiency is slightly lower.
|
|
98
188
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
99
189
|
* @example
|
|
100
190
|
* // using Red-Black Tree as a price-based index for stock data
|
|
@@ -176,7 +266,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
176
266
|
* @param color - See parameter type for details.
|
|
177
267
|
* @returns A new RedBlackTreeNode instance.
|
|
178
268
|
*/
|
|
179
|
-
override
|
|
269
|
+
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): RedBlackTreeNode<K, V> {
|
|
180
270
|
return new RedBlackTreeNode<K, V>(key, this._isMapMode ? undefined : value, color);
|
|
181
271
|
}
|
|
182
272
|
|
|
@@ -380,10 +470,10 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
380
470
|
*/
|
|
381
471
|
|
|
382
472
|
protected _insert(node: RedBlackTreeNode<K, V>): CRUD {
|
|
383
|
-
let current = this.root;
|
|
473
|
+
let current = this.root ?? this.NIL;
|
|
384
474
|
let parent: RedBlackTreeNode<K, V> | undefined = undefined;
|
|
385
475
|
|
|
386
|
-
while (this.
|
|
476
|
+
while (current !== this.NIL) {
|
|
387
477
|
parent = current;
|
|
388
478
|
const compared = this._compare(node.key, current.key);
|
|
389
479
|
if (compared < 0) {
|
|
@@ -459,7 +549,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
459
549
|
this._leftRotate(z);
|
|
460
550
|
}
|
|
461
551
|
|
|
462
|
-
if (z &&
|
|
552
|
+
if (z && z.parent && z.parent.parent) {
|
|
463
553
|
z.parent.color = 'BLACK';
|
|
464
554
|
z.parent.parent.color = 'RED';
|
|
465
555
|
this._rightRotate(z.parent.parent);
|
|
@@ -478,7 +568,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
478
568
|
this._rightRotate(z);
|
|
479
569
|
}
|
|
480
570
|
|
|
481
|
-
if (z &&
|
|
571
|
+
if (z && z.parent && z.parent.parent) {
|
|
482
572
|
z.parent.color = 'BLACK';
|
|
483
573
|
z.parent.parent.color = 'RED';
|
|
484
574
|
this._leftRotate(z.parent.parent);
|
|
@@ -575,7 +665,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
575
665
|
const y = x.right;
|
|
576
666
|
x.right = y.left;
|
|
577
667
|
|
|
578
|
-
if (
|
|
668
|
+
if (y.left && y.left !== this.NIL) {
|
|
579
669
|
y.left.parent = x;
|
|
580
670
|
}
|
|
581
671
|
|
|
@@ -608,7 +698,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
608
698
|
const x = y.left;
|
|
609
699
|
y.left = x.right;
|
|
610
700
|
|
|
611
|
-
if (
|
|
701
|
+
if (x.right && x.right !== this.NIL) {
|
|
612
702
|
x.right.parent = y;
|
|
613
703
|
}
|
|
614
704
|
|
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
BinaryTreeOptions,
|
|
12
12
|
BSTNOptKeyOrNode,
|
|
13
13
|
EntryCallback,
|
|
14
|
+
FamilyPosition,
|
|
14
15
|
IterationType,
|
|
15
16
|
OptNode,
|
|
16
17
|
RBTNColor,
|
|
@@ -19,7 +20,7 @@ import type {
|
|
|
19
20
|
import { BSTOptions } from '../../types';
|
|
20
21
|
import { BSTNode } from './bst';
|
|
21
22
|
import { IBinaryTree } from '../../interfaces';
|
|
22
|
-
import { RedBlackTree
|
|
23
|
+
import { RedBlackTree } from './red-black-tree';
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* RB-tree node with an extra 'count' field; keeps parent/child links.
|
|
@@ -27,8 +28,10 @@ import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
|
27
28
|
* @template K
|
|
28
29
|
* @template V
|
|
29
30
|
*/
|
|
30
|
-
export class TreeCounterNode<K = any, V = any>
|
|
31
|
-
|
|
31
|
+
export class TreeCounterNode<K = any, V = any> {
|
|
32
|
+
key: K;
|
|
33
|
+
value?: V;
|
|
34
|
+
parent?: TreeCounterNode<K, V> = undefined;
|
|
32
35
|
|
|
33
36
|
/**
|
|
34
37
|
* Create a tree counter node.
|
|
@@ -40,18 +43,20 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
|
40
43
|
* @returns New TreeCounterNode instance.
|
|
41
44
|
*/
|
|
42
45
|
constructor(key: K, value?: V, count = 1, color: RBTNColor = 'BLACK') {
|
|
43
|
-
|
|
46
|
+
this.key = key;
|
|
47
|
+
this.value = value;
|
|
48
|
+
this.color = color;
|
|
44
49
|
this.count = count;
|
|
45
50
|
}
|
|
46
51
|
|
|
47
|
-
|
|
52
|
+
_left?: TreeCounterNode<K, V> | null | undefined = undefined;
|
|
48
53
|
|
|
49
54
|
/**
|
|
50
55
|
* Get the left child pointer.
|
|
51
56
|
* @remarks Time O(1), Space O(1)
|
|
52
57
|
* @returns Left child node, or null/undefined.
|
|
53
58
|
*/
|
|
54
|
-
|
|
59
|
+
get left(): TreeCounterNode<K, V> | null | undefined {
|
|
55
60
|
return this._left;
|
|
56
61
|
}
|
|
57
62
|
|
|
@@ -61,21 +66,21 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
|
61
66
|
* @param v - New left child node, or null/undefined.
|
|
62
67
|
* @returns void
|
|
63
68
|
*/
|
|
64
|
-
|
|
69
|
+
set left(v: TreeCounterNode<K, V> | null | undefined) {
|
|
65
70
|
if (v) {
|
|
66
71
|
v.parent = this;
|
|
67
72
|
}
|
|
68
73
|
this._left = v;
|
|
69
74
|
}
|
|
70
75
|
|
|
71
|
-
|
|
76
|
+
_right?: TreeCounterNode<K, V> | null | undefined = undefined;
|
|
72
77
|
|
|
73
78
|
/**
|
|
74
79
|
* Get the right child pointer.
|
|
75
80
|
* @remarks Time O(1), Space O(1)
|
|
76
81
|
* @returns Right child node, or null/undefined.
|
|
77
82
|
*/
|
|
78
|
-
|
|
83
|
+
get right(): TreeCounterNode<K, V> | null | undefined {
|
|
79
84
|
return this._right;
|
|
80
85
|
}
|
|
81
86
|
|
|
@@ -85,12 +90,98 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
|
85
90
|
* @param v - New right child node, or null/undefined.
|
|
86
91
|
* @returns void
|
|
87
92
|
*/
|
|
88
|
-
|
|
93
|
+
set right(v: TreeCounterNode<K, V> | null | undefined) {
|
|
89
94
|
if (v) {
|
|
90
95
|
v.parent = this;
|
|
91
96
|
}
|
|
92
97
|
this._right = v;
|
|
93
98
|
}
|
|
99
|
+
|
|
100
|
+
_height: number = 0;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
104
|
+
* @remarks Time O(1), Space O(1)
|
|
105
|
+
*
|
|
106
|
+
* @returns The height.
|
|
107
|
+
*/
|
|
108
|
+
get height(): number {
|
|
109
|
+
return this._height;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Sets the height of the node.
|
|
114
|
+
* @remarks Time O(1), Space O(1)
|
|
115
|
+
*
|
|
116
|
+
* @param value - The new height.
|
|
117
|
+
*/
|
|
118
|
+
set height(value: number) {
|
|
119
|
+
this._height = value;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
_color: RBTNColor = 'BLACK';
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
126
|
+
* @remarks Time O(1), Space O(1)
|
|
127
|
+
*
|
|
128
|
+
* @returns The node's color.
|
|
129
|
+
*/
|
|
130
|
+
get color(): RBTNColor {
|
|
131
|
+
return this._color;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Sets the color of the node.
|
|
136
|
+
* @remarks Time O(1), Space O(1)
|
|
137
|
+
*
|
|
138
|
+
* @param value - The new color.
|
|
139
|
+
*/
|
|
140
|
+
set color(value: RBTNColor) {
|
|
141
|
+
this._color = value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
_count: number = 1;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
148
|
+
* @remarks Time O(1), Space O(1)
|
|
149
|
+
*
|
|
150
|
+
* @returns The subtree node count.
|
|
151
|
+
*/
|
|
152
|
+
get count(): number {
|
|
153
|
+
return this._count;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Sets the count of nodes in the subtree.
|
|
158
|
+
* @remarks Time O(1), Space O(1)
|
|
159
|
+
*
|
|
160
|
+
* @param value - The new count.
|
|
161
|
+
*/
|
|
162
|
+
set count(value: number) {
|
|
163
|
+
this._count = value;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Gets the position of the node relative to its parent.
|
|
168
|
+
* @remarks Time O(1), Space O(1)
|
|
169
|
+
*
|
|
170
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
171
|
+
*/
|
|
172
|
+
get familyPosition(): FamilyPosition {
|
|
173
|
+
if (!this.parent) {
|
|
174
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (this.parent.left === this) {
|
|
178
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
179
|
+
} else if (this.parent.right === this) {
|
|
180
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return 'MAL_NODE';
|
|
184
|
+
}
|
|
94
185
|
}
|
|
95
186
|
|
|
96
187
|
/**
|
|
@@ -142,7 +233,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
|
|
|
142
233
|
return sum;
|
|
143
234
|
}
|
|
144
235
|
|
|
145
|
-
override
|
|
236
|
+
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): TreeCounterNode<K, V> {
|
|
146
237
|
return new TreeCounterNode(key, this._isMapMode ? undefined : value, count, color) as TreeCounterNode<K, V>;
|
|
147
238
|
}
|
|
148
239
|
|
|
@@ -427,10 +518,10 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
|
|
|
427
518
|
const [key, entryValue] = keyNodeOrEntry;
|
|
428
519
|
if (key === undefined || key === null) return [undefined, undefined];
|
|
429
520
|
const finalValue = value ?? entryValue;
|
|
430
|
-
return [this.
|
|
521
|
+
return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
431
522
|
}
|
|
432
523
|
|
|
433
|
-
return [this.
|
|
524
|
+
return [this.createNode(keyNodeOrEntry, value, 'BLACK', count), value];
|
|
434
525
|
}
|
|
435
526
|
|
|
436
527
|
/**
|
|
@@ -448,7 +539,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
|
|
|
448
539
|
destNode = this.ensureNode(destNode);
|
|
449
540
|
if (srcNode && destNode) {
|
|
450
541
|
const { key, value, count, color } = destNode;
|
|
451
|
-
const tempNode = this.
|
|
542
|
+
const tempNode = this.createNode(key, value, color, count);
|
|
452
543
|
if (tempNode) {
|
|
453
544
|
tempNode.color = color;
|
|
454
545
|
|