doubly-linked-list-typed 1.51.8 → 1.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -226
- package/dist/data-structures/binary-tree/binary-tree.js +475 -363
- package/dist/data-structures/binary-tree/bst.d.ts +192 -202
- package/dist/data-structures/binary-tree/bst.js +207 -249
- package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +21 -20
- package/dist/data-structures/queue/deque.js +29 -23
- package/dist/data-structures/queue/queue.d.ts +8 -28
- package/dist/data-structures/queue/queue.js +15 -31
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +19 -14
- package/dist/data-structures/trie/trie.js +27 -16
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +1 -2
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
- package/src/data-structures/binary-tree/avl-tree.ts +96 -69
- package/src/data-structures/binary-tree/binary-tree.ts +535 -403
- package/src/data-structures/binary-tree/bst.ts +247 -277
- package/src/data-structures/binary-tree/rb-tree.ts +123 -103
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +46 -53
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +37 -26
- package/src/data-structures/queue/queue.ts +23 -36
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +35 -20
- package/src/interfaces/binary-tree.ts +9 -9
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/bst.ts +4 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +3 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/utils/utils.ts +3 -3
|
@@ -11,23 +11,23 @@ import type {
|
|
|
11
11
|
BSTNodeNested,
|
|
12
12
|
BSTOptions,
|
|
13
13
|
BTNCallback,
|
|
14
|
-
|
|
15
|
-
Comparable,
|
|
14
|
+
BTNPureKeyOrNodeOrEntry,
|
|
16
15
|
Comparator,
|
|
17
16
|
CP,
|
|
18
17
|
DFSOrderPattern,
|
|
19
18
|
IterationType,
|
|
20
19
|
KeyOrNodeOrEntry
|
|
21
20
|
} from '../../types';
|
|
21
|
+
import { BTNEntry } from '../../types';
|
|
22
22
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
23
23
|
import { IBinaryTree } from '../../interfaces';
|
|
24
24
|
import { Queue } from '../queue';
|
|
25
25
|
|
|
26
|
-
export class BSTNode<
|
|
27
|
-
K
|
|
28
|
-
V
|
|
29
|
-
NODE
|
|
30
|
-
>
|
|
26
|
+
export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<
|
|
27
|
+
K,
|
|
28
|
+
V,
|
|
29
|
+
NODE
|
|
30
|
+
> {
|
|
31
31
|
override parent?: NODE;
|
|
32
32
|
|
|
33
33
|
constructor(key: K, value?: V) {
|
|
@@ -93,23 +93,26 @@ export class BSTNode<
|
|
|
93
93
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
94
94
|
*/
|
|
95
95
|
export class BST<
|
|
96
|
-
K
|
|
96
|
+
K = any,
|
|
97
97
|
V = any,
|
|
98
|
+
R = BTNEntry<K, V>,
|
|
98
99
|
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
99
|
-
TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>
|
|
100
|
+
TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
|
|
100
101
|
>
|
|
101
|
-
extends BinaryTree<K, V, NODE, TREE>
|
|
102
|
-
implements IBinaryTree<K, V, NODE, TREE> {
|
|
103
|
-
/**
|
|
104
|
-
* This is the constructor function for a Binary Search Tree class in TypeScript
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
102
|
+
extends BinaryTree<K, V, R, NODE, TREE>
|
|
103
|
+
implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
104
|
+
/**
|
|
105
|
+
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
106
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
107
|
+
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
108
|
+
* added to the binary search tree during the construction of the object.
|
|
109
|
+
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
110
|
+
* It can include a comparator function that defines the order of the elements in the tree.
|
|
111
|
+
*/
|
|
112
|
+
constructor(
|
|
113
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
114
|
+
options?: BSTOptions<K, V, R>
|
|
115
|
+
) {
|
|
113
116
|
super([], options);
|
|
114
117
|
|
|
115
118
|
if (options) {
|
|
@@ -117,7 +120,7 @@ export class BST<
|
|
|
117
120
|
if (comparator) this._comparator = comparator;
|
|
118
121
|
}
|
|
119
122
|
|
|
120
|
-
if (
|
|
123
|
+
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
121
124
|
}
|
|
122
125
|
|
|
123
126
|
protected override _root?: NODE = undefined;
|
|
@@ -130,20 +133,6 @@ export class BST<
|
|
|
130
133
|
return this._root;
|
|
131
134
|
}
|
|
132
135
|
|
|
133
|
-
protected _comparator: Comparator<K> = (a: K, b: K): CP => {
|
|
134
|
-
if (a > b) return 1;
|
|
135
|
-
if (a < b) return -1;
|
|
136
|
-
return 0;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* The function returns the value of the _comparator property.
|
|
141
|
-
* @returns The `_comparator` property is being returned.
|
|
142
|
-
*/
|
|
143
|
-
get comparator() {
|
|
144
|
-
return this._comparator;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
136
|
/**
|
|
148
137
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
149
138
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -159,13 +148,12 @@ export class BST<
|
|
|
159
148
|
/**
|
|
160
149
|
* The function creates a new binary search tree with the specified options.
|
|
161
150
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
162
|
-
* behavior of the `createTree` method. It
|
|
163
|
-
*
|
|
164
|
-
* @returns a new instance of the BST class
|
|
165
|
-
* options. The returned value is casted as TREE.
|
|
151
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
152
|
+
* following properties:
|
|
153
|
+
* @returns a new instance of the BST class with the provided options.
|
|
166
154
|
*/
|
|
167
|
-
override createTree(options?: Partial<BSTOptions<K>>): TREE {
|
|
168
|
-
return new BST<K, V, NODE, TREE>([], {
|
|
155
|
+
override createTree(options?: Partial<BSTOptions<K, V, R>>): TREE {
|
|
156
|
+
return new BST<K, V, R, NODE, TREE>([], {
|
|
169
157
|
iterationType: this.iterationType,
|
|
170
158
|
comparator: this.comparator,
|
|
171
159
|
...options
|
|
@@ -173,97 +161,69 @@ export class BST<
|
|
|
173
161
|
}
|
|
174
162
|
|
|
175
163
|
/**
|
|
176
|
-
* The function
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
* `
|
|
181
|
-
*
|
|
182
|
-
|
|
183
|
-
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
184
|
-
let node: NODE | undefined;
|
|
185
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
186
|
-
return;
|
|
187
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
188
|
-
node = keyOrNodeOrEntry;
|
|
189
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
190
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
191
|
-
if (key === undefined || key === null) {
|
|
192
|
-
return;
|
|
193
|
-
} else {
|
|
194
|
-
node = this.createNode(key, value);
|
|
195
|
-
}
|
|
196
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
197
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
198
|
-
} else {
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
return node;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Time Complexity: O(log n)
|
|
206
|
-
* Space Complexity: O(log n)
|
|
164
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
165
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
|
|
166
|
+
* type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
167
|
+
* element.
|
|
168
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
169
|
+
* value associated with a key in a key-value pair.
|
|
170
|
+
* @returns either a NODE object or undefined.
|
|
207
171
|
*/
|
|
172
|
+
override keyValueOrEntryOrRawElementToNode(
|
|
173
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
174
|
+
value?: V
|
|
175
|
+
): NODE | undefined {
|
|
176
|
+
return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
|
|
177
|
+
}
|
|
208
178
|
|
|
209
179
|
/**
|
|
210
180
|
* Time Complexity: O(log n)
|
|
211
181
|
* Space Complexity: O(log n)
|
|
212
182
|
*
|
|
213
|
-
* The function
|
|
214
|
-
*
|
|
215
|
-
* @param {
|
|
216
|
-
* `
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
183
|
+
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
184
|
+
* it doesn't exist.
|
|
185
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
186
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
|
|
187
|
+
* entry, or raw element that needs to be ensured in the tree.
|
|
188
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
189
|
+
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
190
|
+
* value of `'ITERATIVE'`.
|
|
191
|
+
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
192
|
+
* not be ensured.
|
|
220
193
|
*/
|
|
221
194
|
override ensureNode(
|
|
222
|
-
|
|
195
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
|
|
223
196
|
iterationType: IterationType = 'ITERATIVE'
|
|
224
197
|
): NODE | undefined {
|
|
225
|
-
|
|
226
|
-
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
227
|
-
return keyOrNodeOrEntry;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
231
|
-
const key = keyOrNodeOrEntry[0];
|
|
232
|
-
if (key === null || key === undefined) return;
|
|
233
|
-
return this.getNodeByKey(key, iterationType);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const key = keyOrNodeOrEntry;
|
|
237
|
-
if (key === null || key === undefined) return;
|
|
238
|
-
return this.getNodeByKey(key, iterationType);
|
|
198
|
+
return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
|
|
239
199
|
}
|
|
240
200
|
|
|
241
201
|
/**
|
|
242
|
-
* The function checks if
|
|
243
|
-
* @param
|
|
244
|
-
*
|
|
202
|
+
* The function checks if the input is an instance of the BSTNode class.
|
|
203
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
204
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
205
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
206
|
+
* an instance of the `BSTNode` class.
|
|
245
207
|
*/
|
|
246
|
-
override isNode(
|
|
247
|
-
|
|
208
|
+
override isNode(
|
|
209
|
+
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
|
|
210
|
+
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
211
|
+
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
248
212
|
}
|
|
249
213
|
|
|
250
|
-
/**
|
|
251
|
-
* Time Complexity: O(log n)
|
|
252
|
-
* Space Complexity: O(1)
|
|
253
|
-
*/
|
|
254
|
-
|
|
255
214
|
/**
|
|
256
215
|
* Time Complexity: O(log n)
|
|
257
216
|
* Space Complexity: O(1)
|
|
258
217
|
*
|
|
259
|
-
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
* @param {V} [value] - The value
|
|
263
|
-
*
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
218
|
+
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
219
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
220
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
221
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
222
|
+
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
223
|
+
* @returns a boolean value.
|
|
224
|
+
*/
|
|
225
|
+
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
226
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
267
227
|
if (newNode === undefined) return false;
|
|
268
228
|
|
|
269
229
|
if (this.root === undefined) {
|
|
@@ -275,17 +235,8 @@ export class BST<
|
|
|
275
235
|
let current = this.root;
|
|
276
236
|
while (current !== undefined) {
|
|
277
237
|
if (this.comparator(current.key, newNode.key) === 0) {
|
|
278
|
-
// if (current !== newNode) {
|
|
279
|
-
// The key value is the same but the reference is different, update the value of the existing node
|
|
280
238
|
this._replaceNode(current, newNode);
|
|
281
239
|
return true;
|
|
282
|
-
|
|
283
|
-
// } else {
|
|
284
|
-
// The key value is the same and the reference is the same, replace the entire node
|
|
285
|
-
// this._replaceNode(current, newNode);
|
|
286
|
-
|
|
287
|
-
// return;
|
|
288
|
-
// }
|
|
289
240
|
} else if (this.comparator(current.key, newNode.key) > 0) {
|
|
290
241
|
if (current.left === undefined) {
|
|
291
242
|
current.left = newNode;
|
|
@@ -307,19 +258,18 @@ export class BST<
|
|
|
307
258
|
}
|
|
308
259
|
|
|
309
260
|
/**
|
|
310
|
-
* Time Complexity: O(
|
|
311
|
-
* Space Complexity: O(
|
|
261
|
+
* Time Complexity: O(log n)
|
|
262
|
+
* Space Complexity: O(log n)
|
|
312
263
|
*/
|
|
313
264
|
|
|
314
265
|
/**
|
|
315
266
|
* Time Complexity: O(k log n)
|
|
316
267
|
* Space Complexity: O(k + log n)
|
|
317
268
|
*
|
|
318
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
* data structure.
|
|
269
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
270
|
+
* an array indicating whether each key or node was successfully inserted.
|
|
271
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
272
|
+
* elements to be added to the data structure.
|
|
323
273
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
324
274
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
325
275
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
@@ -328,14 +278,13 @@ export class BST<
|
|
|
328
278
|
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
329
279
|
* value is true.
|
|
330
280
|
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
331
|
-
* specifies the type of iteration to use when adding multiple keys or nodes to the binary
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
* or entry was successfully inserted into the data structure.
|
|
281
|
+
* specifies the type of iteration to use when adding multiple keys or nodes to the binary search
|
|
282
|
+
* tree. It can have two possible values:
|
|
283
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
284
|
+
* successfully inserted into the data structure.
|
|
336
285
|
*/
|
|
337
286
|
override addMany(
|
|
338
|
-
|
|
287
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
|
|
339
288
|
values?: Iterable<V | undefined>,
|
|
340
289
|
isBalanceAdd = true,
|
|
341
290
|
iterationType: IterationType = this.iterationType
|
|
@@ -349,7 +298,7 @@ export class BST<
|
|
|
349
298
|
}
|
|
350
299
|
|
|
351
300
|
if (!isBalanceAdd) {
|
|
352
|
-
for (const kve of
|
|
301
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
353
302
|
const value = valuesIterator?.next().value;
|
|
354
303
|
const nn = this.add(kve, value);
|
|
355
304
|
inserted.push(nn);
|
|
@@ -357,29 +306,36 @@ export class BST<
|
|
|
357
306
|
return inserted;
|
|
358
307
|
}
|
|
359
308
|
|
|
360
|
-
const realBTNExemplars:
|
|
309
|
+
const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
|
|
361
310
|
|
|
362
|
-
const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, NODE>): kve is
|
|
311
|
+
const isRealBTNExemplar = (kve: R | KeyOrNodeOrEntry<K, V, NODE>): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
|
|
363
312
|
if (kve === undefined || kve === null) return false;
|
|
364
313
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
365
314
|
};
|
|
366
315
|
|
|
367
|
-
for (const kve of
|
|
316
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
368
317
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
369
318
|
}
|
|
370
319
|
|
|
371
|
-
let sorted:
|
|
320
|
+
let sorted: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
|
|
372
321
|
|
|
373
322
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
374
323
|
let keyA: K | undefined | null, keyB: K | undefined | null;
|
|
375
|
-
if (this.isEntry(a))
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
324
|
+
if (this.isEntry(a)) keyA = a[0];
|
|
325
|
+
else if (this.isRealNode(a)) keyA = a.key;
|
|
326
|
+
else if (this.toEntryFn) {
|
|
327
|
+
keyA = this.toEntryFn(a as R)[0];
|
|
328
|
+
} else {
|
|
329
|
+
keyA = a as K;
|
|
330
|
+
}
|
|
379
331
|
|
|
380
332
|
if (this.isEntry(b)) keyB = b[0];
|
|
381
333
|
else if (this.isRealNode(b)) keyB = b.key;
|
|
382
|
-
else
|
|
334
|
+
else if (this.toEntryFn) {
|
|
335
|
+
keyB = this.toEntryFn(b as R)[0];
|
|
336
|
+
} else {
|
|
337
|
+
keyB = b as K;
|
|
338
|
+
}
|
|
383
339
|
|
|
384
340
|
if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
|
|
385
341
|
return this.comparator(keyA, keyB);
|
|
@@ -387,7 +343,7 @@ export class BST<
|
|
|
387
343
|
return 0;
|
|
388
344
|
});
|
|
389
345
|
|
|
390
|
-
const _dfs = (arr:
|
|
346
|
+
const _dfs = (arr: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[]) => {
|
|
391
347
|
if (arr.length === 0) return;
|
|
392
348
|
|
|
393
349
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
@@ -425,38 +381,32 @@ export class BST<
|
|
|
425
381
|
}
|
|
426
382
|
|
|
427
383
|
/**
|
|
428
|
-
* Time Complexity: O(log n)
|
|
429
|
-
* Space Complexity: O(k + log n)
|
|
430
|
-
* /
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
384
|
* Time Complexity: O(log n)
|
|
434
385
|
* Space Complexity: O(k + log n)
|
|
435
386
|
*
|
|
436
|
-
* The
|
|
437
|
-
*
|
|
387
|
+
* The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
|
|
388
|
+
* identifier and callback function.
|
|
438
389
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
439
|
-
* want to search for in the
|
|
440
|
-
*
|
|
441
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node
|
|
442
|
-
*
|
|
443
|
-
* function
|
|
444
|
-
* @param [onlyOne=false] - A boolean
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
*
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
*
|
|
452
|
-
*
|
|
453
|
-
* @returns The method returns an array of nodes (`NODE[]`).
|
|
390
|
+
* want to search for in the binary tree. It can be of any type that is returned by the callback
|
|
391
|
+
* function.
|
|
392
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
393
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
394
|
+
* `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
395
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
|
|
396
|
+
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
|
|
397
|
+
* false, all matching nodes will be returned. The default value is false.
|
|
398
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
399
|
+
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
|
|
400
|
+
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
|
|
401
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
402
|
+
* iteration to be performed. It can have two possible values:
|
|
403
|
+
* @returns The method `getNodes` returns an array of `NODE` objects.
|
|
454
404
|
*/
|
|
455
405
|
override getNodes<C extends BTNCallback<NODE>>(
|
|
456
406
|
identifier: ReturnType<C> | undefined,
|
|
457
407
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
458
408
|
onlyOne = false,
|
|
459
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
409
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
460
410
|
iterationType: IterationType = this.iterationType
|
|
461
411
|
): NODE[] {
|
|
462
412
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -524,58 +474,57 @@ export class BST<
|
|
|
524
474
|
* Time Complexity: O(log n)
|
|
525
475
|
* Space Complexity: O(1)
|
|
526
476
|
*
|
|
527
|
-
* The `getNode`
|
|
528
|
-
*
|
|
529
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
533
|
-
* the
|
|
534
|
-
*
|
|
535
|
-
*
|
|
477
|
+
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
478
|
+
* function in a binary search tree.
|
|
479
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
480
|
+
* want to search for in the binary search tree. It can be of any type that is compatible with the
|
|
481
|
+
* type returned by the callback function.
|
|
482
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
483
|
+
* node matches the desired criteria. It should be a function that takes a node as an argument and
|
|
484
|
+
* returns a boolean value indicating whether the node matches the criteria or not. If no callback is
|
|
485
|
+
* provided, the default callback will be
|
|
536
486
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
537
|
-
* search tree. It can be either a key or a node. If it is a key,
|
|
538
|
-
*
|
|
539
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
540
|
-
* be performed when searching for nodes in the binary search tree. It
|
|
541
|
-
*
|
|
542
|
-
* @returns The method is returning a
|
|
487
|
+
* search tree. It can be either a key or a node. If it is a key, the search will start from the node
|
|
488
|
+
* with that key. If it is a node, the search will start from that node.
|
|
489
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
490
|
+
* of iteration to be performed when searching for nodes in the binary search tree. It can have one
|
|
491
|
+
* of the following values:
|
|
492
|
+
* @returns The method is returning a NODE object or undefined.
|
|
543
493
|
*/
|
|
544
494
|
override getNode<C extends BTNCallback<NODE>>(
|
|
545
495
|
identifier: ReturnType<C> | undefined,
|
|
546
496
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
547
|
-
beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
|
|
497
|
+
beginRoot: R | BSTNKeyOrNode<K, NODE> = this.root,
|
|
548
498
|
iterationType: IterationType = this.iterationType
|
|
549
499
|
): NODE | undefined {
|
|
550
500
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
551
501
|
}
|
|
552
502
|
|
|
553
503
|
/**
|
|
554
|
-
* Time Complexity: O(log n)
|
|
555
|
-
* Space Complexity: O(
|
|
504
|
+
* Time Complexity: O(k log n)
|
|
505
|
+
* Space Complexity: O(k + log n)
|
|
556
506
|
*/
|
|
557
507
|
|
|
558
508
|
/**
|
|
559
509
|
* Time Complexity: O(log n)
|
|
560
510
|
* Space Complexity: O(1)
|
|
561
511
|
*
|
|
562
|
-
* The function `getNodeByKey`
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
* @param iterationType - The `iterationType` parameter is an optional
|
|
567
|
-
* type of iteration to
|
|
568
|
-
*
|
|
569
|
-
* @returns The
|
|
570
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
512
|
+
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
513
|
+
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
514
|
+
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
515
|
+
* in the tree structure.
|
|
516
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
517
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
518
|
+
* It has a default value of `'ITERATIVE'`.
|
|
519
|
+
* @returns The method is returning a NODE object or undefined.
|
|
571
520
|
*/
|
|
572
521
|
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
573
522
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
574
523
|
}
|
|
575
524
|
|
|
576
525
|
/**
|
|
577
|
-
* Time
|
|
578
|
-
* Space
|
|
526
|
+
* Time Complexity: O(log n)
|
|
527
|
+
* Space Complexity: O(k + log n)
|
|
579
528
|
*/
|
|
580
529
|
|
|
581
530
|
/**
|
|
@@ -585,30 +534,31 @@ export class BST<
|
|
|
585
534
|
* The function overrides the depth-first search method and returns an array of the return types of
|
|
586
535
|
* the callback function.
|
|
587
536
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
588
|
-
* during the depth-first search traversal. It is an optional parameter and
|
|
589
|
-
*
|
|
590
|
-
* @param {DFSOrderPattern} [pattern=
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
537
|
+
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
538
|
+
* `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
|
|
539
|
+
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
540
|
+
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
541
|
+
* take one of the following values:
|
|
542
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
543
|
+
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
544
|
+
* node entry. If not specified, the default value is the root of the tree.
|
|
545
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
546
|
+
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
597
547
|
* following values:
|
|
598
548
|
* @returns The method is returning an array of the return type of the callback function.
|
|
599
549
|
*/
|
|
600
550
|
override dfs<C extends BTNCallback<NODE>>(
|
|
601
551
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
602
552
|
pattern: DFSOrderPattern = 'IN',
|
|
603
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
553
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
604
554
|
iterationType: IterationType = 'ITERATIVE'
|
|
605
555
|
): ReturnType<C>[] {
|
|
606
556
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
607
557
|
}
|
|
608
558
|
|
|
609
559
|
/**
|
|
610
|
-
* Time
|
|
611
|
-
* Space
|
|
560
|
+
* Time Complexity: O(log n)
|
|
561
|
+
* Space Complexity: O(1)
|
|
612
562
|
*/
|
|
613
563
|
|
|
614
564
|
/**
|
|
@@ -618,85 +568,85 @@ export class BST<
|
|
|
618
568
|
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
619
569
|
* the callback function.
|
|
620
570
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
621
|
-
* visited during the breadth-first search
|
|
622
|
-
*
|
|
623
|
-
* @param beginRoot - The `beginRoot` parameter is the starting
|
|
624
|
-
*
|
|
625
|
-
* the
|
|
626
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
627
|
-
* be performed during the breadth-first search (BFS) traversal. It
|
|
628
|
-
*
|
|
629
|
-
* @returns
|
|
571
|
+
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
572
|
+
* node being visited, and it can return a value of any type.
|
|
573
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
574
|
+
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
575
|
+
* object. If no value is provided, the default value is the root of the tree.
|
|
576
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
577
|
+
* of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
|
|
578
|
+
* the following values:
|
|
579
|
+
* @returns an array of the return type of the callback function.
|
|
630
580
|
*/
|
|
631
581
|
override bfs<C extends BTNCallback<NODE>>(
|
|
632
582
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
633
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
583
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
634
584
|
iterationType: IterationType = this.iterationType
|
|
635
585
|
): ReturnType<C>[] {
|
|
636
586
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
637
587
|
}
|
|
638
588
|
|
|
639
589
|
/**
|
|
640
|
-
* Time
|
|
641
|
-
* Space
|
|
590
|
+
* Time Complexity: O(log n)
|
|
591
|
+
* Space Complexity: O(1)
|
|
642
592
|
*/
|
|
643
593
|
|
|
644
594
|
/**
|
|
645
595
|
* Time complexity: O(n)
|
|
646
596
|
* Space complexity: O(n)
|
|
647
597
|
*
|
|
648
|
-
* The function overrides the listLevels method and returns an array of arrays
|
|
649
|
-
*
|
|
598
|
+
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
599
|
+
* containing the results of the callback function applied to each level of the tree.
|
|
650
600
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
651
|
-
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
652
|
-
* during the
|
|
653
|
-
* @param beginRoot - The `beginRoot` parameter is
|
|
654
|
-
* levels of
|
|
655
|
-
*
|
|
656
|
-
*
|
|
657
|
-
*
|
|
658
|
-
* iteration.
|
|
601
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
602
|
+
* tree during the iteration process.
|
|
603
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
604
|
+
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
605
|
+
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
606
|
+
* value is provided, the root of
|
|
607
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
608
|
+
* of iteration to be performed on the tree. It can have one of the following values:
|
|
659
609
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
660
610
|
* function.
|
|
661
611
|
*/
|
|
662
612
|
override listLevels<C extends BTNCallback<NODE>>(
|
|
663
613
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
664
|
-
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
614
|
+
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
665
615
|
iterationType: IterationType = this.iterationType
|
|
666
616
|
): ReturnType<C>[][] {
|
|
667
617
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
668
618
|
}
|
|
669
619
|
|
|
670
620
|
/**
|
|
671
|
-
* Time
|
|
672
|
-
* Space
|
|
621
|
+
* Time complexity: O(n)
|
|
622
|
+
* Space complexity: O(n)
|
|
673
623
|
*/
|
|
674
624
|
|
|
675
625
|
/**
|
|
676
|
-
* Time
|
|
677
|
-
* Space
|
|
626
|
+
* Time complexity: O(n)
|
|
627
|
+
* Space complexity: O(n)
|
|
678
628
|
*
|
|
679
|
-
* The `lesserOrGreaterTraverse` function traverses a binary tree and
|
|
680
|
-
*
|
|
629
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
630
|
+
* each node that meets a certain condition based on a target node and a comparison value.
|
|
681
631
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
682
|
-
* that
|
|
683
|
-
*
|
|
632
|
+
* that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
|
|
633
|
+
* which is the current node being traversed, and returns a value of any type.
|
|
684
634
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
685
|
-
* traverse nodes that are lesser
|
|
686
|
-
*
|
|
687
|
-
* `
|
|
688
|
-
*
|
|
689
|
-
*
|
|
690
|
-
*
|
|
691
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
692
|
-
* performed on the binary tree. It can have two possible values:
|
|
635
|
+
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
636
|
+
* 0, or 1, where:
|
|
637
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
|
|
638
|
+
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
639
|
+
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
640
|
+
* `targetNode` is provided,
|
|
641
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
642
|
+
* traversal to be performed on the binary tree. It can have two possible values:
|
|
693
643
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
694
644
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
695
645
|
*/
|
|
696
646
|
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
|
|
697
647
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
698
648
|
lesserOrGreater: CP = -1,
|
|
699
|
-
targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
649
|
+
targetNode: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
700
650
|
iterationType: IterationType = this.iterationType
|
|
701
651
|
): ReturnType<C>[] {
|
|
702
652
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
@@ -734,19 +684,20 @@ export class BST<
|
|
|
734
684
|
}
|
|
735
685
|
|
|
736
686
|
/**
|
|
737
|
-
* Time
|
|
738
|
-
* Space
|
|
687
|
+
* Time complexity: O(n)
|
|
688
|
+
* Space complexity: O(n)
|
|
739
689
|
*/
|
|
740
690
|
|
|
741
691
|
/**
|
|
742
|
-
* Time
|
|
743
|
-
* Space
|
|
692
|
+
* Time complexity: O(n)
|
|
693
|
+
* Space complexity: O(n)
|
|
744
694
|
*
|
|
745
|
-
* The `perfectlyBalance` function
|
|
746
|
-
*
|
|
747
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
748
|
-
* type of iteration to use when building a balanced binary search tree. It
|
|
749
|
-
*
|
|
695
|
+
* The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
|
|
696
|
+
* the binary search tree is perfectly balanced, otherwise it returns `false`.
|
|
697
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
698
|
+
* specifies the type of iteration to use when building a balanced binary search tree. It has a
|
|
699
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
700
|
+
* current instance of the class.
|
|
750
701
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
751
702
|
*/
|
|
752
703
|
perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
@@ -787,27 +738,20 @@ export class BST<
|
|
|
787
738
|
}
|
|
788
739
|
|
|
789
740
|
/**
|
|
790
|
-
*
|
|
791
|
-
*
|
|
792
|
-
* 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.
|
|
793
|
-
*
|
|
794
|
-
* Use Cases and Efficiency:
|
|
795
|
-
* 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.
|
|
796
|
-
* 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).
|
|
797
|
-
*/
|
|
798
|
-
|
|
799
|
-
/**
|
|
800
|
-
* Time Complexity: O(n)
|
|
801
|
-
* Space Complexity: O(log n)
|
|
741
|
+
* Time complexity: O(n)
|
|
742
|
+
* Space complexity: O(n)
|
|
802
743
|
*/
|
|
803
744
|
|
|
804
745
|
/**
|
|
805
746
|
* Time Complexity: O(n)
|
|
806
747
|
* Space Complexity: O(log n)
|
|
807
748
|
*
|
|
808
|
-
* The function checks if a binary tree is AVL balanced using either recursive or
|
|
809
|
-
*
|
|
810
|
-
*
|
|
749
|
+
* The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
|
|
750
|
+
* iterative approach.
|
|
751
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
752
|
+
* specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
|
|
753
|
+
* value of `this.iterationType`, which means it will use the iteration type specified in the current
|
|
754
|
+
* instance of the AVL tree.
|
|
811
755
|
* @returns a boolean value.
|
|
812
756
|
*/
|
|
813
757
|
isAVLBalanced(iterationType: IterationType = this.iterationType): boolean {
|
|
@@ -854,10 +798,36 @@ export class BST<
|
|
|
854
798
|
return balanced;
|
|
855
799
|
}
|
|
856
800
|
|
|
801
|
+
protected _DEFAULT_COMPARATOR = (a: K, b: K): number => {
|
|
802
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
803
|
+
throw TypeError(
|
|
804
|
+
`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`
|
|
805
|
+
);
|
|
806
|
+
}
|
|
807
|
+
if (a > b) return 1;
|
|
808
|
+
if (a < b) return -1;
|
|
809
|
+
return 0;
|
|
810
|
+
};
|
|
811
|
+
|
|
812
|
+
protected _comparator: Comparator<K> = this._DEFAULT_COMPARATOR;
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* Time Complexity: O(n)
|
|
816
|
+
* Space Complexity: O(log n)
|
|
817
|
+
*/
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* The function returns the value of the _comparator property.
|
|
821
|
+
* @returns The `_comparator` property is being returned.
|
|
822
|
+
*/
|
|
823
|
+
get comparator() {
|
|
824
|
+
return this._comparator;
|
|
825
|
+
}
|
|
826
|
+
|
|
857
827
|
/**
|
|
858
|
-
* The function sets the root
|
|
859
|
-
*
|
|
860
|
-
*
|
|
828
|
+
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
829
|
+
* root.
|
|
830
|
+
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
861
831
|
*/
|
|
862
832
|
protected override _setRoot(v: NODE | undefined) {
|
|
863
833
|
if (v) {
|