red-black-tree-typed 1.53.6 → 1.54.1
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/README.md +52 -0
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +28 -0
- package/dist/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -331
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +131 -71
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +309 -208
- package/dist/data-structures/binary-tree/binary-tree.js +382 -300
- package/dist/data-structures/binary-tree/bst.d.ts +245 -127
- package/dist/data-structures/binary-tree/bst.js +366 -163
- package/dist/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/data-structures/binary-tree/index.js +3 -1
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +181 -108
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
- package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/hash/hash-map.d.ts +31 -1
- package/dist/data-structures/hash/hash-map.js +35 -5
- package/dist/data-structures/heap/heap.d.ts +26 -9
- package/dist/data-structures/heap/heap.js +37 -17
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +64 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +92 -31
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +48 -12
- package/dist/data-structures/linked-list/singly-linked-list.js +74 -27
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +111 -10
- package/dist/data-structures/trie/trie.js +123 -18
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/index.js +2 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +25 -0
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +152 -373
- package/src/data-structures/binary-tree/avl-tree.ts +164 -106
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +563 -447
- package/src/data-structures/binary-tree/bst.ts +433 -237
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +224 -146
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/hash/hash-map.ts +37 -7
- package/src/data-structures/heap/heap.ts +72 -49
- package/src/data-structures/linked-list/doubly-linked-list.ts +186 -118
- package/src/data-structures/linked-list/singly-linked-list.ts +81 -28
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +123 -17
- package/src/index.ts +4 -3
- package/src/interfaces/binary-tree.ts +10 -21
- package/src/types/data-structures/base/base.ts +1 -1
- 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 +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -6
- package/src/types/data-structures/binary-tree/bst.ts +8 -7
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -205
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
|
@@ -4,44 +4,35 @@ exports.BST = exports.BSTNode = void 0;
|
|
|
4
4
|
const binary_tree_1 = require("./binary-tree");
|
|
5
5
|
const queue_1 = require("../queue");
|
|
6
6
|
const utils_1 = require("../../utils");
|
|
7
|
+
const common_1 = require("../../common");
|
|
7
8
|
class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
9
|
+
/**
|
|
10
|
+
* This TypeScript constructor function initializes an instance with a key and an optional value.
|
|
11
|
+
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
|
|
12
|
+
* within a data structure. It serves as a reference or identifier for accessing or manipulating the
|
|
13
|
+
* associated value.
|
|
14
|
+
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
|
|
15
|
+
* have to be provided when creating an instance of the class. If a value is not provided, it will
|
|
16
|
+
* default to `undefined`.
|
|
17
|
+
*/
|
|
8
18
|
constructor(key, value) {
|
|
9
19
|
super(key, value);
|
|
10
20
|
this.parent = undefined;
|
|
11
21
|
this._left = undefined;
|
|
12
22
|
this._right = undefined;
|
|
13
23
|
}
|
|
14
|
-
/**
|
|
15
|
-
* The function returns the value of the `_left` property.
|
|
16
|
-
* @returns The `_left` property of the current object is being returned.
|
|
17
|
-
*/
|
|
18
24
|
get left() {
|
|
19
25
|
return this._left;
|
|
20
26
|
}
|
|
21
|
-
/**
|
|
22
|
-
* The function sets the left child of a node and updates the parent reference of the child.
|
|
23
|
-
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
|
|
24
|
-
* instance of the `NODE` class or `undefined`.
|
|
25
|
-
*/
|
|
26
27
|
set left(v) {
|
|
27
28
|
if (v) {
|
|
28
29
|
v.parent = this;
|
|
29
30
|
}
|
|
30
31
|
this._left = v;
|
|
31
32
|
}
|
|
32
|
-
/**
|
|
33
|
-
* The function returns the right node of a binary tree or undefined if there is no right node.
|
|
34
|
-
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
35
|
-
* `undefined`.
|
|
36
|
-
*/
|
|
37
33
|
get right() {
|
|
38
34
|
return this._right;
|
|
39
35
|
}
|
|
40
|
-
/**
|
|
41
|
-
* The function sets the right child of a node and updates the parent reference of the child.
|
|
42
|
-
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
|
|
43
|
-
* `NODE` object or `undefined`.
|
|
44
|
-
*/
|
|
45
36
|
set right(v) {
|
|
46
37
|
if (v) {
|
|
47
38
|
v.parent = this;
|
|
@@ -58,57 +49,137 @@ exports.BSTNode = BSTNode;
|
|
|
58
49
|
* 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
|
|
59
50
|
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
60
51
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
52
|
+
* @example
|
|
53
|
+
* // Merge 3 sorted datasets
|
|
54
|
+
* const dataset1 = new BST<number, string>([
|
|
55
|
+
* [1, 'A'],
|
|
56
|
+
* [7, 'G']
|
|
57
|
+
* ]);
|
|
58
|
+
* const dataset2 = [
|
|
59
|
+
* [2, 'B'],
|
|
60
|
+
* [6, 'F']
|
|
61
|
+
* ];
|
|
62
|
+
* const dataset3 = new BST<number, string>([
|
|
63
|
+
* [3, 'C'],
|
|
64
|
+
* [5, 'E'],
|
|
65
|
+
* [4, 'D']
|
|
66
|
+
* ]);
|
|
67
|
+
*
|
|
68
|
+
* // Merge datasets into a single BinarySearchTree
|
|
69
|
+
* const merged = new BST<number, string>(dataset1);
|
|
70
|
+
* merged.addMany(dataset2);
|
|
71
|
+
* merged.merge(dataset3);
|
|
72
|
+
*
|
|
73
|
+
* // Verify merged dataset is in sorted order
|
|
74
|
+
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
|
75
|
+
* @example
|
|
76
|
+
* // Find elements in a range
|
|
77
|
+
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
78
|
+
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
|
|
79
|
+
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
|
|
80
|
+
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
|
|
81
|
+
* console.log(bst.rangeSearch([15, 20])); // [15, 18]
|
|
82
|
+
* console.log(bst.search(new Range(15, 20, false))); // [18]
|
|
83
|
+
* @example
|
|
84
|
+
* // Find lowest common ancestor
|
|
85
|
+
* const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
86
|
+
*
|
|
87
|
+
* // LCA helper function
|
|
88
|
+
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
89
|
+
* const path1 = bst.getPathToRoot(num1);
|
|
90
|
+
* const path2 = bst.getPathToRoot(num2);
|
|
91
|
+
* // Find the first common ancestor
|
|
92
|
+
* return findFirstCommon(path1, path2);
|
|
93
|
+
* };
|
|
94
|
+
*
|
|
95
|
+
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
|
|
96
|
+
* for (const num of arr1) {
|
|
97
|
+
* if (arr2.indexOf(num) !== -1) {
|
|
98
|
+
* return num;
|
|
99
|
+
* }
|
|
100
|
+
* }
|
|
101
|
+
* return undefined;
|
|
102
|
+
* }
|
|
103
|
+
*
|
|
104
|
+
* // Assertions
|
|
105
|
+
* console.log(findLCA(3, 10)); // 7
|
|
106
|
+
* console.log(findLCA(5, 35)); // 15
|
|
107
|
+
* console.log(findLCA(20, 30)); // 25
|
|
61
108
|
*/
|
|
62
109
|
class BST extends binary_tree_1.BinaryTree {
|
|
63
110
|
/**
|
|
64
|
-
* This
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
111
|
+
* This TypeScript constructor initializes a binary search tree with optional options and adds
|
|
112
|
+
* elements if provided.
|
|
113
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
114
|
+
* iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to
|
|
115
|
+
* initialize the binary search tree with keys, nodes, entries, or raw data.
|
|
116
|
+
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
117
|
+
* properties:
|
|
70
118
|
*/
|
|
71
119
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
72
120
|
super([], options);
|
|
73
121
|
this._root = undefined;
|
|
74
|
-
this.
|
|
122
|
+
this._isReverse = false;
|
|
123
|
+
this._comparator = (a, b) => {
|
|
124
|
+
if ((0, utils_1.isComparable)(a) && (0, utils_1.isComparable)(b)) {
|
|
125
|
+
if (a > b)
|
|
126
|
+
return 1;
|
|
127
|
+
if (a < b)
|
|
128
|
+
return -1;
|
|
129
|
+
return 0;
|
|
130
|
+
}
|
|
131
|
+
if (this._specifyComparable) {
|
|
132
|
+
if (this._specifyComparable(a) > this._specifyComparable(b))
|
|
133
|
+
return 1;
|
|
134
|
+
if (this._specifyComparable(a) < this._specifyComparable(b))
|
|
135
|
+
return -1;
|
|
136
|
+
return 0;
|
|
137
|
+
}
|
|
75
138
|
if (typeof a === 'object' || typeof b === 'object') {
|
|
76
|
-
throw TypeError(`When comparing object types, a custom
|
|
139
|
+
throw TypeError(`When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`);
|
|
77
140
|
}
|
|
78
|
-
if (a > b)
|
|
79
|
-
return 1;
|
|
80
|
-
if (a < b)
|
|
81
|
-
return -1;
|
|
82
141
|
return 0;
|
|
83
142
|
};
|
|
84
|
-
this._comparator = this._DEFAULT_COMPARATOR;
|
|
85
143
|
if (options) {
|
|
86
|
-
const {
|
|
87
|
-
if (
|
|
88
|
-
this.
|
|
144
|
+
const { specifyComparable, isReverse } = options;
|
|
145
|
+
if (typeof specifyComparable === 'function')
|
|
146
|
+
this._specifyComparable = specifyComparable;
|
|
147
|
+
if (isReverse !== undefined)
|
|
148
|
+
this._isReverse = isReverse;
|
|
89
149
|
}
|
|
90
150
|
if (keysNodesEntriesOrRaws)
|
|
91
151
|
this.addMany(keysNodesEntriesOrRaws);
|
|
92
152
|
}
|
|
93
|
-
/**
|
|
94
|
-
* The function returns the root node of a tree structure.
|
|
95
|
-
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
96
|
-
*/
|
|
97
153
|
get root() {
|
|
98
154
|
return this._root;
|
|
99
155
|
}
|
|
156
|
+
get isReverse() {
|
|
157
|
+
return this._isReverse;
|
|
158
|
+
}
|
|
159
|
+
get comparator() {
|
|
160
|
+
return this._comparator;
|
|
161
|
+
}
|
|
162
|
+
get specifyComparable() {
|
|
163
|
+
return this._specifyComparable;
|
|
164
|
+
}
|
|
100
165
|
/**
|
|
166
|
+
* Time Complexity: O(1)
|
|
167
|
+
* Space Complexity: O(1)
|
|
168
|
+
*
|
|
101
169
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
102
170
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
103
171
|
* being created.
|
|
104
172
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
105
173
|
* value associated with the key in the node being created.
|
|
106
|
-
* @returns The method is returning a new instance of the BSTNode class, casted as the
|
|
174
|
+
* @returns The method is returning a new instance of the BSTNode class, casted as the BSTNode<K, V> type.
|
|
107
175
|
*/
|
|
108
176
|
createNode(key, value) {
|
|
109
177
|
return new BSTNode(key, this._isMapMode ? undefined : value);
|
|
110
178
|
}
|
|
111
179
|
/**
|
|
180
|
+
* Time Complexity: O(1)
|
|
181
|
+
* Space Complexity: O(1)
|
|
182
|
+
*
|
|
112
183
|
* The function creates a new binary search tree with the specified options.
|
|
113
184
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
114
185
|
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
@@ -116,22 +187,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
116
187
|
* @returns a new instance of the BST class with the provided options.
|
|
117
188
|
*/
|
|
118
189
|
createTree(options) {
|
|
119
|
-
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
123
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
124
|
-
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
125
|
-
* element.
|
|
126
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
127
|
-
* value associated with a key in a key-value pair.
|
|
128
|
-
* @returns either a NODE object or undefined.
|
|
129
|
-
*/
|
|
130
|
-
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
131
|
-
const [node, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
132
|
-
if (node === null)
|
|
133
|
-
return [undefined, undefined];
|
|
134
|
-
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
190
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
135
191
|
}
|
|
136
192
|
/**
|
|
137
193
|
* Time Complexity: O(log n)
|
|
@@ -139,8 +195,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
139
195
|
*
|
|
140
196
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
141
197
|
* it doesn't exist.
|
|
142
|
-
* @param {BTNRep<K, V,
|
|
143
|
-
* `
|
|
198
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
|
|
199
|
+
* `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node,
|
|
144
200
|
* entry, or raw element that needs to be ensured in the tree.
|
|
145
201
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
146
202
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -148,44 +204,50 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
148
204
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
149
205
|
* not be ensured.
|
|
150
206
|
*/
|
|
151
|
-
ensureNode(
|
|
207
|
+
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
152
208
|
var _a;
|
|
153
|
-
return (_a = super.ensureNode(
|
|
209
|
+
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) !== null && _a !== void 0 ? _a : undefined;
|
|
154
210
|
}
|
|
155
211
|
/**
|
|
212
|
+
* Time Complexity: O(1)
|
|
213
|
+
* Space Complexity: O(1)
|
|
214
|
+
*
|
|
156
215
|
* The function checks if the input is an instance of the BSTNode class.
|
|
157
|
-
* @param {BTNRep<K, V,
|
|
158
|
-
* `
|
|
159
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
216
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
|
|
217
|
+
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`.
|
|
218
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
160
219
|
* an instance of the `BSTNode` class.
|
|
161
220
|
*/
|
|
162
|
-
isNode(
|
|
163
|
-
return
|
|
221
|
+
isNode(keyNodeOrEntry) {
|
|
222
|
+
return keyNodeOrEntry instanceof BSTNode;
|
|
164
223
|
}
|
|
165
224
|
/**
|
|
166
|
-
*
|
|
225
|
+
* Time Complexity: O(1)
|
|
226
|
+
* Space Complexity: O(1)
|
|
227
|
+
*
|
|
228
|
+
* The function "override isValidKey" checks if a key is comparable based on a given comparator.
|
|
167
229
|
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
|
|
168
230
|
* type `K`.
|
|
169
|
-
* @returns The `override
|
|
170
|
-
* the result of the `isComparable` function with the condition `this.
|
|
231
|
+
* @returns The `override isValidKey(key: any): key is K` function is returning a boolean value based on
|
|
232
|
+
* the result of the `isComparable` function with the condition `this._compare !==
|
|
171
233
|
* this._DEFAULT_COMPARATOR`.
|
|
172
234
|
*/
|
|
173
|
-
|
|
174
|
-
return (0, utils_1.isComparable)(key, this.
|
|
235
|
+
isValidKey(key) {
|
|
236
|
+
return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined);
|
|
175
237
|
}
|
|
176
238
|
/**
|
|
177
239
|
* Time Complexity: O(log n)
|
|
178
|
-
* Space Complexity: O(
|
|
240
|
+
* Space Complexity: O(log n)
|
|
179
241
|
*
|
|
180
242
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
181
|
-
* @param {BTNRep<K, V,
|
|
182
|
-
* `
|
|
243
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
|
|
244
|
+
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`.
|
|
183
245
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
184
246
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
185
247
|
* @returns a boolean value.
|
|
186
248
|
*/
|
|
187
|
-
add(
|
|
188
|
-
const [newNode, newValue] = this.
|
|
249
|
+
add(keyNodeOrEntry, value) {
|
|
250
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
189
251
|
if (newNode === undefined)
|
|
190
252
|
return false;
|
|
191
253
|
if (this._root === undefined) {
|
|
@@ -197,13 +259,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
197
259
|
}
|
|
198
260
|
let current = this._root;
|
|
199
261
|
while (current !== undefined) {
|
|
200
|
-
if (this.
|
|
262
|
+
if (this._compare(current.key, newNode.key) === 0) {
|
|
201
263
|
this._replaceNode(current, newNode);
|
|
202
264
|
if (this._isMapMode)
|
|
203
265
|
this._setValue(current.key, newValue);
|
|
204
266
|
return true;
|
|
205
267
|
}
|
|
206
|
-
else if (this.
|
|
268
|
+
else if (this._compare(current.key, newNode.key) > 0) {
|
|
207
269
|
if (current.left === undefined) {
|
|
208
270
|
current.left = newNode;
|
|
209
271
|
if (this._isMapMode)
|
|
@@ -211,7 +273,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
211
273
|
this._size++;
|
|
212
274
|
return true;
|
|
213
275
|
}
|
|
214
|
-
|
|
276
|
+
if (current.left !== null)
|
|
277
|
+
current = current.left;
|
|
215
278
|
}
|
|
216
279
|
else {
|
|
217
280
|
if (current.right === undefined) {
|
|
@@ -221,7 +284,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
221
284
|
this._size++;
|
|
222
285
|
return true;
|
|
223
286
|
}
|
|
224
|
-
|
|
287
|
+
if (current.right !== null)
|
|
288
|
+
current = current.right;
|
|
225
289
|
}
|
|
226
290
|
}
|
|
227
291
|
return false;
|
|
@@ -254,8 +318,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
254
318
|
valuesIterator = values[Symbol.iterator]();
|
|
255
319
|
}
|
|
256
320
|
if (!isBalanceAdd) {
|
|
257
|
-
for (
|
|
321
|
+
for (let kve of keysNodesEntriesOrRaws) {
|
|
258
322
|
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
323
|
+
if (this.isRaw(kve))
|
|
324
|
+
kve = this._toEntryFn(kve);
|
|
259
325
|
inserted.push(this.add(kve, value));
|
|
260
326
|
}
|
|
261
327
|
return inserted;
|
|
@@ -269,41 +335,47 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
269
335
|
let sorted = [];
|
|
270
336
|
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
271
337
|
let keyA, keyB;
|
|
272
|
-
if (this.
|
|
338
|
+
if (this.isRaw(a))
|
|
339
|
+
keyA = this._toEntryFn(a)[0];
|
|
340
|
+
else if (this.isEntry(a))
|
|
273
341
|
keyA = a[0];
|
|
274
342
|
else if (this.isRealNode(a))
|
|
275
343
|
keyA = a.key;
|
|
276
|
-
else if (this._toEntryFn) {
|
|
277
|
-
keyA = this._toEntryFn(a)[0];
|
|
278
|
-
}
|
|
279
344
|
else {
|
|
280
345
|
keyA = a;
|
|
281
346
|
}
|
|
282
|
-
if (this.
|
|
347
|
+
if (this.isRaw(b))
|
|
348
|
+
keyB = this._toEntryFn(b)[0];
|
|
349
|
+
else if (this.isEntry(b))
|
|
283
350
|
keyB = b[0];
|
|
284
351
|
else if (this.isRealNode(b))
|
|
285
352
|
keyB = b.key;
|
|
286
|
-
else if (this._toEntryFn) {
|
|
287
|
-
keyB = this._toEntryFn(b)[0];
|
|
288
|
-
}
|
|
289
353
|
else {
|
|
290
354
|
keyB = b;
|
|
291
355
|
}
|
|
292
356
|
if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
|
|
293
|
-
return this.
|
|
357
|
+
return this._compare(keyA, keyB);
|
|
294
358
|
}
|
|
295
359
|
return 0;
|
|
296
360
|
});
|
|
297
361
|
const _dfs = (arr) => {
|
|
362
|
+
var _a;
|
|
298
363
|
if (arr.length === 0)
|
|
299
364
|
return;
|
|
300
365
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
301
|
-
|
|
366
|
+
let { key, value } = arr[mid];
|
|
367
|
+
const { orgIndex } = arr[mid];
|
|
368
|
+
if (this.isRaw(key)) {
|
|
369
|
+
const entry = this._toEntryFn(key);
|
|
370
|
+
key = entry[0];
|
|
371
|
+
value = (_a = entry[1]) !== null && _a !== void 0 ? _a : value;
|
|
372
|
+
}
|
|
302
373
|
inserted[orgIndex] = this.add(key, value);
|
|
303
374
|
_dfs(arr.slice(0, mid));
|
|
304
375
|
_dfs(arr.slice(mid + 1));
|
|
305
376
|
};
|
|
306
377
|
const _iterate = () => {
|
|
378
|
+
var _a;
|
|
307
379
|
const n = sorted.length;
|
|
308
380
|
const stack = [[0, n - 1]];
|
|
309
381
|
while (stack.length > 0) {
|
|
@@ -312,7 +384,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
312
384
|
const [l, r] = popped;
|
|
313
385
|
if (l <= r) {
|
|
314
386
|
const m = l + Math.floor((r - l) / 2);
|
|
315
|
-
|
|
387
|
+
let { key, value } = sorted[m];
|
|
388
|
+
const { orgIndex } = sorted[m];
|
|
389
|
+
if (this.isRaw(key)) {
|
|
390
|
+
const entry = this._toEntryFn(key);
|
|
391
|
+
key = entry[0];
|
|
392
|
+
value = (_a = entry[1]) !== null && _a !== void 0 ? _a : value;
|
|
393
|
+
}
|
|
316
394
|
inserted[orgIndex] = this.add(key, value);
|
|
317
395
|
stack.push([m + 1, r]);
|
|
318
396
|
stack.push([l, m - 1]);
|
|
@@ -332,55 +410,92 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
332
410
|
* Time Complexity: O(log n)
|
|
333
411
|
* Space Complexity: O(k + log n)
|
|
334
412
|
*
|
|
335
|
-
* The function `
|
|
336
|
-
*
|
|
337
|
-
* @param {BTNRep<K, V,
|
|
338
|
-
* parameter in the `
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
413
|
+
* The function `search` in TypeScript overrides the search behavior in a binary tree structure based
|
|
414
|
+
* on specified criteria.
|
|
415
|
+
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
416
|
+
* `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the
|
|
417
|
+
* following types:
|
|
418
|
+
* @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
|
|
419
|
+
* search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
|
|
420
|
+
* search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
|
|
421
|
+
* @param {C} callback - The `callback` parameter in the `override search` function is a function
|
|
422
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
423
|
+
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its
|
|
424
|
+
* argument and
|
|
425
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search`
|
|
426
|
+
* method represents the node from which the search operation will begin. It is the starting point
|
|
427
|
+
* for searching within the tree data structure. The method ensures that the `startNode` is a valid
|
|
428
|
+
* node before proceeding with the search operation. If the `
|
|
429
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `override search`
|
|
430
|
+
* function determines the type of iteration to be used during the search operation. It can have two
|
|
431
|
+
* possible values:
|
|
432
|
+
* @returns The `override search` method returns an array of values that match the search criteria
|
|
433
|
+
* specified by the input parameters. The method performs a search operation on a binary tree
|
|
434
|
+
* structure based on the provided key, predicate, and other options. The search results are
|
|
435
|
+
* collected in an array and returned as the output of the method.
|
|
353
436
|
*/
|
|
354
|
-
|
|
355
|
-
if (
|
|
437
|
+
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
438
|
+
if (keyNodeEntryOrPredicate === undefined)
|
|
356
439
|
return [];
|
|
357
|
-
if (
|
|
440
|
+
if (keyNodeEntryOrPredicate === null)
|
|
358
441
|
return [];
|
|
359
442
|
startNode = this.ensureNode(startNode);
|
|
360
443
|
if (!startNode)
|
|
361
444
|
return [];
|
|
362
|
-
|
|
445
|
+
let predicate;
|
|
446
|
+
const isRange = this.isRange(keyNodeEntryOrPredicate);
|
|
447
|
+
// Set predicate based on parameter type
|
|
448
|
+
if (isRange) {
|
|
449
|
+
predicate = node => keyNodeEntryOrPredicate.isInRange(node.key, this._comparator);
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
453
|
+
}
|
|
454
|
+
const isToLeftByRange = (cur) => {
|
|
455
|
+
if (isRange) {
|
|
456
|
+
const range = keyNodeEntryOrPredicate;
|
|
457
|
+
const leftS = this.isReverse ? range.high : range.low;
|
|
458
|
+
const leftI = this.isReverse ? range.includeHigh : range.includeLow;
|
|
459
|
+
return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
|
|
460
|
+
}
|
|
461
|
+
return false;
|
|
462
|
+
};
|
|
463
|
+
const isToRightByRange = (cur) => {
|
|
464
|
+
if (isRange) {
|
|
465
|
+
const range = keyNodeEntryOrPredicate;
|
|
466
|
+
const rightS = this.isReverse ? range.low : range.high;
|
|
467
|
+
const rightI = this.isReverse ? range.includeLow : range.includeLow;
|
|
468
|
+
return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
|
|
469
|
+
}
|
|
470
|
+
return false;
|
|
471
|
+
};
|
|
363
472
|
const ans = [];
|
|
364
473
|
if (iterationType === 'RECURSIVE') {
|
|
365
474
|
const dfs = (cur) => {
|
|
366
|
-
if (
|
|
367
|
-
ans.push(cur);
|
|
475
|
+
if (predicate(cur)) {
|
|
476
|
+
ans.push(callback(cur));
|
|
368
477
|
if (onlyOne)
|
|
369
478
|
return;
|
|
370
479
|
}
|
|
371
480
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
372
481
|
return;
|
|
373
|
-
if (
|
|
374
|
-
|
|
482
|
+
if (isRange) {
|
|
483
|
+
if (this.isRealNode(cur.left) && isToLeftByRange(cur))
|
|
484
|
+
dfs(cur.left);
|
|
485
|
+
if (this.isRealNode(cur.right) && isToRightByRange(cur))
|
|
486
|
+
dfs(cur.right);
|
|
487
|
+
}
|
|
488
|
+
else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
489
|
+
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
|
|
375
490
|
if (this.isRealNode(cur.left) &&
|
|
376
491
|
benchmarkKey !== null &&
|
|
377
492
|
benchmarkKey !== undefined &&
|
|
378
|
-
this.
|
|
493
|
+
this._compare(cur.key, benchmarkKey) > 0)
|
|
379
494
|
dfs(cur.left);
|
|
380
495
|
if (this.isRealNode(cur.right) &&
|
|
381
496
|
benchmarkKey !== null &&
|
|
382
497
|
benchmarkKey !== undefined &&
|
|
383
|
-
this.
|
|
498
|
+
this._compare(cur.key, benchmarkKey) < 0)
|
|
384
499
|
dfs(cur.right);
|
|
385
500
|
}
|
|
386
501
|
else {
|
|
@@ -396,22 +511,28 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
396
511
|
const stack = [startNode];
|
|
397
512
|
while (stack.length > 0) {
|
|
398
513
|
const cur = stack.pop();
|
|
399
|
-
if (
|
|
400
|
-
ans.push(cur);
|
|
514
|
+
if (predicate(cur)) {
|
|
515
|
+
ans.push(callback(cur));
|
|
401
516
|
if (onlyOne)
|
|
402
517
|
return ans;
|
|
403
518
|
}
|
|
404
|
-
if (
|
|
405
|
-
|
|
519
|
+
if (isRange) {
|
|
520
|
+
if (this.isRealNode(cur.left) && isToLeftByRange(cur))
|
|
521
|
+
stack.push(cur.left);
|
|
522
|
+
if (this.isRealNode(cur.right) && isToRightByRange(cur))
|
|
523
|
+
stack.push(cur.right);
|
|
524
|
+
}
|
|
525
|
+
else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
526
|
+
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
|
|
406
527
|
if (this.isRealNode(cur.right) &&
|
|
407
528
|
benchmarkKey !== null &&
|
|
408
529
|
benchmarkKey !== undefined &&
|
|
409
|
-
this.
|
|
530
|
+
this._compare(cur.key, benchmarkKey) < 0)
|
|
410
531
|
stack.push(cur.right);
|
|
411
532
|
if (this.isRealNode(cur.left) &&
|
|
412
533
|
benchmarkKey !== null &&
|
|
413
534
|
benchmarkKey !== undefined &&
|
|
414
|
-
this.
|
|
535
|
+
this._compare(cur.key, benchmarkKey) > 0)
|
|
415
536
|
stack.push(cur.left);
|
|
416
537
|
}
|
|
417
538
|
else {
|
|
@@ -426,12 +547,37 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
426
547
|
}
|
|
427
548
|
/**
|
|
428
549
|
* Time Complexity: O(log n)
|
|
429
|
-
* Space Complexity: O(
|
|
550
|
+
* Space Complexity: O(k + log n)
|
|
551
|
+
*
|
|
552
|
+
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
|
|
553
|
+
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
|
|
554
|
+
* either a `Range` object or an array of two elements representing the range boundaries.
|
|
555
|
+
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
|
|
556
|
+
* function that is used to process each node that is found within the specified range during the
|
|
557
|
+
* search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the
|
|
558
|
+
* data structure.
|
|
559
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch`
|
|
560
|
+
* function represents the node from which the search for nodes within the specified range will
|
|
561
|
+
* begin. It is the starting point for the range search operation.
|
|
562
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
|
|
563
|
+
* is used to specify the type of iteration to be performed during the search operation. It has a
|
|
564
|
+
* default value of `this.iterationType`, which suggests that it is likely a property of the class or
|
|
565
|
+
* object that the `rangeSearch`
|
|
566
|
+
* @returns The `rangeSearch` function is returning the result of calling the `search` method with
|
|
567
|
+
* the specified parameters.
|
|
568
|
+
*/
|
|
569
|
+
rangeSearch(range, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
570
|
+
const searchRange = range instanceof common_1.Range ? range : new common_1.Range(range[0], range[1]);
|
|
571
|
+
return this.search(searchRange, false, callback, startNode, iterationType);
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Time Complexity: O(log n)
|
|
575
|
+
* Space Complexity: O(log n)
|
|
430
576
|
*
|
|
431
|
-
* This function retrieves a node based on a given
|
|
432
|
-
* @param {BTNRep<K, V,
|
|
433
|
-
* parameter can be of type `BTNRep<K, V,
|
|
434
|
-
* @param {
|
|
577
|
+
* This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure.
|
|
578
|
+
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate`
|
|
579
|
+
* parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`.
|
|
580
|
+
* @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method
|
|
435
581
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
436
582
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
437
583
|
* node of the binary search tree.
|
|
@@ -439,30 +585,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
439
585
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
440
586
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
441
587
|
* no value is provided when calling the method.
|
|
442
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<
|
|
443
|
-
* It is using the `getNodes` method to find the node based on the provided
|
|
588
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<BSTNode<K, V>>`).
|
|
589
|
+
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryOrPredicate, beginning at
|
|
444
590
|
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
445
591
|
* returns the first node found or `undefined` if no node is found.
|
|
446
592
|
*/
|
|
447
|
-
getNode(
|
|
593
|
+
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
448
594
|
var _a;
|
|
449
|
-
return (_a = this.getNodes(
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Time Complexity: O(log n)
|
|
453
|
-
* Space Complexity: O(1)
|
|
454
|
-
*
|
|
455
|
-
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
456
|
-
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
457
|
-
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
458
|
-
* in the tree structure.
|
|
459
|
-
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
460
|
-
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
461
|
-
* It has a default value of `'ITERATIVE'`.
|
|
462
|
-
* @returns The method is returning a NODE object or undefined.
|
|
463
|
-
*/
|
|
464
|
-
getNodeByKey(key, iterationType = this.iterationType) {
|
|
465
|
-
return this.getNode(key, this._root, iterationType);
|
|
595
|
+
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
466
596
|
}
|
|
467
597
|
/**
|
|
468
598
|
* Time complexity: O(n)
|
|
@@ -476,7 +606,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
476
606
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
477
607
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
478
608
|
* take one of the following values:
|
|
479
|
-
* @param {BTNRep<K, V,
|
|
609
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
480
610
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
481
611
|
* node entry. If not specified, the default value is the root of the tree.
|
|
482
612
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -496,7 +626,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
496
626
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
497
627
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
498
628
|
* node being visited, and it can return a value of any type.
|
|
499
|
-
* @param {BTNRep<K, V,
|
|
629
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
500
630
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
501
631
|
* object. If no value is provided, the default value is the root of the tree.
|
|
502
632
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -514,9 +644,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
514
644
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
515
645
|
* containing the results of the callback function applied to each level of the tree.
|
|
516
646
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
517
|
-
* `NodeCallback<
|
|
647
|
+
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the
|
|
518
648
|
* tree during the iteration process.
|
|
519
|
-
* @param {BTNRep<K, V,
|
|
649
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
520
650
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
521
651
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
522
652
|
* value is provided, the root of
|
|
@@ -540,7 +670,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
540
670
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
541
671
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
542
672
|
* 0, or 1, where:
|
|
543
|
-
* @param {BTNRep<K, V,
|
|
673
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in
|
|
544
674
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
545
675
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
546
676
|
* `targetNode` is provided,
|
|
@@ -559,9 +689,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
559
689
|
const targetKey = targetNodeEnsured.key;
|
|
560
690
|
if (iterationType === 'RECURSIVE') {
|
|
561
691
|
const dfs = (cur) => {
|
|
562
|
-
const compared = this.
|
|
692
|
+
const compared = this._compare(cur.key, targetKey);
|
|
563
693
|
if (Math.sign(compared) === lesserOrGreater)
|
|
564
694
|
ans.push(callback(cur));
|
|
695
|
+
// TODO here can be optimized to O(log n)
|
|
565
696
|
if (this.isRealNode(cur.left))
|
|
566
697
|
dfs(cur.left);
|
|
567
698
|
if (this.isRealNode(cur.right))
|
|
@@ -575,7 +706,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
575
706
|
while (queue.size > 0) {
|
|
576
707
|
const cur = queue.shift();
|
|
577
708
|
if (this.isRealNode(cur)) {
|
|
578
|
-
const compared = this.
|
|
709
|
+
const compared = this._compare(cur.key, targetKey);
|
|
579
710
|
if (Math.sign(compared) === lesserOrGreater)
|
|
580
711
|
ans.push(callback(cur));
|
|
581
712
|
if (this.isRealNode(cur.left))
|
|
@@ -675,7 +806,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
675
806
|
while (stack.length > 0 || node) {
|
|
676
807
|
if (node) {
|
|
677
808
|
stack.push(node);
|
|
678
|
-
|
|
809
|
+
if (node.left !== null)
|
|
810
|
+
node = node.left;
|
|
679
811
|
}
|
|
680
812
|
else {
|
|
681
813
|
node = stack[stack.length - 1];
|
|
@@ -699,16 +831,70 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
699
831
|
return balanced;
|
|
700
832
|
}
|
|
701
833
|
/**
|
|
702
|
-
*
|
|
703
|
-
*
|
|
834
|
+
* Time complexity: O(n)
|
|
835
|
+
* Space complexity: O(n)
|
|
836
|
+
*
|
|
837
|
+
* The `map` function in TypeScript overrides the default map behavior for a binary search tree by
|
|
838
|
+
* applying a callback function to each entry and creating a new tree with the results.
|
|
839
|
+
* @param callback - A function that will be called for each entry in the BST. It takes four
|
|
840
|
+
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to
|
|
841
|
+
* the BST itself.
|
|
842
|
+
* @param [options] - The `options` parameter in the `override map` method is of type `BSTOptions<MK,
|
|
843
|
+
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the Binary
|
|
844
|
+
* Search Tree (BST) being created in the `map` method. These options could include configuration
|
|
845
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` method is used to specify
|
|
846
|
+
* the value of `this` that should be used when executing the `callback` function. It allows you to
|
|
847
|
+
* set the context or scope in which the callback function will be called. This can be useful when
|
|
848
|
+
* you want
|
|
849
|
+
* @returns The `map` method is returning a new Binary Search Tree (`BST`) instance with the entries
|
|
850
|
+
* transformed by the provided callback function.
|
|
704
851
|
*/
|
|
705
|
-
|
|
706
|
-
|
|
852
|
+
map(callback, options, thisArg) {
|
|
853
|
+
const newTree = new BST([], options);
|
|
854
|
+
let index = 0;
|
|
855
|
+
for (const [key, value] of this) {
|
|
856
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
857
|
+
}
|
|
858
|
+
return newTree;
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Time complexity: O(n)
|
|
862
|
+
* Space complexity: O(n)
|
|
863
|
+
*
|
|
864
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
865
|
+
* structure.
|
|
866
|
+
* @returns The `cloned` object is being returned.
|
|
867
|
+
*/
|
|
868
|
+
clone() {
|
|
869
|
+
const cloned = this.createTree();
|
|
870
|
+
this._clone(cloned);
|
|
871
|
+
return cloned;
|
|
707
872
|
}
|
|
708
873
|
/**
|
|
874
|
+
* Time Complexity: O(1)
|
|
875
|
+
* Space Complexity: O(1)
|
|
876
|
+
*
|
|
877
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
878
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of
|
|
879
|
+
* type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw
|
|
880
|
+
* element.
|
|
881
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
882
|
+
* value associated with a key in a key-value pair.
|
|
883
|
+
* @returns either a BSTNode<K, V> object or undefined.
|
|
884
|
+
*/
|
|
885
|
+
_keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value) {
|
|
886
|
+
const [node, entryValue] = super._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
887
|
+
if (node === null)
|
|
888
|
+
return [undefined, undefined];
|
|
889
|
+
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Time Complexity: O(1)
|
|
893
|
+
* Space Complexity: O(1)
|
|
894
|
+
*
|
|
709
895
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
710
896
|
* root.
|
|
711
|
-
* @param {OptNode<
|
|
897
|
+
* @param {OptNode<BSTNode<K, V>>} v - v is a parameter of type BSTNode<K, V> or undefined.
|
|
712
898
|
*/
|
|
713
899
|
_setRoot(v) {
|
|
714
900
|
if (v) {
|
|
@@ -716,5 +902,22 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
716
902
|
}
|
|
717
903
|
this._root = v;
|
|
718
904
|
}
|
|
905
|
+
/**
|
|
906
|
+
* Time Complexity: O(1)
|
|
907
|
+
* Space Complexity: O(1)
|
|
908
|
+
*
|
|
909
|
+
* The _compare function compares two values using a specified comparator function and optionally
|
|
910
|
+
* reverses the result.
|
|
911
|
+
* @param {K} a - The parameter `a` is of type `K`, which is used as an input for comparison in the
|
|
912
|
+
* `_compare` method.
|
|
913
|
+
* @param {K} b - The parameter `b` in the `_compare` function is of type `K`.
|
|
914
|
+
* @returns The `_compare` method is returning the result of the ternary expression. If `_isReverse`
|
|
915
|
+
* is true, it returns the negation of the result of calling the `_comparator` function with
|
|
916
|
+
* arguments `a` and `b`. If `_isReverse` is false, it returns the result of calling the
|
|
917
|
+
* `_comparator` function with arguments `a` and `b`.
|
|
918
|
+
*/
|
|
919
|
+
_compare(a, b) {
|
|
920
|
+
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
|
|
921
|
+
}
|
|
719
922
|
}
|
|
720
923
|
exports.BST = BST;
|