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,645 @@
|
|
|
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, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparator, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodePredicate, OptNode, RBTNColor } from '../../types';
|
|
9
|
+
import { BinaryTree } from './binary-tree';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
import { Range } from '../../common';
|
|
12
|
+
/**
|
|
13
|
+
* Represents a Node in a Binary Search Tree.
|
|
14
|
+
*
|
|
15
|
+
* @template K - The type of the key.
|
|
16
|
+
* @template V - The type of the value.
|
|
17
|
+
*/
|
|
18
|
+
export declare class BSTNode<K = any, V = any> {
|
|
19
|
+
key: K;
|
|
20
|
+
value?: V;
|
|
21
|
+
parent?: BSTNode<K, V>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates an instance of BSTNode.
|
|
24
|
+
* @remarks Time O(1), Space O(1)
|
|
25
|
+
*
|
|
26
|
+
* @param key - The key of the node.
|
|
27
|
+
* @param [value] - The value associated with the key.
|
|
28
|
+
*/
|
|
29
|
+
constructor(key: K, value?: V);
|
|
30
|
+
_left?: BSTNode<K, V> | null | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Gets the left child of the node.
|
|
33
|
+
* @remarks Time O(1), Space O(1)
|
|
34
|
+
*
|
|
35
|
+
* @returns The left child.
|
|
36
|
+
*/
|
|
37
|
+
get left(): BSTNode<K, V> | null | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Sets the left child of the node and updates its parent reference.
|
|
40
|
+
* @remarks Time O(1), Space O(1)
|
|
41
|
+
*
|
|
42
|
+
* @param v - The node to set as the left child.
|
|
43
|
+
*/
|
|
44
|
+
set left(v: BSTNode<K, V> | null | undefined);
|
|
45
|
+
_right?: BSTNode<K, V> | null | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Gets the right child of the node.
|
|
48
|
+
* @remarks Time O(1), Space O(1)
|
|
49
|
+
*
|
|
50
|
+
* @returns The right child.
|
|
51
|
+
*/
|
|
52
|
+
get right(): BSTNode<K, V> | null | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Sets the right child of the node and updates its parent reference.
|
|
55
|
+
* @remarks Time O(1), Space O(1)
|
|
56
|
+
*
|
|
57
|
+
* @param v - The node to set as the right child.
|
|
58
|
+
*/
|
|
59
|
+
set right(v: BSTNode<K, V> | null | undefined);
|
|
60
|
+
_height: number;
|
|
61
|
+
/**
|
|
62
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
63
|
+
* @remarks Time O(1), Space O(1)
|
|
64
|
+
*
|
|
65
|
+
* @returns The height.
|
|
66
|
+
*/
|
|
67
|
+
get height(): number;
|
|
68
|
+
/**
|
|
69
|
+
* Sets the height of the node.
|
|
70
|
+
* @remarks Time O(1), Space O(1)
|
|
71
|
+
*
|
|
72
|
+
* @param value - The new height.
|
|
73
|
+
*/
|
|
74
|
+
set height(value: number);
|
|
75
|
+
_color: RBTNColor;
|
|
76
|
+
/**
|
|
77
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
78
|
+
* @remarks Time O(1), Space O(1)
|
|
79
|
+
*
|
|
80
|
+
* @returns The node's color.
|
|
81
|
+
*/
|
|
82
|
+
get color(): RBTNColor;
|
|
83
|
+
/**
|
|
84
|
+
* Sets the color of the node.
|
|
85
|
+
* @remarks Time O(1), Space O(1)
|
|
86
|
+
*
|
|
87
|
+
* @param value - The new color.
|
|
88
|
+
*/
|
|
89
|
+
set color(value: RBTNColor);
|
|
90
|
+
_count: number;
|
|
91
|
+
/**
|
|
92
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
93
|
+
* @remarks Time O(1), Space O(1)
|
|
94
|
+
*
|
|
95
|
+
* @returns The subtree node count.
|
|
96
|
+
*/
|
|
97
|
+
get count(): number;
|
|
98
|
+
/**
|
|
99
|
+
* Sets the count of nodes in the subtree.
|
|
100
|
+
* @remarks Time O(1), Space O(1)
|
|
101
|
+
*
|
|
102
|
+
* @param value - The new count.
|
|
103
|
+
*/
|
|
104
|
+
set count(value: number);
|
|
105
|
+
/**
|
|
106
|
+
* Gets the position of the node relative to its parent.
|
|
107
|
+
* @remarks Time O(1), Space O(1)
|
|
108
|
+
*
|
|
109
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
110
|
+
*/
|
|
111
|
+
get familyPosition(): FamilyPosition;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Represents a Binary Search Tree (BST).
|
|
115
|
+
* Keys are ordered, allowing for faster search operations compared to a standard Binary Tree.
|
|
116
|
+
* @template K - The type of the key.
|
|
117
|
+
* @template V - The type of the value.
|
|
118
|
+
* @template R - The type of the raw data object (if using `toEntryFn`).
|
|
119
|
+
*
|
|
120
|
+
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
121
|
+
* 2. Unique Keys: No duplicate keys in a standard BST.
|
|
122
|
+
* 3. Efficient Search: Enables quick search, minimum, and maximum operations.
|
|
123
|
+
* 4. Inorder Traversal: Yields nodes in ascending order.
|
|
124
|
+
* 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
|
|
125
|
+
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
126
|
+
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* // basic BST creation and add operation
|
|
130
|
+
* // Create a simple BST with numeric keys
|
|
131
|
+
* const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
132
|
+
*
|
|
133
|
+
* bst.print();
|
|
134
|
+
* // _______8__________
|
|
135
|
+
* // / \
|
|
136
|
+
* // ___4___ ____12_____
|
|
137
|
+
* // / \ / \
|
|
138
|
+
* // _2_ _6_ _10__ _14__
|
|
139
|
+
* // / \ / \ / \ / \
|
|
140
|
+
* // 1 3 5 7 9 11 13 15__
|
|
141
|
+
* // \
|
|
142
|
+
* // 16
|
|
143
|
+
*
|
|
144
|
+
* // Verify size
|
|
145
|
+
* console.log(bst.size); // 16;
|
|
146
|
+
*
|
|
147
|
+
* // Add new elements
|
|
148
|
+
* bst.set(17);
|
|
149
|
+
* bst.set(0);
|
|
150
|
+
* console.log(bst.size); // 18;
|
|
151
|
+
*
|
|
152
|
+
* // Verify keys are searchable
|
|
153
|
+
* console.log(bst.has(11)); // true;
|
|
154
|
+
* console.log(bst.has(100)); // false;
|
|
155
|
+
* @example
|
|
156
|
+
* // BST delete and search after deletion
|
|
157
|
+
* const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
158
|
+
*
|
|
159
|
+
* // Delete a leaf node
|
|
160
|
+
* bst.delete(1);
|
|
161
|
+
* console.log(bst.has(1)); // false;
|
|
162
|
+
*
|
|
163
|
+
* // Delete a node with one child
|
|
164
|
+
* bst.delete(2);
|
|
165
|
+
* console.log(bst.has(2)); // false;
|
|
166
|
+
*
|
|
167
|
+
* // Delete a node with two children
|
|
168
|
+
* bst.delete(3);
|
|
169
|
+
* console.log(bst.has(3)); // false;
|
|
170
|
+
*
|
|
171
|
+
* // Size decreases with each deletion
|
|
172
|
+
* console.log(bst.size); // 13;
|
|
173
|
+
*
|
|
174
|
+
* // Other nodes remain searchable
|
|
175
|
+
* console.log(bst.has(11)); // true;
|
|
176
|
+
* console.log(bst.has(15)); // true;
|
|
177
|
+
* @example
|
|
178
|
+
* // Merge 3 sorted datasets
|
|
179
|
+
* const dataset1 = new BST<number, string>([
|
|
180
|
+
* [1, 'A'],
|
|
181
|
+
* [7, 'G']
|
|
182
|
+
* ]);
|
|
183
|
+
* const dataset2 = [
|
|
184
|
+
* [2, 'B'],
|
|
185
|
+
* [6, 'F']
|
|
186
|
+
* ];
|
|
187
|
+
* const dataset3 = new BST<number, string>([
|
|
188
|
+
* [3, 'C'],
|
|
189
|
+
* [5, 'E'],
|
|
190
|
+
* [4, 'D']
|
|
191
|
+
* ]);
|
|
192
|
+
*
|
|
193
|
+
* // Merge datasets into a single BinarySearchTree
|
|
194
|
+
* const merged = new BST<number, string>(dataset1);
|
|
195
|
+
* merged.setMany(dataset2);
|
|
196
|
+
* merged.merge(dataset3);
|
|
197
|
+
*
|
|
198
|
+
* // Verify merged dataset is in sorted order
|
|
199
|
+
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
|
|
200
|
+
* @example
|
|
201
|
+
* // BST with custom objects for expression evaluation
|
|
202
|
+
* interface Expression {
|
|
203
|
+
* id: number;
|
|
204
|
+
* operator: string;
|
|
205
|
+
* precedence: number;
|
|
206
|
+
* }
|
|
207
|
+
*
|
|
208
|
+
* // BST efficiently stores and retrieves operators by precedence
|
|
209
|
+
* const operatorTree = new BST<number, Expression>(
|
|
210
|
+
* [
|
|
211
|
+
* [1, { id: 1, operator: '+', precedence: 1 }],
|
|
212
|
+
* [2, { id: 2, operator: '*', precedence: 2 }],
|
|
213
|
+
* [3, { id: 3, operator: '/', precedence: 2 }],
|
|
214
|
+
* [4, { id: 4, operator: '-', precedence: 1 }],
|
|
215
|
+
* [5, { id: 5, operator: '^', precedence: 3 }]
|
|
216
|
+
* ],
|
|
217
|
+
* { isMapMode: false }
|
|
218
|
+
* );
|
|
219
|
+
*
|
|
220
|
+
* console.log(operatorTree.size); // 5;
|
|
221
|
+
*
|
|
222
|
+
* // Quick lookup of operators
|
|
223
|
+
* const mult = operatorTree.get(2);
|
|
224
|
+
* console.log(mult?.operator); // '*';
|
|
225
|
+
* console.log(mult?.precedence); // 2;
|
|
226
|
+
*
|
|
227
|
+
* // Check if operator exists
|
|
228
|
+
* console.log(operatorTree.has(5)); // true;
|
|
229
|
+
* console.log(operatorTree.has(99)); // false;
|
|
230
|
+
*
|
|
231
|
+
* // Retrieve operator by precedence level
|
|
232
|
+
* const expNode = operatorTree.getNode(3);
|
|
233
|
+
* console.log(expNode?.key); // 3;
|
|
234
|
+
* console.log(expNode?.value?.precedence); // 2;
|
|
235
|
+
*
|
|
236
|
+
* // Delete operator and verify
|
|
237
|
+
* operatorTree.delete(1);
|
|
238
|
+
* console.log(operatorTree.has(1)); // false;
|
|
239
|
+
* console.log(operatorTree.size); // 4;
|
|
240
|
+
*
|
|
241
|
+
* // Get tree height for optimization analysis
|
|
242
|
+
* const treeHeight = operatorTree.getHeight();
|
|
243
|
+
* console.log(treeHeight); // > 0;
|
|
244
|
+
*
|
|
245
|
+
* // Remaining operators are still accessible
|
|
246
|
+
* const remaining = operatorTree.get(2);
|
|
247
|
+
* console.log(remaining); // defined;
|
|
248
|
+
* @example
|
|
249
|
+
* // Find lowest common ancestor
|
|
250
|
+
* const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
251
|
+
*
|
|
252
|
+
* // LCA helper function
|
|
253
|
+
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
254
|
+
* const path1 = bst.getPathToRoot(num1);
|
|
255
|
+
* const path2 = bst.getPathToRoot(num2);
|
|
256
|
+
* // Find the first common ancestor
|
|
257
|
+
* return findFirstCommon(path1, path2);
|
|
258
|
+
* };
|
|
259
|
+
*
|
|
260
|
+
* function findFirstCommon(arr1: (number | undefined)[], arr2: (number | undefined)[]): number | undefined {
|
|
261
|
+
* for (const num of arr1) {
|
|
262
|
+
* if (arr2.indexOf(num) !== -1) {
|
|
263
|
+
* return num;
|
|
264
|
+
* }
|
|
265
|
+
* }
|
|
266
|
+
* return undefined;
|
|
267
|
+
* }
|
|
268
|
+
*
|
|
269
|
+
* // Assertions
|
|
270
|
+
* console.log(findLCA(3, 10)); // 7;
|
|
271
|
+
* console.log(findLCA(5, 35)); // 15;
|
|
272
|
+
* console.log(findLCA(20, 30)); // 25;
|
|
273
|
+
*/
|
|
274
|
+
export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
275
|
+
/**
|
|
276
|
+
* Creates an instance of BST.
|
|
277
|
+
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
278
|
+
*
|
|
279
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
280
|
+
* @param [options] - Configuration options for the BST, including comparator.
|
|
281
|
+
*/
|
|
282
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>);
|
|
283
|
+
protected _root?: BSTNode<K, V>;
|
|
284
|
+
/**
|
|
285
|
+
* Gets the root node of the tree.
|
|
286
|
+
* @remarks Time O(1)
|
|
287
|
+
*
|
|
288
|
+
* @returns The root node.
|
|
289
|
+
*/
|
|
290
|
+
get root(): OptNode<BSTNode<K, V>>;
|
|
291
|
+
/**
|
|
292
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
293
|
+
|
|
294
|
+
* @remarks Time O(1) Space O(1)
|
|
295
|
+
*/
|
|
296
|
+
protected _comparator: Comparator<K>;
|
|
297
|
+
/**
|
|
298
|
+
* Gets the comparator function used by the tree.
|
|
299
|
+
* @remarks Time O(1)
|
|
300
|
+
*
|
|
301
|
+
* @returns The comparator function.
|
|
302
|
+
*/
|
|
303
|
+
get comparator(): Comparator<K>;
|
|
304
|
+
/**
|
|
305
|
+
* (Protected) Creates a new BST node.
|
|
306
|
+
* @remarks Time O(1), Space O(1)
|
|
307
|
+
*
|
|
308
|
+
* @param key - The key for the new node.
|
|
309
|
+
* @param [value] - The value for the new node (used if not in Map mode).
|
|
310
|
+
* @returns The newly created BSTNode.
|
|
311
|
+
*/
|
|
312
|
+
createNode(key: K, value?: V): BSTNode<K, V>;
|
|
313
|
+
/**
|
|
314
|
+
* Ensures the input is a node. If it's a key or entry, it searches for the node.
|
|
315
|
+
* @remarks Time O(log N) (height of the tree), O(N) worst-case.
|
|
316
|
+
*
|
|
317
|
+
* @param keyNodeOrEntry - The item to resolve to a node.
|
|
318
|
+
* @param [iterationType=this.iterationType] - The traversal method to use if searching.
|
|
319
|
+
* @returns The resolved node, or undefined if not found.
|
|
320
|
+
*/
|
|
321
|
+
ensureNode(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): OptNode<BSTNode<K, V>>;
|
|
322
|
+
/**
|
|
323
|
+
* Checks if the given item is a `BSTNode` instance.
|
|
324
|
+
* @remarks Time O(1), Space O(1)
|
|
325
|
+
*
|
|
326
|
+
* @param keyNodeOrEntry - The item to check.
|
|
327
|
+
* @returns True if it's a BSTNode, false otherwise.
|
|
328
|
+
*/
|
|
329
|
+
isNode(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BSTNode<K, V>;
|
|
330
|
+
/**
|
|
331
|
+
* Checks if the given key is valid (comparable).
|
|
332
|
+
* @remarks Time O(1)
|
|
333
|
+
*
|
|
334
|
+
* @param key - The key to validate.
|
|
335
|
+
* @returns True if the key is valid, false otherwise.
|
|
336
|
+
*/
|
|
337
|
+
isValidKey(key: any): key is K;
|
|
338
|
+
dfs(): (K | undefined)[];
|
|
339
|
+
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
340
|
+
bfs(): (K | undefined)[];
|
|
341
|
+
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
342
|
+
listLevels(): (K | undefined)[][];
|
|
343
|
+
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[][];
|
|
344
|
+
/**
|
|
345
|
+
* Gets the first node matching a predicate.
|
|
346
|
+
* @remarks Time O(log N) if searching by key, O(N) if searching by predicate. Space O(log N) or O(N).
|
|
347
|
+
*
|
|
348
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
349
|
+
* @param [startNode=this._root] - The node to start the search from.
|
|
350
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
351
|
+
* @returns The first matching node, or undefined if not found.
|
|
352
|
+
*/
|
|
353
|
+
getNode(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, startNode?: BSTNOptKeyOrNode<K, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>;
|
|
354
|
+
search(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean): (K | undefined)[];
|
|
355
|
+
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne: boolean, callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
356
|
+
rangeSearch(range: Range<K> | [K, K]): (K | undefined)[];
|
|
357
|
+
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
358
|
+
/**
|
|
359
|
+
* Adds a new node to the BST based on key comparison.
|
|
360
|
+
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
361
|
+
*
|
|
362
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
363
|
+
* @param [value] - The value, if providing just a key.
|
|
364
|
+
* @returns True if the addition was successful, false otherwise.
|
|
365
|
+
*/
|
|
366
|
+
set(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
367
|
+
/**
|
|
368
|
+
* Adds multiple items to the tree.
|
|
369
|
+
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
|
|
370
|
+
* If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
|
|
371
|
+
* Space O(N) for sorting and recursion/iteration stack.
|
|
372
|
+
*
|
|
373
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
374
|
+
* @param [values] - An optional parallel iterable of values.
|
|
375
|
+
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
376
|
+
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
377
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
378
|
+
*/
|
|
379
|
+
setMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
380
|
+
/**
|
|
381
|
+
* Returns the first key with a value >= target.
|
|
382
|
+
* Equivalent to Java TreeMap.ceiling.
|
|
383
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
384
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
385
|
+
*/
|
|
386
|
+
ceiling(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
387
|
+
/**
|
|
388
|
+
* Returns the first node with a key >= target and applies callback.
|
|
389
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
390
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
391
|
+
*/
|
|
392
|
+
ceiling<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
393
|
+
/**
|
|
394
|
+
* Returns the first key with a value > target.
|
|
395
|
+
* Equivalent to Java TreeMap.higher.
|
|
396
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
397
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
398
|
+
*/
|
|
399
|
+
higher(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
400
|
+
/**
|
|
401
|
+
* Returns the first node with a key > target and applies callback.
|
|
402
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
403
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
404
|
+
*/
|
|
405
|
+
higher<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
406
|
+
/**
|
|
407
|
+
* Returns the first key with a value <= target.
|
|
408
|
+
* Equivalent to Java TreeMap.floor.
|
|
409
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
410
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
411
|
+
*/
|
|
412
|
+
floor(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
413
|
+
/**
|
|
414
|
+
* Returns the first node with a key <= target and applies callback.
|
|
415
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
416
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
417
|
+
*/
|
|
418
|
+
floor<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
419
|
+
/**
|
|
420
|
+
* Returns the first key with a value < target.
|
|
421
|
+
* Equivalent to Java TreeMap.lower.
|
|
422
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
423
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
424
|
+
*/
|
|
425
|
+
lower(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
426
|
+
/**
|
|
427
|
+
* Returns the first node with a key < target and applies callback.
|
|
428
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
429
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
430
|
+
*/
|
|
431
|
+
lower<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
432
|
+
lesserOrGreaterTraverse(): (K | undefined)[];
|
|
433
|
+
lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>(callback: C, lesserOrGreater?: number, targetNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
434
|
+
/**
|
|
435
|
+
* Rebuilds the tree to be perfectly balanced.
|
|
436
|
+
* @remarks Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
437
|
+
*
|
|
438
|
+
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
439
|
+
* @returns True if successful, false if the tree was empty.
|
|
440
|
+
*/
|
|
441
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
442
|
+
/**
|
|
443
|
+
* Checks if the tree meets the AVL balance condition (height difference <= 1).
|
|
444
|
+
* @remarks Time O(N), as it must visit every node to compute height. Space O(log N) for recursion or O(N) for iterative map.
|
|
445
|
+
*
|
|
446
|
+
* @param [iterationType=this.iterationType] - The traversal method.
|
|
447
|
+
* @returns True if the tree is AVL balanced, false otherwise.
|
|
448
|
+
*/
|
|
449
|
+
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
450
|
+
/**
|
|
451
|
+
* Creates a new BST by mapping each [key, value] pair to a new entry.
|
|
452
|
+
* @remarks Time O(N * H), where N is nodes in this tree, and H is height of the new tree during insertion.
|
|
453
|
+
* Space O(N) for the new tree.
|
|
454
|
+
*
|
|
455
|
+
* @template MK - New key type.
|
|
456
|
+
* @template MV - New value type.
|
|
457
|
+
* @template MR - New raw type.
|
|
458
|
+
* @param callback - A function to map each [key, value] pair.
|
|
459
|
+
* @param [options] - Options for the new BST.
|
|
460
|
+
* @param [thisArg] - `this` context for the callback.
|
|
461
|
+
* @returns A new, mapped BST.
|
|
462
|
+
*/
|
|
463
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BSTOptions<MK, MV, MR>>, thisArg?: unknown): BST<MK, MV, MR>;
|
|
464
|
+
/**
|
|
465
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
466
|
+
*
|
|
467
|
+
* @remarks
|
|
468
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
469
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
470
|
+
*
|
|
471
|
+
* @template K - The key type.
|
|
472
|
+
* @template V - The value type.
|
|
473
|
+
*
|
|
474
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
475
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
476
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
477
|
+
* - An entry tuple: searches for the key-value pair.
|
|
478
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
479
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
480
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
481
|
+
*
|
|
482
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
483
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
484
|
+
*
|
|
485
|
+
* @param startNode - The node to start the search from. Can be:
|
|
486
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
487
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
488
|
+
* - Default value: this._root (the tree's root).
|
|
489
|
+
*
|
|
490
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
491
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
492
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
493
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
494
|
+
*
|
|
495
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
496
|
+
* - Key: the matched node's key.
|
|
497
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
498
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
499
|
+
*/
|
|
500
|
+
deleteWhere(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeDeleteResult<BSTNode<K, V>>[];
|
|
501
|
+
/**
|
|
502
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
503
|
+
* @remarks Time O(1) Space O(1)
|
|
504
|
+
* @returns The default comparator function.
|
|
505
|
+
*/
|
|
506
|
+
protected _createDefaultComparator(): Comparator<K>;
|
|
507
|
+
/**
|
|
508
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
509
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
510
|
+
* Finds first node where key <= target.
|
|
511
|
+
* @remarks Time O(h) where h is tree height.
|
|
512
|
+
*
|
|
513
|
+
* @param key - The target key to search for.
|
|
514
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
515
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
516
|
+
*/
|
|
517
|
+
protected _floorByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
518
|
+
/**
|
|
519
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
520
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
521
|
+
* Returns the last node that satisfies the predicate function.
|
|
522
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
523
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
524
|
+
*
|
|
525
|
+
* @param predicate - The predicate function to test nodes.
|
|
526
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
527
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
528
|
+
*/
|
|
529
|
+
protected _floorByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
530
|
+
/**
|
|
531
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
532
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
533
|
+
* Finds first node where key < target.
|
|
534
|
+
* @remarks Time O(h) where h is tree height.
|
|
535
|
+
*
|
|
536
|
+
* @param key - The target key to search for.
|
|
537
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
538
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
539
|
+
*/
|
|
540
|
+
protected _lowerByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
541
|
+
/**
|
|
542
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
543
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
544
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
545
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
546
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
547
|
+
*
|
|
548
|
+
* @param predicate - The predicate function to test nodes.
|
|
549
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
550
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
551
|
+
*/
|
|
552
|
+
protected _lowerByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
553
|
+
/**
|
|
554
|
+
* (Protected) Core bound search implementation supporting all parameter types.
|
|
555
|
+
* Unified logic for both lowerBound and upperBound.
|
|
556
|
+
* Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
|
|
557
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
558
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
559
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
560
|
+
* @returns The first matching node, or undefined if no such node exists.
|
|
561
|
+
*/
|
|
562
|
+
protected _bound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, isLower: boolean, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
563
|
+
/**
|
|
564
|
+
* (Protected) Binary search for bound by key with pruning optimization.
|
|
565
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
566
|
+
* For lowerBound: finds first node where key >= target.
|
|
567
|
+
* For upperBound: finds first node where key > target.
|
|
568
|
+
* @param key - The target key to search for.
|
|
569
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
570
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
571
|
+
* @returns The first node matching the bound condition, or undefined if none exists.
|
|
572
|
+
*/
|
|
573
|
+
protected _boundByKey(key: K, isLower: boolean, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
574
|
+
/**
|
|
575
|
+
* (Protected) In-order traversal search by predicate.
|
|
576
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
577
|
+
* Returns the first node that satisfies the predicate function.
|
|
578
|
+
* Note: Predicate-based search cannot leverage BST's binary search optimization.
|
|
579
|
+
* Time Complexity: O(n) since it may visit every node.
|
|
580
|
+
* @param predicate - The predicate function to test nodes.
|
|
581
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
582
|
+
* @returns The first node satisfying predicate, or undefined if none found.
|
|
583
|
+
*/
|
|
584
|
+
protected _boundByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
585
|
+
/**
|
|
586
|
+
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
587
|
+
* @remarks Time O(1)
|
|
588
|
+
*
|
|
589
|
+
* @template TK, TV, TR - Generic types for the new instance.
|
|
590
|
+
* @param [options] - Options for the new BST.
|
|
591
|
+
* @returns A new, empty BST.
|
|
592
|
+
*/
|
|
593
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this;
|
|
594
|
+
/**
|
|
595
|
+
* (Protected) Creates a new instance of the same BST constructor, potentially with different generic types.
|
|
596
|
+
* @remarks Time O(N log N) or O(N^2) (from constructor) due to processing the iterable.
|
|
597
|
+
*
|
|
598
|
+
* @template TK, TV, TR - Generic types for the new instance.
|
|
599
|
+
* @param [iter=[]] - An iterable to populate the new BST.
|
|
600
|
+
* @param [options] - Options for the new BST.
|
|
601
|
+
* @returns A new BST.
|
|
602
|
+
*/
|
|
603
|
+
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>>): BST<TK, TV, TR>;
|
|
604
|
+
/**
|
|
605
|
+
* (Protected) Snapshots the current BST's configuration options.
|
|
606
|
+
* @remarks Time O(1)
|
|
607
|
+
*
|
|
608
|
+
* @template TK, TV, TR - Generic types for the options.
|
|
609
|
+
* @returns The options object.
|
|
610
|
+
*/
|
|
611
|
+
protected _snapshotOptions<TK = K, TV = V, TR = R>(): BSTOptions<TK, TV, TR>;
|
|
612
|
+
/**
|
|
613
|
+
* (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
614
|
+
* @remarks Time O(1)
|
|
615
|
+
*
|
|
616
|
+
* @param keyNodeOrEntry - The input item.
|
|
617
|
+
* @param [value] - An optional value (used if input is just a key).
|
|
618
|
+
* @returns A tuple of [node, value].
|
|
619
|
+
*/
|
|
620
|
+
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): [OptNode<BSTNode<K, V>>, V | undefined];
|
|
621
|
+
/**
|
|
622
|
+
* (Protected) Sets the root node and clears its parent reference.
|
|
623
|
+
* @remarks Time O(1)
|
|
624
|
+
*
|
|
625
|
+
* @param v - The node to set as root.
|
|
626
|
+
*/
|
|
627
|
+
protected _setRoot(v: OptNode<BSTNode<K, V>>): void;
|
|
628
|
+
/**
|
|
629
|
+
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
630
|
+
* @remarks Time O(1) Space O(1)
|
|
631
|
+
*
|
|
632
|
+
* @param a - The first key.
|
|
633
|
+
* @param b - The second key.
|
|
634
|
+
* @returns A number (1, -1, or 0) representing the comparison.
|
|
635
|
+
*/
|
|
636
|
+
protected _compare(a: K, b: K): number;
|
|
637
|
+
/**
|
|
638
|
+
* (Private) Deletes a node by its key.
|
|
639
|
+
* @remarks Standard BST deletion algorithm. Time O(log N), O(N) worst-case. Space O(1).
|
|
640
|
+
*
|
|
641
|
+
* @param key - The key of the node to delete.
|
|
642
|
+
* @returns True if the node was found and deleted, false otherwise.
|
|
643
|
+
*/
|
|
644
|
+
protected _deleteByKey(key: K): boolean;
|
|
645
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './binary-tree';
|
|
2
|
+
export * from './bst';
|
|
3
|
+
export * from './binary-indexed-tree';
|
|
4
|
+
export * from './segment-tree';
|
|
5
|
+
export * from './avl-tree';
|
|
6
|
+
export * from './red-black-tree';
|
|
7
|
+
export * from './avl-tree-multi-map';
|
|
8
|
+
export * from './tree-multi-map';
|
|
9
|
+
export * from './tree-counter';
|
|
10
|
+
export * from './avl-tree-counter';
|