data-structure-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/CHANGELOG.md +1 -1
- package/README.md +24 -18
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +182 -182
- package/dist/cjs/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/data-structures/base/index.js +18 -0
- package/dist/cjs/data-structures/base/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/cjs/data-structures/base/iterable-base.js +313 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +78 -129
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/cjs/data-structures/hash/hash-map.js +69 -173
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/cjs/data-structures/heap/heap.js +60 -30
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/index.d.ts +1 -0
- package/dist/cjs/data-structures/index.js +1 -0
- package/dist/cjs/data-structures/index.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/cjs/data-structures/queue/deque.js +36 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/cjs/data-structures/queue/queue.js +69 -82
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/cjs/data-structures/stack/stack.js +50 -31
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/cjs/data-structures/trie/trie.js +53 -32
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/cjs/types/data-structures/base/base.js +3 -0
- package/dist/cjs/types/data-structures/base/base.js.map +1 -0
- package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/base/index.js +18 -0
- package/dist/cjs/types/data-structures/base/index.js.map +1 -0
- package/dist/cjs/types/data-structures/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/index.js +1 -0
- package/dist/cjs/types/data-structures/index.js.map +1 -1
- package/dist/mjs/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/data-structures/base/index.js +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/mjs/data-structures/base/iterable-base.js +307 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +78 -129
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
- package/dist/mjs/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/mjs/data-structures/hash/hash-map.js +69 -173
- package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/mjs/data-structures/heap/heap.js +60 -30
- package/dist/mjs/data-structures/index.d.ts +1 -0
- package/dist/mjs/data-structures/index.js +1 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/mjs/data-structures/queue/deque.js +36 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/mjs/data-structures/queue/queue.js +66 -79
- package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/mjs/data-structures/stack/stack.js +50 -31
- package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/mjs/data-structures/trie/trie.js +53 -32
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/mjs/types/data-structures/base/base.js +1 -0
- package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/base/index.js +1 -0
- package/dist/mjs/types/data-structures/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/index.js +1 -0
- package/dist/umd/data-structure-typed.js +846 -715
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- 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
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +10 -10
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +10 -10
- package/test/unit/data-structures/binary-tree/bst.test.ts +10 -10
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +10 -10
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
- package/test/unit/data-structures/queue/deque.test.ts +25 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
export class IterablePairBase {
|
|
2
|
+
/**
|
|
3
|
+
* Time Complexity: O(n)
|
|
4
|
+
* Space Complexity: O(1)
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Time Complexity: O(n)
|
|
8
|
+
* Space Complexity: O(1)
|
|
9
|
+
*
|
|
10
|
+
* The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
|
|
11
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
12
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
13
|
+
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
14
|
+
*/
|
|
15
|
+
*[Symbol.iterator](...args) {
|
|
16
|
+
yield* this._getIterator(...args);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Time Complexity: O(n)
|
|
20
|
+
* Space Complexity: O(n)
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Time Complexity: O(n)
|
|
24
|
+
* Space Complexity: O(n)
|
|
25
|
+
*
|
|
26
|
+
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
27
|
+
* be undefined.
|
|
28
|
+
*/
|
|
29
|
+
*entries() {
|
|
30
|
+
for (const item of this) {
|
|
31
|
+
yield item;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Time Complexity: O(n)
|
|
36
|
+
* Space Complexity: O(n)
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* Time Complexity: O(n)
|
|
40
|
+
* Space Complexity: O(n)
|
|
41
|
+
*
|
|
42
|
+
* The function returns an iterator that yields the keys of a data structure.
|
|
43
|
+
*/
|
|
44
|
+
*keys() {
|
|
45
|
+
for (const item of this) {
|
|
46
|
+
yield item[0];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Time Complexity: O(n)
|
|
51
|
+
* Space Complexity: O(n)
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* Time Complexity: O(n)
|
|
55
|
+
* Space Complexity: O(n)
|
|
56
|
+
*
|
|
57
|
+
* The function returns an iterator that yields the values of a collection.
|
|
58
|
+
*/
|
|
59
|
+
*values() {
|
|
60
|
+
for (const item of this) {
|
|
61
|
+
yield item[1];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Time Complexity: O(n)
|
|
66
|
+
* Space Complexity: O(1)
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* Time Complexity: O(n)
|
|
70
|
+
* Space Complexity: O(1)
|
|
71
|
+
*
|
|
72
|
+
* The `every` function checks if every element in a collection satisfies a given condition.
|
|
73
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
74
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
75
|
+
* met for the current element in the iteration.
|
|
76
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
77
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
78
|
+
* passed as the first argument to the `predicate` function. If `thisArg` is not provided
|
|
79
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
80
|
+
* the collection satisfies the provided predicate function, and `false` otherwise.
|
|
81
|
+
*/
|
|
82
|
+
every(predicate, thisArg) {
|
|
83
|
+
let index = 0;
|
|
84
|
+
for (const item of this) {
|
|
85
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Time Complexity: O(n)
|
|
93
|
+
* Space Complexity: O(1)
|
|
94
|
+
*/
|
|
95
|
+
/**
|
|
96
|
+
* Time Complexity: O(n)
|
|
97
|
+
* Space Complexity: O(1)
|
|
98
|
+
*
|
|
99
|
+
* The "some" function iterates over a collection and returns true if at least one element satisfies
|
|
100
|
+
* a given predicate.
|
|
101
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
102
|
+
* `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
|
|
103
|
+
* met for the current element in the iteration.
|
|
104
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
105
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
106
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
107
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any pair in
|
|
108
|
+
* the collection, and false otherwise.
|
|
109
|
+
*/
|
|
110
|
+
some(predicate, thisArg) {
|
|
111
|
+
let index = 0;
|
|
112
|
+
for (const item of this) {
|
|
113
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Time Complexity: O(n)
|
|
121
|
+
* Space Complexity: O(1)
|
|
122
|
+
*/
|
|
123
|
+
/**
|
|
124
|
+
* Time Complexity: O(n)
|
|
125
|
+
* Space Complexity: O(1)
|
|
126
|
+
*
|
|
127
|
+
* The `forEach` function iterates over each key-value pair in a collection and executes a callback
|
|
128
|
+
* function for each pair.
|
|
129
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
130
|
+
* It takes four parameters: the value of the current element, the key of the current element, the
|
|
131
|
+
* index of the current element, and the collection itself.
|
|
132
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
133
|
+
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
134
|
+
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
135
|
+
*/
|
|
136
|
+
forEach(callbackfn, thisArg) {
|
|
137
|
+
let index = 0;
|
|
138
|
+
for (const item of this) {
|
|
139
|
+
const [key, value] = item;
|
|
140
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Time Complexity: O(n)
|
|
145
|
+
* Space Complexity: O(1)
|
|
146
|
+
*/
|
|
147
|
+
/**
|
|
148
|
+
* Time Complexity: O(n)
|
|
149
|
+
* Space Complexity: O(1)
|
|
150
|
+
*
|
|
151
|
+
* The `reduce` function iterates over key-value pairs and applies a callback function to each pair,
|
|
152
|
+
* accumulating a single value.
|
|
153
|
+
* @param callbackfn - The callback function that will be called for each element in the collection.
|
|
154
|
+
* It takes four arguments: the current accumulator value, the current value of the element, the key
|
|
155
|
+
* of the element, and the index of the element in the collection. It should return the updated
|
|
156
|
+
* accumulator value.
|
|
157
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
158
|
+
* is the value that will be used as the first argument to the `callbackfn` function when reducing
|
|
159
|
+
* the elements of the collection.
|
|
160
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
161
|
+
* all the elements in the collection.
|
|
162
|
+
*/
|
|
163
|
+
reduce(callbackfn, initialValue) {
|
|
164
|
+
let accumulator = initialValue;
|
|
165
|
+
let index = 0;
|
|
166
|
+
for (const item of this) {
|
|
167
|
+
const [key, value] = item;
|
|
168
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
169
|
+
}
|
|
170
|
+
return accumulator;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
export class IterableElementBase {
|
|
174
|
+
/**
|
|
175
|
+
* Time Complexity: O(n)
|
|
176
|
+
* Space Complexity: O(1)
|
|
177
|
+
*/
|
|
178
|
+
/**
|
|
179
|
+
* Time Complexity: O(n)
|
|
180
|
+
* Space Complexity: O(1)
|
|
181
|
+
*
|
|
182
|
+
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
183
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
184
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
185
|
+
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
186
|
+
*/
|
|
187
|
+
*[Symbol.iterator](...args) {
|
|
188
|
+
yield* this._getIterator(...args);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Time Complexity: O(n)
|
|
192
|
+
* Space Complexity: O(n)
|
|
193
|
+
*/
|
|
194
|
+
/**
|
|
195
|
+
* Time Complexity: O(n)
|
|
196
|
+
* Space Complexity: O(n)
|
|
197
|
+
*
|
|
198
|
+
* The function returns an iterator that yields all the values in the object.
|
|
199
|
+
*/
|
|
200
|
+
*values() {
|
|
201
|
+
for (const item of this) {
|
|
202
|
+
yield item;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Time Complexity: O(n)
|
|
207
|
+
* Space Complexity: O(1)
|
|
208
|
+
*/
|
|
209
|
+
/**
|
|
210
|
+
* Time Complexity: O(n)
|
|
211
|
+
* Space Complexity: O(1)
|
|
212
|
+
*
|
|
213
|
+
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
214
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
215
|
+
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
216
|
+
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
217
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
218
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
219
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
220
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
221
|
+
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
222
|
+
*/
|
|
223
|
+
every(predicate, thisArg) {
|
|
224
|
+
let index = 0;
|
|
225
|
+
for (const item of this) {
|
|
226
|
+
if (!predicate.call(thisArg, item, index++, this)) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Time Complexity: O(n)
|
|
234
|
+
* Space Complexity: O(1)
|
|
235
|
+
*/
|
|
236
|
+
/**
|
|
237
|
+
* Time Complexity: O(n)
|
|
238
|
+
* Space Complexity: O(1)
|
|
239
|
+
*
|
|
240
|
+
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
241
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
242
|
+
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
243
|
+
* element satisfies the condition.
|
|
244
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
245
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
246
|
+
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
247
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
248
|
+
* in the collection, and false otherwise.
|
|
249
|
+
*/
|
|
250
|
+
some(predicate, thisArg) {
|
|
251
|
+
let index = 0;
|
|
252
|
+
for (const item of this) {
|
|
253
|
+
if (predicate.call(thisArg, item, index++, this)) {
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Time Complexity: O(n)
|
|
261
|
+
* Space Complexity: O(1)
|
|
262
|
+
*/
|
|
263
|
+
/**
|
|
264
|
+
* Time Complexity: O(n)
|
|
265
|
+
* Space Complexity: O(1)
|
|
266
|
+
*
|
|
267
|
+
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
268
|
+
* function for each element.
|
|
269
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
270
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
271
|
+
* element, and the array that forEach was called upon.
|
|
272
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
273
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
274
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
275
|
+
*/
|
|
276
|
+
forEach(callbackfn, thisArg) {
|
|
277
|
+
let index = 0;
|
|
278
|
+
for (const item of this) {
|
|
279
|
+
callbackfn.call(thisArg, item, index++, this);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Time Complexity: O(n)
|
|
284
|
+
* Space Complexity: O(1)
|
|
285
|
+
*/
|
|
286
|
+
/**
|
|
287
|
+
* Time Complexity: O(n)
|
|
288
|
+
* Space Complexity: O(1)
|
|
289
|
+
*
|
|
290
|
+
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
|
291
|
+
* function to reduce them into a single value.
|
|
292
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
293
|
+
* the array. It takes four arguments:
|
|
294
|
+
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
295
|
+
* is the value that the accumulator starts with before the reduction operation begins.
|
|
296
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
297
|
+
* all the elements in the array and applying the callback function to each element.
|
|
298
|
+
*/
|
|
299
|
+
reduce(callbackfn, initialValue) {
|
|
300
|
+
let accumulator = initialValue;
|
|
301
|
+
let index = 0;
|
|
302
|
+
for (const item of this) {
|
|
303
|
+
accumulator = callbackfn(accumulator, item, index++, this);
|
|
304
|
+
}
|
|
305
|
+
return accumulator;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey, BTNodeEntry, BTNodeExemplar, BTNodeKeyOrNode } from '../../types';
|
|
9
|
-
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout } from '../../types';
|
|
9
|
+
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout, PairCallback } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
import { IterablePairBase } from "../base";
|
|
11
12
|
/**
|
|
12
13
|
* Represents a node in a binary tree.
|
|
13
14
|
* @template V - The type of data stored in the node.
|
|
@@ -41,7 +42,7 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
41
42
|
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
42
43
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
43
44
|
*/
|
|
44
|
-
export declare 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>>> implements IBinaryTree<V, N, TREE> {
|
|
45
|
+
export declare 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> implements IBinaryTree<V, N, TREE> {
|
|
45
46
|
iterationType: IterationType;
|
|
46
47
|
/**
|
|
47
48
|
* The constructor function initializes a binary tree object with optional elements and options.
|
|
@@ -460,31 +461,6 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
460
461
|
* by the return type of the `callback` function.
|
|
461
462
|
*/
|
|
462
463
|
morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNodeKeyOrNode<N>): ReturnType<C>[];
|
|
463
|
-
/**
|
|
464
|
-
* Time complexity: O(n)
|
|
465
|
-
* Space complexity: O(n)
|
|
466
|
-
*/
|
|
467
|
-
/**
|
|
468
|
-
* Time complexity: O(n)
|
|
469
|
-
* Space complexity: O(n)
|
|
470
|
-
*
|
|
471
|
-
* The function "keys" returns an array of keys from a given object.
|
|
472
|
-
* @returns an array of BTNKey objects.
|
|
473
|
-
*/
|
|
474
|
-
keys(): BTNKey[];
|
|
475
|
-
/**
|
|
476
|
-
* Time complexity: O(n)
|
|
477
|
-
* Space complexity: O(n)
|
|
478
|
-
*/
|
|
479
|
-
/**
|
|
480
|
-
* Time complexity: O(n)
|
|
481
|
-
* Space complexity: O(n)
|
|
482
|
-
*
|
|
483
|
-
* The function "values" returns an array of values from a map-like object.
|
|
484
|
-
* @returns The `values()` method is returning an array of values (`V`) from the entries in the
|
|
485
|
-
* object.
|
|
486
|
-
*/
|
|
487
|
-
values(): (V | undefined)[];
|
|
488
464
|
/**
|
|
489
465
|
* Time complexity: O(n)
|
|
490
466
|
* Space complexity: O(n)
|
|
@@ -499,55 +475,45 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
499
475
|
*/
|
|
500
476
|
clone(): TREE;
|
|
501
477
|
/**
|
|
502
|
-
* Time
|
|
503
|
-
* Space
|
|
478
|
+
* Time Complexity: O(n)
|
|
479
|
+
* Space Complexity: O(n)
|
|
504
480
|
*/
|
|
505
481
|
/**
|
|
506
|
-
*
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
* tree
|
|
510
|
-
|
|
511
|
-
|
|
482
|
+
* Time Complexity: O(n)
|
|
483
|
+
* Space Complexity: O(n)
|
|
484
|
+
*
|
|
485
|
+
* The `filter` function creates a new tree by iterating over the elements of the current tree and
|
|
486
|
+
* adding only the elements that satisfy the given predicate function.
|
|
487
|
+
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
488
|
+
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
489
|
+
* included in the filtered tree or not.
|
|
490
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
491
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
492
|
+
* it will be passed as the first argument to the `predicate` function. If `thisArg` is
|
|
493
|
+
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that
|
|
494
|
+
* pass the given predicate function.
|
|
495
|
+
*/
|
|
496
|
+
filter(predicate: PairCallback<BTNKey, V | undefined, boolean>, thisArg?: any): TREE;
|
|
512
497
|
/**
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
* @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
|
|
516
|
-
* `tree`.
|
|
517
|
-
* @returns The `filter` method is returning a new tree object that contains only the entries that
|
|
518
|
-
* satisfy the given predicate function.
|
|
498
|
+
* Time Complexity: O(n)
|
|
499
|
+
* Space Complexity: O(n)
|
|
519
500
|
*/
|
|
520
|
-
filter(predicate: (entry: [BTNKey, V | undefined], tree: this) => boolean): TREE;
|
|
521
501
|
/**
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
*
|
|
502
|
+
* Time Complexity: O(n)
|
|
503
|
+
* Space Complexity: O(n)
|
|
504
|
+
*
|
|
505
|
+
* The `map` function creates a new tree by applying a callback function to each key-value pair in
|
|
506
|
+
* the original tree.
|
|
507
|
+
* @param callback - The callback parameter is a function that will be called for each key-value pair
|
|
508
|
+
* in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
|
|
509
|
+
* the index of the current pair, and a reference to the tree itself. The callback function should
|
|
510
|
+
* return a new
|
|
511
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
512
|
+
* specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
|
|
513
|
+
* will be used as the `this` value when the callback function is called. If you don't pass a value
|
|
525
514
|
* @returns The `map` method is returning a new tree object.
|
|
526
515
|
*/
|
|
527
|
-
map(callback:
|
|
528
|
-
/**
|
|
529
|
-
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
530
|
-
* entry, accumulating a single value.
|
|
531
|
-
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
532
|
-
* entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
|
|
533
|
-
* based on the logic defined in the callback function.
|
|
534
|
-
* @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
535
|
-
* is the value that will be passed as the first argument to the callback function when reducing the
|
|
536
|
-
* elements of the tree.
|
|
537
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
538
|
-
* all the entries in the tree and applying the callback function to each entry.
|
|
539
|
-
*/
|
|
540
|
-
reduce<T>(callback: (accumulator: T, entry: [BTNKey, V | undefined], tree: this) => T, initialValue: T): T;
|
|
541
|
-
/**
|
|
542
|
-
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
543
|
-
* either an iterative or recursive manner.
|
|
544
|
-
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
545
|
-
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
546
|
-
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
547
|
-
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
548
|
-
* binary tree nodes in a specific order.
|
|
549
|
-
*/
|
|
550
|
-
[Symbol.iterator](node?: N | null | undefined): Generator<[BTNKey, V | undefined], void, undefined>;
|
|
516
|
+
map(callback: PairCallback<BTNKey, V | undefined, V>, thisArg?: any): TREE;
|
|
551
517
|
/**
|
|
552
518
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
553
519
|
* @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
|
|
@@ -556,6 +522,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
556
522
|
* @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.
|
|
557
523
|
*/
|
|
558
524
|
print(beginRoot?: BTNodeKeyOrNode<N>, options?: BinaryTreePrintOptions): void;
|
|
525
|
+
protected _getIterator(node?: N | null | undefined): IterableIterator<[BTNKey, V | undefined]>;
|
|
559
526
|
protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
|
|
560
527
|
protected _defaultOneParamCallback: (node: N) => number;
|
|
561
528
|
/**
|