min-heap-typed 1.50.1 → 1.50.3
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/iterable-base.d.ts +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -105,6 +105,72 @@ export declare abstract class IterableEntryBase<K = any, V = any> {
|
|
|
105
105
|
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
106
106
|
*/
|
|
107
107
|
forEach(callbackfn: EntryCallback<K, V, void>, thisArg?: any): void;
|
|
108
|
+
/**
|
|
109
|
+
* Time Complexity: O(n)
|
|
110
|
+
* Space Complexity: O(1)
|
|
111
|
+
*/
|
|
112
|
+
/**
|
|
113
|
+
* Time Complexity: O(n)
|
|
114
|
+
* Space Complexity: O(1)
|
|
115
|
+
*
|
|
116
|
+
* The `find` function iterates over the entries of a collection and returns the first value for
|
|
117
|
+
* which the callback function returns true.
|
|
118
|
+
* @param callbackfn - The callback function that will be called for each entry in the collection. It
|
|
119
|
+
* takes three arguments: the value of the entry, the key of the entry, and the index of the entry in
|
|
120
|
+
* the collection. It should return a boolean value indicating whether the current entry matches the
|
|
121
|
+
* desired condition.
|
|
122
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
123
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
124
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
125
|
+
* @returns The method `find` returns the value of the first element in the iterable that satisfies
|
|
126
|
+
* the provided callback function. If no element satisfies the callback function, `undefined` is
|
|
127
|
+
* returned.
|
|
128
|
+
*/
|
|
129
|
+
find(callbackfn: EntryCallback<K, V, [K, V]>, thisArg?: any): [K, V] | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* Time Complexity: O(n)
|
|
132
|
+
* Space Complexity: O(1)
|
|
133
|
+
*/
|
|
134
|
+
/**
|
|
135
|
+
* Time Complexity: O(n)
|
|
136
|
+
* Space Complexity: O(1)
|
|
137
|
+
*
|
|
138
|
+
* The function checks if a given key exists in a collection.
|
|
139
|
+
* @param {K} key - The parameter "key" is of type K, which means it can be any type. It represents
|
|
140
|
+
* the key that we want to check for existence in the data structure.
|
|
141
|
+
* @returns a boolean value. It returns true if the key is found in the collection, and false
|
|
142
|
+
* otherwise.
|
|
143
|
+
*/
|
|
144
|
+
has(key: K): boolean;
|
|
145
|
+
/**
|
|
146
|
+
* Time Complexity: O(n)
|
|
147
|
+
* Space Complexity: O(1)
|
|
148
|
+
*/
|
|
149
|
+
/**
|
|
150
|
+
* Time Complexity: O(n)
|
|
151
|
+
* Space Complexity: O(1)
|
|
152
|
+
*
|
|
153
|
+
* The function checks if a given value exists in a collection.
|
|
154
|
+
* @param {V} value - The parameter "value" is the value that we want to check if it exists in the
|
|
155
|
+
* collection.
|
|
156
|
+
* @returns a boolean value, either true or false.
|
|
157
|
+
*/
|
|
158
|
+
hasValue(value: V): boolean;
|
|
159
|
+
/**
|
|
160
|
+
* Time Complexity: O(n)
|
|
161
|
+
* Space Complexity: O(1)
|
|
162
|
+
*/
|
|
163
|
+
/**
|
|
164
|
+
* Time Complexity: O(n)
|
|
165
|
+
* Space Complexity: O(1)
|
|
166
|
+
*
|
|
167
|
+
* The `get` function retrieves the value associated with a given key from a collection.
|
|
168
|
+
* @param {K} key - K (the type of the key) - This parameter represents the key that is being
|
|
169
|
+
* searched for in the collection.
|
|
170
|
+
* @returns The `get` method returns the value associated with the specified key if it exists in the
|
|
171
|
+
* collection, otherwise it returns `undefined`.
|
|
172
|
+
*/
|
|
173
|
+
get(key: K): V | undefined;
|
|
108
174
|
/**
|
|
109
175
|
* Time Complexity: O(n)
|
|
110
176
|
* Space Complexity: O(1)
|
|
@@ -126,15 +192,19 @@ export declare abstract class IterableEntryBase<K = any, V = any> {
|
|
|
126
192
|
* all the elements in the collection.
|
|
127
193
|
*/
|
|
128
194
|
reduce<U>(callbackfn: ReduceEntryCallback<K, V, U>, initialValue: U): U;
|
|
129
|
-
hasValue(value: V): boolean;
|
|
130
195
|
/**
|
|
131
196
|
* Time Complexity: O(n)
|
|
132
197
|
* Space Complexity: O(n)
|
|
133
198
|
*/
|
|
134
199
|
print(): void;
|
|
200
|
+
abstract isEmpty(): boolean;
|
|
201
|
+
abstract clear(): void;
|
|
202
|
+
abstract clone(): any;
|
|
203
|
+
abstract map(...args: any[]): any;
|
|
204
|
+
abstract filter(...args: any[]): any;
|
|
135
205
|
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
136
206
|
}
|
|
137
|
-
export declare abstract class IterableElementBase<
|
|
207
|
+
export declare abstract class IterableElementBase<E = any, C = any> {
|
|
138
208
|
/**
|
|
139
209
|
* Time Complexity: O(n)
|
|
140
210
|
* Space Complexity: O(1)
|
|
@@ -148,7 +218,7 @@ export declare abstract class IterableElementBase<V> {
|
|
|
148
218
|
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
149
219
|
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
150
220
|
*/
|
|
151
|
-
[Symbol.iterator](...args: any[]): IterableIterator<
|
|
221
|
+
[Symbol.iterator](...args: any[]): IterableIterator<E>;
|
|
152
222
|
/**
|
|
153
223
|
* Time Complexity: O(n)
|
|
154
224
|
* Space Complexity: O(n)
|
|
@@ -159,7 +229,7 @@ export declare abstract class IterableElementBase<V> {
|
|
|
159
229
|
*
|
|
160
230
|
* The function returns an iterator that yields all the values in the object.
|
|
161
231
|
*/
|
|
162
|
-
values(): IterableIterator<
|
|
232
|
+
values(): IterableIterator<E>;
|
|
163
233
|
/**
|
|
164
234
|
* Time Complexity: O(n)
|
|
165
235
|
* Space Complexity: O(1)
|
|
@@ -178,7 +248,7 @@ export declare abstract class IterableElementBase<V> {
|
|
|
178
248
|
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
179
249
|
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
180
250
|
*/
|
|
181
|
-
every(predicate: ElementCallback<
|
|
251
|
+
every(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean;
|
|
182
252
|
/**
|
|
183
253
|
* Time Complexity: O(n)
|
|
184
254
|
* Space Complexity: O(1)
|
|
@@ -197,7 +267,7 @@ export declare abstract class IterableElementBase<V> {
|
|
|
197
267
|
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
198
268
|
* in the collection, and false otherwise.
|
|
199
269
|
*/
|
|
200
|
-
some(predicate: ElementCallback<
|
|
270
|
+
some(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean;
|
|
201
271
|
/**
|
|
202
272
|
* Time Complexity: O(n)
|
|
203
273
|
* Space Complexity: O(1)
|
|
@@ -215,7 +285,43 @@ export declare abstract class IterableElementBase<V> {
|
|
|
215
285
|
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
216
286
|
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
217
287
|
*/
|
|
218
|
-
forEach(callbackfn: ElementCallback<
|
|
288
|
+
forEach(callbackfn: ElementCallback<E, void>, thisArg?: any): void;
|
|
289
|
+
/**
|
|
290
|
+
* Time Complexity: O(n)
|
|
291
|
+
* Space Complexity: O(1)
|
|
292
|
+
*/
|
|
293
|
+
/**
|
|
294
|
+
* Time Complexity: O(n)
|
|
295
|
+
* Space Complexity: O(1)
|
|
296
|
+
*
|
|
297
|
+
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
298
|
+
* element that satisfies the provided callback function.
|
|
299
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
300
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
301
|
+
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
302
|
+
* current element matches the desired condition.
|
|
303
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
304
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
305
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
306
|
+
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
307
|
+
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
308
|
+
*/
|
|
309
|
+
find(callbackfn: ElementCallback<E, boolean>, thisArg?: any): E | undefined;
|
|
310
|
+
/**
|
|
311
|
+
* Time Complexity: O(n)
|
|
312
|
+
* Space Complexity: O(1)
|
|
313
|
+
*/
|
|
314
|
+
/**
|
|
315
|
+
* Time Complexity: O(n)
|
|
316
|
+
* Space Complexity: O(1)
|
|
317
|
+
*
|
|
318
|
+
* The function checks if a given element exists in a collection.
|
|
319
|
+
* @param {E} element - The parameter "element" is of type E, which means it can be any type. It
|
|
320
|
+
* represents the element that we want to check for existence in the collection.
|
|
321
|
+
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
|
322
|
+
* otherwise.
|
|
323
|
+
*/
|
|
324
|
+
has(element: E): boolean;
|
|
219
325
|
/**
|
|
220
326
|
* Time Complexity: O(n)
|
|
221
327
|
* Space Complexity: O(1)
|
|
@@ -233,11 +339,16 @@ export declare abstract class IterableElementBase<V> {
|
|
|
233
339
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
234
340
|
* all the elements in the array and applying the callback function to each element.
|
|
235
341
|
*/
|
|
236
|
-
reduce<U>(callbackfn: ReduceElementCallback<
|
|
342
|
+
reduce<U>(callbackfn: ReduceElementCallback<E, U>, initialValue: U): U;
|
|
237
343
|
/**
|
|
238
344
|
* Time Complexity: O(n)
|
|
239
345
|
* Space Complexity: O(n)
|
|
240
346
|
*/
|
|
241
347
|
print(): void;
|
|
242
|
-
|
|
348
|
+
abstract isEmpty(): boolean;
|
|
349
|
+
abstract clear(): void;
|
|
350
|
+
abstract clone(): C;
|
|
351
|
+
abstract map(...args: any[]): any;
|
|
352
|
+
abstract filter(...args: any[]): any;
|
|
353
|
+
protected abstract _getIterator(...args: any[]): IterableIterator<E>;
|
|
243
354
|
}
|
|
@@ -143,6 +143,100 @@ class IterableEntryBase {
|
|
|
143
143
|
callbackfn.call(thisArg, value, key, index++, this);
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Time Complexity: O(n)
|
|
148
|
+
* Space Complexity: O(1)
|
|
149
|
+
*/
|
|
150
|
+
/**
|
|
151
|
+
* Time Complexity: O(n)
|
|
152
|
+
* Space Complexity: O(1)
|
|
153
|
+
*
|
|
154
|
+
* The `find` function iterates over the entries of a collection and returns the first value for
|
|
155
|
+
* which the callback function returns true.
|
|
156
|
+
* @param callbackfn - The callback function that will be called for each entry in the collection. It
|
|
157
|
+
* takes three arguments: the value of the entry, the key of the entry, and the index of the entry in
|
|
158
|
+
* the collection. It should return a boolean value indicating whether the current entry matches the
|
|
159
|
+
* desired condition.
|
|
160
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
161
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
162
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
163
|
+
* @returns The method `find` returns the value of the first element in the iterable that satisfies
|
|
164
|
+
* the provided callback function. If no element satisfies the callback function, `undefined` is
|
|
165
|
+
* returned.
|
|
166
|
+
*/
|
|
167
|
+
find(callbackfn, thisArg) {
|
|
168
|
+
let index = 0;
|
|
169
|
+
for (const item of this) {
|
|
170
|
+
const [key, value] = item;
|
|
171
|
+
if (callbackfn.call(thisArg, value, key, index++, this))
|
|
172
|
+
return item;
|
|
173
|
+
}
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Time Complexity: O(n)
|
|
178
|
+
* Space Complexity: O(1)
|
|
179
|
+
*/
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(n)
|
|
182
|
+
* Space Complexity: O(1)
|
|
183
|
+
*
|
|
184
|
+
* The function checks if a given key exists in a collection.
|
|
185
|
+
* @param {K} key - The parameter "key" is of type K, which means it can be any type. It represents
|
|
186
|
+
* the key that we want to check for existence in the data structure.
|
|
187
|
+
* @returns a boolean value. It returns true if the key is found in the collection, and false
|
|
188
|
+
* otherwise.
|
|
189
|
+
*/
|
|
190
|
+
has(key) {
|
|
191
|
+
for (const item of this) {
|
|
192
|
+
const [itemKey] = item;
|
|
193
|
+
if (itemKey === key)
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Time Complexity: O(n)
|
|
200
|
+
* Space Complexity: O(1)
|
|
201
|
+
*/
|
|
202
|
+
/**
|
|
203
|
+
* Time Complexity: O(n)
|
|
204
|
+
* Space Complexity: O(1)
|
|
205
|
+
*
|
|
206
|
+
* The function checks if a given value exists in a collection.
|
|
207
|
+
* @param {V} value - The parameter "value" is the value that we want to check if it exists in the
|
|
208
|
+
* collection.
|
|
209
|
+
* @returns a boolean value, either true or false.
|
|
210
|
+
*/
|
|
211
|
+
hasValue(value) {
|
|
212
|
+
for (const [, elementValue] of this) {
|
|
213
|
+
if (elementValue === value)
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Time Complexity: O(n)
|
|
220
|
+
* Space Complexity: O(1)
|
|
221
|
+
*/
|
|
222
|
+
/**
|
|
223
|
+
* Time Complexity: O(n)
|
|
224
|
+
* Space Complexity: O(1)
|
|
225
|
+
*
|
|
226
|
+
* The `get` function retrieves the value associated with a given key from a collection.
|
|
227
|
+
* @param {K} key - K (the type of the key) - This parameter represents the key that is being
|
|
228
|
+
* searched for in the collection.
|
|
229
|
+
* @returns The `get` method returns the value associated with the specified key if it exists in the
|
|
230
|
+
* collection, otherwise it returns `undefined`.
|
|
231
|
+
*/
|
|
232
|
+
get(key) {
|
|
233
|
+
for (const item of this) {
|
|
234
|
+
const [itemKey, value] = item;
|
|
235
|
+
if (itemKey === key)
|
|
236
|
+
return value;
|
|
237
|
+
}
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
146
240
|
/**
|
|
147
241
|
* Time Complexity: O(n)
|
|
148
242
|
* Space Complexity: O(1)
|
|
@@ -172,13 +266,6 @@ class IterableEntryBase {
|
|
|
172
266
|
}
|
|
173
267
|
return accumulator;
|
|
174
268
|
}
|
|
175
|
-
hasValue(value) {
|
|
176
|
-
for (const [, elementValue] of this) {
|
|
177
|
-
if (elementValue === value)
|
|
178
|
-
return true;
|
|
179
|
-
}
|
|
180
|
-
return false;
|
|
181
|
-
}
|
|
182
269
|
/**
|
|
183
270
|
* Time Complexity: O(n)
|
|
184
271
|
* Space Complexity: O(n)
|
|
@@ -297,6 +384,55 @@ class IterableElementBase {
|
|
|
297
384
|
callbackfn.call(thisArg, item, index++, this);
|
|
298
385
|
}
|
|
299
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* Time Complexity: O(n)
|
|
389
|
+
* Space Complexity: O(1)
|
|
390
|
+
*/
|
|
391
|
+
/**
|
|
392
|
+
* Time Complexity: O(n)
|
|
393
|
+
* Space Complexity: O(1)
|
|
394
|
+
*
|
|
395
|
+
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
396
|
+
* element that satisfies the provided callback function.
|
|
397
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
398
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
399
|
+
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
400
|
+
* current element matches the desired condition.
|
|
401
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
402
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
403
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
404
|
+
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
405
|
+
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
406
|
+
*/
|
|
407
|
+
find(callbackfn, thisArg) {
|
|
408
|
+
let index = 0;
|
|
409
|
+
for (const item of this) {
|
|
410
|
+
if (callbackfn.call(thisArg, item, index++, this))
|
|
411
|
+
return item;
|
|
412
|
+
}
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Time Complexity: O(n)
|
|
417
|
+
* Space Complexity: O(1)
|
|
418
|
+
*/
|
|
419
|
+
/**
|
|
420
|
+
* Time Complexity: O(n)
|
|
421
|
+
* Space Complexity: O(1)
|
|
422
|
+
*
|
|
423
|
+
* The function checks if a given element exists in a collection.
|
|
424
|
+
* @param {E} element - The parameter "element" is of type E, which means it can be any type. It
|
|
425
|
+
* represents the element that we want to check for existence in the collection.
|
|
426
|
+
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
|
427
|
+
* otherwise.
|
|
428
|
+
*/
|
|
429
|
+
has(element) {
|
|
430
|
+
for (const ele of this) {
|
|
431
|
+
if (ele === element)
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
300
436
|
/**
|
|
301
437
|
* Time Complexity: O(n)
|
|
302
438
|
* Space Complexity: O(1)
|
|
@@ -8,9 +8,28 @@
|
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
9
|
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
export declare class AVLTreeNode<K = any, V = any,
|
|
12
|
-
|
|
11
|
+
export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
12
|
+
/**
|
|
13
|
+
* The constructor function initializes a new instance of a class with a key and an optional value,
|
|
14
|
+
* and sets the height property to 0.
|
|
15
|
+
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
|
|
16
|
+
* constructor. It is used to initialize the key property of the object being created.
|
|
17
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
18
|
+
* value associated with the key in the constructor.
|
|
19
|
+
*/
|
|
13
20
|
constructor(key: K, value?: V);
|
|
21
|
+
protected _height: number;
|
|
22
|
+
/**
|
|
23
|
+
* The function returns the value of the height property.
|
|
24
|
+
* @returns The height of the object.
|
|
25
|
+
*/
|
|
26
|
+
get height(): number;
|
|
27
|
+
/**
|
|
28
|
+
* The above function sets the value of the height property.
|
|
29
|
+
* @param {number} value - The value parameter is a number that represents the new height value to be
|
|
30
|
+
* set.
|
|
31
|
+
*/
|
|
32
|
+
set height(value: number);
|
|
14
33
|
}
|
|
15
34
|
/**
|
|
16
35
|
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
@@ -21,27 +40,27 @@ export declare class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N
|
|
|
21
40
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
22
41
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
23
42
|
*/
|
|
24
|
-
export declare class AVLTree<K = any, V = any,
|
|
43
|
+
export declare class AVLTree<K = any, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, NODE, TREE> = AVLTree<K, V, NODE, AVLTreeNested<K, V, NODE>>> extends BST<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
25
44
|
/**
|
|
26
45
|
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
|
|
27
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
46
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
28
47
|
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
29
48
|
* initialization.
|
|
30
49
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
31
50
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
32
51
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
33
52
|
*/
|
|
34
|
-
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V,
|
|
53
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: AVLTreeOptions<K>);
|
|
35
54
|
/**
|
|
36
55
|
* The function creates a new AVL tree node with the specified key and value.
|
|
37
56
|
* @param {K} key - The key parameter is the key value that will be associated with
|
|
38
57
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
39
58
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
40
59
|
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
41
|
-
* node type `
|
|
60
|
+
* node type `NODE`.
|
|
42
61
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
43
62
|
*/
|
|
44
|
-
createNode(key: K, value?: V):
|
|
63
|
+
createNode(key: K, value?: V): NODE;
|
|
45
64
|
/**
|
|
46
65
|
* The function creates a new AVL tree with the specified options and returns it.
|
|
47
66
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
@@ -52,10 +71,10 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
52
71
|
createTree(options?: AVLTreeOptions<K>): TREE;
|
|
53
72
|
/**
|
|
54
73
|
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
55
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
74
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
56
75
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
57
76
|
*/
|
|
58
|
-
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
77
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
|
|
59
78
|
/**
|
|
60
79
|
* Time Complexity: O(log n)
|
|
61
80
|
* Space Complexity: O(1)
|
|
@@ -73,7 +92,7 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
73
92
|
* being added to the binary tree.
|
|
74
93
|
* @returns The method is returning either the inserted node or undefined.
|
|
75
94
|
*/
|
|
76
|
-
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
95
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
|
|
77
96
|
/**
|
|
78
97
|
* Time Complexity: O(log n)
|
|
79
98
|
* Space Complexity: O(1)
|
|
@@ -90,40 +109,38 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
90
109
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
91
110
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
92
111
|
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single
|
|
93
|
-
* parameter of type `
|
|
94
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<
|
|
112
|
+
* parameter of type `NODE
|
|
113
|
+
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
95
114
|
*/
|
|
96
|
-
delete<C extends BTNCallback<
|
|
115
|
+
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
97
116
|
/**
|
|
98
117
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
99
118
|
* tree.
|
|
100
|
-
* @param {K |
|
|
101
|
-
* needs to be swapped with the destination node. It can be of type `K`, `
|
|
102
|
-
* @param {K |
|
|
119
|
+
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
120
|
+
* needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
|
|
121
|
+
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
103
122
|
* node where the values from the source node will be swapped to.
|
|
104
123
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
105
124
|
* if either `srcNode` or `destNode` is undefined.
|
|
106
125
|
*/
|
|
107
|
-
protected _swapProperties(srcNode: BSTNKeyOrNode<K,
|
|
126
|
+
protected _swapProperties(srcNode: BSTNKeyOrNode<K, NODE>, destNode: BSTNKeyOrNode<K, NODE>): NODE | undefined;
|
|
108
127
|
/**
|
|
109
128
|
* Time Complexity: O(1)
|
|
110
129
|
* Space Complexity: O(1)
|
|
111
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
112
130
|
*/
|
|
113
131
|
/**
|
|
114
132
|
* Time Complexity: O(1)
|
|
115
133
|
* Space Complexity: O(1)
|
|
116
134
|
*
|
|
117
135
|
* The function calculates the balance factor of a node in a binary tree.
|
|
118
|
-
* @param {
|
|
136
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
119
137
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
120
138
|
* height of the left subtree from the height of the right subtree.
|
|
121
139
|
*/
|
|
122
|
-
protected _balanceFactor(node:
|
|
140
|
+
protected _balanceFactor(node: NODE): number;
|
|
123
141
|
/**
|
|
124
142
|
* Time Complexity: O(1)
|
|
125
143
|
* Space Complexity: O(1)
|
|
126
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
127
144
|
*/
|
|
128
145
|
/**
|
|
129
146
|
* Time Complexity: O(1)
|
|
@@ -131,37 +148,33 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
131
148
|
*
|
|
132
149
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
133
150
|
* right children.
|
|
134
|
-
* @param {
|
|
151
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
135
152
|
*/
|
|
136
|
-
protected _updateHeight(node:
|
|
153
|
+
protected _updateHeight(node: NODE): void;
|
|
137
154
|
/**
|
|
138
|
-
* Time Complexity: O(
|
|
155
|
+
* Time Complexity: O(1)
|
|
139
156
|
* Space Complexity: O(1)
|
|
140
|
-
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
|
|
141
157
|
*/
|
|
142
158
|
/**
|
|
143
|
-
* Time Complexity: O(
|
|
159
|
+
* Time Complexity: O(1)
|
|
144
160
|
* Space Complexity: O(1)
|
|
145
161
|
*
|
|
146
|
-
* The `
|
|
147
|
-
*
|
|
148
|
-
* @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
149
|
-
* AVL tree that needs to be balanced.
|
|
162
|
+
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
163
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
150
164
|
*/
|
|
151
|
-
protected
|
|
165
|
+
protected _balanceLL(A: NODE): void;
|
|
152
166
|
/**
|
|
153
167
|
* Time Complexity: O(1)
|
|
154
168
|
* Space Complexity: O(1)
|
|
155
|
-
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
156
169
|
*/
|
|
157
170
|
/**
|
|
158
171
|
* Time Complexity: O(1)
|
|
159
172
|
* Space Complexity: O(1)
|
|
160
173
|
*
|
|
161
|
-
* The
|
|
162
|
-
* @param {
|
|
174
|
+
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
175
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
163
176
|
*/
|
|
164
|
-
protected
|
|
177
|
+
protected _balanceLR(A: NODE): void;
|
|
165
178
|
/**
|
|
166
179
|
* Time Complexity: O(1)
|
|
167
180
|
* Space Complexity: O(1)
|
|
@@ -170,10 +183,10 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
170
183
|
* Time Complexity: O(1)
|
|
171
184
|
* Space Complexity: O(1)
|
|
172
185
|
*
|
|
173
|
-
* The `
|
|
174
|
-
* @param {
|
|
186
|
+
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
187
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
175
188
|
*/
|
|
176
|
-
protected
|
|
189
|
+
protected _balanceRR(A: NODE): void;
|
|
177
190
|
/**
|
|
178
191
|
* Time Complexity: O(1)
|
|
179
192
|
* Space Complexity: O(1)
|
|
@@ -182,21 +195,33 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
182
195
|
* Time Complexity: O(1)
|
|
183
196
|
* Space Complexity: O(1)
|
|
184
197
|
*
|
|
185
|
-
* The function `
|
|
186
|
-
* @param {
|
|
198
|
+
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
199
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
187
200
|
*/
|
|
188
|
-
protected
|
|
201
|
+
protected _balanceRL(A: NODE): void;
|
|
189
202
|
/**
|
|
190
|
-
* Time Complexity: O(
|
|
203
|
+
* Time Complexity: O(log n)
|
|
191
204
|
* Space Complexity: O(1)
|
|
205
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
|
|
192
206
|
*/
|
|
193
207
|
/**
|
|
194
|
-
* Time Complexity: O(
|
|
208
|
+
* Time Complexity: O(log n)
|
|
195
209
|
* Space Complexity: O(1)
|
|
196
210
|
*
|
|
197
|
-
* The
|
|
198
|
-
*
|
|
211
|
+
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
212
|
+
* to restore balance in an AVL tree after inserting a node.
|
|
213
|
+
* @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
214
|
+
* AVL tree that needs to be balanced.
|
|
215
|
+
*/
|
|
216
|
+
protected _balancePath(node: KeyOrNodeOrEntry<K, V, NODE>): void;
|
|
217
|
+
/**
|
|
218
|
+
* The function replaces an old node with a new node while preserving the height of the old node.
|
|
219
|
+
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace with the
|
|
220
|
+
* `newNode`.
|
|
221
|
+
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
222
|
+
* the data structure.
|
|
223
|
+
* @returns the result of calling the `_replaceNode` method on the superclass, passing in the
|
|
224
|
+
* `oldNode` and `newNode` as arguments.
|
|
199
225
|
*/
|
|
200
|
-
protected
|
|
201
|
-
protected _replaceNode(oldNode: N, newNode: N): N;
|
|
226
|
+
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
|
|
202
227
|
}
|