red-black-tree-typed 1.53.7 → 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.js +5 -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 -328
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +130 -70
- 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 +268 -202
- package/dist/data-structures/binary-tree/binary-tree.js +311 -263
- package/dist/data-structures/binary-tree/bst.d.ts +193 -139
- package/dist/data-structures/binary-tree/bst.js +248 -164
- 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} +176 -107
- 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 +20 -3
- package/dist/data-structures/heap/heap.js +31 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
- package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +47 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +73 -26
- 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 +8 -7
- package/dist/data-structures/trie/trie.js +8 -7
- 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 +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- 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/package.json +2 -2
- package/src/common/index.ts +7 -1
- 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 +151 -370
- package/src/data-structures/binary-tree/avl-tree.ts +162 -105
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +488 -416
- package/src/data-structures/binary-tree/bst.ts +326 -251
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +219 -145
- 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 +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
- package/src/data-structures/linked-list/singly-linked-list.ts +80 -27
- 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 +8 -7
- package/src/index.ts +2 -2
- 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 +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- 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/dist/data-structures/binary-tree/rb-tree.d.ts +0 -209
- 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;
|
|
@@ -59,32 +50,48 @@ exports.BSTNode = BSTNode;
|
|
|
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.
|
|
61
52
|
* @example
|
|
62
|
-
* //
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
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
|
+
* ]);
|
|
66
67
|
*
|
|
67
|
-
* //
|
|
68
|
-
* const
|
|
69
|
-
*
|
|
70
|
-
*
|
|
68
|
+
* // Merge datasets into a single BinarySearchTree
|
|
69
|
+
* const merged = new BST<number, string>(dataset1);
|
|
70
|
+
* merged.addMany(dataset2);
|
|
71
|
+
* merged.merge(dataset3);
|
|
71
72
|
*
|
|
72
|
-
* //
|
|
73
|
-
* console.log(
|
|
74
|
-
* console.log(findKthSmallest(3)); // 4
|
|
75
|
-
* console.log(findKthSmallest(7)); // 8
|
|
73
|
+
* // Verify merged dataset is in sorted order
|
|
74
|
+
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
|
76
75
|
* @example
|
|
77
76
|
* // Find elements in a range
|
|
78
77
|
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
79
78
|
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
|
|
80
|
-
* console.log(bst.
|
|
79
|
+
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
|
|
81
80
|
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
|
|
82
|
-
* console.log(bst.
|
|
81
|
+
* console.log(bst.rangeSearch([15, 20])); // [15, 18]
|
|
83
82
|
* console.log(bst.search(new Range(15, 20, false))); // [18]
|
|
84
83
|
* @example
|
|
85
84
|
* // Find lowest common ancestor
|
|
86
85
|
* const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
87
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
|
+
*
|
|
88
95
|
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
|
|
89
96
|
* for (const num of arr1) {
|
|
90
97
|
* if (arr2.indexOf(num) !== -1) {
|
|
@@ -94,14 +101,6 @@ exports.BSTNode = BSTNode;
|
|
|
94
101
|
* return undefined;
|
|
95
102
|
* }
|
|
96
103
|
*
|
|
97
|
-
* // LCA helper function
|
|
98
|
-
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
99
|
-
* const path1 = bst.getPathToRoot(num1);
|
|
100
|
-
* const path2 = bst.getPathToRoot(num2);
|
|
101
|
-
* // Find the first common ancestor
|
|
102
|
-
* return findFirstCommon(path1, path2);
|
|
103
|
-
* };
|
|
104
|
-
*
|
|
105
104
|
* // Assertions
|
|
106
105
|
* console.log(findLCA(3, 10)); // 7
|
|
107
106
|
* console.log(findLCA(5, 35)); // 15
|
|
@@ -109,12 +108,13 @@ exports.BSTNode = BSTNode;
|
|
|
109
108
|
*/
|
|
110
109
|
class BST extends binary_tree_1.BinaryTree {
|
|
111
110
|
/**
|
|
112
|
-
* This
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
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:
|
|
118
118
|
*/
|
|
119
119
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
120
120
|
super([], options);
|
|
@@ -128,55 +128,58 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
128
128
|
return -1;
|
|
129
129
|
return 0;
|
|
130
130
|
}
|
|
131
|
-
if (this.
|
|
132
|
-
if (this.
|
|
131
|
+
if (this._specifyComparable) {
|
|
132
|
+
if (this._specifyComparable(a) > this._specifyComparable(b))
|
|
133
133
|
return 1;
|
|
134
|
-
if (this.
|
|
134
|
+
if (this._specifyComparable(a) < this._specifyComparable(b))
|
|
135
135
|
return -1;
|
|
136
136
|
return 0;
|
|
137
137
|
}
|
|
138
138
|
if (typeof a === 'object' || typeof b === 'object') {
|
|
139
|
-
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.`);
|
|
140
140
|
}
|
|
141
141
|
return 0;
|
|
142
142
|
};
|
|
143
143
|
if (options) {
|
|
144
|
-
const {
|
|
145
|
-
if (typeof
|
|
146
|
-
this.
|
|
144
|
+
const { specifyComparable, isReverse } = options;
|
|
145
|
+
if (typeof specifyComparable === 'function')
|
|
146
|
+
this._specifyComparable = specifyComparable;
|
|
147
147
|
if (isReverse !== undefined)
|
|
148
148
|
this._isReverse = isReverse;
|
|
149
149
|
}
|
|
150
150
|
if (keysNodesEntriesOrRaws)
|
|
151
151
|
this.addMany(keysNodesEntriesOrRaws);
|
|
152
152
|
}
|
|
153
|
-
/**
|
|
154
|
-
* The function returns the root node of a tree structure.
|
|
155
|
-
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
156
|
-
*/
|
|
157
153
|
get root() {
|
|
158
154
|
return this._root;
|
|
159
155
|
}
|
|
160
|
-
/**
|
|
161
|
-
* The above function is a getter method in TypeScript that returns the value of the private property
|
|
162
|
-
* `_isReverse`.
|
|
163
|
-
* @returns The `isReverse` property of the object, which is a boolean value.
|
|
164
|
-
*/
|
|
165
156
|
get isReverse() {
|
|
166
157
|
return this._isReverse;
|
|
167
158
|
}
|
|
159
|
+
get comparator() {
|
|
160
|
+
return this._comparator;
|
|
161
|
+
}
|
|
162
|
+
get specifyComparable() {
|
|
163
|
+
return this._specifyComparable;
|
|
164
|
+
}
|
|
168
165
|
/**
|
|
166
|
+
* Time Complexity: O(1)
|
|
167
|
+
* Space Complexity: O(1)
|
|
168
|
+
*
|
|
169
169
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
170
170
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
171
171
|
* being created.
|
|
172
172
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
173
173
|
* value associated with the key in the node being created.
|
|
174
|
-
* @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.
|
|
175
175
|
*/
|
|
176
176
|
createNode(key, value) {
|
|
177
177
|
return new BSTNode(key, this._isMapMode ? undefined : value);
|
|
178
178
|
}
|
|
179
179
|
/**
|
|
180
|
+
* Time Complexity: O(1)
|
|
181
|
+
* Space Complexity: O(1)
|
|
182
|
+
*
|
|
180
183
|
* The function creates a new binary search tree with the specified options.
|
|
181
184
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
182
185
|
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
@@ -184,22 +187,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
184
187
|
* @returns a new instance of the BST class with the provided options.
|
|
185
188
|
*/
|
|
186
189
|
createTree(options) {
|
|
187
|
-
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
191
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
192
|
-
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
193
|
-
* element.
|
|
194
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
195
|
-
* value associated with a key in a key-value pair.
|
|
196
|
-
* @returns either a NODE object or undefined.
|
|
197
|
-
*/
|
|
198
|
-
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
199
|
-
const [node, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
200
|
-
if (node === null)
|
|
201
|
-
return [undefined, undefined];
|
|
202
|
-
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));
|
|
203
191
|
}
|
|
204
192
|
/**
|
|
205
193
|
* Time Complexity: O(log n)
|
|
@@ -207,8 +195,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
207
195
|
*
|
|
208
196
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
209
197
|
* it doesn't exist.
|
|
210
|
-
* @param {BTNRep<K, V,
|
|
211
|
-
* `
|
|
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,
|
|
212
200
|
* entry, or raw element that needs to be ensured in the tree.
|
|
213
201
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
214
202
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -216,44 +204,50 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
216
204
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
217
205
|
* not be ensured.
|
|
218
206
|
*/
|
|
219
|
-
ensureNode(
|
|
207
|
+
ensureNode(keyNodeOrEntry, iterationType = this.iterationType) {
|
|
220
208
|
var _a;
|
|
221
|
-
return (_a = super.ensureNode(
|
|
209
|
+
return (_a = super.ensureNode(keyNodeOrEntry, iterationType)) !== null && _a !== void 0 ? _a : undefined;
|
|
222
210
|
}
|
|
223
211
|
/**
|
|
212
|
+
* Time Complexity: O(1)
|
|
213
|
+
* Space Complexity: O(1)
|
|
214
|
+
*
|
|
224
215
|
* The function checks if the input is an instance of the BSTNode class.
|
|
225
|
-
* @param {BTNRep<K, V,
|
|
226
|
-
* `
|
|
227
|
-
* @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
|
|
228
219
|
* an instance of the `BSTNode` class.
|
|
229
220
|
*/
|
|
230
|
-
isNode(
|
|
231
|
-
return
|
|
221
|
+
isNode(keyNodeOrEntry) {
|
|
222
|
+
return keyNodeOrEntry instanceof BSTNode;
|
|
232
223
|
}
|
|
233
224
|
/**
|
|
234
|
-
*
|
|
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.
|
|
235
229
|
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
|
|
236
230
|
* type `K`.
|
|
237
|
-
* @returns The `override
|
|
231
|
+
* @returns The `override isValidKey(key: any): key is K` function is returning a boolean value based on
|
|
238
232
|
* the result of the `isComparable` function with the condition `this._compare !==
|
|
239
233
|
* this._DEFAULT_COMPARATOR`.
|
|
240
234
|
*/
|
|
241
|
-
|
|
242
|
-
return (0, utils_1.isComparable)(key, this.
|
|
235
|
+
isValidKey(key) {
|
|
236
|
+
return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined);
|
|
243
237
|
}
|
|
244
238
|
/**
|
|
245
239
|
* Time Complexity: O(log n)
|
|
246
|
-
* Space Complexity: O(
|
|
240
|
+
* Space Complexity: O(log n)
|
|
247
241
|
*
|
|
248
242
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
249
|
-
* @param {BTNRep<K, V,
|
|
250
|
-
* `
|
|
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>>`.
|
|
251
245
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
252
246
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
253
247
|
* @returns a boolean value.
|
|
254
248
|
*/
|
|
255
|
-
add(
|
|
256
|
-
const [newNode, newValue] = this.
|
|
249
|
+
add(keyNodeOrEntry, value) {
|
|
250
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
257
251
|
if (newNode === undefined)
|
|
258
252
|
return false;
|
|
259
253
|
if (this._root === undefined) {
|
|
@@ -279,7 +273,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
279
273
|
this._size++;
|
|
280
274
|
return true;
|
|
281
275
|
}
|
|
282
|
-
|
|
276
|
+
if (current.left !== null)
|
|
277
|
+
current = current.left;
|
|
283
278
|
}
|
|
284
279
|
else {
|
|
285
280
|
if (current.right === undefined) {
|
|
@@ -289,7 +284,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
289
284
|
this._size++;
|
|
290
285
|
return true;
|
|
291
286
|
}
|
|
292
|
-
|
|
287
|
+
if (current.right !== null)
|
|
288
|
+
current = current.right;
|
|
293
289
|
}
|
|
294
290
|
}
|
|
295
291
|
return false;
|
|
@@ -322,8 +318,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
322
318
|
valuesIterator = values[Symbol.iterator]();
|
|
323
319
|
}
|
|
324
320
|
if (!isBalanceAdd) {
|
|
325
|
-
for (
|
|
321
|
+
for (let kve of keysNodesEntriesOrRaws) {
|
|
326
322
|
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
323
|
+
if (this.isRaw(kve))
|
|
324
|
+
kve = this._toEntryFn(kve);
|
|
327
325
|
inserted.push(this.add(kve, value));
|
|
328
326
|
}
|
|
329
327
|
return inserted;
|
|
@@ -337,23 +335,21 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
337
335
|
let sorted = [];
|
|
338
336
|
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
339
337
|
let keyA, keyB;
|
|
340
|
-
if (this.
|
|
338
|
+
if (this.isRaw(a))
|
|
339
|
+
keyA = this._toEntryFn(a)[0];
|
|
340
|
+
else if (this.isEntry(a))
|
|
341
341
|
keyA = a[0];
|
|
342
342
|
else if (this.isRealNode(a))
|
|
343
343
|
keyA = a.key;
|
|
344
|
-
else if (this._toEntryFn) {
|
|
345
|
-
keyA = this._toEntryFn(a)[0];
|
|
346
|
-
}
|
|
347
344
|
else {
|
|
348
345
|
keyA = a;
|
|
349
346
|
}
|
|
350
|
-
if (this.
|
|
347
|
+
if (this.isRaw(b))
|
|
348
|
+
keyB = this._toEntryFn(b)[0];
|
|
349
|
+
else if (this.isEntry(b))
|
|
351
350
|
keyB = b[0];
|
|
352
351
|
else if (this.isRealNode(b))
|
|
353
352
|
keyB = b.key;
|
|
354
|
-
else if (this._toEntryFn) {
|
|
355
|
-
keyB = this._toEntryFn(b)[0];
|
|
356
|
-
}
|
|
357
353
|
else {
|
|
358
354
|
keyB = b;
|
|
359
355
|
}
|
|
@@ -363,15 +359,23 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
363
359
|
return 0;
|
|
364
360
|
});
|
|
365
361
|
const _dfs = (arr) => {
|
|
362
|
+
var _a;
|
|
366
363
|
if (arr.length === 0)
|
|
367
364
|
return;
|
|
368
365
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
369
|
-
|
|
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
|
+
}
|
|
370
373
|
inserted[orgIndex] = this.add(key, value);
|
|
371
374
|
_dfs(arr.slice(0, mid));
|
|
372
375
|
_dfs(arr.slice(mid + 1));
|
|
373
376
|
};
|
|
374
377
|
const _iterate = () => {
|
|
378
|
+
var _a;
|
|
375
379
|
const n = sorted.length;
|
|
376
380
|
const stack = [[0, n - 1]];
|
|
377
381
|
while (stack.length > 0) {
|
|
@@ -380,7 +384,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
380
384
|
const [l, r] = popped;
|
|
381
385
|
if (l <= r) {
|
|
382
386
|
const m = l + Math.floor((r - l) / 2);
|
|
383
|
-
|
|
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
|
+
}
|
|
384
394
|
inserted[orgIndex] = this.add(key, value);
|
|
385
395
|
stack.push([m + 1, r]);
|
|
386
396
|
stack.push([l, m - 1]);
|
|
@@ -396,35 +406,23 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
396
406
|
}
|
|
397
407
|
return inserted;
|
|
398
408
|
}
|
|
399
|
-
/**
|
|
400
|
-
* Time Complexity: O(n)
|
|
401
|
-
* Space Complexity: O(1)
|
|
402
|
-
*
|
|
403
|
-
* The `merge` function overrides the base class method by adding elements from another
|
|
404
|
-
* binary search tree.
|
|
405
|
-
* @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
|
|
406
|
-
* value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
|
|
407
|
-
*/
|
|
408
|
-
merge(anotherTree) {
|
|
409
|
-
this.addMany(anotherTree, [], false);
|
|
410
|
-
}
|
|
411
409
|
/**
|
|
412
410
|
* Time Complexity: O(log n)
|
|
413
411
|
* Space Complexity: O(k + log n)
|
|
414
412
|
*
|
|
415
413
|
* The function `search` in TypeScript overrides the search behavior in a binary tree structure based
|
|
416
414
|
* on specified criteria.
|
|
417
|
-
* @param {BTNRep<K, V,
|
|
418
|
-
* `
|
|
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
|
|
419
417
|
* following types:
|
|
420
418
|
* @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
|
|
421
419
|
* search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
|
|
422
420
|
* search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
|
|
423
421
|
* @param {C} callback - The `callback` parameter in the `override search` function is a function
|
|
424
422
|
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
425
|
-
* extends `NodeCallback<
|
|
423
|
+
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its
|
|
426
424
|
* argument and
|
|
427
|
-
* @param {BTNRep<K, V,
|
|
425
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search`
|
|
428
426
|
* method represents the node from which the search operation will begin. It is the starting point
|
|
429
427
|
* for searching within the tree data structure. The method ensures that the `startNode` is a valid
|
|
430
428
|
* node before proceeding with the search operation. If the `
|
|
@@ -436,26 +434,26 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
436
434
|
* structure based on the provided key, predicate, and other options. The search results are
|
|
437
435
|
* collected in an array and returned as the output of the method.
|
|
438
436
|
*/
|
|
439
|
-
search(
|
|
440
|
-
if (
|
|
437
|
+
search(keyNodeEntryOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
438
|
+
if (keyNodeEntryOrPredicate === undefined)
|
|
441
439
|
return [];
|
|
442
|
-
if (
|
|
440
|
+
if (keyNodeEntryOrPredicate === null)
|
|
443
441
|
return [];
|
|
444
442
|
startNode = this.ensureNode(startNode);
|
|
445
443
|
if (!startNode)
|
|
446
444
|
return [];
|
|
447
445
|
let predicate;
|
|
448
|
-
const isRange = this.isRange(
|
|
446
|
+
const isRange = this.isRange(keyNodeEntryOrPredicate);
|
|
449
447
|
// Set predicate based on parameter type
|
|
450
448
|
if (isRange) {
|
|
451
|
-
predicate = node =>
|
|
449
|
+
predicate = node => keyNodeEntryOrPredicate.isInRange(node.key, this._comparator);
|
|
452
450
|
}
|
|
453
451
|
else {
|
|
454
|
-
predicate = this._ensurePredicate(
|
|
452
|
+
predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
|
|
455
453
|
}
|
|
456
454
|
const isToLeftByRange = (cur) => {
|
|
457
455
|
if (isRange) {
|
|
458
|
-
const range =
|
|
456
|
+
const range = keyNodeEntryOrPredicate;
|
|
459
457
|
const leftS = this.isReverse ? range.high : range.low;
|
|
460
458
|
const leftI = this.isReverse ? range.includeHigh : range.includeLow;
|
|
461
459
|
return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
|
|
@@ -464,7 +462,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
464
462
|
};
|
|
465
463
|
const isToRightByRange = (cur) => {
|
|
466
464
|
if (isRange) {
|
|
467
|
-
const range =
|
|
465
|
+
const range = keyNodeEntryOrPredicate;
|
|
468
466
|
const rightS = this.isReverse ? range.low : range.high;
|
|
469
467
|
const rightI = this.isReverse ? range.includeLow : range.includeLow;
|
|
470
468
|
return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
|
|
@@ -487,8 +485,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
487
485
|
if (this.isRealNode(cur.right) && isToRightByRange(cur))
|
|
488
486
|
dfs(cur.right);
|
|
489
487
|
}
|
|
490
|
-
else if (!this._isPredicate(
|
|
491
|
-
const benchmarkKey = this._extractKey(
|
|
488
|
+
else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
489
|
+
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
|
|
492
490
|
if (this.isRealNode(cur.left) &&
|
|
493
491
|
benchmarkKey !== null &&
|
|
494
492
|
benchmarkKey !== undefined &&
|
|
@@ -524,8 +522,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
524
522
|
if (this.isRealNode(cur.right) && isToRightByRange(cur))
|
|
525
523
|
stack.push(cur.right);
|
|
526
524
|
}
|
|
527
|
-
else if (!this._isPredicate(
|
|
528
|
-
const benchmarkKey = this._extractKey(
|
|
525
|
+
else if (!this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
526
|
+
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate);
|
|
529
527
|
if (this.isRealNode(cur.right) &&
|
|
530
528
|
benchmarkKey !== null &&
|
|
531
529
|
benchmarkKey !== undefined &&
|
|
@@ -549,12 +547,37 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
549
547
|
}
|
|
550
548
|
/**
|
|
551
549
|
* Time Complexity: O(log n)
|
|
552
|
-
* Space Complexity: O(
|
|
550
|
+
* Space Complexity: O(k + log n)
|
|
553
551
|
*
|
|
554
|
-
*
|
|
555
|
-
* @param {
|
|
556
|
-
*
|
|
557
|
-
* @param {
|
|
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)
|
|
576
|
+
*
|
|
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
|
|
558
581
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
559
582
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
560
583
|
* node of the binary search tree.
|
|
@@ -562,14 +585,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
562
585
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
563
586
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
564
587
|
* no value is provided when calling the method.
|
|
565
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<
|
|
566
|
-
* 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
|
|
567
590
|
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
568
591
|
* returns the first node found or `undefined` if no node is found.
|
|
569
592
|
*/
|
|
570
|
-
getNode(
|
|
593
|
+
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
571
594
|
var _a;
|
|
572
|
-
return (_a = this.getNodes(
|
|
595
|
+
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
573
596
|
}
|
|
574
597
|
/**
|
|
575
598
|
* Time complexity: O(n)
|
|
@@ -583,7 +606,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
583
606
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
584
607
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
585
608
|
* take one of the following values:
|
|
586
|
-
* @param {BTNRep<K, V,
|
|
609
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
587
610
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
588
611
|
* node entry. If not specified, the default value is the root of the tree.
|
|
589
612
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -603,7 +626,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
603
626
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
604
627
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
605
628
|
* node being visited, and it can return a value of any type.
|
|
606
|
-
* @param {BTNRep<K, V,
|
|
629
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
607
630
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
608
631
|
* object. If no value is provided, the default value is the root of the tree.
|
|
609
632
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -621,9 +644,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
621
644
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
622
645
|
* containing the results of the callback function applied to each level of the tree.
|
|
623
646
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
624
|
-
* `NodeCallback<
|
|
647
|
+
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the
|
|
625
648
|
* tree during the iteration process.
|
|
626
|
-
* @param {BTNRep<K, V,
|
|
649
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
627
650
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
628
651
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
629
652
|
* value is provided, the root of
|
|
@@ -647,7 +670,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
647
670
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
648
671
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
649
672
|
* 0, or 1, where:
|
|
650
|
-
* @param {BTNRep<K, V,
|
|
673
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in
|
|
651
674
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
652
675
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
653
676
|
* `targetNode` is provided,
|
|
@@ -783,7 +806,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
783
806
|
while (stack.length > 0 || node) {
|
|
784
807
|
if (node) {
|
|
785
808
|
stack.push(node);
|
|
786
|
-
|
|
809
|
+
if (node.left !== null)
|
|
810
|
+
node = node.left;
|
|
787
811
|
}
|
|
788
812
|
else {
|
|
789
813
|
node = stack[stack.length - 1];
|
|
@@ -807,24 +831,70 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
807
831
|
return balanced;
|
|
808
832
|
}
|
|
809
833
|
/**
|
|
810
|
-
*
|
|
811
|
-
*
|
|
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.
|
|
851
|
+
*/
|
|
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.
|
|
812
867
|
*/
|
|
813
|
-
|
|
814
|
-
|
|
868
|
+
clone() {
|
|
869
|
+
const cloned = this.createTree();
|
|
870
|
+
this._clone(cloned);
|
|
871
|
+
return cloned;
|
|
815
872
|
}
|
|
816
873
|
/**
|
|
817
|
-
*
|
|
818
|
-
*
|
|
819
|
-
*
|
|
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.
|
|
820
884
|
*/
|
|
821
|
-
|
|
822
|
-
|
|
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];
|
|
823
890
|
}
|
|
824
891
|
/**
|
|
892
|
+
* Time Complexity: O(1)
|
|
893
|
+
* Space Complexity: O(1)
|
|
894
|
+
*
|
|
825
895
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
826
896
|
* root.
|
|
827
|
-
* @param {OptNode<
|
|
897
|
+
* @param {OptNode<BSTNode<K, V>>} v - v is a parameter of type BSTNode<K, V> or undefined.
|
|
828
898
|
*/
|
|
829
899
|
_setRoot(v) {
|
|
830
900
|
if (v) {
|
|
@@ -832,6 +902,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
832
902
|
}
|
|
833
903
|
this._root = v;
|
|
834
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
|
+
*/
|
|
835
919
|
_compare(a, b) {
|
|
836
920
|
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
|
|
837
921
|
}
|