bst-typed 1.48.1 → 1.48.2
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 +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +36 -69
- package/dist/data-structures/binary-tree/binary-tree.js +78 -129
- package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/data-structures/graph/abstract-graph.js +50 -27
- package/dist/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/data-structures/hash/hash-map.js +69 -173
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/data-structures/queue/deque.d.ts +29 -51
- package/dist/data-structures/queue/deque.js +36 -71
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/binary-tree.ts +82 -138
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +76 -185
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/index.ts +1 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { ElementCallback, PairCallback, ReduceElementCallback, ReducePairCallback } from "../../types";
|
|
2
|
+
|
|
3
|
+
export abstract class IterablePairBase<K = any, V = any> {
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Time Complexity: O(n)
|
|
7
|
+
* Space Complexity: O(1)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Time Complexity: O(n)
|
|
12
|
+
* Space Complexity: O(1)
|
|
13
|
+
*
|
|
14
|
+
* The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
|
|
15
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
16
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
17
|
+
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
18
|
+
*/
|
|
19
|
+
* [Symbol.iterator](...args: any[]): IterableIterator<[K, V]> {
|
|
20
|
+
yield* this._getIterator(...args);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Time Complexity: O(n)
|
|
25
|
+
* Space Complexity: O(n)
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Time Complexity: O(n)
|
|
29
|
+
* Space Complexity: O(n)
|
|
30
|
+
*
|
|
31
|
+
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
32
|
+
* be undefined.
|
|
33
|
+
*/
|
|
34
|
+
* entries(): IterableIterator<[K, V | undefined]> {
|
|
35
|
+
for (const item of this) {
|
|
36
|
+
yield item;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Time Complexity: O(n)
|
|
42
|
+
* Space Complexity: O(n)
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* Time Complexity: O(n)
|
|
46
|
+
* Space Complexity: O(n)
|
|
47
|
+
*
|
|
48
|
+
* The function returns an iterator that yields the keys of a data structure.
|
|
49
|
+
*/
|
|
50
|
+
* keys(): IterableIterator<K> {
|
|
51
|
+
for (const item of this) {
|
|
52
|
+
yield item[0];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Time Complexity: O(n)
|
|
58
|
+
* Space Complexity: O(n)
|
|
59
|
+
*/
|
|
60
|
+
/**
|
|
61
|
+
* Time Complexity: O(n)
|
|
62
|
+
* Space Complexity: O(n)
|
|
63
|
+
*
|
|
64
|
+
* The function returns an iterator that yields the values of a collection.
|
|
65
|
+
*/
|
|
66
|
+
* values(): IterableIterator<V> {
|
|
67
|
+
for (const item of this) {
|
|
68
|
+
yield item[1];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Time Complexity: O(n)
|
|
74
|
+
* Space Complexity: O(1)
|
|
75
|
+
*/
|
|
76
|
+
/**
|
|
77
|
+
* Time Complexity: O(n)
|
|
78
|
+
* Space Complexity: O(1)
|
|
79
|
+
*
|
|
80
|
+
* The `every` function checks if every element in a collection satisfies a given condition.
|
|
81
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
82
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
83
|
+
* met for the current element in the iteration.
|
|
84
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
85
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
86
|
+
* passed as the first argument to the `predicate` function. If `thisArg` is not provided
|
|
87
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
88
|
+
* the collection satisfies the provided predicate function, and `false` otherwise.
|
|
89
|
+
*/
|
|
90
|
+
every(predicate: PairCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
91
|
+
let index = 0;
|
|
92
|
+
for (const item of this) {
|
|
93
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Time Complexity: O(n)
|
|
102
|
+
* Space Complexity: O(1)
|
|
103
|
+
*/
|
|
104
|
+
/**
|
|
105
|
+
* Time Complexity: O(n)
|
|
106
|
+
* Space Complexity: O(1)
|
|
107
|
+
*
|
|
108
|
+
* The "some" function iterates over a collection and returns true if at least one element satisfies
|
|
109
|
+
* a given predicate.
|
|
110
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
111
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
112
|
+
* met for the current element in the iteration.
|
|
113
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
114
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
115
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
116
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any pair in
|
|
117
|
+
* the collection, and false otherwise.
|
|
118
|
+
*/
|
|
119
|
+
some(predicate: PairCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
120
|
+
let index = 0;
|
|
121
|
+
for (const item of this) {
|
|
122
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Time Complexity: O(n)
|
|
131
|
+
* Space Complexity: O(1)
|
|
132
|
+
*/
|
|
133
|
+
/**
|
|
134
|
+
* Time Complexity: O(n)
|
|
135
|
+
* Space Complexity: O(1)
|
|
136
|
+
*
|
|
137
|
+
* The `forEach` function iterates over each key-value pair in a collection and executes a callback
|
|
138
|
+
* function for each pair.
|
|
139
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
140
|
+
* It takes four parameters: the value of the current element, the key of the current element, the
|
|
141
|
+
* index of the current element, and the collection itself.
|
|
142
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
143
|
+
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
144
|
+
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
145
|
+
*/
|
|
146
|
+
forEach(callbackfn: PairCallback<K, V, void>, thisArg?: any): void {
|
|
147
|
+
let index = 0;
|
|
148
|
+
for (const item of this) {
|
|
149
|
+
const [key, value] = item;
|
|
150
|
+
callbackfn.call(thisArg, value, key, index++, this)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Time Complexity: O(n)
|
|
156
|
+
* Space Complexity: O(1)
|
|
157
|
+
*/
|
|
158
|
+
/**
|
|
159
|
+
* Time Complexity: O(n)
|
|
160
|
+
* Space Complexity: O(1)
|
|
161
|
+
*
|
|
162
|
+
* The `reduce` function iterates over key-value pairs and applies a callback function to each pair,
|
|
163
|
+
* accumulating a single value.
|
|
164
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
165
|
+
* It takes four arguments: the current accumulator value, the current value of the element, the key
|
|
166
|
+
* of the element, and the index of the element in the collection. It should return the updated
|
|
167
|
+
* accumulator value.
|
|
168
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
169
|
+
* is the value that will be used as the first argument to the `callbackfn` function when reducing
|
|
170
|
+
* the elements of the collection.
|
|
171
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
172
|
+
* all the elements in the collection.
|
|
173
|
+
*/
|
|
174
|
+
reduce<U>(callbackfn: ReducePairCallback<K, V, U>, initialValue: U): U {
|
|
175
|
+
let accumulator = initialValue;
|
|
176
|
+
let index = 0;
|
|
177
|
+
for (const item of this) {
|
|
178
|
+
const [key, value] = item;
|
|
179
|
+
accumulator = callbackfn(accumulator, value, key, index++, this)
|
|
180
|
+
}
|
|
181
|
+
return accumulator;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export abstract class IterableElementBase<V> {
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Time Complexity: O(n)
|
|
191
|
+
* Space Complexity: O(1)
|
|
192
|
+
*/
|
|
193
|
+
/**
|
|
194
|
+
* Time Complexity: O(n)
|
|
195
|
+
* Space Complexity: O(1)
|
|
196
|
+
*
|
|
197
|
+
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
198
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
199
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
200
|
+
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
201
|
+
*/
|
|
202
|
+
* [Symbol.iterator](...args: any[]): IterableIterator<V> {
|
|
203
|
+
yield* this._getIterator(...args);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Time Complexity: O(n)
|
|
208
|
+
* Space Complexity: O(n)
|
|
209
|
+
*/
|
|
210
|
+
/**
|
|
211
|
+
* Time Complexity: O(n)
|
|
212
|
+
* Space Complexity: O(n)
|
|
213
|
+
*
|
|
214
|
+
* The function returns an iterator that yields all the values in the object.
|
|
215
|
+
*/
|
|
216
|
+
* values(): IterableIterator<V> {
|
|
217
|
+
for (const item of this) {
|
|
218
|
+
yield item;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Time Complexity: O(n)
|
|
224
|
+
* Space Complexity: O(1)
|
|
225
|
+
*/
|
|
226
|
+
/**
|
|
227
|
+
* Time Complexity: O(n)
|
|
228
|
+
* Space Complexity: O(1)
|
|
229
|
+
*
|
|
230
|
+
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
231
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
232
|
+
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
233
|
+
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
234
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
235
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
236
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
237
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
238
|
+
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
239
|
+
*/
|
|
240
|
+
every(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean {
|
|
241
|
+
let index = 0;
|
|
242
|
+
for (const item of this) {
|
|
243
|
+
if (!predicate.call(thisArg, item as V, index++, this)) {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Time Complexity: O(n)
|
|
252
|
+
* Space Complexity: O(1)
|
|
253
|
+
*/
|
|
254
|
+
/**
|
|
255
|
+
* Time Complexity: O(n)
|
|
256
|
+
* Space Complexity: O(1)
|
|
257
|
+
*
|
|
258
|
+
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
259
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
260
|
+
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
261
|
+
* element satisfies the condition.
|
|
262
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
263
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
264
|
+
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
265
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
266
|
+
* in the collection, and false otherwise.
|
|
267
|
+
*/
|
|
268
|
+
some(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean {
|
|
269
|
+
let index = 0;
|
|
270
|
+
for (const item of this) {
|
|
271
|
+
if (predicate.call(thisArg, item as V, index++, this)) {
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Time Complexity: O(n)
|
|
280
|
+
* Space Complexity: O(1)
|
|
281
|
+
*/
|
|
282
|
+
/**
|
|
283
|
+
* Time Complexity: O(n)
|
|
284
|
+
* Space Complexity: O(1)
|
|
285
|
+
*
|
|
286
|
+
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
287
|
+
* function for each element.
|
|
288
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
289
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
290
|
+
* element, and the array that forEach was called upon.
|
|
291
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
292
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
293
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
294
|
+
*/
|
|
295
|
+
forEach(callbackfn: ElementCallback<V, void>, thisArg?: any): void {
|
|
296
|
+
let index = 0;
|
|
297
|
+
for (const item of this) {
|
|
298
|
+
callbackfn.call(thisArg, item as V, index++, this)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Time Complexity: O(n)
|
|
304
|
+
* Space Complexity: O(1)
|
|
305
|
+
*/
|
|
306
|
+
/**
|
|
307
|
+
* Time Complexity: O(n)
|
|
308
|
+
* Space Complexity: O(1)
|
|
309
|
+
*
|
|
310
|
+
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
|
311
|
+
* function to reduce them into a single value.
|
|
312
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
313
|
+
* the array. It takes four arguments:
|
|
314
|
+
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
315
|
+
* is the value that the accumulator starts with before the reduction operation begins.
|
|
316
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
317
|
+
* all the elements in the array and applying the callback function to each element.
|
|
318
|
+
*/
|
|
319
|
+
reduce<U>(callbackfn: ReduceElementCallback<V, U>, initialValue: U): U {
|
|
320
|
+
let accumulator = initialValue;
|
|
321
|
+
let index = 0;
|
|
322
|
+
for (const item of this) {
|
|
323
|
+
accumulator = callbackfn(accumulator, item as V, index++, this)
|
|
324
|
+
}
|
|
325
|
+
return accumulator;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
protected abstract _getIterator(...args: any[]): IterableIterator<V>;
|
|
329
|
+
}
|
|
@@ -13,7 +13,7 @@ import type {
|
|
|
13
13
|
BTNKey,
|
|
14
14
|
BTNodeEntry,
|
|
15
15
|
BTNodeExemplar,
|
|
16
|
-
BTNodeKeyOrNode
|
|
16
|
+
BTNodeKeyOrNode,
|
|
17
17
|
} from '../../types';
|
|
18
18
|
import {
|
|
19
19
|
BinaryTreeNested,
|
|
@@ -22,11 +22,13 @@ import {
|
|
|
22
22
|
DFSOrderPattern,
|
|
23
23
|
FamilyPosition,
|
|
24
24
|
IterationType,
|
|
25
|
-
NodeDisplayLayout
|
|
25
|
+
NodeDisplayLayout,
|
|
26
|
+
PairCallback
|
|
26
27
|
} from '../../types';
|
|
27
28
|
import { IBinaryTree } from '../../interfaces';
|
|
28
29
|
import { trampoline } from '../../utils';
|
|
29
30
|
import { Queue } from '../queue';
|
|
31
|
+
import { IterablePairBase } from "../base";
|
|
30
32
|
|
|
31
33
|
/**
|
|
32
34
|
* Represents a node in a binary tree.
|
|
@@ -103,7 +105,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
103
105
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
104
106
|
*/
|
|
105
107
|
|
|
106
|
-
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>>
|
|
108
|
+
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> extends IterablePairBase<BTNKey, V | undefined>
|
|
107
109
|
|
|
108
110
|
implements IBinaryTree<V, N, TREE> {
|
|
109
111
|
iterationType = IterationType.ITERATIVE
|
|
@@ -118,7 +120,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
118
120
|
* required.
|
|
119
121
|
*/
|
|
120
122
|
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<BinaryTreeOptions>) {
|
|
121
|
-
|
|
123
|
+
super();
|
|
122
124
|
if (options) {
|
|
123
125
|
const { iterationType } = options;
|
|
124
126
|
if (iterationType) {
|
|
@@ -1724,47 +1726,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1724
1726
|
return ans;
|
|
1725
1727
|
}
|
|
1726
1728
|
|
|
1727
|
-
/**
|
|
1728
|
-
* Time complexity: O(n)
|
|
1729
|
-
* Space complexity: O(n)
|
|
1730
|
-
*/
|
|
1731
|
-
|
|
1732
|
-
/**
|
|
1733
|
-
* Time complexity: O(n)
|
|
1734
|
-
* Space complexity: O(n)
|
|
1735
|
-
*
|
|
1736
|
-
* The function "keys" returns an array of keys from a given object.
|
|
1737
|
-
* @returns an array of BTNKey objects.
|
|
1738
|
-
*/
|
|
1739
|
-
keys(): BTNKey[] {
|
|
1740
|
-
const keys: BTNKey[] = [];
|
|
1741
|
-
for (const entry of this) {
|
|
1742
|
-
keys.push(entry[0]);
|
|
1743
|
-
}
|
|
1744
|
-
return keys;
|
|
1745
|
-
}
|
|
1746
|
-
|
|
1747
|
-
/**
|
|
1748
|
-
* Time complexity: O(n)
|
|
1749
|
-
* Space complexity: O(n)
|
|
1750
|
-
*/
|
|
1751
|
-
|
|
1752
|
-
/**
|
|
1753
|
-
* Time complexity: O(n)
|
|
1754
|
-
* Space complexity: O(n)
|
|
1755
|
-
*
|
|
1756
|
-
* The function "values" returns an array of values from a map-like object.
|
|
1757
|
-
* @returns The `values()` method is returning an array of values (`V`) from the entries in the
|
|
1758
|
-
* object.
|
|
1759
|
-
*/
|
|
1760
|
-
values(): (V | undefined)[] {
|
|
1761
|
-
const values: (V | undefined)[] = [];
|
|
1762
|
-
for (const entry of this) {
|
|
1763
|
-
values.push(entry[1]);
|
|
1764
|
-
}
|
|
1765
|
-
return values;
|
|
1766
|
-
}
|
|
1767
|
-
|
|
1768
1729
|
/**
|
|
1769
1730
|
* Time complexity: O(n)
|
|
1770
1731
|
* Space complexity: O(n)
|
|
@@ -1785,34 +1746,30 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1785
1746
|
}
|
|
1786
1747
|
|
|
1787
1748
|
/**
|
|
1788
|
-
* Time
|
|
1789
|
-
* Space
|
|
1749
|
+
* Time Complexity: O(n)
|
|
1750
|
+
* Space Complexity: O(n)
|
|
1790
1751
|
*/
|
|
1791
1752
|
|
|
1792
1753
|
/**
|
|
1793
|
-
*
|
|
1794
|
-
*
|
|
1795
|
-
*
|
|
1796
|
-
* tree
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
*
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
* `tree`.
|
|
1809
|
-
* @returns The `filter` method is returning a new tree object that contains only the entries that
|
|
1810
|
-
* satisfy the given predicate function.
|
|
1811
|
-
*/
|
|
1812
|
-
filter(predicate: (entry: [BTNKey, V | undefined], tree: this) => boolean) {
|
|
1754
|
+
* Time Complexity: O(n)
|
|
1755
|
+
* Space Complexity: O(n)
|
|
1756
|
+
*
|
|
1757
|
+
* The `filter` function creates a new tree by iterating over the elements of the current tree and
|
|
1758
|
+
* adding only the elements that satisfy the given predicate function.
|
|
1759
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
1760
|
+
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
1761
|
+
* included in the filtered tree or not.
|
|
1762
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1763
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
1764
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
1765
|
+
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that
|
|
1766
|
+
* pass the given predicate function.
|
|
1767
|
+
*/
|
|
1768
|
+
filter(predicate: PairCallback<BTNKey, V | undefined, boolean>, thisArg?: any) {
|
|
1813
1769
|
const newTree = this.createTree();
|
|
1770
|
+
let index = 0;
|
|
1814
1771
|
for (const [key, value] of this) {
|
|
1815
|
-
if (predicate(
|
|
1772
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
1816
1773
|
newTree.add([key, value]);
|
|
1817
1774
|
}
|
|
1818
1775
|
}
|
|
@@ -1820,58 +1777,74 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1820
1777
|
}
|
|
1821
1778
|
|
|
1822
1779
|
/**
|
|
1823
|
-
*
|
|
1824
|
-
*
|
|
1825
|
-
|
|
1780
|
+
* Time Complexity: O(n)
|
|
1781
|
+
* Space Complexity: O(n)
|
|
1782
|
+
*/
|
|
1783
|
+
|
|
1784
|
+
/**
|
|
1785
|
+
* Time Complexity: O(n)
|
|
1786
|
+
* Space Complexity: O(n)
|
|
1787
|
+
*
|
|
1788
|
+
* The `map` function creates a new tree by applying a callback function to each key-value pair in
|
|
1789
|
+
* the original tree.
|
|
1790
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
1791
|
+
* in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
|
|
1792
|
+
* the index of the current pair, and a reference to the tree itself. The callback function should
|
|
1793
|
+
* return a new
|
|
1794
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1795
|
+
* specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
|
|
1796
|
+
* will be used as the `this` value when the callback function is called. If you don't pass a value
|
|
1826
1797
|
* @returns The `map` method is returning a new tree object.
|
|
1827
1798
|
*/
|
|
1828
|
-
map(callback:
|
|
1799
|
+
map(callback: PairCallback<BTNKey, V | undefined, V>, thisArg?: any) {
|
|
1829
1800
|
const newTree = this.createTree();
|
|
1801
|
+
let index = 0;
|
|
1830
1802
|
for (const [key, value] of this) {
|
|
1831
|
-
newTree.add([key, callback(
|
|
1803
|
+
newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
|
|
1832
1804
|
}
|
|
1833
1805
|
return newTree;
|
|
1834
1806
|
}
|
|
1835
1807
|
|
|
1836
|
-
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1837
|
-
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
1838
|
-
// const newTree = this.createTree();
|
|
1839
|
-
// for (const [key, value] of this) {
|
|
1840
|
-
// newTree.add(key, callback([key, value], this));
|
|
1841
|
-
// }
|
|
1842
|
-
// return newTree;
|
|
1843
|
-
// }
|
|
1844
|
-
|
|
1845
|
-
/**
|
|
1846
|
-
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
1847
|
-
* entry, accumulating a single value.
|
|
1848
|
-
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
1849
|
-
* entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
|
|
1850
|
-
* based on the logic defined in the callback function.
|
|
1851
|
-
* @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
1852
|
-
* is the value that will be passed as the first argument to the callback function when reducing the
|
|
1853
|
-
* elements of the tree.
|
|
1854
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
1855
|
-
* all the entries in the tree and applying the callback function to each entry.
|
|
1856
|
-
*/
|
|
1857
|
-
reduce<T>(callback: (accumulator: T, entry: [BTNKey, V | undefined], tree: this) => T, initialValue: T): T {
|
|
1858
|
-
let accumulator = initialValue;
|
|
1859
|
-
for (const [key, value] of this) {
|
|
1860
|
-
accumulator = callback(accumulator, [key, value], this);
|
|
1861
|
-
}
|
|
1862
|
-
return accumulator;
|
|
1863
|
-
}
|
|
1808
|
+
// // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1809
|
+
// // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
1810
|
+
// // const newTree = this.createTree();
|
|
1811
|
+
// // for (const [key, value] of this) {
|
|
1812
|
+
// // newTree.add(key, callback([key, value], this));
|
|
1813
|
+
// // }
|
|
1814
|
+
// // return newTree;
|
|
1815
|
+
// // }
|
|
1816
|
+
//
|
|
1864
1817
|
|
|
1865
1818
|
/**
|
|
1866
|
-
* The
|
|
1867
|
-
*
|
|
1868
|
-
*
|
|
1869
|
-
*
|
|
1870
|
-
*
|
|
1871
|
-
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1872
|
-
* binary tree nodes in a specific order.
|
|
1819
|
+
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1820
|
+
* @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
|
|
1821
|
+
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1822
|
+
* following types:
|
|
1823
|
+
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
1873
1824
|
*/
|
|
1874
|
-
|
|
1825
|
+
print(beginRoot: BTNodeKeyOrNode<N> = this.root, options?: BinaryTreePrintOptions): void {
|
|
1826
|
+
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1827
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1828
|
+
if (!beginRoot) return;
|
|
1829
|
+
|
|
1830
|
+
if (opts.isShowUndefined) console.log(`U for undefined
|
|
1831
|
+
`);
|
|
1832
|
+
if (opts.isShowNull) console.log(`N for null
|
|
1833
|
+
`);
|
|
1834
|
+
if (opts.isShowRedBlackNIL) console.log(`S for Sentinel Node
|
|
1835
|
+
`);
|
|
1836
|
+
|
|
1837
|
+
const display = (root: N | null | undefined): void => {
|
|
1838
|
+
const [lines, , ,] = this._displayAux(root, opts);
|
|
1839
|
+
for (const line of lines) {
|
|
1840
|
+
console.log(line);
|
|
1841
|
+
}
|
|
1842
|
+
};
|
|
1843
|
+
|
|
1844
|
+
display(beginRoot);
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1847
|
+
protected* _getIterator(node = this.root): IterableIterator<[BTNKey, V | undefined]> {
|
|
1875
1848
|
if (!node) return;
|
|
1876
1849
|
|
|
1877
1850
|
if (this.iterationType === IterationType.ITERATIVE) {
|
|
@@ -1902,35 +1875,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1902
1875
|
}
|
|
1903
1876
|
}
|
|
1904
1877
|
|
|
1905
|
-
/**
|
|
1906
|
-
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1907
|
-
* @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
|
|
1908
|
-
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1909
|
-
* following types:
|
|
1910
|
-
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
1911
|
-
*/
|
|
1912
|
-
print(beginRoot: BTNodeKeyOrNode<N> = this.root, options?: BinaryTreePrintOptions): void {
|
|
1913
|
-
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1914
|
-
beginRoot = this.ensureNode(beginRoot);
|
|
1915
|
-
if (!beginRoot) return;
|
|
1916
|
-
|
|
1917
|
-
if (opts.isShowUndefined) console.log(`U for undefined
|
|
1918
|
-
`);
|
|
1919
|
-
if (opts.isShowNull) console.log(`N for null
|
|
1920
|
-
`);
|
|
1921
|
-
if (opts.isShowRedBlackNIL) console.log(`S for Sentinel Node
|
|
1922
|
-
`);
|
|
1923
|
-
|
|
1924
|
-
const display = (root: N | null | undefined): void => {
|
|
1925
|
-
const [lines, , ,] = this._displayAux(root, opts);
|
|
1926
|
-
for (const line of lines) {
|
|
1927
|
-
console.log(line);
|
|
1928
|
-
}
|
|
1929
|
-
};
|
|
1930
|
-
|
|
1931
|
-
display(beginRoot);
|
|
1932
|
-
}
|
|
1933
|
-
|
|
1934
1878
|
protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout {
|
|
1935
1879
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1936
1880
|
const emptyDisplayLayout = <NodeDisplayLayout>[['─'], 1, 0, 0];
|