tree-multimap-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 +4298 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs-legacy/index.cjs +4300 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +4287 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm-legacy/index.mjs +4289 -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 +17 -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/tree-multimap-typed.js +4289 -0
- package/dist/umd/tree-multimap-typed.js.map +1 -0
- package/dist/umd/tree-multimap-typed.min.js +13 -0
- package/dist/umd/tree-multimap-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 -18
- package/dist/index.js +0 -34
- 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
|
@@ -1,3 +1,262 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { BinaryTreeDeleteResult, BinaryTreeOptions, CRUD, EntryCallback, FamilyPosition, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
9
|
+
import { BST } from './bst';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
export declare class RedBlackTreeNode<K = any, V = any> {
|
|
12
|
+
key: K;
|
|
13
|
+
value?: V;
|
|
14
|
+
parent?: RedBlackTreeNode<K, V>;
|
|
15
|
+
/**
|
|
16
|
+
* Create a Red-Black Tree and optionally bulk-insert items.
|
|
17
|
+
* @remarks Time O(n log n), Space O(n)
|
|
18
|
+
* @param key - See parameter type for details.
|
|
19
|
+
* @param [value]- See parameter type for details.
|
|
20
|
+
* @param color - See parameter type for details.
|
|
21
|
+
* @returns New RedBlackTree instance.
|
|
22
|
+
*/
|
|
23
|
+
constructor(key: K, value?: V, color?: RBTNColor);
|
|
24
|
+
_left?: RedBlackTreeNode<K, V> | null | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Get the left child pointer.
|
|
27
|
+
* @remarks Time O(1), Space O(1)
|
|
28
|
+
* @returns Left child node, or null/undefined.
|
|
29
|
+
*/
|
|
30
|
+
get left(): RedBlackTreeNode<K, V> | null | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Set the left child and update its parent pointer.
|
|
33
|
+
* @remarks Time O(1), Space O(1)
|
|
34
|
+
* @param v - New left node, or null/undefined.
|
|
35
|
+
* @returns void
|
|
36
|
+
*/
|
|
37
|
+
set left(v: RedBlackTreeNode<K, V> | null | undefined);
|
|
38
|
+
_right?: RedBlackTreeNode<K, V> | null | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Get the right child pointer.
|
|
41
|
+
* @remarks Time O(1), Space O(1)
|
|
42
|
+
* @returns Right child node, or null/undefined.
|
|
43
|
+
*/
|
|
44
|
+
get right(): RedBlackTreeNode<K, V> | null | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Set the right child and update its parent pointer.
|
|
47
|
+
* @remarks Time O(1), Space O(1)
|
|
48
|
+
* @param v - New right node, or null/undefined.
|
|
49
|
+
* @returns void
|
|
50
|
+
*/
|
|
51
|
+
set right(v: RedBlackTreeNode<K, V> | null | undefined);
|
|
52
|
+
_height: number;
|
|
53
|
+
/**
|
|
54
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
55
|
+
* @remarks Time O(1), Space O(1)
|
|
56
|
+
*
|
|
57
|
+
* @returns The height.
|
|
58
|
+
*/
|
|
59
|
+
get height(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Sets the height of the node.
|
|
62
|
+
* @remarks Time O(1), Space O(1)
|
|
63
|
+
*
|
|
64
|
+
* @param value - The new height.
|
|
65
|
+
*/
|
|
66
|
+
set height(value: number);
|
|
67
|
+
_color: RBTNColor;
|
|
68
|
+
/**
|
|
69
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
70
|
+
* @remarks Time O(1), Space O(1)
|
|
71
|
+
*
|
|
72
|
+
* @returns The node's color.
|
|
73
|
+
*/
|
|
74
|
+
get color(): RBTNColor;
|
|
75
|
+
/**
|
|
76
|
+
* Sets the color of the node.
|
|
77
|
+
* @remarks Time O(1), Space O(1)
|
|
78
|
+
*
|
|
79
|
+
* @param value - The new color.
|
|
80
|
+
*/
|
|
81
|
+
set color(value: RBTNColor);
|
|
82
|
+
_count: number;
|
|
83
|
+
/**
|
|
84
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
85
|
+
* @remarks Time O(1), Space O(1)
|
|
86
|
+
*
|
|
87
|
+
* @returns The subtree node count.
|
|
88
|
+
*/
|
|
89
|
+
get count(): number;
|
|
90
|
+
/**
|
|
91
|
+
* Sets the count of nodes in the subtree.
|
|
92
|
+
* @remarks Time O(1), Space O(1)
|
|
93
|
+
*
|
|
94
|
+
* @param value - The new count.
|
|
95
|
+
*/
|
|
96
|
+
set count(value: number);
|
|
97
|
+
/**
|
|
98
|
+
* Gets the position of the node relative to its parent.
|
|
99
|
+
* @remarks Time O(1), Space O(1)
|
|
100
|
+
*
|
|
101
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
102
|
+
*/
|
|
103
|
+
get familyPosition(): FamilyPosition;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Represents a Red-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
|
|
107
|
+
* @remarks Time O(1), Space O(1)
|
|
108
|
+
* @template K
|
|
109
|
+
* @template V
|
|
110
|
+
* @template R
|
|
111
|
+
* 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.
|
|
112
|
+
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
113
|
+
* @example
|
|
114
|
+
* // using Red-Black Tree as a price-based index for stock data
|
|
115
|
+
* // Define the structure of individual stock records
|
|
116
|
+
* interface StockRecord {
|
|
117
|
+
* price: number; // Stock price (key for indexing)
|
|
118
|
+
* symbol: string; // Stock ticker symbol
|
|
119
|
+
* volume: number; // Trade volume
|
|
120
|
+
* }
|
|
121
|
+
*
|
|
122
|
+
* // Simulate stock market data as it might come from an external feed
|
|
123
|
+
* const marketStockData: StockRecord[] = [
|
|
124
|
+
* { price: 142.5, symbol: 'AAPL', volume: 1000000 },
|
|
125
|
+
* { price: 335.2, symbol: 'MSFT', volume: 800000 },
|
|
126
|
+
* { price: 3285.04, symbol: 'AMZN', volume: 500000 },
|
|
127
|
+
* { price: 267.98, symbol: 'META', volume: 750000 },
|
|
128
|
+
* { price: 234.57, symbol: 'GOOGL', volume: 900000 }
|
|
129
|
+
* ];
|
|
130
|
+
*
|
|
131
|
+
* // Extend the stock record type to include metadata for database usage
|
|
132
|
+
* type StockTableRecord = StockRecord & { lastUpdated: Date };
|
|
133
|
+
*
|
|
134
|
+
* // Create a Red-Black Tree to index stock records by price
|
|
135
|
+
* // Simulates a database index with stock price as the key for quick lookups
|
|
136
|
+
* const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
|
|
137
|
+
* toEntryFn: stockRecord => [
|
|
138
|
+
* stockRecord.price, // Use stock price as the key
|
|
139
|
+
* {
|
|
140
|
+
* ...stockRecord,
|
|
141
|
+
* lastUpdated: new Date() // Add a timestamp for when the record was indexed
|
|
142
|
+
* }
|
|
143
|
+
* ]
|
|
144
|
+
* });
|
|
145
|
+
*
|
|
146
|
+
* // Query the stock with the highest price
|
|
147
|
+
* const highestPricedStock = priceIndex.getRightMost();
|
|
148
|
+
* console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
|
|
149
|
+
*
|
|
150
|
+
* // Query stocks within a specific price range (200 to 400)
|
|
151
|
+
* const stocksInRange = priceIndex.rangeSearch(
|
|
152
|
+
* [200, 400], // Price range
|
|
153
|
+
* node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
|
|
154
|
+
* );
|
|
155
|
+
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
|
|
156
|
+
*/
|
|
157
|
+
export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
158
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>);
|
|
159
|
+
protected _root: RedBlackTreeNode<K, V> | undefined;
|
|
160
|
+
/**
|
|
161
|
+
* Get the current root node.
|
|
162
|
+
* @remarks Time O(1), Space O(1)
|
|
163
|
+
* @returns Root node, or undefined.
|
|
164
|
+
*/
|
|
165
|
+
get root(): RedBlackTreeNode<K, V> | undefined;
|
|
166
|
+
/**
|
|
167
|
+
* Create a red-black node for the given key/value (value ignored in map mode).
|
|
168
|
+
* @remarks Time O(1), Space O(1)
|
|
169
|
+
* @param key - See parameter type for details.
|
|
170
|
+
* @param [value] - See parameter type for details.
|
|
171
|
+
* @param color - See parameter type for details.
|
|
172
|
+
* @returns A new RedBlackTreeNode instance.
|
|
173
|
+
*/
|
|
174
|
+
createNode(key: K, value?: V, color?: RBTNColor): RedBlackTreeNode<K, V>;
|
|
175
|
+
/**
|
|
176
|
+
* Type guard: check whether the input is a RedBlackTreeNode.
|
|
177
|
+
* @remarks Time O(1), Space O(1)
|
|
178
|
+
* @param keyNodeOrEntry - See parameter type for details.
|
|
179
|
+
* @returns True if the value is a RedBlackTreeNode.
|
|
180
|
+
*/
|
|
181
|
+
isNode(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is RedBlackTreeNode<K, V>;
|
|
182
|
+
/**
|
|
183
|
+
* Remove all nodes and clear the key→value store (if in map mode).
|
|
184
|
+
* @remarks Time O(n), Space O(1)
|
|
185
|
+
* @returns void
|
|
186
|
+
*/
|
|
187
|
+
clear(): void;
|
|
188
|
+
/**
|
|
189
|
+
* Insert or replace an entry using BST order and red-black fix-up.
|
|
190
|
+
* @remarks Time O(log n), Space O(1)
|
|
191
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
|
|
192
|
+
* @param [value]- See parameter type for details.
|
|
193
|
+
* @returns True if inserted or updated; false if ignored.
|
|
194
|
+
*/
|
|
195
|
+
add(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Delete a node by key/node/entry and rebalance as needed.
|
|
198
|
+
* @remarks Time O(log n), Space O(1)
|
|
199
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node to delete.
|
|
200
|
+
* @returns Array with deletion metadata (removed node, rebalancing hint if any).
|
|
201
|
+
*/
|
|
202
|
+
delete(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[];
|
|
203
|
+
/**
|
|
204
|
+
* Transform entries into a like-kind red-black tree with possibly different key/value types.
|
|
205
|
+
* @remarks Time O(n), Space O(n)
|
|
206
|
+
* @template MK
|
|
207
|
+
* @template MV
|
|
208
|
+
* @template MR
|
|
209
|
+
* @param callback - Mapping function from (key, value, index, tree) to a new [key, value].
|
|
210
|
+
* @param [options] - See parameter type for details.
|
|
211
|
+
* @param [thisArg] - See parameter type for details.
|
|
212
|
+
* @returns A new RedBlackTree with mapped entries.
|
|
213
|
+
*/
|
|
214
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
215
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
|
|
216
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
|
|
217
|
+
protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void;
|
|
218
|
+
protected _replaceNode(oldNode: RedBlackTreeNode<K, V>, newNode: RedBlackTreeNode<K, V>): RedBlackTreeNode<K, V>;
|
|
219
|
+
/**
|
|
220
|
+
* (Protected) Standard BST insert followed by red-black fix-up.
|
|
221
|
+
* @remarks Time O(log n), Space O(1)
|
|
222
|
+
* @param node - Node to insert.
|
|
223
|
+
* @returns Status string: 'CREATED' or 'UPDATED'.
|
|
224
|
+
*/
|
|
225
|
+
protected _insert(node: RedBlackTreeNode<K, V>): CRUD;
|
|
226
|
+
/**
|
|
227
|
+
* (Protected) Transplant a subtree in place of another during deletion.
|
|
228
|
+
* @remarks Time O(1), Space O(1)
|
|
229
|
+
* @param u - Node to replace.
|
|
230
|
+
* @param v - Replacement subtree root (may be undefined).
|
|
231
|
+
* @returns void
|
|
232
|
+
*/
|
|
233
|
+
protected _transplant(u: RedBlackTreeNode<K, V>, v: RedBlackTreeNode<K, V> | undefined): void;
|
|
234
|
+
/**
|
|
235
|
+
* (Protected) Restore red-black properties after insertion (recolor/rotate).
|
|
236
|
+
* @remarks Time O(log n), Space O(1)
|
|
237
|
+
* @param z - Recently inserted node.
|
|
238
|
+
* @returns void
|
|
239
|
+
*/
|
|
240
|
+
protected _insertFixup(z: RedBlackTreeNode<K, V> | undefined): void;
|
|
241
|
+
/**
|
|
242
|
+
* (Protected) Restore red-black properties after deletion (recolor/rotate).
|
|
243
|
+
* @remarks Time O(log n), Space O(1)
|
|
244
|
+
* @param node - Child that replaced the deleted node (may be undefined).
|
|
245
|
+
* @returns void
|
|
246
|
+
*/
|
|
247
|
+
protected _deleteFixup(node: RedBlackTreeNode<K, V> | undefined): void;
|
|
248
|
+
/**
|
|
249
|
+
* (Protected) Perform a left rotation around x.
|
|
250
|
+
* @remarks Time O(1), Space O(1)
|
|
251
|
+
* @param x - Pivot node to rotate around.
|
|
252
|
+
* @returns void
|
|
253
|
+
*/
|
|
254
|
+
protected _leftRotate(x: RedBlackTreeNode<K, V> | undefined): void;
|
|
255
|
+
/**
|
|
256
|
+
* (Protected) Perform a right rotation around y.
|
|
257
|
+
* @remarks Time O(1), Space O(1)
|
|
258
|
+
* @param y - Pivot node to rotate around.
|
|
259
|
+
* @returns void
|
|
260
|
+
*/
|
|
261
|
+
protected _rightRotate(y: RedBlackTreeNode<K, V> | undefined): void;
|
|
262
|
+
}
|
|
@@ -1 +1,160 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { SegmentTreeNodeVal } from '../../types';
|
|
9
|
+
export declare class SegmentTreeNode {
|
|
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
|
+
*/
|
|
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);
|
|
94
|
+
}
|
|
95
|
+
export declare class SegmentTree {
|
|
96
|
+
/**
|
|
97
|
+
* The constructor initializes the values, start, end, and root properties of an object.
|
|
98
|
+
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
|
|
99
|
+
* @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
|
|
100
|
+
* be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
|
|
101
|
+
* the beginning of the array.
|
|
102
|
+
* @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
|
|
103
|
+
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
104
|
+
*/
|
|
105
|
+
constructor(values: number[], start?: number, end?: number);
|
|
106
|
+
protected _values: number[];
|
|
107
|
+
/**
|
|
108
|
+
* The function returns an array of numbers.
|
|
109
|
+
* @returns An array of numbers is being returned.
|
|
110
|
+
*/
|
|
111
|
+
get values(): number[];
|
|
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
|
+
*/
|
|
117
|
+
get start(): number;
|
|
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
|
+
*/
|
|
123
|
+
get end(): number;
|
|
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
|
+
*/
|
|
129
|
+
get root(): SegmentTreeNode | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|
|
132
|
+
* the sum of values to each segment.
|
|
133
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
|
|
134
|
+
* building the segment tree.
|
|
135
|
+
* @param {number} end - The "end" parameter represents the ending index of the segment or range for which we want to
|
|
136
|
+
* build a segment tree.
|
|
137
|
+
* @returns a SegmentTreeNode object.
|
|
138
|
+
*/
|
|
139
|
+
build(start: number, end: number): SegmentTreeNode;
|
|
140
|
+
/**
|
|
141
|
+
* The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
|
|
142
|
+
* @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
|
|
143
|
+
* updated.
|
|
144
|
+
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
145
|
+
* the `SegmentTreeNode` at the specified `index`.
|
|
146
|
+
* @param {SegmentTreeNodeVal} [value] - The `value` parameter is an optional value that can be assigned to the `value`
|
|
147
|
+
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
148
|
+
* cur.value = value;` and pass a value for `value` in the
|
|
149
|
+
* @returns The function does not return anything.
|
|
150
|
+
*/
|
|
151
|
+
updateNode(index: number, sum: number, value?: SegmentTreeNodeVal): void;
|
|
152
|
+
/**
|
|
153
|
+
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
154
|
+
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
155
|
+
* @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
|
|
156
|
+
* calculate the sum.
|
|
157
|
+
* @returns The function `querySumByRange` returns a number.
|
|
158
|
+
*/
|
|
159
|
+
querySumByRange(indexA: number, indexB: number): number;
|
|
160
|
+
}
|
|
@@ -1,2 +1,244 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
|
|
9
|
+
import { BSTOptions } from '../../types';
|
|
10
|
+
import { BSTNode } from './bst';
|
|
11
|
+
import { IBinaryTree } from '../../interfaces';
|
|
12
|
+
import { RedBlackTree } from './red-black-tree';
|
|
13
|
+
/**
|
|
14
|
+
* RB-tree node with an extra 'count' field; keeps parent/child links.
|
|
15
|
+
* @remarks Time O(1), Space O(1)
|
|
16
|
+
* @template K
|
|
17
|
+
* @template V
|
|
18
|
+
*/
|
|
19
|
+
export declare class TreeCounterNode<K = any, V = any> {
|
|
20
|
+
key: K;
|
|
21
|
+
value?: V;
|
|
22
|
+
parent?: TreeCounterNode<K, V>;
|
|
23
|
+
/**
|
|
24
|
+
* Create a tree counter node.
|
|
25
|
+
* @remarks Time O(1), Space O(1)
|
|
26
|
+
* @param key - Key of the node.
|
|
27
|
+
* @param [value] - Value associated with the key (ignored in map mode).
|
|
28
|
+
* @param [count] - Initial count for this node (default 1).
|
|
29
|
+
* @param color - Initial color ('RED' or 'BLACK').
|
|
30
|
+
* @returns New TreeCounterNode instance.
|
|
31
|
+
*/
|
|
32
|
+
constructor(key: K, value?: V, count?: number, color?: RBTNColor);
|
|
33
|
+
_left?: TreeCounterNode<K, V> | null | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Get the left child pointer.
|
|
36
|
+
* @remarks Time O(1), Space O(1)
|
|
37
|
+
* @returns Left child node, or null/undefined.
|
|
38
|
+
*/
|
|
39
|
+
get left(): TreeCounterNode<K, V> | null | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Set the left child and update its parent pointer.
|
|
42
|
+
* @remarks Time O(1), Space O(1)
|
|
43
|
+
* @param v - New left child node, or null/undefined.
|
|
44
|
+
* @returns void
|
|
45
|
+
*/
|
|
46
|
+
set left(v: TreeCounterNode<K, V> | null | undefined);
|
|
47
|
+
_right?: TreeCounterNode<K, V> | null | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Get the right child pointer.
|
|
50
|
+
* @remarks Time O(1), Space O(1)
|
|
51
|
+
* @returns Right child node, or null/undefined.
|
|
52
|
+
*/
|
|
53
|
+
get right(): TreeCounterNode<K, V> | null | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Set the right child and update its parent pointer.
|
|
56
|
+
* @remarks Time O(1), Space O(1)
|
|
57
|
+
* @param v - New right child node, or null/undefined.
|
|
58
|
+
* @returns void
|
|
59
|
+
*/
|
|
60
|
+
set right(v: TreeCounterNode<K, V> | null | undefined);
|
|
61
|
+
_height: number;
|
|
62
|
+
/**
|
|
63
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
64
|
+
* @remarks Time O(1), Space O(1)
|
|
65
|
+
*
|
|
66
|
+
* @returns The height.
|
|
67
|
+
*/
|
|
68
|
+
get height(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Sets the height of the node.
|
|
71
|
+
* @remarks Time O(1), Space O(1)
|
|
72
|
+
*
|
|
73
|
+
* @param value - The new height.
|
|
74
|
+
*/
|
|
75
|
+
set height(value: number);
|
|
76
|
+
_color: RBTNColor;
|
|
77
|
+
/**
|
|
78
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
79
|
+
* @remarks Time O(1), Space O(1)
|
|
80
|
+
*
|
|
81
|
+
* @returns The node's color.
|
|
82
|
+
*/
|
|
83
|
+
get color(): RBTNColor;
|
|
84
|
+
/**
|
|
85
|
+
* Sets the color of the node.
|
|
86
|
+
* @remarks Time O(1), Space O(1)
|
|
87
|
+
*
|
|
88
|
+
* @param value - The new color.
|
|
89
|
+
*/
|
|
90
|
+
set color(value: RBTNColor);
|
|
91
|
+
_count: number;
|
|
92
|
+
/**
|
|
93
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
94
|
+
* @remarks Time O(1), Space O(1)
|
|
95
|
+
*
|
|
96
|
+
* @returns The subtree node count.
|
|
97
|
+
*/
|
|
98
|
+
get count(): number;
|
|
99
|
+
/**
|
|
100
|
+
* Sets the count of nodes in the subtree.
|
|
101
|
+
* @remarks Time O(1), Space O(1)
|
|
102
|
+
*
|
|
103
|
+
* @param value - The new count.
|
|
104
|
+
*/
|
|
105
|
+
set count(value: number);
|
|
106
|
+
/**
|
|
107
|
+
* Gets the position of the node relative to its parent.
|
|
108
|
+
* @remarks Time O(1), Space O(1)
|
|
109
|
+
*
|
|
110
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
111
|
+
*/
|
|
112
|
+
get familyPosition(): FamilyPosition;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Red-Black Tree–based counter map (key → value with per-node count). Supports O(log N) updates and map-like mode.
|
|
116
|
+
* @remarks Time O(1), Space O(1)
|
|
117
|
+
* @template K
|
|
118
|
+
* @template V
|
|
119
|
+
* @template R
|
|
120
|
+
*/
|
|
121
|
+
export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
122
|
+
/**
|
|
123
|
+
* Create a TreeCounter and optionally bulk-insert items.
|
|
124
|
+
* @remarks Time O(N log N), Space O(N)
|
|
125
|
+
* @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
|
|
126
|
+
* @param [options] - Options for TreeCounter (comparator, reverse, map mode).
|
|
127
|
+
* @returns New TreeCounter instance.
|
|
128
|
+
*/
|
|
129
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: TreeCounterOptions<K, V, R>);
|
|
130
|
+
protected _count: number;
|
|
131
|
+
/**
|
|
132
|
+
* Get the total aggregate count across all nodes.
|
|
133
|
+
* @remarks Time O(1), Space O(1)
|
|
134
|
+
* @returns Total count.
|
|
135
|
+
*/
|
|
136
|
+
get count(): number;
|
|
137
|
+
/**
|
|
138
|
+
* Compute the total count by traversing the tree (sums node.count).
|
|
139
|
+
* @remarks Time O(N), Space O(H)
|
|
140
|
+
* @returns Total count recomputed from nodes.
|
|
141
|
+
*/
|
|
142
|
+
getComputedCount(): number;
|
|
143
|
+
createNode(key: K, value?: V, color?: RBTNColor, count?: number): TreeCounterNode<K, V>;
|
|
144
|
+
/**
|
|
145
|
+
* Type guard: check whether the input is a TreeCounterNode.
|
|
146
|
+
* @remarks Time O(1), Space O(1)
|
|
147
|
+
* @returns True if the value is a TreeCounterNode.
|
|
148
|
+
*/
|
|
149
|
+
isNode(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is TreeCounterNode<K, V>;
|
|
150
|
+
/**
|
|
151
|
+
* Insert or increment a node and update aggregate count.
|
|
152
|
+
* @remarks Time O(log N), Space O(1)
|
|
153
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
|
|
154
|
+
* @param [value] - Value when a bare key is provided (ignored in map mode).
|
|
155
|
+
* @param [count] - How much to increase the node's count (default 1).
|
|
156
|
+
* @returns True if inserted/updated; false if ignored.
|
|
157
|
+
*/
|
|
158
|
+
add(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean;
|
|
159
|
+
/**
|
|
160
|
+
* Delete a node (or decrement its count) and rebalance if needed.
|
|
161
|
+
* @remarks Time O(log N), Space O(1)
|
|
162
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
|
|
163
|
+
* @param [ignoreCount] - If true, remove the node regardless of its count.
|
|
164
|
+
* @returns Array of deletion results including deleted node and a rebalance hint when present.
|
|
165
|
+
*/
|
|
166
|
+
delete(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, ignoreCount?: boolean): BinaryTreeDeleteResult<TreeCounterNode<K, V>>[];
|
|
167
|
+
/**
|
|
168
|
+
* Remove all nodes and reset aggregate counters.
|
|
169
|
+
* @remarks Time O(N), Space O(1)
|
|
170
|
+
* @returns void
|
|
171
|
+
*/
|
|
172
|
+
clear(): void;
|
|
173
|
+
/**
|
|
174
|
+
* Rebuild the tree into a perfectly balanced form using in-order nodes.
|
|
175
|
+
* @remarks Time O(N), Space O(N)
|
|
176
|
+
* @param [iterationType] - Traversal style to use when constructing the balanced tree.
|
|
177
|
+
* @returns True if rebalancing succeeded (tree not empty).
|
|
178
|
+
*/
|
|
179
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
180
|
+
/**
|
|
181
|
+
* Create a new TreeCounter by mapping each [key, value] entry.
|
|
182
|
+
* @remarks Time O(N log N), Space O(N)
|
|
183
|
+
* @template MK
|
|
184
|
+
* @template MV
|
|
185
|
+
* @template MR
|
|
186
|
+
* @param callback - Function mapping (key, value, index, tree) → [newKey, newValue].
|
|
187
|
+
* @param [options] - Options for the output tree.
|
|
188
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
189
|
+
* @returns A new TreeCounter with mapped entries.
|
|
190
|
+
*/
|
|
191
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): TreeCounter<MK, MV, MR>;
|
|
192
|
+
/**
|
|
193
|
+
* Deep copy this tree, preserving map mode and aggregate counts.
|
|
194
|
+
* @remarks Time O(N), Space O(N)
|
|
195
|
+
* @returns A deep copy of this tree.
|
|
196
|
+
*/
|
|
197
|
+
clone(): this;
|
|
198
|
+
/**
|
|
199
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
200
|
+
* @remarks Time O(1), Space O(1)
|
|
201
|
+
* @template TK
|
|
202
|
+
* @template TV
|
|
203
|
+
* @template TR
|
|
204
|
+
* @param [options] - Optional constructor options for the like-kind instance.
|
|
205
|
+
* @returns An empty like-kind instance.
|
|
206
|
+
*/
|
|
207
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this;
|
|
208
|
+
/**
|
|
209
|
+
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
210
|
+
* @remarks Time O(N log N), Space O(N)
|
|
211
|
+
* @template TK
|
|
212
|
+
* @template TV
|
|
213
|
+
* @template TR
|
|
214
|
+
* @param iter - Iterable used to seed the new tree.
|
|
215
|
+
* @param [options] - Options merged with the current snapshot.
|
|
216
|
+
* @returns A like-kind TreeCounter built from the iterable.
|
|
217
|
+
*/
|
|
218
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<BSTOptions<TK, TV, TR>>): TreeCounter<TK, TV, TR>;
|
|
219
|
+
/**
|
|
220
|
+
* (Protected) Normalize input into a node plus its effective value and count.
|
|
221
|
+
* @remarks Time O(1), Space O(1)
|
|
222
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry.
|
|
223
|
+
* @param [value] - Value used when a bare key is provided.
|
|
224
|
+
* @param [count] - Count increment to apply (default 1).
|
|
225
|
+
* @returns Tuple [node, value] where node may be undefined.
|
|
226
|
+
*/
|
|
227
|
+
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): [TreeCounterNode<K, V> | undefined, V | undefined];
|
|
228
|
+
/**
|
|
229
|
+
* (Protected) Swap keys/values/counters between the source and destination nodes.
|
|
230
|
+
* @remarks Time O(1), Space O(1)
|
|
231
|
+
* @param srcNode - Source node (or key) whose properties will be moved.
|
|
232
|
+
* @param destNode - Destination node (or key) to receive properties.
|
|
233
|
+
* @returns Destination node after swap, or undefined.
|
|
234
|
+
*/
|
|
235
|
+
protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>, destNode: BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>): TreeCounterNode<K, V> | undefined;
|
|
236
|
+
/**
|
|
237
|
+
* (Protected) Replace one node by another and adjust counters accordingly.
|
|
238
|
+
* @remarks Time O(1), Space O(1)
|
|
239
|
+
* @param oldNode - Node being replaced.
|
|
240
|
+
* @param newNode - Replacement node.
|
|
241
|
+
* @returns The new node after replacement.
|
|
242
|
+
*/
|
|
243
|
+
protected _replaceNode(oldNode: TreeCounterNode<K, V>, newNode: TreeCounterNode<K, V>): TreeCounterNode<K, V>;
|
|
244
|
+
}
|