tree-set-typed 2.3.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/.eslintrc.js +61 -0
- package/.prettierignore +6 -0
- package/.prettierrc.js +16 -0
- package/LICENSE +21 -0
- package/README.md +482 -0
- package/coverage/clover.xml +13 -0
- package/coverage/coverage-final.json +96 -0
- package/coverage/coverage-summary.json +60 -0
- package/coverage/lcov-report/base.css +403 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +119 -0
- package/coverage/lcov-report/index.ts.html +109 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +206 -0
- package/coverage/lcov.info +14 -0
- package/dist/cjs/index.cjs +12 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs-legacy/index.cjs +12 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +3 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm-legacy/index.mjs +3 -0
- package/dist/esm-legacy/index.mjs.map +1 -0
- package/dist/types/common/index.d.ts +12 -0
- package/dist/types/constants/index.d.ts +4 -0
- package/dist/types/data-structures/base/index.d.ts +2 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +219 -0
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +150 -0
- package/dist/types/data-structures/base/linear-base.d.ts +335 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +236 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +197 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +440 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +807 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +645 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +10 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +312 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -0
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +243 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +333 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +332 -0
- package/dist/types/data-structures/graph/index.d.ts +4 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +347 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +428 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.d.ts +552 -0
- package/dist/types/data-structures/heap/index.d.ts +3 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -0
- package/dist/types/data-structures/index.d.ts +12 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +437 -0
- package/dist/types/data-structures/linked-list/index.d.ts +3 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +567 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -0
- package/dist/types/data-structures/matrix/index.d.ts +2 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +55 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -0
- package/dist/types/data-structures/queue/deque.d.ts +459 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/queue.d.ts +364 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.d.ts +324 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/tree.d.ts +62 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.d.ts +412 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/interfaces/binary-tree.d.ts +60 -0
- package/dist/types/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/types/interfaces/graph.d.ts +21 -0
- package/dist/types/interfaces/heap.d.ts +1 -0
- package/dist/types/interfaces/index.d.ts +8 -0
- package/dist/types/interfaces/navigator.d.ts +1 -0
- package/dist/types/interfaces/priority-queue.d.ts +1 -0
- package/dist/types/interfaces/segment-tree.d.ts +1 -0
- package/dist/types/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/types/types/common.d.ts +15 -0
- package/dist/types/types/data-structures/base/base.d.ts +13 -0
- 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/types/types/data-structures/binary-tree/index.d.ts +9 -0
- 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/types/types/data-structures/graph/index.d.ts +3 -0
- 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/index.d.ts +12 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/index.d.ts +3 -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/index.d.ts +2 -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/index.d.ts +3 -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/index.d.ts +2 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/index.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/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/number.d.ts +14 -0
- package/dist/types/utils/utils.d.ts +209 -0
- package/dist/umd/red-black-tree-typed.js +14578 -0
- package/dist/umd/red-black-tree-typed.js.map +1 -0
- package/dist/umd/red-black-tree-typed.min.js +44 -0
- package/dist/umd/red-black-tree-typed.min.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1383 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +263 -0
- package/docs/index.html +523 -0
- package/docs/modules.html +45 -0
- package/jest.config.js +8 -0
- package/package.json +113 -0
- package/src/common/index.ts +23 -0
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +352 -0
- package/src/data-structures/base/iterable-entry-base.ts +246 -0
- package/src/data-structures/base/linear-base.ts +643 -0
- package/src/data-structures/binary-tree/avl-tree-counter.ts +539 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +438 -0
- package/src/data-structures/binary-tree/avl-tree.ts +840 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +331 -0
- package/src/data-structures/binary-tree/binary-tree.ts +2492 -0
- package/src/data-structures/binary-tree/bst.ts +2024 -0
- package/src/data-structures/binary-tree/index.ts +10 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +767 -0
- package/src/data-structures/binary-tree/segment-tree.ts +324 -0
- package/src/data-structures/binary-tree/tree-counter.ts +575 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +549 -0
- package/src/data-structures/graph/abstract-graph.ts +1081 -0
- package/src/data-structures/graph/directed-graph.ts +715 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +132 -0
- package/src/data-structures/graph/undirected-graph.ts +626 -0
- package/src/data-structures/hash/hash-map.ts +813 -0
- package/src/data-structures/hash/index.ts +1 -0
- package/src/data-structures/heap/heap.ts +1020 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +47 -0
- package/src/data-structures/heap/min-heap.ts +36 -0
- package/src/data-structures/index.ts +12 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +876 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +1050 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +173 -0
- package/src/data-structures/matrix/index.ts +2 -0
- package/src/data-structures/matrix/matrix.ts +491 -0
- package/src/data-structures/matrix/navigator.ts +124 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +42 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +29 -0
- package/src/data-structures/priority-queue/priority-queue.ts +19 -0
- package/src/data-structures/queue/deque.ts +1001 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +592 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +469 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +115 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +756 -0
- package/src/index.ts +24 -0
- package/src/interfaces/binary-tree.ts +252 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/graph.ts +44 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +8 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/types/common.ts +25 -0
- package/src/types/data-structures/base/base.ts +34 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -0
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
- package/src/types/data-structures/binary-tree/bst.ts +19 -0
- package/src/types/data-structures/binary-tree/index.ts +9 -0
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -0
- package/src/types/data-structures/graph/abstract-graph.ts +18 -0
- package/src/types/data-structures/graph/directed-graph.ts +2 -0
- package/src/types/data-structures/graph/index.ts +3 -0
- package/src/types/data-structures/graph/map-graph.ts +1 -0
- package/src/types/data-structures/graph/undirected-graph.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +19 -0
- package/src/types/data-structures/hash/index.ts +3 -0
- package/src/types/data-structures/heap/heap.ts +6 -0
- package/src/types/data-structures/heap/index.ts +1 -0
- package/src/types/data-structures/heap/max-heap.ts +1 -0
- package/src/types/data-structures/heap/min-heap.ts +1 -0
- package/src/types/data-structures/index.ts +12 -0
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -0
- package/src/types/data-structures/linked-list/index.ts +3 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +2 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -0
- package/src/types/data-structures/matrix/navigator.ts +14 -0
- package/src/types/data-structures/priority-queue/index.ts +3 -0
- package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -0
- package/src/types/data-structures/queue/deque.ts +5 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +5 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +3 -0
- package/src/types/data-structures/tree/index.ts +1 -0
- package/src/types/data-structures/tree/tree.ts +1 -0
- package/src/types/data-structures/trie/index.ts +1 -0
- package/src/types/data-structures/trie/trie.ts +3 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +33 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/number.ts +22 -0
- package/src/utils/utils.ts +350 -0
- package/test/index.test.ts +111 -0
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +12 -0
- 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
|
@@ -0,0 +1,312 @@
|
|
|
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, 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
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* // basic Red-Black Tree with simple number keys
|
|
116
|
+
* // Create a simple Red-Black Tree with numeric keys
|
|
117
|
+
* const tree = new RedBlackTree([5, 2, 8, 1, 9]);
|
|
118
|
+
*
|
|
119
|
+
* tree.print();
|
|
120
|
+
* // _2___
|
|
121
|
+
* // / \
|
|
122
|
+
* // 1 _8_
|
|
123
|
+
* // / \
|
|
124
|
+
* // 5 9
|
|
125
|
+
*
|
|
126
|
+
* // Verify the tree maintains sorted order
|
|
127
|
+
* console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
|
|
128
|
+
*
|
|
129
|
+
* // Check size
|
|
130
|
+
* console.log(tree.size); // 5;
|
|
131
|
+
* @example
|
|
132
|
+
* // Red-Black Tree with key-value pairs for lookups
|
|
133
|
+
* interface Employee {
|
|
134
|
+
* id: number;
|
|
135
|
+
* name: string;
|
|
136
|
+
* }
|
|
137
|
+
*
|
|
138
|
+
* // Create tree with employee data
|
|
139
|
+
* const employees = new RedBlackTree<number, Employee>([
|
|
140
|
+
* [1, { id: 1, name: 'Alice' }],
|
|
141
|
+
* [3, { id: 3, name: 'Charlie' }],
|
|
142
|
+
* [2, { id: 2, name: 'Bob' }]
|
|
143
|
+
* ]);
|
|
144
|
+
*
|
|
145
|
+
* // Retrieve employee by ID
|
|
146
|
+
* const alice = employees.get(1);
|
|
147
|
+
* console.log(alice?.name); // 'Alice';
|
|
148
|
+
*
|
|
149
|
+
* // Verify sorted order by ID
|
|
150
|
+
* console.log([...employees.keys()]); // [1, 2, 3];
|
|
151
|
+
* @example
|
|
152
|
+
* // Red-Black Tree range search for filtering
|
|
153
|
+
* interface Product {
|
|
154
|
+
* name: string;
|
|
155
|
+
* price: number;
|
|
156
|
+
* }
|
|
157
|
+
*
|
|
158
|
+
* const products = new RedBlackTree<number, Product>([
|
|
159
|
+
* [10, { name: 'Item A', price: 10 }],
|
|
160
|
+
* [25, { name: 'Item B', price: 25 }],
|
|
161
|
+
* [40, { name: 'Item C', price: 40 }],
|
|
162
|
+
* [50, { name: 'Item D', price: 50 }]
|
|
163
|
+
* ]);
|
|
164
|
+
*
|
|
165
|
+
* // Find products in price range [20, 45]
|
|
166
|
+
* const pricesInRange = products.rangeSearch([20, 45], node => {
|
|
167
|
+
* return products.get(node)?.name;
|
|
168
|
+
* });
|
|
169
|
+
*
|
|
170
|
+
* console.log(pricesInRange); // ['Item B', 'Item C'];
|
|
171
|
+
* @example
|
|
172
|
+
* // Red-Black Tree as database index for stock market data
|
|
173
|
+
* interface StockPrice {
|
|
174
|
+
* symbol: string;
|
|
175
|
+
* volume: number;
|
|
176
|
+
* timestamp: Date;
|
|
177
|
+
* }
|
|
178
|
+
*
|
|
179
|
+
* // Simulate real-time stock price index
|
|
180
|
+
* const priceIndex = new RedBlackTree<number, StockPrice>([
|
|
181
|
+
* [142.5, { symbol: 'AAPL', volume: 1000000, timestamp: new Date() }],
|
|
182
|
+
* [335.2, { symbol: 'MSFT', volume: 800000, timestamp: new Date() }],
|
|
183
|
+
* [3285.04, { symbol: 'AMZN', volume: 500000, timestamp: new Date() }],
|
|
184
|
+
* [267.98, { symbol: 'META', volume: 750000, timestamp: new Date() }],
|
|
185
|
+
* [234.57, { symbol: 'GOOGL', volume: 900000, timestamp: new Date() }]
|
|
186
|
+
* ]);
|
|
187
|
+
*
|
|
188
|
+
* // Find highest-priced stock
|
|
189
|
+
* const maxPrice = priceIndex.getRightMost();
|
|
190
|
+
* console.log(priceIndex.get(maxPrice)?.symbol); // 'AMZN';
|
|
191
|
+
*
|
|
192
|
+
* // Find stocks in price range [200, 400] for portfolio balancing
|
|
193
|
+
* const stocksInRange = priceIndex.rangeSearch([200, 400], node => {
|
|
194
|
+
* const stock = priceIndex.get(node);
|
|
195
|
+
* return {
|
|
196
|
+
* symbol: stock?.symbol,
|
|
197
|
+
* price: node,
|
|
198
|
+
* volume: stock?.volume
|
|
199
|
+
* };
|
|
200
|
+
* });
|
|
201
|
+
*
|
|
202
|
+
* console.log(stocksInRange.length); // 3;
|
|
203
|
+
* console.log(stocksInRange.some((s: any) => s.symbol === 'GOOGL')); // true;
|
|
204
|
+
* console.log(stocksInRange.some((s: any) => s.symbol === 'META')); // true;
|
|
205
|
+
* console.log(stocksInRange.some((s: any) => s.symbol === 'MSFT')); // true;
|
|
206
|
+
*/
|
|
207
|
+
export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
208
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>);
|
|
209
|
+
protected _root: RedBlackTreeNode<K, V> | undefined;
|
|
210
|
+
/**
|
|
211
|
+
* Get the current root node.
|
|
212
|
+
* @remarks Time O(1), Space O(1)
|
|
213
|
+
* @returns Root node, or undefined.
|
|
214
|
+
*/
|
|
215
|
+
get root(): RedBlackTreeNode<K, V> | undefined;
|
|
216
|
+
/**
|
|
217
|
+
* Create a red-black node for the given key/value (value ignored in map mode).
|
|
218
|
+
* @remarks Time O(1), Space O(1)
|
|
219
|
+
* @param key - See parameter type for details.
|
|
220
|
+
* @param [value] - See parameter type for details.
|
|
221
|
+
* @param color - See parameter type for details.
|
|
222
|
+
* @returns A new RedBlackTreeNode instance.
|
|
223
|
+
*/
|
|
224
|
+
createNode(key: K, value?: V, color?: RBTNColor): RedBlackTreeNode<K, V>;
|
|
225
|
+
/**
|
|
226
|
+
* Type guard: check whether the input is a RedBlackTreeNode.
|
|
227
|
+
* @remarks Time O(1), Space O(1)
|
|
228
|
+
* @param keyNodeOrEntry - See parameter type for details.
|
|
229
|
+
* @returns True if the value is a RedBlackTreeNode.
|
|
230
|
+
*/
|
|
231
|
+
isNode(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is RedBlackTreeNode<K, V>;
|
|
232
|
+
/**
|
|
233
|
+
* Remove all nodes and clear the key→value store (if in map mode).
|
|
234
|
+
* @remarks Time O(n), Space O(1)
|
|
235
|
+
* @returns void
|
|
236
|
+
*/
|
|
237
|
+
clear(): void;
|
|
238
|
+
/**
|
|
239
|
+
* Insert or replace an entry using BST order and red-black fix-up.
|
|
240
|
+
* @remarks Time O(log n), Space O(1)
|
|
241
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
|
|
242
|
+
* @param [value]- See parameter type for details.
|
|
243
|
+
* @returns True if inserted or updated; false if ignored.
|
|
244
|
+
*/
|
|
245
|
+
set(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
246
|
+
/**
|
|
247
|
+
* Delete a node by key/node/entry and rebalance as needed.
|
|
248
|
+
* @remarks Time O(log n), Space O(1)
|
|
249
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node to delete.
|
|
250
|
+
* @returns Array with deletion metadata (removed node, rebalancing hint if any).
|
|
251
|
+
*/
|
|
252
|
+
delete(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[];
|
|
253
|
+
/**
|
|
254
|
+
* Transform entries into a like-kind red-black tree with possibly different key/value types.
|
|
255
|
+
* @remarks Time O(n), Space O(n)
|
|
256
|
+
* @template MK
|
|
257
|
+
* @template MV
|
|
258
|
+
* @template MR
|
|
259
|
+
* @param callback - Mapping function from (key, value, index, tree) to a new [key, value].
|
|
260
|
+
* @param [options] - See parameter type for details.
|
|
261
|
+
* @param [thisArg] - See parameter type for details.
|
|
262
|
+
* @returns A new RedBlackTree with mapped entries.
|
|
263
|
+
*/
|
|
264
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<RedBlackTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
265
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
|
|
266
|
+
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>;
|
|
267
|
+
protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void;
|
|
268
|
+
protected _replaceNode(oldNode: RedBlackTreeNode<K, V>, newNode: RedBlackTreeNode<K, V>): RedBlackTreeNode<K, V>;
|
|
269
|
+
/**
|
|
270
|
+
* (Protected) Standard BST insert followed by red-black fix-up.
|
|
271
|
+
* @remarks Time O(log n), Space O(1)
|
|
272
|
+
* @param node - Node to insert.
|
|
273
|
+
* @returns Status string: 'CREATED' or 'UPDATED'.
|
|
274
|
+
*/
|
|
275
|
+
protected _insert(node: RedBlackTreeNode<K, V>): CRUD;
|
|
276
|
+
/**
|
|
277
|
+
* (Protected) Transplant a subtree in place of another during deletion.
|
|
278
|
+
* @remarks Time O(1), Space O(1)
|
|
279
|
+
* @param u - Node to replace.
|
|
280
|
+
* @param v - Replacement subtree root (may be undefined).
|
|
281
|
+
* @returns void
|
|
282
|
+
*/
|
|
283
|
+
protected _transplant(u: RedBlackTreeNode<K, V>, v: RedBlackTreeNode<K, V> | undefined): void;
|
|
284
|
+
/**
|
|
285
|
+
* (Protected) Restore red-black properties after insertion (recolor/rotate).
|
|
286
|
+
* @remarks Time O(log n), Space O(1)
|
|
287
|
+
* @param z - Recently inserted node.
|
|
288
|
+
* @returns void
|
|
289
|
+
*/
|
|
290
|
+
protected _insertFixup(z: RedBlackTreeNode<K, V> | undefined): void;
|
|
291
|
+
/**
|
|
292
|
+
* (Protected) Restore red-black properties after deletion (recolor/rotate).
|
|
293
|
+
* @remarks Time O(log n), Space O(1)
|
|
294
|
+
* @param node - Child that replaced the deleted node (may be undefined).
|
|
295
|
+
* @returns void
|
|
296
|
+
*/
|
|
297
|
+
protected _deleteFixup(node: RedBlackTreeNode<K, V> | undefined): void;
|
|
298
|
+
/**
|
|
299
|
+
* (Protected) Perform a left rotation around x.
|
|
300
|
+
* @remarks Time O(1), Space O(1)
|
|
301
|
+
* @param x - Pivot node to rotate around.
|
|
302
|
+
* @returns void
|
|
303
|
+
*/
|
|
304
|
+
protected _leftRotate(x: RedBlackTreeNode<K, V> | undefined): void;
|
|
305
|
+
/**
|
|
306
|
+
* (Protected) Perform a right rotation around y.
|
|
307
|
+
* @remarks Time O(1), Space O(1)
|
|
308
|
+
* @param y - Pivot node to rotate around.
|
|
309
|
+
* @returns void
|
|
310
|
+
*/
|
|
311
|
+
protected _rightRotate(y: RedBlackTreeNode<K, V> | undefined): void;
|
|
312
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
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
|
+
}
|