linked-list-typed 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +2152 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +2143 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +11 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/linked-list-typed.js +2146 -0
- package/dist/umd/linked-list-typed.js.map +1 -0
- package/dist/umd/linked-list-typed.min.js +9 -0
- package/dist/umd/linked-list-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -12
- package/dist/index.js +0 -28
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -1,903 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Pablo Zeng
|
|
6
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.BST = exports.BSTNode = void 0;
|
|
11
|
-
const binary_tree_1 = require("./binary-tree");
|
|
12
|
-
const queue_1 = require("../queue");
|
|
13
|
-
const utils_1 = require("../../utils");
|
|
14
|
-
const common_1 = require("../../common");
|
|
15
|
-
/**
|
|
16
|
-
* Represents a Node in a Binary Search Tree.
|
|
17
|
-
*
|
|
18
|
-
* @template K - The type of the key.
|
|
19
|
-
* @template V - The type of the value.
|
|
20
|
-
*/
|
|
21
|
-
class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
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, value) {
|
|
30
|
-
super(key, value);
|
|
31
|
-
this.parent = undefined;
|
|
32
|
-
this._left = undefined;
|
|
33
|
-
this._right = undefined;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Gets the left child of the node.
|
|
37
|
-
* @remarks Time O(1), Space O(1)
|
|
38
|
-
*
|
|
39
|
-
* @returns The left child.
|
|
40
|
-
*/
|
|
41
|
-
get left() {
|
|
42
|
-
return this._left;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Sets the left child of the node and updates its parent reference.
|
|
46
|
-
* @remarks Time O(1), Space O(1)
|
|
47
|
-
*
|
|
48
|
-
* @param v - The node to set as the left child.
|
|
49
|
-
*/
|
|
50
|
-
set left(v) {
|
|
51
|
-
if (v)
|
|
52
|
-
v.parent = this;
|
|
53
|
-
this._left = v;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Gets the right child of the node.
|
|
57
|
-
* @remarks Time O(1), Space O(1)
|
|
58
|
-
*
|
|
59
|
-
* @returns The right child.
|
|
60
|
-
*/
|
|
61
|
-
get right() {
|
|
62
|
-
return this._right;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Sets the right child of the node and updates its parent reference.
|
|
66
|
-
* @remarks Time O(1), Space O(1)
|
|
67
|
-
*
|
|
68
|
-
* @param v - The node to set as the right child.
|
|
69
|
-
*/
|
|
70
|
-
set right(v) {
|
|
71
|
-
if (v)
|
|
72
|
-
v.parent = this;
|
|
73
|
-
this._right = v;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
exports.BSTNode = BSTNode;
|
|
77
|
-
/**
|
|
78
|
-
* Represents a Binary Search Tree (BST).
|
|
79
|
-
* Keys are ordered, allowing for faster search operations compared to a standard Binary Tree.
|
|
80
|
-
* @template K - The type of the key.
|
|
81
|
-
* @template V - The type of the value.
|
|
82
|
-
* @template R - The type of the raw data object (if using `toEntryFn`).
|
|
83
|
-
*
|
|
84
|
-
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
85
|
-
* 2. Unique Keys: No duplicate keys in a standard BST.
|
|
86
|
-
* 3. Efficient Search: Enables quick search, minimum, and maximum operations.
|
|
87
|
-
* 4. Inorder Traversal: Yields nodes in ascending order.
|
|
88
|
-
* 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
|
|
89
|
-
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
90
|
-
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
91
|
-
* @example
|
|
92
|
-
* // Merge 3 sorted datasets
|
|
93
|
-
* const dataset1 = new BST<number, string>([
|
|
94
|
-
* [1, 'A'],
|
|
95
|
-
* [7, 'G']
|
|
96
|
-
* ]);
|
|
97
|
-
* const dataset2 = [
|
|
98
|
-
* [2, 'B'],
|
|
99
|
-
* [6, 'F']
|
|
100
|
-
* ];
|
|
101
|
-
* const dataset3 = new BST<number, string>([
|
|
102
|
-
* [3, 'C'],
|
|
103
|
-
* [5, 'E'],
|
|
104
|
-
* [4, 'D']
|
|
105
|
-
* ]);
|
|
106
|
-
*
|
|
107
|
-
* // Merge datasets into a single BinarySearchTree
|
|
108
|
-
* const merged = new BST<number, string>(dataset1);
|
|
109
|
-
* merged.addMany(dataset2);
|
|
110
|
-
* merged.merge(dataset3);
|
|
111
|
-
*
|
|
112
|
-
* // Verify merged dataset is in sorted order
|
|
113
|
-
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
|
114
|
-
* @example
|
|
115
|
-
* // Find elements in a range
|
|
116
|
-
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
117
|
-
* console.log(bst.search(new Range(5, 10))); // [5, 7, 10]
|
|
118
|
-
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['5', '7', '10', '12']
|
|
119
|
-
* console.log(bst.search(new Range(4, 12, true, false))); // [5, 7, 10]
|
|
120
|
-
* console.log(bst.rangeSearch([15, 20])); // [15, 18]
|
|
121
|
-
* console.log(bst.search(new Range(15, 20, false))); // [18]
|
|
122
|
-
* @example
|
|
123
|
-
* // Find lowest common ancestor
|
|
124
|
-
* const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
125
|
-
*
|
|
126
|
-
* // LCA helper function
|
|
127
|
-
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
128
|
-
* const path1 = bst.getPathToRoot(num1);
|
|
129
|
-
* const path2 = bst.getPathToRoot(num2);
|
|
130
|
-
* // Find the first common ancestor
|
|
131
|
-
* return findFirstCommon(path1, path2);
|
|
132
|
-
* };
|
|
133
|
-
*
|
|
134
|
-
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
|
|
135
|
-
* for (const num of arr1) {
|
|
136
|
-
* if (arr2.indexOf(num) !== -1) {
|
|
137
|
-
* return num;
|
|
138
|
-
* }
|
|
139
|
-
* }
|
|
140
|
-
* return undefined;
|
|
141
|
-
* }
|
|
142
|
-
*
|
|
143
|
-
* // Assertions
|
|
144
|
-
* console.log(findLCA(3, 10)); // 7
|
|
145
|
-
* console.log(findLCA(5, 35)); // 15
|
|
146
|
-
* console.log(findLCA(20, 30)); // 25
|
|
147
|
-
*/
|
|
148
|
-
class BST extends binary_tree_1.BinaryTree {
|
|
149
|
-
/**
|
|
150
|
-
* Creates an instance of BST.
|
|
151
|
-
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
152
|
-
*
|
|
153
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
|
|
154
|
-
* @param [options] - Configuration options for the BST, including comparator.
|
|
155
|
-
*/
|
|
156
|
-
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
157
|
-
super([], options);
|
|
158
|
-
this._root = undefined;
|
|
159
|
-
this._isReverse = false;
|
|
160
|
-
/**
|
|
161
|
-
* The default comparator function.
|
|
162
|
-
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
163
|
-
*/
|
|
164
|
-
this._comparator = (a, b) => {
|
|
165
|
-
if ((0, utils_1.isComparable)(a) && (0, utils_1.isComparable)(b)) {
|
|
166
|
-
if (a > b)
|
|
167
|
-
return 1;
|
|
168
|
-
if (a < b)
|
|
169
|
-
return -1;
|
|
170
|
-
return 0;
|
|
171
|
-
}
|
|
172
|
-
if (this._specifyComparable) {
|
|
173
|
-
const va = this._specifyComparable(a);
|
|
174
|
-
const vb = this._specifyComparable(b);
|
|
175
|
-
if (va > vb)
|
|
176
|
-
return 1;
|
|
177
|
-
if (va < vb)
|
|
178
|
-
return -1;
|
|
179
|
-
return 0;
|
|
180
|
-
}
|
|
181
|
-
if (typeof a === 'object' || typeof b === 'object') {
|
|
182
|
-
throw TypeError(`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`);
|
|
183
|
-
}
|
|
184
|
-
return 0;
|
|
185
|
-
};
|
|
186
|
-
if (options) {
|
|
187
|
-
const { specifyComparable, isReverse } = options;
|
|
188
|
-
if (typeof specifyComparable === 'function')
|
|
189
|
-
this._specifyComparable = specifyComparable;
|
|
190
|
-
if (isReverse !== undefined)
|
|
191
|
-
this._isReverse = isReverse;
|
|
192
|
-
}
|
|
193
|
-
if (keysNodesEntriesOrRaws)
|
|
194
|
-
this.addMany(keysNodesEntriesOrRaws);
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Gets the root node of the tree.
|
|
198
|
-
* @remarks Time O(1)
|
|
199
|
-
*
|
|
200
|
-
* @returns The root node.
|
|
201
|
-
*/
|
|
202
|
-
get root() {
|
|
203
|
-
return this._root;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Gets whether the tree's comparison logic is reversed.
|
|
207
|
-
* @remarks Time O(1)
|
|
208
|
-
*
|
|
209
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
210
|
-
*/
|
|
211
|
-
get isReverse() {
|
|
212
|
-
return this._isReverse;
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Gets the comparator function used by the tree.
|
|
216
|
-
* @remarks Time O(1)
|
|
217
|
-
*
|
|
218
|
-
* @returns The comparator function.
|
|
219
|
-
*/
|
|
220
|
-
get comparator() {
|
|
221
|
-
return this._comparator;
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
225
|
-
* @remarks Time O(1)
|
|
226
|
-
*
|
|
227
|
-
* @returns The key-to-comparable conversion function.
|
|
228
|
-
*/
|
|
229
|
-
get specifyComparable() {
|
|
230
|
-
return this._specifyComparable;
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* (Protected) Creates a new BST node.
|
|
234
|
-
* @remarks Time O(1), Space O(1)
|
|
235
|
-
*
|
|
236
|
-
* @param key - The key for the new node.
|
|
237
|
-
* @param [value] - The value for the new node (used if not in Map mode).
|
|
238
|
-
* @returns The newly created BSTNode.
|
|
239
|
-
*/
|
|
240
|
-
_createNode(key, value) {
|
|
241
|
-
return new BSTNode(key, this._isMapMode ? undefined : value);
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Ensures the input is a node. If it's a key or entry, it searches for the node.
|
|
245
|
-
* @remarks Time O(log N) (height of the tree), O(N) worst-case.
|
|
246
|
-
*
|
|
247
|
-
* @param keyNodeOrEntry - The item to resolve to a node.
|
|
248
|
-
* @param [iterationType=this.iterationType] - The traversal method to use if searching.
|
|
249
|
-
* @returns The resolved node, or undefined if not found.
|
|
250
|
-
*/
|
|
251
|
-
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
252
|
-
var _a;
|
|
253
|
-
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) !== null && _a !== void 0 ? _a : undefined;
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Checks if the given item is a `BSTNode` instance.
|
|
257
|
-
* @remarks Time O(1), Space O(1)
|
|
258
|
-
*
|
|
259
|
-
* @param keyNodeOrEntry - The item to check.
|
|
260
|
-
* @returns True if it's a BSTNode, false otherwise.
|
|
261
|
-
*/
|
|
262
|
-
isNode(keyNodeOrEntry) {
|
|
263
|
-
return keyNodeOrEntry instanceof BSTNode;
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Checks if the given key is valid (comparable).
|
|
267
|
-
* @remarks Time O(1)
|
|
268
|
-
*
|
|
269
|
-
* @param key - The key to validate.
|
|
270
|
-
* @returns True if the key is valid, false otherwise.
|
|
271
|
-
*/
|
|
272
|
-
isValidKey(key) {
|
|
273
|
-
return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined);
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Performs a Depth-First Search (DFS) traversal.
|
|
277
|
-
* @remarks Time O(N), visits every node. Space O(log N) for the call/explicit stack. O(N) worst-case.
|
|
278
|
-
*
|
|
279
|
-
* @template C - The type of the callback function.
|
|
280
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
281
|
-
* @param [pattern='IN'] - The traversal order ('IN', 'PRE', 'POST').
|
|
282
|
-
* @param [onlyOne=false] - If true, stops after the first callback.
|
|
283
|
-
* @param [startNode=this._root] - The node to start from.
|
|
284
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
285
|
-
* @returns An array of callback results.
|
|
286
|
-
*/
|
|
287
|
-
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
288
|
-
return super.dfs(callback, pattern, onlyOne, startNode, iterationType);
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Performs a Breadth-First Search (BFS) or Level-Order traversal.
|
|
292
|
-
* @remarks Time O(N), visits every node. Space O(N) in the worst case for the queue.
|
|
293
|
-
*
|
|
294
|
-
* @template C - The type of the callback function.
|
|
295
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
296
|
-
* @param [startNode=this._root] - The node to start from.
|
|
297
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
298
|
-
* @returns An array of callback results.
|
|
299
|
-
*/
|
|
300
|
-
bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
301
|
-
return super.bfs(callback, startNode, iterationType, false);
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Returns a 2D array of nodes, grouped by level.
|
|
305
|
-
* @remarks Time O(N), visits every node. Space O(N) for the result array and the queue/stack.
|
|
306
|
-
*
|
|
307
|
-
* @template C - The type of the callback function.
|
|
308
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
309
|
-
* @param [startNode=this._root] - The node to start from.
|
|
310
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
311
|
-
* @returns A 2D array of callback results.
|
|
312
|
-
*/
|
|
313
|
-
listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
314
|
-
return super.listLevels(callback, startNode, iterationType, false);
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Gets the first node matching a predicate.
|
|
318
|
-
* @remarks Time O(log N) if searching by key, O(N) if searching by predicate. Space O(log N) or O(N).
|
|
319
|
-
*
|
|
320
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
321
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
322
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
323
|
-
* @returns The first matching node, or undefined if not found.
|
|
324
|
-
*/
|
|
325
|
-
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
326
|
-
var _a;
|
|
327
|
-
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* Searches the tree for nodes matching a predicate, key, or range.
|
|
331
|
-
* @remarks This is an optimized search for a BST. If searching by key or range, it prunes branches.
|
|
332
|
-
* Time O(H + M) for key/range search (H=height, M=matches). O(N) for predicate search.
|
|
333
|
-
* Space O(log N) for the stack.
|
|
334
|
-
*
|
|
335
|
-
* @template C - The type of the callback function.
|
|
336
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, predicate, or range to search for.
|
|
337
|
-
* @param [onlyOne=false] - If true, stops after finding the first match.
|
|
338
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
339
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
340
|
-
* @param [iterationType=this.iterationType] - Whether to use 'RECURSIVE' or 'ITERATIVE' search.
|
|
341
|
-
* @returns An array of results from the callback function for each matching node.
|
|
342
|
-
*/
|
|
343
|
-
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
344
|
-
if (keyNodeEntryOrPredicate === undefined)
|
|
345
|
-
return [];
|
|
346
|
-
if (keyNodeEntryOrPredicate === null)
|
|
347
|
-
return [];
|
|
348
|
-
startNode = this.ensureNode(startNode);
|
|
349
|
-
if (!startNode)
|
|
350
|
-
return [];
|
|
351
|
-
let predicate;
|
|
352
|
-
const isRange = this.isRange(keyNodeEntryOrPredicate);
|
|
353
|
-
if (isRange) {
|
|
354
|
-
predicate = node => {
|
|
355
|
-
if (!node)
|
|
356
|
-
return false;
|
|
357
|
-
return keyNodeEntryOrPredicate.isInRange(node.key, this._comparator);
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
else {
|
|
361
|
-
predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
362
|
-
}
|
|
363
|
-
// Optimization: Pruning logic
|
|
364
|
-
const shouldVisitLeft = (cur) => {
|
|
365
|
-
if (!cur)
|
|
366
|
-
return false;
|
|
367
|
-
if (!this.isRealNode(cur.left))
|
|
368
|
-
return false;
|
|
369
|
-
if (isRange) {
|
|
370
|
-
// Range search: Only go left if the current key is >= the lower bound
|
|
371
|
-
const range = keyNodeEntryOrPredicate;
|
|
372
|
-
const leftS = this.isReverse ? range.high : range.low;
|
|
373
|
-
const leftI = this.isReverse ? range.includeHigh : range.includeLow;
|
|
374
|
-
return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
|
|
375
|
-
}
|
|
376
|
-
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
377
|
-
// Key search: Only go left if current key > target key
|
|
378
|
-
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
|
|
379
|
-
return benchmarkKey !== null && benchmarkKey !== undefined && this._compare(cur.key, benchmarkKey) > 0;
|
|
380
|
-
}
|
|
381
|
-
return true; // Predicate search: must visit all
|
|
382
|
-
};
|
|
383
|
-
const shouldVisitRight = (cur) => {
|
|
384
|
-
if (!cur)
|
|
385
|
-
return false;
|
|
386
|
-
if (!this.isRealNode(cur.right))
|
|
387
|
-
return false;
|
|
388
|
-
if (isRange) {
|
|
389
|
-
// Range search: Only go right if current key <= upper bound
|
|
390
|
-
const range = keyNodeEntryOrPredicate;
|
|
391
|
-
const rightS = this.isReverse ? range.low : range.high;
|
|
392
|
-
const rightI = this.isReverse ? range.includeLow : range.includeHigh;
|
|
393
|
-
return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
|
|
394
|
-
}
|
|
395
|
-
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
396
|
-
// Key search: Only go right if current key < target key
|
|
397
|
-
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
|
|
398
|
-
return benchmarkKey !== null && benchmarkKey !== undefined && this._compare(cur.key, benchmarkKey) < 0;
|
|
399
|
-
}
|
|
400
|
-
return true; // Predicate search: must visit all
|
|
401
|
-
};
|
|
402
|
-
return super._dfs(callback, 'IN', // In-order is efficient for range/key search
|
|
403
|
-
onlyOne, startNode, iterationType, false, shouldVisitLeft, shouldVisitRight, () => true, // shouldVisitRoot (always visit)
|
|
404
|
-
// shouldVisitRoot (always visit)
|
|
405
|
-
cur => !!cur && predicate(cur) // shouldProcessRoot (only process if predicate matches)
|
|
406
|
-
);
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Performs an optimized search for nodes within a given key range.
|
|
410
|
-
* @remarks Time O(H + M), where H is tree height and M is the number of matches.
|
|
411
|
-
*
|
|
412
|
-
* @template C - The type of the callback function.
|
|
413
|
-
* @param range - A `Range` object or a `[low, high]` tuple.
|
|
414
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
415
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
416
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
417
|
-
* @returns An array of callback results.
|
|
418
|
-
*/
|
|
419
|
-
rangeSearch(range, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
420
|
-
const searchRange = range instanceof common_1.Range ? range : new common_1.Range(range[0], range[1]);
|
|
421
|
-
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* Adds a new node to the BST based on key comparison.
|
|
425
|
-
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
426
|
-
*
|
|
427
|
-
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
428
|
-
* @param [value] - The value, if providing just a key.
|
|
429
|
-
* @returns True if the addition was successful, false otherwise.
|
|
430
|
-
*/
|
|
431
|
-
add(keyNodeOrEntry, value) {
|
|
432
|
-
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
433
|
-
if (newNode === undefined)
|
|
434
|
-
return false;
|
|
435
|
-
if (this._root === undefined) {
|
|
436
|
-
this._setRoot(newNode);
|
|
437
|
-
if (this._isMapMode)
|
|
438
|
-
this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
|
|
439
|
-
this._size++;
|
|
440
|
-
return true;
|
|
441
|
-
}
|
|
442
|
-
let current = this._root;
|
|
443
|
-
while (current !== undefined) {
|
|
444
|
-
if (this._compare(current.key, newNode.key) === 0) {
|
|
445
|
-
// Key exists, replace node
|
|
446
|
-
this._replaceNode(current, newNode);
|
|
447
|
-
if (this._isMapMode)
|
|
448
|
-
this._setValue(current.key, newValue);
|
|
449
|
-
return true;
|
|
450
|
-
}
|
|
451
|
-
else if (this._compare(current.key, newNode.key) > 0) {
|
|
452
|
-
// Go left
|
|
453
|
-
if (current.left === undefined) {
|
|
454
|
-
current.left = newNode;
|
|
455
|
-
if (this._isMapMode)
|
|
456
|
-
this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
|
|
457
|
-
this._size++;
|
|
458
|
-
return true;
|
|
459
|
-
}
|
|
460
|
-
if (current.left !== null)
|
|
461
|
-
current = current.left;
|
|
462
|
-
}
|
|
463
|
-
else {
|
|
464
|
-
// Go right
|
|
465
|
-
if (current.right === undefined) {
|
|
466
|
-
current.right = newNode;
|
|
467
|
-
if (this._isMapMode)
|
|
468
|
-
this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
|
|
469
|
-
this._size++;
|
|
470
|
-
return true;
|
|
471
|
-
}
|
|
472
|
-
if (current.right !== null)
|
|
473
|
-
current = current.right;
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
return false;
|
|
477
|
-
}
|
|
478
|
-
/**
|
|
479
|
-
* Adds multiple items to the tree.
|
|
480
|
-
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced add).
|
|
481
|
-
* If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
|
|
482
|
-
* Space O(N) for sorting and recursion/iteration stack.
|
|
483
|
-
*
|
|
484
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to add.
|
|
485
|
-
* @param [values] - An optional parallel iterable of values.
|
|
486
|
-
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
487
|
-
* @param [iterationType=this.iterationType] - The traversal method for balanced add (recursive or iterative).
|
|
488
|
-
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
489
|
-
*/
|
|
490
|
-
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
491
|
-
const inserted = [];
|
|
492
|
-
const valuesIterator = values === null || values === void 0 ? void 0 : values[Symbol.iterator]();
|
|
493
|
-
if (!isBalanceAdd) {
|
|
494
|
-
// Standard O(N*H) insertion
|
|
495
|
-
for (let kve of keysNodesEntriesOrRaws) {
|
|
496
|
-
const val = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
497
|
-
if (this.isRaw(kve))
|
|
498
|
-
kve = this._toEntryFn(kve);
|
|
499
|
-
inserted.push(this.add(kve, val));
|
|
500
|
-
}
|
|
501
|
-
return inserted;
|
|
502
|
-
}
|
|
503
|
-
// Balanced O(N log N) insertion
|
|
504
|
-
const realBTNExemplars = [];
|
|
505
|
-
let i = 0;
|
|
506
|
-
for (const kve of keysNodesEntriesOrRaws) {
|
|
507
|
-
realBTNExemplars.push({ key: kve, value: valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value, orgIndex: i++ });
|
|
508
|
-
}
|
|
509
|
-
// Sort items by key
|
|
510
|
-
const sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
511
|
-
let keyA, keyB;
|
|
512
|
-
if (this.isRaw(a))
|
|
513
|
-
keyA = this._toEntryFn(a)[0];
|
|
514
|
-
else if (this.isEntry(a))
|
|
515
|
-
keyA = a[0];
|
|
516
|
-
else if (this.isRealNode(a))
|
|
517
|
-
keyA = a.key;
|
|
518
|
-
else
|
|
519
|
-
keyA = a;
|
|
520
|
-
if (this.isRaw(b))
|
|
521
|
-
keyB = this._toEntryFn(b)[0];
|
|
522
|
-
else if (this.isEntry(b))
|
|
523
|
-
keyB = b[0];
|
|
524
|
-
else if (this.isRealNode(b))
|
|
525
|
-
keyB = b.key;
|
|
526
|
-
else
|
|
527
|
-
keyB = b;
|
|
528
|
-
if (keyA != null && keyB != null)
|
|
529
|
-
return this._compare(keyA, keyB);
|
|
530
|
-
return 0;
|
|
531
|
-
});
|
|
532
|
-
// Recursive balanced build
|
|
533
|
-
const _dfs = (arr) => {
|
|
534
|
-
if (arr.length === 0)
|
|
535
|
-
return;
|
|
536
|
-
const mid = Math.floor((arr.length - 1) / 2);
|
|
537
|
-
const { key, value, orgIndex } = arr[mid];
|
|
538
|
-
if (this.isRaw(key)) {
|
|
539
|
-
const entry = this._toEntryFn(key);
|
|
540
|
-
inserted[orgIndex] = this.add(entry);
|
|
541
|
-
}
|
|
542
|
-
else {
|
|
543
|
-
inserted[orgIndex] = this.add(key, value);
|
|
544
|
-
}
|
|
545
|
-
_dfs(arr.slice(0, mid));
|
|
546
|
-
_dfs(arr.slice(mid + 1));
|
|
547
|
-
};
|
|
548
|
-
// Iterative balanced build
|
|
549
|
-
const _iterate = () => {
|
|
550
|
-
const n = sorted.length;
|
|
551
|
-
const stack = [[0, n - 1]];
|
|
552
|
-
while (stack.length > 0) {
|
|
553
|
-
const popped = stack.pop();
|
|
554
|
-
if (!popped)
|
|
555
|
-
continue;
|
|
556
|
-
const [l, r] = popped;
|
|
557
|
-
if (l > r)
|
|
558
|
-
continue;
|
|
559
|
-
const m = l + Math.floor((r - l) / 2);
|
|
560
|
-
const { key, value, orgIndex } = sorted[m];
|
|
561
|
-
if (this.isRaw(key)) {
|
|
562
|
-
const entry = this._toEntryFn(key);
|
|
563
|
-
inserted[orgIndex] = this.add(entry);
|
|
564
|
-
}
|
|
565
|
-
else {
|
|
566
|
-
inserted[orgIndex] = this.add(key, value);
|
|
567
|
-
}
|
|
568
|
-
stack.push([m + 1, r]);
|
|
569
|
-
stack.push([l, m - 1]);
|
|
570
|
-
}
|
|
571
|
-
};
|
|
572
|
-
if (iterationType === 'RECURSIVE')
|
|
573
|
-
_dfs(sorted);
|
|
574
|
-
else
|
|
575
|
-
_iterate();
|
|
576
|
-
return inserted;
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
580
|
-
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
581
|
-
*
|
|
582
|
-
* @template C - The type of the callback function.
|
|
583
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on matching nodes.
|
|
584
|
-
* @param [lesserOrGreater=-1] - -1 for lesser, 1 for greater, 0 for equal.
|
|
585
|
-
* @param [targetNode=this._root] - The node to compare against.
|
|
586
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
587
|
-
* @returns An array of callback results.
|
|
588
|
-
*/
|
|
589
|
-
lesserOrGreaterTraverse(callback = this._DEFAULT_NODE_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
|
|
590
|
-
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
591
|
-
const ans = [];
|
|
592
|
-
if (!this._root || !targetNodeEnsured)
|
|
593
|
-
return ans;
|
|
594
|
-
const targetKey = targetNodeEnsured.key;
|
|
595
|
-
if (iterationType === 'RECURSIVE') {
|
|
596
|
-
const dfs = (cur) => {
|
|
597
|
-
const compared = this._compare(cur.key, targetKey);
|
|
598
|
-
if (Math.sign(compared) == lesserOrGreater)
|
|
599
|
-
ans.push(callback(cur));
|
|
600
|
-
if (this.isRealNode(cur.left))
|
|
601
|
-
dfs(cur.left);
|
|
602
|
-
if (this.isRealNode(cur.right))
|
|
603
|
-
dfs(cur.right);
|
|
604
|
-
};
|
|
605
|
-
dfs(this._root);
|
|
606
|
-
return ans;
|
|
607
|
-
}
|
|
608
|
-
else {
|
|
609
|
-
const queue = new queue_1.Queue([this._root]);
|
|
610
|
-
while (queue.length > 0) {
|
|
611
|
-
const cur = queue.shift();
|
|
612
|
-
if (this.isRealNode(cur)) {
|
|
613
|
-
const compared = this._compare(cur.key, targetKey);
|
|
614
|
-
if (Math.sign(compared) == lesserOrGreater)
|
|
615
|
-
ans.push(callback(cur));
|
|
616
|
-
if (this.isRealNode(cur.left))
|
|
617
|
-
queue.push(cur.left);
|
|
618
|
-
if (this.isRealNode(cur.right))
|
|
619
|
-
queue.push(cur.right);
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
return ans;
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
/**
|
|
626
|
-
* Rebuilds the tree to be perfectly balanced.
|
|
627
|
-
* @remarks Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
628
|
-
*
|
|
629
|
-
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
630
|
-
* @returns True if successful, false if the tree was empty.
|
|
631
|
-
*/
|
|
632
|
-
perfectlyBalance(iterationType = this.iterationType) {
|
|
633
|
-
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
|
|
634
|
-
const n = nodes.length;
|
|
635
|
-
this._clearNodes();
|
|
636
|
-
if (n === 0)
|
|
637
|
-
return false;
|
|
638
|
-
// Build balanced tree from sorted array
|
|
639
|
-
const build = (l, r, parent) => {
|
|
640
|
-
if (l > r)
|
|
641
|
-
return undefined;
|
|
642
|
-
const m = l + ((r - l) >> 1);
|
|
643
|
-
const root = nodes[m];
|
|
644
|
-
const leftChild = build(l, m - 1, root);
|
|
645
|
-
const rightChild = build(m + 1, r, root);
|
|
646
|
-
root.left = leftChild;
|
|
647
|
-
root.right = rightChild;
|
|
648
|
-
root.parent = parent;
|
|
649
|
-
return root;
|
|
650
|
-
};
|
|
651
|
-
const newRoot = build(0, n - 1, undefined);
|
|
652
|
-
this._setRoot(newRoot);
|
|
653
|
-
this._size = n;
|
|
654
|
-
return true;
|
|
655
|
-
}
|
|
656
|
-
/**
|
|
657
|
-
* Checks if the tree meets the AVL balance condition (height difference <= 1).
|
|
658
|
-
* @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.
|
|
659
|
-
*
|
|
660
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
661
|
-
* @returns True if the tree is AVL balanced, false otherwise.
|
|
662
|
-
*/
|
|
663
|
-
isAVLBalanced(iterationType = this.iterationType) {
|
|
664
|
-
if (!this._root)
|
|
665
|
-
return true;
|
|
666
|
-
let balanced = true;
|
|
667
|
-
if (iterationType === 'RECURSIVE') {
|
|
668
|
-
// Recursive height check
|
|
669
|
-
const _height = (cur) => {
|
|
670
|
-
if (!cur)
|
|
671
|
-
return 0;
|
|
672
|
-
const leftHeight = _height(cur.left);
|
|
673
|
-
const rightHeight = _height(cur.right);
|
|
674
|
-
if (Math.abs(leftHeight - rightHeight) > 1)
|
|
675
|
-
balanced = false;
|
|
676
|
-
return Math.max(leftHeight, rightHeight) + 1;
|
|
677
|
-
};
|
|
678
|
-
_height(this._root);
|
|
679
|
-
}
|
|
680
|
-
else {
|
|
681
|
-
// Iterative post-order height check
|
|
682
|
-
const stack = [];
|
|
683
|
-
let node = this._root, last = undefined;
|
|
684
|
-
const depths = new Map();
|
|
685
|
-
while (stack.length > 0 || node) {
|
|
686
|
-
if (node) {
|
|
687
|
-
stack.push(node);
|
|
688
|
-
if (node.left !== null)
|
|
689
|
-
node = node.left;
|
|
690
|
-
}
|
|
691
|
-
else {
|
|
692
|
-
node = stack[stack.length - 1];
|
|
693
|
-
if (!node.right || last === node.right) {
|
|
694
|
-
node = stack.pop();
|
|
695
|
-
if (node) {
|
|
696
|
-
const left = node.left ? depths.get(node.left) : -1;
|
|
697
|
-
const right = node.right ? depths.get(node.right) : -1;
|
|
698
|
-
if (Math.abs(left - right) > 1)
|
|
699
|
-
return false;
|
|
700
|
-
depths.set(node, 1 + Math.max(left, right));
|
|
701
|
-
last = node;
|
|
702
|
-
node = undefined;
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
else
|
|
706
|
-
node = node.right;
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
return balanced;
|
|
711
|
-
}
|
|
712
|
-
/**
|
|
713
|
-
* Creates a new BST by mapping each [key, value] pair to a new entry.
|
|
714
|
-
* @remarks Time O(N * H), where N is nodes in this tree, and H is height of the new tree during insertion.
|
|
715
|
-
* Space O(N) for the new tree.
|
|
716
|
-
*
|
|
717
|
-
* @template MK - New key type.
|
|
718
|
-
* @template MV - New value type.
|
|
719
|
-
* @template MR - New raw type.
|
|
720
|
-
* @param callback - A function to map each [key, value] pair.
|
|
721
|
-
* @param [options] - Options for the new BST.
|
|
722
|
-
* @param [thisArg] - `this` context for the callback.
|
|
723
|
-
* @returns A new, mapped BST.
|
|
724
|
-
*/
|
|
725
|
-
map(callback, options, thisArg) {
|
|
726
|
-
const out = this._createLike([], options);
|
|
727
|
-
let index = 0;
|
|
728
|
-
// Iterates in-order
|
|
729
|
-
for (const [key, value] of this) {
|
|
730
|
-
out.add(callback.call(thisArg, key, value, index++, this));
|
|
731
|
-
}
|
|
732
|
-
return out;
|
|
733
|
-
}
|
|
734
|
-
/**
|
|
735
|
-
* Deletes the first node found that satisfies the predicate.
|
|
736
|
-
* @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
|
|
737
|
-
*
|
|
738
|
-
* @param predicate - A function to test each [key, value] pair.
|
|
739
|
-
* @returns True if a node was deleted, false otherwise.
|
|
740
|
-
*/
|
|
741
|
-
deleteWhere(predicate) {
|
|
742
|
-
const stack = [];
|
|
743
|
-
let cur = this._root;
|
|
744
|
-
let index = 0;
|
|
745
|
-
// In-order traversal to find the node
|
|
746
|
-
while (stack.length > 0 || cur !== undefined) {
|
|
747
|
-
while (cur !== undefined && cur !== null) {
|
|
748
|
-
stack.push(cur);
|
|
749
|
-
cur = cur.left;
|
|
750
|
-
}
|
|
751
|
-
const node = stack.pop();
|
|
752
|
-
if (!node)
|
|
753
|
-
break;
|
|
754
|
-
const key = node.key;
|
|
755
|
-
const val = node.value;
|
|
756
|
-
if (predicate(key, val, index++, this)) {
|
|
757
|
-
return this._deleteByKey(key); // Found, now delete
|
|
758
|
-
}
|
|
759
|
-
cur = node.right;
|
|
760
|
-
}
|
|
761
|
-
return false;
|
|
762
|
-
}
|
|
763
|
-
/**
|
|
764
|
-
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
765
|
-
* @remarks Time O(1)
|
|
766
|
-
*
|
|
767
|
-
* @template TK, TV, TR - Generic types for the new instance.
|
|
768
|
-
* @param [options] - Options for the new BST.
|
|
769
|
-
* @returns A new, empty BST.
|
|
770
|
-
*/
|
|
771
|
-
_createInstance(options) {
|
|
772
|
-
const Ctor = this.constructor;
|
|
773
|
-
return new Ctor([], Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
|
|
774
|
-
}
|
|
775
|
-
/**
|
|
776
|
-
* (Protected) Creates a new instance of the same BST constructor, potentially with different generic types.
|
|
777
|
-
* @remarks Time O(N log N) or O(N^2) (from constructor) due to processing the iterable.
|
|
778
|
-
*
|
|
779
|
-
* @template TK, TV, TR - Generic types for the new instance.
|
|
780
|
-
* @param [iter=[]] - An iterable to populate the new BST.
|
|
781
|
-
* @param [options] - Options for the new BST.
|
|
782
|
-
* @returns A new BST.
|
|
783
|
-
*/
|
|
784
|
-
_createLike(iter = [], options) {
|
|
785
|
-
const Ctor = this.constructor;
|
|
786
|
-
return new Ctor(iter, Object.assign(Object.assign({}, this._snapshotOptions()), (options !== null && options !== void 0 ? options : {})));
|
|
787
|
-
}
|
|
788
|
-
/**
|
|
789
|
-
* (Protected) Snapshots the current BST's configuration options.
|
|
790
|
-
* @remarks Time O(1)
|
|
791
|
-
*
|
|
792
|
-
* @template TK, TV, TR - Generic types for the options.
|
|
793
|
-
* @returns The options object.
|
|
794
|
-
*/
|
|
795
|
-
_snapshotOptions() {
|
|
796
|
-
return Object.assign(Object.assign({}, super._snapshotOptions()), { specifyComparable: this.specifyComparable, isReverse: this.isReverse });
|
|
797
|
-
}
|
|
798
|
-
/**
|
|
799
|
-
* (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
800
|
-
* @remarks Time O(1)
|
|
801
|
-
*
|
|
802
|
-
* @param keyNodeOrEntry - The input item.
|
|
803
|
-
* @param [value] - An optional value (used if input is just a key).
|
|
804
|
-
* @returns A tuple of [node, value].
|
|
805
|
-
*/
|
|
806
|
-
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
807
|
-
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
808
|
-
if (node === null)
|
|
809
|
-
return [undefined, undefined]; // BST handles null differently (as undefined)
|
|
810
|
-
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
811
|
-
}
|
|
812
|
-
/**
|
|
813
|
-
* (Protected) Sets the root node and clears its parent reference.
|
|
814
|
-
* @remarks Time O(1)
|
|
815
|
-
*
|
|
816
|
-
* @param v - The node to set as root.
|
|
817
|
-
*/
|
|
818
|
-
_setRoot(v) {
|
|
819
|
-
if (v)
|
|
820
|
-
v.parent = undefined;
|
|
821
|
-
this._root = v;
|
|
822
|
-
}
|
|
823
|
-
/**
|
|
824
|
-
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
825
|
-
* @remarks Time O(1) (or O(C) if `specifyComparable` is used).
|
|
826
|
-
*
|
|
827
|
-
* @param a - The first key.
|
|
828
|
-
* @param b - The second key.
|
|
829
|
-
* @returns A number (1, -1, or 0) representing the comparison.
|
|
830
|
-
*/
|
|
831
|
-
_compare(a, b) {
|
|
832
|
-
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
|
|
833
|
-
}
|
|
834
|
-
/**
|
|
835
|
-
* (Private) Deletes a node by its key.
|
|
836
|
-
* @remarks Standard BST deletion algorithm. Time O(log N), O(N) worst-case. Space O(1).
|
|
837
|
-
*
|
|
838
|
-
* @param key - The key of the node to delete.
|
|
839
|
-
* @returns True if the node was found and deleted, false otherwise.
|
|
840
|
-
*/
|
|
841
|
-
_deleteByKey(key) {
|
|
842
|
-
var _a;
|
|
843
|
-
let node = this._root;
|
|
844
|
-
// 1. Find the node
|
|
845
|
-
while (node) {
|
|
846
|
-
const cmp = this._compare(node.key, key);
|
|
847
|
-
if (cmp === 0)
|
|
848
|
-
break;
|
|
849
|
-
node = cmp > 0 ? node.left : node.right;
|
|
850
|
-
}
|
|
851
|
-
if (!node)
|
|
852
|
-
return false; // Not found
|
|
853
|
-
// Helper to replace node `u` with node `v`
|
|
854
|
-
const transplant = (u, v) => {
|
|
855
|
-
const p = u === null || u === void 0 ? void 0 : u.parent;
|
|
856
|
-
if (!p) {
|
|
857
|
-
this._setRoot(v);
|
|
858
|
-
}
|
|
859
|
-
else if (p.left === u) {
|
|
860
|
-
p.left = v;
|
|
861
|
-
}
|
|
862
|
-
else {
|
|
863
|
-
p.right = v;
|
|
864
|
-
}
|
|
865
|
-
if (v)
|
|
866
|
-
v.parent = p;
|
|
867
|
-
};
|
|
868
|
-
// Helper to find the minimum node in a subtree
|
|
869
|
-
const minNode = (x) => {
|
|
870
|
-
if (!x)
|
|
871
|
-
return undefined;
|
|
872
|
-
while (x.left !== undefined && x.left !== null)
|
|
873
|
-
x = x.left;
|
|
874
|
-
return x;
|
|
875
|
-
};
|
|
876
|
-
// 2. Perform deletion
|
|
877
|
-
if (node.left === undefined) {
|
|
878
|
-
// Case 1: No left child
|
|
879
|
-
transplant(node, node.right);
|
|
880
|
-
}
|
|
881
|
-
else if (node.right === undefined) {
|
|
882
|
-
// Case 2: No right child
|
|
883
|
-
transplant(node, node.left);
|
|
884
|
-
}
|
|
885
|
-
else {
|
|
886
|
-
// Case 3: Two children
|
|
887
|
-
const succ = minNode(node.right); // Find successor
|
|
888
|
-
if (succ.parent !== node) {
|
|
889
|
-
transplant(succ, succ.right);
|
|
890
|
-
succ.right = node.right;
|
|
891
|
-
if (succ.right)
|
|
892
|
-
succ.right.parent = succ;
|
|
893
|
-
}
|
|
894
|
-
transplant(node, succ);
|
|
895
|
-
succ.left = node.left;
|
|
896
|
-
if (succ.left)
|
|
897
|
-
succ.left.parent = succ;
|
|
898
|
-
}
|
|
899
|
-
this._size = Math.max(0, ((_a = this._size) !== null && _a !== void 0 ? _a : 0) - 1);
|
|
900
|
-
return true;
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
exports.BST = BST;
|