data-structure-typed 1.51.7 → 1.51.9
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/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +1 -37
- package/benchmark/report.json +15 -387
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +492 -392
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/cjs/data-structures/binary-tree/bst.js +256 -358
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/cjs/data-structures/hash/hash-map.js +40 -55
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +1 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -3
- package/dist/cjs/data-structures/queue/deque.js +2 -3
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +1 -1
- package/dist/cjs/data-structures/trie/trie.js +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +7 -7
- package/dist/cjs/types/common.d.ts +2 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +10 -1
- package/dist/cjs/utils/utils.d.ts +2 -1
- package/dist/cjs/utils/utils.js +27 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +493 -393
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/mjs/data-structures/binary-tree/bst.js +252 -357
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/mjs/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/mjs/data-structures/hash/hash-map.js +41 -55
- package/dist/mjs/data-structures/heap/heap.d.ts +1 -3
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -3
- package/dist/mjs/data-structures/queue/deque.js +2 -3
- package/dist/mjs/data-structures/trie/trie.d.ts +1 -1
- package/dist/mjs/data-structures/trie/trie.js +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +7 -7
- package/dist/mjs/types/common.d.ts +2 -3
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +10 -1
- package/dist/mjs/utils/utils.d.ts +2 -1
- package/dist/mjs/utils/utils.js +25 -0
- package/dist/umd/data-structure-typed.js +1427 -1343
- package/dist/umd/data-structure-typed.min.js +4 -4
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
- package/src/data-structures/binary-tree/avl-tree.ts +109 -80
- package/src/data-structures/binary-tree/binary-tree.ts +556 -433
- package/src/data-structures/binary-tree/bst.ts +286 -375
- package/src/data-structures/binary-tree/rb-tree.ts +132 -125
- package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +11 -9
- package/src/types/common.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
- package/test/integration/all-in-one.test.ts +2 -2
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +3 -3
- package/test/integration/index.html +95 -11
- package/test/performance/reportor.ts +2 -3
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +75 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/overall.test.ts +24 -20
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +6 -6
- package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
- package/test/unit/utils/utils.test.ts +5 -0
- package/test/utils/big-o.ts +12 -6
- package/test/utils/console.ts +1 -1
- package/tsconfig-base.json +1 -0
|
@@ -59,25 +59,24 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
59
59
|
*/
|
|
60
60
|
export class BST extends BinaryTree {
|
|
61
61
|
/**
|
|
62
|
-
* This is the constructor function for a
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* to
|
|
66
|
-
* @param [options] -
|
|
67
|
-
*
|
|
68
|
-
*/
|
|
69
|
-
constructor(
|
|
62
|
+
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
63
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
64
|
+
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
65
|
+
* added to the binary search tree during the construction of the object.
|
|
66
|
+
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
67
|
+
* It can include a comparator function that defines the order of the elements in the tree.
|
|
68
|
+
*/
|
|
69
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
70
70
|
super([], options);
|
|
71
71
|
if (options) {
|
|
72
|
-
const {
|
|
73
|
-
if (
|
|
74
|
-
this.
|
|
72
|
+
const { comparator } = options;
|
|
73
|
+
if (comparator)
|
|
74
|
+
this._comparator = comparator;
|
|
75
75
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
this.addMany(keysOrNodesOrEntries);
|
|
76
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
77
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
79
78
|
}
|
|
80
|
-
_root;
|
|
79
|
+
_root = undefined;
|
|
81
80
|
/**
|
|
82
81
|
* The function returns the root node of a tree structure.
|
|
83
82
|
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
@@ -85,14 +84,6 @@ export class BST extends BinaryTree {
|
|
|
85
84
|
get root() {
|
|
86
85
|
return this._root;
|
|
87
86
|
}
|
|
88
|
-
_variant = 'STANDARD';
|
|
89
|
-
/**
|
|
90
|
-
* The function returns the value of the _variant property.
|
|
91
|
-
* @returns The value of the `_variant` property.
|
|
92
|
-
*/
|
|
93
|
-
get variant() {
|
|
94
|
-
return this._variant;
|
|
95
|
-
}
|
|
96
87
|
/**
|
|
97
88
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
98
89
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -107,110 +98,70 @@ export class BST extends BinaryTree {
|
|
|
107
98
|
/**
|
|
108
99
|
* The function creates a new binary search tree with the specified options.
|
|
109
100
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
110
|
-
* behavior of the `createTree` method. It
|
|
111
|
-
*
|
|
112
|
-
* @returns a new instance of the BST class
|
|
113
|
-
* options. The returned value is casted as TREE.
|
|
101
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
102
|
+
* following properties:
|
|
103
|
+
* @returns a new instance of the BST class with the provided options.
|
|
114
104
|
*/
|
|
115
105
|
createTree(options) {
|
|
116
106
|
return new BST([], {
|
|
117
107
|
iterationType: this.iterationType,
|
|
118
|
-
|
|
108
|
+
comparator: this.comparator,
|
|
119
109
|
...options
|
|
120
110
|
});
|
|
121
111
|
}
|
|
122
112
|
/**
|
|
123
|
-
* The function
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* `
|
|
128
|
-
*
|
|
129
|
-
|
|
130
|
-
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
131
|
-
let node;
|
|
132
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
136
|
-
node = keyOrNodeOrEntry;
|
|
137
|
-
}
|
|
138
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
139
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
140
|
-
if (key === undefined || key === null) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
node = this.createNode(key, value);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
148
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
return node;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Time Complexity: O(log n)
|
|
157
|
-
* Space Complexity: O(log n)
|
|
113
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
114
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
|
|
115
|
+
* type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
116
|
+
* element.
|
|
117
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
118
|
+
* value associated with a key in a key-value pair.
|
|
119
|
+
* @returns either a NODE object or undefined.
|
|
158
120
|
*/
|
|
121
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
122
|
+
return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
|
|
123
|
+
}
|
|
159
124
|
/**
|
|
160
125
|
* Time Complexity: O(log n)
|
|
161
126
|
* Space Complexity: O(log n)
|
|
162
127
|
*
|
|
163
|
-
* The function
|
|
164
|
-
*
|
|
165
|
-
* @param {
|
|
166
|
-
* `
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
178
|
-
const key = keyOrNodeOrEntry[0];
|
|
179
|
-
if (key === null || key === undefined)
|
|
180
|
-
return;
|
|
181
|
-
return this.getNodeByKey(key, iterationType);
|
|
182
|
-
}
|
|
183
|
-
const key = keyOrNodeOrEntry;
|
|
184
|
-
if (key === null || key === undefined)
|
|
185
|
-
return;
|
|
186
|
-
return this.getNodeByKey(key, iterationType);
|
|
128
|
+
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
129
|
+
* it doesn't exist.
|
|
130
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
131
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
|
|
132
|
+
* entry, or raw element that needs to be ensured in the tree.
|
|
133
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
134
|
+
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
135
|
+
* value of `'ITERATIVE'`.
|
|
136
|
+
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
137
|
+
* not be ensured.
|
|
138
|
+
*/
|
|
139
|
+
ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
|
|
140
|
+
return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
|
|
187
141
|
}
|
|
188
142
|
/**
|
|
189
|
-
* The function checks if
|
|
190
|
-
* @param
|
|
191
|
-
*
|
|
143
|
+
* The function checks if the input is an instance of the BSTNode class.
|
|
144
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
145
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
146
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
147
|
+
* an instance of the `BSTNode` class.
|
|
192
148
|
*/
|
|
193
|
-
isNode(
|
|
194
|
-
return
|
|
149
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
150
|
+
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
195
151
|
}
|
|
196
|
-
/**
|
|
197
|
-
* Time Complexity: O(log n)
|
|
198
|
-
* Space Complexity: O(1)
|
|
199
|
-
*/
|
|
200
152
|
/**
|
|
201
153
|
* Time Complexity: O(log n)
|
|
202
154
|
* Space Complexity: O(1)
|
|
203
155
|
*
|
|
204
|
-
* The `add` function adds a new node to a binary tree
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
* @param {V} [value] - The `value` parameter
|
|
208
|
-
*
|
|
209
|
-
* @returns
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
156
|
+
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
157
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
158
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
159
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
160
|
+
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
161
|
+
* @returns a boolean value.
|
|
162
|
+
*/
|
|
163
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
164
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
214
165
|
if (newNode === undefined)
|
|
215
166
|
return false;
|
|
216
167
|
if (this.root === undefined) {
|
|
@@ -220,18 +171,11 @@ export class BST extends BinaryTree {
|
|
|
220
171
|
}
|
|
221
172
|
let current = this.root;
|
|
222
173
|
while (current !== undefined) {
|
|
223
|
-
if (this.
|
|
224
|
-
// if (current !== newNode) {
|
|
225
|
-
// The key value is the same but the reference is different, update the value of the existing node
|
|
174
|
+
if (this.comparator(current.key, newNode.key) === 0) {
|
|
226
175
|
this._replaceNode(current, newNode);
|
|
227
176
|
return true;
|
|
228
|
-
// } else {
|
|
229
|
-
// The key value is the same and the reference is the same, replace the entire node
|
|
230
|
-
// this._replaceNode(current, newNode);
|
|
231
|
-
// return;
|
|
232
|
-
// }
|
|
233
177
|
}
|
|
234
|
-
else if (this.
|
|
178
|
+
else if (this.comparator(current.key, newNode.key) > 0) {
|
|
235
179
|
if (current.left === undefined) {
|
|
236
180
|
current.left = newNode;
|
|
237
181
|
this._size++;
|
|
@@ -251,37 +195,38 @@ export class BST extends BinaryTree {
|
|
|
251
195
|
return false;
|
|
252
196
|
}
|
|
253
197
|
/**
|
|
254
|
-
* Time Complexity: O(
|
|
255
|
-
* Space Complexity: O(
|
|
198
|
+
* Time Complexity: O(log n)
|
|
199
|
+
* Space Complexity: O(log n)
|
|
256
200
|
*/
|
|
257
201
|
/**
|
|
258
202
|
* Time Complexity: O(k log n)
|
|
259
203
|
* Space Complexity: O(k + log n)
|
|
260
204
|
*
|
|
261
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a
|
|
262
|
-
*
|
|
263
|
-
* @param
|
|
264
|
-
* the
|
|
205
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
206
|
+
* an array indicating whether each key or node was successfully inserted.
|
|
207
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
208
|
+
* elements to be added to the data structure.
|
|
265
209
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
266
210
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
267
211
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
268
|
-
* @param [isBalanceAdd=true] - A boolean flag indicating whether the
|
|
269
|
-
*
|
|
270
|
-
* algorithm. If set to false, the
|
|
271
|
-
*
|
|
272
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
273
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
274
|
-
*
|
|
275
|
-
* @returns The function `addMany` returns an array of
|
|
276
|
-
|
|
277
|
-
|
|
212
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
213
|
+
* adding the elements. If set to true, the tree will be balanced using a binary search tree
|
|
214
|
+
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
215
|
+
* value is true.
|
|
216
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
217
|
+
* specifies the type of iteration to use when adding multiple keys or nodes to the binary search
|
|
218
|
+
* tree. It can have two possible values:
|
|
219
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
220
|
+
* successfully inserted into the data structure.
|
|
221
|
+
*/
|
|
222
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
278
223
|
const inserted = [];
|
|
279
224
|
let valuesIterator;
|
|
280
225
|
if (values) {
|
|
281
226
|
valuesIterator = values[Symbol.iterator]();
|
|
282
227
|
}
|
|
283
228
|
if (!isBalanceAdd) {
|
|
284
|
-
for (const kve of
|
|
229
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
285
230
|
const value = valuesIterator?.next().value;
|
|
286
231
|
const nn = this.add(kve, value);
|
|
287
232
|
inserted.push(nn);
|
|
@@ -294,25 +239,36 @@ export class BST extends BinaryTree {
|
|
|
294
239
|
return false;
|
|
295
240
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
296
241
|
};
|
|
297
|
-
for (const kve of
|
|
242
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
298
243
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
299
244
|
}
|
|
300
245
|
let sorted = [];
|
|
301
246
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
302
|
-
let
|
|
247
|
+
let keyA, keyB;
|
|
303
248
|
if (this.isEntry(a))
|
|
304
|
-
|
|
249
|
+
keyA = a[0];
|
|
305
250
|
else if (this.isRealNode(a))
|
|
306
|
-
|
|
307
|
-
else
|
|
308
|
-
|
|
251
|
+
keyA = a.key;
|
|
252
|
+
else if (this.toEntryFn) {
|
|
253
|
+
keyA = this.toEntryFn(a)[0];
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
keyA = a;
|
|
257
|
+
}
|
|
309
258
|
if (this.isEntry(b))
|
|
310
|
-
|
|
259
|
+
keyB = b[0];
|
|
311
260
|
else if (this.isRealNode(b))
|
|
312
|
-
|
|
313
|
-
else
|
|
314
|
-
|
|
315
|
-
|
|
261
|
+
keyB = b.key;
|
|
262
|
+
else if (this.toEntryFn) {
|
|
263
|
+
keyB = this.toEntryFn(b)[0];
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
keyB = b;
|
|
267
|
+
}
|
|
268
|
+
if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
|
|
269
|
+
return this.comparator(keyA, keyB);
|
|
270
|
+
}
|
|
271
|
+
return 0;
|
|
316
272
|
});
|
|
317
273
|
const _dfs = (arr) => {
|
|
318
274
|
if (arr.length === 0)
|
|
@@ -349,32 +305,26 @@ export class BST extends BinaryTree {
|
|
|
349
305
|
return inserted;
|
|
350
306
|
}
|
|
351
307
|
/**
|
|
352
|
-
* Time Complexity: O(log n)
|
|
353
|
-
* Space Complexity: O(k + log n)
|
|
354
|
-
* /
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
308
|
* Time Complexity: O(log n)
|
|
358
309
|
* Space Complexity: O(k + log n)
|
|
359
310
|
*
|
|
360
|
-
* The
|
|
361
|
-
*
|
|
311
|
+
* The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
|
|
312
|
+
* identifier and callback function.
|
|
362
313
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
363
|
-
* want to search for in the
|
|
364
|
-
*
|
|
365
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node
|
|
366
|
-
*
|
|
367
|
-
* function
|
|
368
|
-
* @param [onlyOne=false] - A boolean
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
373
|
-
*
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
* @returns The method returns an array of nodes (`NODE[]`).
|
|
314
|
+
* want to search for in the binary tree. It can be of any type that is returned by the callback
|
|
315
|
+
* function.
|
|
316
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
317
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
318
|
+
* `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
319
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
|
|
320
|
+
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
|
|
321
|
+
* false, all matching nodes will be returned. The default value is false.
|
|
322
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
323
|
+
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
|
|
324
|
+
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
|
|
325
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
326
|
+
* iteration to be performed. It can have two possible values:
|
|
327
|
+
* @returns The method `getNodes` returns an array of `NODE` objects.
|
|
378
328
|
*/
|
|
379
329
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
380
330
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -394,9 +344,9 @@ export class BST extends BinaryTree {
|
|
|
394
344
|
return;
|
|
395
345
|
// TODO potential bug
|
|
396
346
|
if (callback === this._DEFAULT_CALLBACK) {
|
|
397
|
-
if (this.isRealNode(cur.left) && this.
|
|
347
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
398
348
|
dfs(cur.left);
|
|
399
|
-
if (this.isRealNode(cur.right) && this.
|
|
349
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
400
350
|
dfs(cur.right);
|
|
401
351
|
}
|
|
402
352
|
else {
|
|
@@ -418,9 +368,9 @@ export class BST extends BinaryTree {
|
|
|
418
368
|
}
|
|
419
369
|
// TODO potential bug
|
|
420
370
|
if (callback === this._DEFAULT_CALLBACK) {
|
|
421
|
-
if (this.isRealNode(cur.right) && this.
|
|
371
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
422
372
|
stack.push(cur.right);
|
|
423
|
-
if (this.isRealNode(cur.left) && this.
|
|
373
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
424
374
|
stack.push(cur.left);
|
|
425
375
|
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
|
|
426
376
|
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
|
|
@@ -445,50 +395,49 @@ export class BST extends BinaryTree {
|
|
|
445
395
|
* Time Complexity: O(log n)
|
|
446
396
|
* Space Complexity: O(1)
|
|
447
397
|
*
|
|
448
|
-
* The `getNode`
|
|
449
|
-
*
|
|
450
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value
|
|
451
|
-
*
|
|
452
|
-
*
|
|
453
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
454
|
-
* the
|
|
455
|
-
*
|
|
456
|
-
*
|
|
398
|
+
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
399
|
+
* function in a binary search tree.
|
|
400
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
401
|
+
* want to search for in the binary search tree. It can be of any type that is compatible with the
|
|
402
|
+
* type returned by the callback function.
|
|
403
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
404
|
+
* node matches the desired criteria. It should be a function that takes a node as an argument and
|
|
405
|
+
* returns a boolean value indicating whether the node matches the criteria or not. If no callback is
|
|
406
|
+
* provided, the default callback will be
|
|
457
407
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
458
|
-
* search tree. It can be either a key or a node. If it is a key,
|
|
459
|
-
*
|
|
460
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
461
|
-
* be performed when searching for nodes in the binary search tree. It
|
|
462
|
-
*
|
|
463
|
-
* @returns The method is returning a
|
|
408
|
+
* search tree. It can be either a key or a node. If it is a key, the search will start from the node
|
|
409
|
+
* with that key. If it is a node, the search will start from that node.
|
|
410
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
411
|
+
* of iteration to be performed when searching for nodes in the binary search tree. It can have one
|
|
412
|
+
* of the following values:
|
|
413
|
+
* @returns The method is returning a NODE object or undefined.
|
|
464
414
|
*/
|
|
465
415
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
466
416
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
467
417
|
}
|
|
468
418
|
/**
|
|
469
|
-
* Time Complexity: O(log n)
|
|
470
|
-
* Space Complexity: O(
|
|
419
|
+
* Time Complexity: O(k log n)
|
|
420
|
+
* Space Complexity: O(k + log n)
|
|
471
421
|
*/
|
|
472
422
|
/**
|
|
473
423
|
* Time Complexity: O(log n)
|
|
474
424
|
* Space Complexity: O(1)
|
|
475
425
|
*
|
|
476
|
-
* The function `getNodeByKey`
|
|
477
|
-
*
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
* @param iterationType - The `iterationType` parameter is an optional
|
|
481
|
-
* type of iteration to
|
|
482
|
-
*
|
|
483
|
-
* @returns The
|
|
484
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
426
|
+
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
427
|
+
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
428
|
+
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
429
|
+
* in the tree structure.
|
|
430
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
431
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
432
|
+
* It has a default value of `'ITERATIVE'`.
|
|
433
|
+
* @returns The method is returning a NODE object or undefined.
|
|
485
434
|
*/
|
|
486
435
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
487
436
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
488
437
|
}
|
|
489
438
|
/**
|
|
490
|
-
* Time
|
|
491
|
-
* Space
|
|
439
|
+
* Time Complexity: O(log n)
|
|
440
|
+
* Space Complexity: O(k + log n)
|
|
492
441
|
*/
|
|
493
442
|
/**
|
|
494
443
|
* Time complexity: O(n)
|
|
@@ -497,15 +446,16 @@ export class BST extends BinaryTree {
|
|
|
497
446
|
* The function overrides the depth-first search method and returns an array of the return types of
|
|
498
447
|
* the callback function.
|
|
499
448
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
500
|
-
* during the depth-first search traversal. It is an optional parameter and
|
|
501
|
-
*
|
|
502
|
-
* @param {DFSOrderPattern} [pattern=
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
*
|
|
508
|
-
*
|
|
449
|
+
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
450
|
+
* `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
|
|
451
|
+
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
452
|
+
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
453
|
+
* take one of the following values:
|
|
454
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
455
|
+
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
456
|
+
* node entry. If not specified, the default value is the root of the tree.
|
|
457
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
458
|
+
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
509
459
|
* following values:
|
|
510
460
|
* @returns The method is returning an array of the return type of the callback function.
|
|
511
461
|
*/
|
|
@@ -513,8 +463,8 @@ export class BST extends BinaryTree {
|
|
|
513
463
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
514
464
|
}
|
|
515
465
|
/**
|
|
516
|
-
* Time
|
|
517
|
-
* Space
|
|
466
|
+
* Time Complexity: O(log n)
|
|
467
|
+
* Space Complexity: O(1)
|
|
518
468
|
*/
|
|
519
469
|
/**
|
|
520
470
|
* Time complexity: O(n)
|
|
@@ -523,38 +473,38 @@ export class BST extends BinaryTree {
|
|
|
523
473
|
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
524
474
|
* the callback function.
|
|
525
475
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
526
|
-
* visited during the breadth-first search
|
|
527
|
-
*
|
|
528
|
-
* @param beginRoot - The `beginRoot` parameter is the starting
|
|
529
|
-
*
|
|
530
|
-
* the
|
|
531
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
532
|
-
* be performed during the breadth-first search (BFS) traversal. It
|
|
533
|
-
*
|
|
534
|
-
* @returns
|
|
476
|
+
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
477
|
+
* node being visited, and it can return a value of any type.
|
|
478
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
479
|
+
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
480
|
+
* object. If no value is provided, the default value is the root of the tree.
|
|
481
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
482
|
+
* of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
|
|
483
|
+
* the following values:
|
|
484
|
+
* @returns an array of the return type of the callback function.
|
|
535
485
|
*/
|
|
536
486
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
537
487
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
538
488
|
}
|
|
539
489
|
/**
|
|
540
|
-
* Time
|
|
541
|
-
* Space
|
|
490
|
+
* Time Complexity: O(log n)
|
|
491
|
+
* Space Complexity: O(1)
|
|
542
492
|
*/
|
|
543
493
|
/**
|
|
544
494
|
* Time complexity: O(n)
|
|
545
495
|
* Space complexity: O(n)
|
|
546
496
|
*
|
|
547
|
-
* The function overrides the listLevels method and returns an array of arrays
|
|
548
|
-
*
|
|
497
|
+
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
498
|
+
* containing the results of the callback function applied to each level of the tree.
|
|
549
499
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
550
|
-
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
551
|
-
* during the
|
|
552
|
-
* @param beginRoot - The `beginRoot` parameter is
|
|
553
|
-
* levels of
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
* iteration.
|
|
500
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
501
|
+
* tree during the iteration process.
|
|
502
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
503
|
+
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
504
|
+
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
505
|
+
* value is provided, the root of
|
|
506
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
507
|
+
* of iteration to be performed on the tree. It can have one of the following values:
|
|
558
508
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
559
509
|
* function.
|
|
560
510
|
*/
|
|
@@ -562,77 +512,42 @@ export class BST extends BinaryTree {
|
|
|
562
512
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
563
513
|
}
|
|
564
514
|
/**
|
|
565
|
-
* Time
|
|
566
|
-
* Space
|
|
567
|
-
*/
|
|
568
|
-
/**
|
|
569
|
-
* Time Complexity: O(log n)
|
|
570
|
-
* Space Complexity: O(1)
|
|
571
|
-
*
|
|
572
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
573
|
-
* leftmost node if the comparison result is greater than.
|
|
574
|
-
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
575
|
-
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
576
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
577
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
578
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
579
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
580
|
-
*/
|
|
581
|
-
lastKey(beginRoot = this.root) {
|
|
582
|
-
let current = this.ensureNode(beginRoot);
|
|
583
|
-
if (!current)
|
|
584
|
-
return undefined;
|
|
585
|
-
if (this._variant === 'STANDARD') {
|
|
586
|
-
// For 'STANDARD', find the rightmost node
|
|
587
|
-
while (current.right !== undefined) {
|
|
588
|
-
current = current.right;
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
else {
|
|
592
|
-
// For BSTVariant.MAX, find the leftmost node
|
|
593
|
-
while (current.left !== undefined) {
|
|
594
|
-
current = current.left;
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
return current.key;
|
|
598
|
-
}
|
|
599
|
-
/**
|
|
600
|
-
* Time Complexity: O(log n)
|
|
601
|
-
* Space Complexity: O(log n)
|
|
515
|
+
* Time complexity: O(n)
|
|
516
|
+
* Space complexity: O(n)
|
|
602
517
|
*/
|
|
603
518
|
/**
|
|
604
|
-
* Time
|
|
605
|
-
* Space
|
|
519
|
+
* Time complexity: O(n)
|
|
520
|
+
* Space complexity: O(n)
|
|
606
521
|
*
|
|
607
|
-
* The `lesserOrGreaterTraverse` function traverses a binary tree and
|
|
608
|
-
*
|
|
522
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
523
|
+
* each node that meets a certain condition based on a target node and a comparison value.
|
|
609
524
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
610
|
-
* that
|
|
611
|
-
*
|
|
525
|
+
* that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
|
|
526
|
+
* which is the current node being traversed, and returns a value of any type.
|
|
612
527
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
613
|
-
* traverse nodes that are lesser
|
|
614
|
-
*
|
|
615
|
-
* `
|
|
616
|
-
*
|
|
617
|
-
*
|
|
618
|
-
*
|
|
619
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
620
|
-
* performed on the binary tree. It can have two possible values:
|
|
528
|
+
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
529
|
+
* 0, or 1, where:
|
|
530
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
|
|
531
|
+
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
532
|
+
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
533
|
+
* `targetNode` is provided,
|
|
534
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
535
|
+
* traversal to be performed on the binary tree. It can have two possible values:
|
|
621
536
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
622
537
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
623
538
|
*/
|
|
624
|
-
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater =
|
|
625
|
-
|
|
539
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = -1, targetNode = this.root, iterationType = this.iterationType) {
|
|
540
|
+
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
626
541
|
const ans = [];
|
|
627
|
-
if (!
|
|
542
|
+
if (!targetNodeEnsured)
|
|
628
543
|
return ans;
|
|
629
544
|
if (!this.root)
|
|
630
545
|
return ans;
|
|
631
|
-
const targetKey =
|
|
546
|
+
const targetKey = targetNodeEnsured.key;
|
|
632
547
|
if (iterationType === 'RECURSIVE') {
|
|
633
548
|
const dfs = (cur) => {
|
|
634
|
-
const compared = this.
|
|
635
|
-
if (compared === lesserOrGreater)
|
|
549
|
+
const compared = this.comparator(cur.key, targetKey);
|
|
550
|
+
if (Math.sign(compared) === lesserOrGreater)
|
|
636
551
|
ans.push(callback(cur));
|
|
637
552
|
if (this.isRealNode(cur.left))
|
|
638
553
|
dfs(cur.left);
|
|
@@ -647,8 +562,8 @@ export class BST extends BinaryTree {
|
|
|
647
562
|
while (queue.size > 0) {
|
|
648
563
|
const cur = queue.shift();
|
|
649
564
|
if (this.isRealNode(cur)) {
|
|
650
|
-
const compared = this.
|
|
651
|
-
if (compared === lesserOrGreater)
|
|
565
|
+
const compared = this.comparator(cur.key, targetKey);
|
|
566
|
+
if (Math.sign(compared) === lesserOrGreater)
|
|
652
567
|
ans.push(callback(cur));
|
|
653
568
|
if (this.isRealNode(cur.left))
|
|
654
569
|
queue.push(cur.left);
|
|
@@ -660,18 +575,19 @@ export class BST extends BinaryTree {
|
|
|
660
575
|
}
|
|
661
576
|
}
|
|
662
577
|
/**
|
|
663
|
-
* Time
|
|
664
|
-
* Space
|
|
578
|
+
* Time complexity: O(n)
|
|
579
|
+
* Space complexity: O(n)
|
|
665
580
|
*/
|
|
666
581
|
/**
|
|
667
|
-
* Time
|
|
668
|
-
* Space
|
|
582
|
+
* Time complexity: O(n)
|
|
583
|
+
* Space complexity: O(n)
|
|
669
584
|
*
|
|
670
|
-
* The `perfectlyBalance` function
|
|
671
|
-
*
|
|
672
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
673
|
-
* type of iteration to use when building a balanced binary search tree. It
|
|
674
|
-
*
|
|
585
|
+
* The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
|
|
586
|
+
* the binary search tree is perfectly balanced, otherwise it returns `false`.
|
|
587
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
588
|
+
* specifies the type of iteration to use when building a balanced binary search tree. It has a
|
|
589
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
590
|
+
* current instance of the class.
|
|
675
591
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
676
592
|
*/
|
|
677
593
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
@@ -711,25 +627,19 @@ export class BST extends BinaryTree {
|
|
|
711
627
|
}
|
|
712
628
|
}
|
|
713
629
|
/**
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
717
|
-
*
|
|
718
|
-
* Use Cases and Efficiency:
|
|
719
|
-
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
720
|
-
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
721
|
-
*/
|
|
722
|
-
/**
|
|
723
|
-
* Time Complexity: O(n)
|
|
724
|
-
* Space Complexity: O(log n)
|
|
630
|
+
* Time complexity: O(n)
|
|
631
|
+
* Space complexity: O(n)
|
|
725
632
|
*/
|
|
726
633
|
/**
|
|
727
634
|
* Time Complexity: O(n)
|
|
728
635
|
* Space Complexity: O(log n)
|
|
729
636
|
*
|
|
730
|
-
* The function checks if a binary tree is AVL balanced using either recursive or
|
|
731
|
-
*
|
|
732
|
-
*
|
|
637
|
+
* The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
|
|
638
|
+
* iterative approach.
|
|
639
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
640
|
+
* specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
|
|
641
|
+
* value of `this.iterationType`, which means it will use the iteration type specified in the current
|
|
642
|
+
* instance of the AVL tree.
|
|
733
643
|
* @returns a boolean value.
|
|
734
644
|
*/
|
|
735
645
|
isAVLBalanced(iterationType = this.iterationType) {
|
|
@@ -778,59 +688,44 @@ export class BST extends BinaryTree {
|
|
|
778
688
|
return balanced;
|
|
779
689
|
}
|
|
780
690
|
/**
|
|
781
|
-
*
|
|
782
|
-
*
|
|
783
|
-
* can either be an object of type `NODE` or it can be `undefined`.
|
|
691
|
+
* Time complexity: O(n)
|
|
692
|
+
* Space complexity: O(n)
|
|
784
693
|
*/
|
|
785
|
-
|
|
786
|
-
if (
|
|
787
|
-
|
|
694
|
+
_DEFAULT_COMPARATOR = (a, b) => {
|
|
695
|
+
if (typeof a === 'object' && typeof b === 'object' && this.comparator === this._DEFAULT_COMPARATOR) {
|
|
696
|
+
throw TypeError('When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.');
|
|
788
697
|
}
|
|
789
|
-
|
|
790
|
-
|
|
698
|
+
if (a > b)
|
|
699
|
+
return 1;
|
|
700
|
+
if (a < b)
|
|
701
|
+
return -1;
|
|
702
|
+
return 0;
|
|
703
|
+
};
|
|
791
704
|
/**
|
|
792
|
-
*
|
|
793
|
-
*
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
797
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
798
|
-
*/
|
|
799
|
-
_compare(a, b) {
|
|
800
|
-
const extractedA = this.extractor(a);
|
|
801
|
-
const extractedB = this.extractor(b);
|
|
802
|
-
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
|
|
803
|
-
if (compared > 0)
|
|
804
|
-
return 'GT';
|
|
805
|
-
if (compared < 0)
|
|
806
|
-
return 'LT';
|
|
807
|
-
return 'EQ';
|
|
808
|
-
}
|
|
705
|
+
* Time complexity: O(n)
|
|
706
|
+
* Space complexity: O(n)
|
|
707
|
+
*/
|
|
708
|
+
_comparator = this._DEFAULT_COMPARATOR;
|
|
809
709
|
/**
|
|
810
|
-
*
|
|
811
|
-
*
|
|
812
|
-
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
813
|
-
* first value to be compared in the function.
|
|
814
|
-
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
815
|
-
* of the arguments for the comparison in the `_lt` function.
|
|
816
|
-
* @returns a boolean value.
|
|
710
|
+
* Time Complexity: O(n)
|
|
711
|
+
* Space Complexity: O(log n)
|
|
817
712
|
*/
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
713
|
+
/**
|
|
714
|
+
* The function returns the value of the _comparator property.
|
|
715
|
+
* @returns The `_comparator` property is being returned.
|
|
716
|
+
*/
|
|
717
|
+
get comparator() {
|
|
718
|
+
return this._comparator;
|
|
822
719
|
}
|
|
823
720
|
/**
|
|
824
|
-
* The function
|
|
825
|
-
*
|
|
826
|
-
* @param {
|
|
827
|
-
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
828
|
-
* of the arguments for the comparison in the function.
|
|
829
|
-
* @returns a boolean value.
|
|
721
|
+
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
722
|
+
* root.
|
|
723
|
+
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
830
724
|
*/
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
725
|
+
_setRoot(v) {
|
|
726
|
+
if (v) {
|
|
727
|
+
v.parent = undefined;
|
|
728
|
+
}
|
|
729
|
+
this._root = v;
|
|
835
730
|
}
|
|
836
731
|
}
|