avl-tree-typed 1.53.7 → 1.53.8
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/common/index.js +5 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +2 -2
- package/dist/data-structures/binary-tree/bst.d.ts +53 -23
- package/dist/data-structures/binary-tree/bst.js +59 -25
- package/dist/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/data-structures/binary-tree/index.js +1 -1
- package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +49 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +50 -1
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/data-structures/hash/hash-map.d.ts +30 -0
- package/dist/data-structures/hash/hash-map.js +30 -0
- package/dist/data-structures/heap/heap.d.ts +20 -3
- package/dist/data-structures/heap/heap.js +31 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +36 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +56 -9
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +34 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +54 -10
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +8 -3
- package/dist/data-structures/trie/trie.js +8 -3
- package/package.json +2 -2
- package/src/common/index.ts +7 -1
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/data-structures/binary-tree/bst.ts +64 -25
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +50 -1
- package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/data-structures/hash/hash-map.ts +30 -0
- package/src/data-structures/heap/heap.ts +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -8
- package/src/data-structures/linked-list/singly-linked-list.ts +60 -10
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +8 -3
|
@@ -60,14 +60,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
60
60
|
const needBucketNum = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize);
|
|
61
61
|
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
62
62
|
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
|
|
63
|
-
|
|
64
|
-
if (this.toElementFn) {
|
|
65
|
-
this.push(this.toElementFn(el));
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
this.push(el);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
63
|
+
this.pushMany(elements);
|
|
71
64
|
}
|
|
72
65
|
/**
|
|
73
66
|
* The bucketSize function returns the size of the bucket.
|
|
@@ -214,6 +207,35 @@ class Deque extends base_1.IterableElementBase {
|
|
|
214
207
|
this._size -= 1;
|
|
215
208
|
return element;
|
|
216
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Time Complexity: O(1)
|
|
212
|
+
* Space Complexity: O(1)
|
|
213
|
+
*
|
|
214
|
+
* The `shift()` function removes and returns the first element from a data structure, updating the
|
|
215
|
+
* internal state variables accordingly.
|
|
216
|
+
* @returns The element that is being removed from the beginning of the data structure is being
|
|
217
|
+
* returned.
|
|
218
|
+
*/
|
|
219
|
+
shift() {
|
|
220
|
+
if (this._size === 0)
|
|
221
|
+
return;
|
|
222
|
+
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
223
|
+
if (this._size !== 1) {
|
|
224
|
+
if (this._firstInBucket < this._bucketSize - 1) {
|
|
225
|
+
this._firstInBucket += 1;
|
|
226
|
+
}
|
|
227
|
+
else if (this._bucketFirst < this._bucketCount - 1) {
|
|
228
|
+
this._bucketFirst += 1;
|
|
229
|
+
this._firstInBucket = 0;
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
this._bucketFirst = 0;
|
|
233
|
+
this._firstInBucket = 0;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
this._size -= 1;
|
|
237
|
+
return element;
|
|
238
|
+
}
|
|
217
239
|
/**
|
|
218
240
|
* Time Complexity: Amortized O(1)
|
|
219
241
|
* Space Complexity: O(n)
|
|
@@ -247,33 +269,55 @@ class Deque extends base_1.IterableElementBase {
|
|
|
247
269
|
return true;
|
|
248
270
|
}
|
|
249
271
|
/**
|
|
250
|
-
* Time Complexity: O(
|
|
251
|
-
* Space Complexity: O(
|
|
272
|
+
* Time Complexity: O(k)
|
|
273
|
+
* Space Complexity: O(k)
|
|
252
274
|
*
|
|
253
|
-
* The `
|
|
254
|
-
*
|
|
255
|
-
* @
|
|
256
|
-
*
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
275
|
+
* The function `pushMany` iterates over elements and pushes them into an array after applying a
|
|
276
|
+
* transformation function if provided.
|
|
277
|
+
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
278
|
+
* parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
|
|
279
|
+
* or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
|
|
280
|
+
* function iterates over each element
|
|
281
|
+
* @returns The `pushMany` function is returning an array of boolean values, where each value
|
|
282
|
+
* represents the result of calling the `push` method on the current object instance with the
|
|
283
|
+
* corresponding element from the input `elements` iterable.
|
|
284
|
+
*/
|
|
285
|
+
pushMany(elements) {
|
|
286
|
+
const ans = [];
|
|
287
|
+
for (const el of elements) {
|
|
288
|
+
if (this.toElementFn) {
|
|
289
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
265
290
|
}
|
|
266
|
-
else
|
|
267
|
-
this.
|
|
268
|
-
|
|
291
|
+
else {
|
|
292
|
+
ans.push(this.push(el));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return ans;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Time Complexity: O(k)
|
|
299
|
+
* Space Complexity: O(k)
|
|
300
|
+
*
|
|
301
|
+
* The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of
|
|
302
|
+
* an array, optionally converting them using a provided function.
|
|
303
|
+
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
304
|
+
* parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
|
|
305
|
+
* can be an array or any other iterable data structure that has a known size or length. The function
|
|
306
|
+
* iterates over each element in the `elements` iterable and
|
|
307
|
+
* @returns The `unshiftMany` function returns an array of boolean values indicating whether each
|
|
308
|
+
* element was successfully added to the beginning of the array.
|
|
309
|
+
*/
|
|
310
|
+
unshiftMany(elements = []) {
|
|
311
|
+
const ans = [];
|
|
312
|
+
for (const el of elements) {
|
|
313
|
+
if (this.toElementFn) {
|
|
314
|
+
ans.push(this.unshift(this.toElementFn(el)));
|
|
269
315
|
}
|
|
270
316
|
else {
|
|
271
|
-
this.
|
|
272
|
-
this._firstInBucket = 0;
|
|
317
|
+
ans.push(this.unshift(el));
|
|
273
318
|
}
|
|
274
319
|
}
|
|
275
|
-
|
|
276
|
-
return element;
|
|
320
|
+
return ans;
|
|
277
321
|
}
|
|
278
322
|
/**
|
|
279
323
|
* Time Complexity: O(1)
|
|
@@ -84,6 +84,18 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
84
84
|
* @returns Always returns true, indicating the element was successfully added.
|
|
85
85
|
*/
|
|
86
86
|
push(element: E): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Time Complexity: O(k)
|
|
89
|
+
* Space Complexity: O(k)
|
|
90
|
+
*
|
|
91
|
+
* The `pushMany` function iterates over elements and pushes them into an array after applying a
|
|
92
|
+
* transformation function if provided.
|
|
93
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
94
|
+
* is an iterable containing elements of type `E` or `R`.
|
|
95
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
96
|
+
* element was successfully pushed into the data structure.
|
|
97
|
+
*/
|
|
98
|
+
pushMany(elements: Iterable<E> | Iterable<R>): boolean[];
|
|
87
99
|
/**
|
|
88
100
|
* Time Complexity: O(1)
|
|
89
101
|
* Space Complexity: O(1)
|
|
@@ -94,12 +106,18 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
94
106
|
*/
|
|
95
107
|
shift(): E | undefined;
|
|
96
108
|
/**
|
|
109
|
+
* Time Complexity: O(n)
|
|
110
|
+
* Space Complexity: O(1)
|
|
111
|
+
*
|
|
97
112
|
* The delete function removes an element from the list.
|
|
98
113
|
* @param {E} element - Specify the element to be deleted
|
|
99
114
|
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
100
115
|
*/
|
|
101
116
|
delete(element: E): boolean;
|
|
102
117
|
/**
|
|
118
|
+
* Time Complexity: O(n)
|
|
119
|
+
* Space Complexity: O(1)
|
|
120
|
+
*
|
|
103
121
|
* The deleteAt function deletes the element at a given index.
|
|
104
122
|
* @param {number} index - Determine the index of the element to be deleted
|
|
105
123
|
* @return A boolean value
|
|
@@ -109,7 +127,12 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
109
127
|
* Time Complexity: O(1)
|
|
110
128
|
* Space Complexity: O(1)
|
|
111
129
|
*
|
|
112
|
-
*
|
|
130
|
+
* The `at` function returns the element at a specified index adjusted by an offset, or `undefined`
|
|
131
|
+
* if the index is out of bounds.
|
|
132
|
+
* @param {number} index - The `index` parameter represents the position of the element you want to
|
|
133
|
+
* retrieve from the data structure.
|
|
134
|
+
* @returns The `at` method is returning the element at the specified index adjusted by the offset
|
|
135
|
+
* `_offset`.
|
|
113
136
|
*/
|
|
114
137
|
at(index: number): E | undefined;
|
|
115
138
|
/**
|
|
@@ -136,6 +159,9 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
136
159
|
*/
|
|
137
160
|
clear(): void;
|
|
138
161
|
/**
|
|
162
|
+
* Time Complexity: O(n)
|
|
163
|
+
* Space Complexity: O(1)
|
|
164
|
+
*
|
|
139
165
|
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
140
166
|
* offset to zero.
|
|
141
167
|
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
@@ -169,6 +195,20 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
169
195
|
/**
|
|
170
196
|
* Time Complexity: O(n)
|
|
171
197
|
* Space Complexity: O(n)
|
|
198
|
+
*
|
|
199
|
+
* The `map` function in TypeScript creates a new Queue by applying a callback function to each
|
|
200
|
+
* element in the original Queue.
|
|
201
|
+
* @param callback - The `callback` parameter is a function that will be applied to each element in
|
|
202
|
+
* the queue. It takes the current element, its index, and the queue itself as arguments, and returns
|
|
203
|
+
* a new element.
|
|
204
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to
|
|
205
|
+
* convert a raw element of type `RM` to a new element of type `EM`. This function is used within the
|
|
206
|
+
* `map` method to transform each raw element before passing it to the `callback` function. If
|
|
207
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the
|
|
208
|
+
* value of `this` when executing the `callback` function. It allows you to set the context (the
|
|
209
|
+
* value of `this`) within the callback function. If `thisArg` is provided, it will be
|
|
210
|
+
* @returns A new Queue object containing elements of type EM, which are the result of applying the
|
|
211
|
+
* callback function to each element in the original Queue object.
|
|
172
212
|
*/
|
|
173
213
|
map<EM, RM>(callback: ElementCallback<E, R, EM, Queue<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Queue<EM, RM>;
|
|
174
214
|
/**
|
|
@@ -22,14 +22,7 @@ class Queue extends base_1.IterableElementBase {
|
|
|
22
22
|
const { autoCompactRatio = 0.5 } = options;
|
|
23
23
|
this._autoCompactRatio = autoCompactRatio;
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
for (const el of elements) {
|
|
27
|
-
if (this.toElementFn)
|
|
28
|
-
this.push(this.toElementFn(el));
|
|
29
|
-
else
|
|
30
|
-
this.push(el);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
25
|
+
this.pushMany(elements);
|
|
33
26
|
}
|
|
34
27
|
/**
|
|
35
28
|
* The elements function returns the elements of this set.
|
|
@@ -114,6 +107,27 @@ class Queue extends base_1.IterableElementBase {
|
|
|
114
107
|
this.elements.push(element);
|
|
115
108
|
return true;
|
|
116
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Time Complexity: O(k)
|
|
112
|
+
* Space Complexity: O(k)
|
|
113
|
+
*
|
|
114
|
+
* The `pushMany` function iterates over elements and pushes them into an array after applying a
|
|
115
|
+
* transformation function if provided.
|
|
116
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
117
|
+
* is an iterable containing elements of type `E` or `R`.
|
|
118
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
119
|
+
* element was successfully pushed into the data structure.
|
|
120
|
+
*/
|
|
121
|
+
pushMany(elements) {
|
|
122
|
+
const ans = [];
|
|
123
|
+
for (const el of elements) {
|
|
124
|
+
if (this.toElementFn)
|
|
125
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
126
|
+
else
|
|
127
|
+
ans.push(this.push(el));
|
|
128
|
+
}
|
|
129
|
+
return ans;
|
|
130
|
+
}
|
|
117
131
|
/**
|
|
118
132
|
* Time Complexity: O(1)
|
|
119
133
|
* Space Complexity: O(1)
|
|
@@ -132,6 +146,9 @@ class Queue extends base_1.IterableElementBase {
|
|
|
132
146
|
return first;
|
|
133
147
|
}
|
|
134
148
|
/**
|
|
149
|
+
* Time Complexity: O(n)
|
|
150
|
+
* Space Complexity: O(1)
|
|
151
|
+
*
|
|
135
152
|
* The delete function removes an element from the list.
|
|
136
153
|
* @param {E} element - Specify the element to be deleted
|
|
137
154
|
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
@@ -141,6 +158,9 @@ class Queue extends base_1.IterableElementBase {
|
|
|
141
158
|
return this.deleteAt(index);
|
|
142
159
|
}
|
|
143
160
|
/**
|
|
161
|
+
* Time Complexity: O(n)
|
|
162
|
+
* Space Complexity: O(1)
|
|
163
|
+
*
|
|
144
164
|
* The deleteAt function deletes the element at a given index.
|
|
145
165
|
* @param {number} index - Determine the index of the element to be deleted
|
|
146
166
|
* @return A boolean value
|
|
@@ -153,7 +173,12 @@ class Queue extends base_1.IterableElementBase {
|
|
|
153
173
|
* Time Complexity: O(1)
|
|
154
174
|
* Space Complexity: O(1)
|
|
155
175
|
*
|
|
156
|
-
*
|
|
176
|
+
* The `at` function returns the element at a specified index adjusted by an offset, or `undefined`
|
|
177
|
+
* if the index is out of bounds.
|
|
178
|
+
* @param {number} index - The `index` parameter represents the position of the element you want to
|
|
179
|
+
* retrieve from the data structure.
|
|
180
|
+
* @returns The `at` method is returning the element at the specified index adjusted by the offset
|
|
181
|
+
* `_offset`.
|
|
157
182
|
*/
|
|
158
183
|
at(index) {
|
|
159
184
|
return this.elements[index + this._offset];
|
|
@@ -189,6 +214,9 @@ class Queue extends base_1.IterableElementBase {
|
|
|
189
214
|
this._offset = 0;
|
|
190
215
|
}
|
|
191
216
|
/**
|
|
217
|
+
* Time Complexity: O(n)
|
|
218
|
+
* Space Complexity: O(1)
|
|
219
|
+
*
|
|
192
220
|
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
193
221
|
* offset to zero.
|
|
194
222
|
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
@@ -238,6 +266,20 @@ class Queue extends base_1.IterableElementBase {
|
|
|
238
266
|
/**
|
|
239
267
|
* Time Complexity: O(n)
|
|
240
268
|
* Space Complexity: O(n)
|
|
269
|
+
*
|
|
270
|
+
* The `map` function in TypeScript creates a new Queue by applying a callback function to each
|
|
271
|
+
* element in the original Queue.
|
|
272
|
+
* @param callback - The `callback` parameter is a function that will be applied to each element in
|
|
273
|
+
* the queue. It takes the current element, its index, and the queue itself as arguments, and returns
|
|
274
|
+
* a new element.
|
|
275
|
+
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be provided to
|
|
276
|
+
* convert a raw element of type `RM` to a new element of type `EM`. This function is used within the
|
|
277
|
+
* `map` method to transform each raw element before passing it to the `callback` function. If
|
|
278
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is used to specify the
|
|
279
|
+
* value of `this` when executing the `callback` function. It allows you to set the context (the
|
|
280
|
+
* value of `this`) within the callback function. If `thisArg` is provided, it will be
|
|
281
|
+
* @returns A new Queue object containing elements of type EM, which are the result of applying the
|
|
282
|
+
* callback function to each element in the original Queue object.
|
|
241
283
|
*/
|
|
242
284
|
map(callback, toElementFn, thisArg) {
|
|
243
285
|
const newDeque = new Queue([], { toElementFn });
|
|
@@ -28,10 +28,6 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
|
|
|
28
28
|
* @returns The size of the elements array.
|
|
29
29
|
*/
|
|
30
30
|
get size(): number;
|
|
31
|
-
/**
|
|
32
|
-
* Time Complexity: O(n)
|
|
33
|
-
* Space Complexity: O(n)
|
|
34
|
-
*/
|
|
35
31
|
/**
|
|
36
32
|
* Time Complexity: O(n)
|
|
37
33
|
* Space Complexity: O(n)
|
|
@@ -43,6 +39,9 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
|
|
|
43
39
|
*/
|
|
44
40
|
static fromArray<E>(elements: E[]): Stack<E>;
|
|
45
41
|
/**
|
|
42
|
+
* Time Complexity: O(1)
|
|
43
|
+
* Space Complexity: O(1)
|
|
44
|
+
*
|
|
46
45
|
* The function checks if an array is empty and returns a boolean value.
|
|
47
46
|
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
48
47
|
*/
|
|
@@ -74,15 +73,33 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
|
|
|
74
73
|
*/
|
|
75
74
|
pop(): E | undefined;
|
|
76
75
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
76
|
+
* Time Complexity: O(k)
|
|
77
|
+
* Space Complexity: O(1)
|
|
78
|
+
*
|
|
79
|
+
* The function `pushMany` iterates over elements and pushes them into an array after applying a
|
|
80
|
+
* transformation function if provided.
|
|
81
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
82
|
+
* is an iterable containing elements of type `E` or `R`. The function iterates over each element in
|
|
83
|
+
* the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
|
|
84
|
+
* provided, it is used to
|
|
85
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
86
|
+
* element was successfully pushed into the data structure.
|
|
87
|
+
*/
|
|
88
|
+
pushMany(elements: Iterable<E> | Iterable<R>): boolean[];
|
|
89
|
+
/**
|
|
90
|
+
* Time Complexity: O(n)
|
|
91
|
+
* Space Complexity: O(1)
|
|
92
|
+
*
|
|
93
|
+
* The toArray function returns a copy of the elements in an array.
|
|
94
|
+
* @returns An array of type E.
|
|
80
95
|
*/
|
|
81
96
|
delete(element: E): boolean;
|
|
82
97
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
98
|
+
* Time Complexity: O(n)
|
|
99
|
+
* Space Complexity: O(1)
|
|
100
|
+
*
|
|
101
|
+
* The toArray function returns a copy of the elements in an array.
|
|
102
|
+
* @returns An array of type E.
|
|
86
103
|
*/
|
|
87
104
|
deleteAt(index: number): boolean;
|
|
88
105
|
/**
|
|
@@ -14,16 +14,7 @@ class Stack extends base_1.IterableElementBase {
|
|
|
14
14
|
constructor(elements = [], options) {
|
|
15
15
|
super(options);
|
|
16
16
|
this._elements = [];
|
|
17
|
-
|
|
18
|
-
for (const el of elements) {
|
|
19
|
-
if (this.toElementFn) {
|
|
20
|
-
this.push(this.toElementFn(el));
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
this.push(el);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
17
|
+
this.pushMany(elements);
|
|
27
18
|
}
|
|
28
19
|
/**
|
|
29
20
|
* The elements function returns the elements of this set.
|
|
@@ -39,10 +30,6 @@ class Stack extends base_1.IterableElementBase {
|
|
|
39
30
|
get size() {
|
|
40
31
|
return this.elements.length;
|
|
41
32
|
}
|
|
42
|
-
/**
|
|
43
|
-
* Time Complexity: O(n)
|
|
44
|
-
* Space Complexity: O(n)
|
|
45
|
-
*/
|
|
46
33
|
/**
|
|
47
34
|
* Time Complexity: O(n)
|
|
48
35
|
* Space Complexity: O(n)
|
|
@@ -56,6 +43,9 @@ class Stack extends base_1.IterableElementBase {
|
|
|
56
43
|
return new Stack(elements);
|
|
57
44
|
}
|
|
58
45
|
/**
|
|
46
|
+
* Time Complexity: O(1)
|
|
47
|
+
* Space Complexity: O(1)
|
|
48
|
+
*
|
|
59
49
|
* The function checks if an array is empty and returns a boolean value.
|
|
60
50
|
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
61
51
|
*/
|
|
@@ -100,18 +90,47 @@ class Stack extends base_1.IterableElementBase {
|
|
|
100
90
|
return this.elements.pop();
|
|
101
91
|
}
|
|
102
92
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
93
|
+
* Time Complexity: O(k)
|
|
94
|
+
* Space Complexity: O(1)
|
|
95
|
+
*
|
|
96
|
+
* The function `pushMany` iterates over elements and pushes them into an array after applying a
|
|
97
|
+
* transformation function if provided.
|
|
98
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
99
|
+
* is an iterable containing elements of type `E` or `R`. The function iterates over each element in
|
|
100
|
+
* the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
|
|
101
|
+
* provided, it is used to
|
|
102
|
+
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
103
|
+
* element was successfully pushed into the data structure.
|
|
104
|
+
*/
|
|
105
|
+
pushMany(elements) {
|
|
106
|
+
const ans = [];
|
|
107
|
+
for (const el of elements) {
|
|
108
|
+
if (this.toElementFn) {
|
|
109
|
+
ans.push(this.push(this.toElementFn(el)));
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
ans.push(this.push(el));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return ans;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Time Complexity: O(n)
|
|
119
|
+
* Space Complexity: O(1)
|
|
120
|
+
*
|
|
121
|
+
* The toArray function returns a copy of the elements in an array.
|
|
122
|
+
* @returns An array of type E.
|
|
106
123
|
*/
|
|
107
124
|
delete(element) {
|
|
108
125
|
const index = this.elements.indexOf(element);
|
|
109
126
|
return this.deleteAt(index);
|
|
110
127
|
}
|
|
111
128
|
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
129
|
+
* Time Complexity: O(n)
|
|
130
|
+
* Space Complexity: O(1)
|
|
131
|
+
*
|
|
132
|
+
* The toArray function returns a copy of the elements in an array.
|
|
133
|
+
* @returns An array of type E.
|
|
115
134
|
*/
|
|
116
135
|
deleteAt(index) {
|
|
117
136
|
const spliced = this.elements.splice(index, 1);
|
|
@@ -200,7 +200,7 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
|
|
|
200
200
|
* @returns The `addMany` method returns an array of boolean values indicating whether each word in
|
|
201
201
|
* the input iterable was successfully added to the data structure.
|
|
202
202
|
*/
|
|
203
|
-
addMany(words
|
|
203
|
+
addMany(words: Iterable<string> | Iterable<R>): boolean[];
|
|
204
204
|
/**
|
|
205
205
|
* Time Complexity: O(l), where l is the length of the input word.
|
|
206
206
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -235,9 +235,14 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
|
|
|
235
235
|
*/
|
|
236
236
|
delete(word: string): boolean;
|
|
237
237
|
/**
|
|
238
|
-
* Time Complexity: O(n)
|
|
239
|
-
* Space Complexity: O(1)
|
|
238
|
+
* Time Complexity: O(n)
|
|
239
|
+
* Space Complexity: O(1)
|
|
240
240
|
*
|
|
241
|
+
* The function `getHeight` calculates the height of a trie data structure starting from the root
|
|
242
|
+
* node.
|
|
243
|
+
* @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
|
|
244
|
+
* the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
|
|
245
|
+
* tree and returns the maximum depth found.
|
|
241
246
|
*/
|
|
242
247
|
getHeight(): number;
|
|
243
248
|
/**
|
|
@@ -243,7 +243,7 @@ class Trie extends base_1.IterableElementBase {
|
|
|
243
243
|
* @returns The `addMany` method returns an array of boolean values indicating whether each word in
|
|
244
244
|
* the input iterable was successfully added to the data structure.
|
|
245
245
|
*/
|
|
246
|
-
addMany(words
|
|
246
|
+
addMany(words) {
|
|
247
247
|
const ans = [];
|
|
248
248
|
for (const word of words) {
|
|
249
249
|
if (this.toElementFn) {
|
|
@@ -338,9 +338,14 @@ class Trie extends base_1.IterableElementBase {
|
|
|
338
338
|
return isDeleted;
|
|
339
339
|
}
|
|
340
340
|
/**
|
|
341
|
-
* Time Complexity: O(n)
|
|
342
|
-
* Space Complexity: O(1)
|
|
341
|
+
* Time Complexity: O(n)
|
|
342
|
+
* Space Complexity: O(1)
|
|
343
343
|
*
|
|
344
|
+
* The function `getHeight` calculates the height of a trie data structure starting from the root
|
|
345
|
+
* node.
|
|
346
|
+
* @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
|
|
347
|
+
* the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
|
|
348
|
+
* tree and returns the maximum depth found.
|
|
344
349
|
*/
|
|
345
350
|
getHeight() {
|
|
346
351
|
const startNode = this.root;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "avl-tree-typed",
|
|
3
|
-
"version": "1.53.
|
|
3
|
+
"version": "1.53.8",
|
|
4
4
|
"description": "AVLTree(Adelson-Velsky and Landis Tree). Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -163,6 +163,6 @@
|
|
|
163
163
|
"typescript": "^4.9.5"
|
|
164
164
|
},
|
|
165
165
|
"dependencies": {
|
|
166
|
-
"data-structure-typed": "^1.53.
|
|
166
|
+
"data-structure-typed": "^1.53.8"
|
|
167
167
|
}
|
|
168
168
|
}
|
package/src/common/index.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
|
+
import { isComparable } from '../utils';
|
|
2
|
+
|
|
1
3
|
export enum DFSOperation {
|
|
2
4
|
VISIT = 0,
|
|
3
5
|
PROCESS = 1
|
|
4
6
|
}
|
|
7
|
+
|
|
5
8
|
export class Range<K> {
|
|
6
9
|
constructor(
|
|
7
10
|
public low: K,
|
|
8
11
|
public high: K,
|
|
9
12
|
public includeLow: boolean = true,
|
|
10
13
|
public includeHigh: boolean = true
|
|
11
|
-
) {
|
|
14
|
+
) {
|
|
15
|
+
if (!(isComparable(low) && isComparable(high))) throw new RangeError('low or high is not comparable');
|
|
16
|
+
if (low > high) throw new RangeError('low must be less than or equal to high');
|
|
17
|
+
}
|
|
12
18
|
|
|
13
19
|
// Determine whether a key is within the range
|
|
14
20
|
isInRange(key: K, comparator: (a: K, b: K) => number): boolean {
|
|
@@ -173,7 +173,7 @@ export class AVLTreeMultiMap<
|
|
|
173
173
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
174
174
|
* @returns either a NODE object or undefined.
|
|
175
175
|
*/
|
|
176
|
-
override
|
|
176
|
+
protected override _keyValueNodeEntryRawToNodeAndValue(
|
|
177
177
|
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
178
178
|
value?: V,
|
|
179
179
|
count = 1
|
|
@@ -217,7 +217,7 @@ export class AVLTreeMultiMap<
|
|
|
217
217
|
* @returns a boolean value.
|
|
218
218
|
*/
|
|
219
219
|
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean {
|
|
220
|
-
const [newNode, newValue] = this.
|
|
220
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
221
221
|
if (newNode === undefined) return false;
|
|
222
222
|
|
|
223
223
|
const orgNodeCount = newNode?.count || 0;
|
|
@@ -216,7 +216,7 @@ export class BinaryTree<
|
|
|
216
216
|
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
217
217
|
* value.
|
|
218
218
|
*/
|
|
219
|
-
|
|
219
|
+
protected _keyValueNodeEntryRawToNodeAndValue(
|
|
220
220
|
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
221
221
|
value?: V
|
|
222
222
|
): [OptNodeOrNull<NODE>, V | undefined] {
|
|
@@ -420,7 +420,7 @@ export class BinaryTree<
|
|
|
420
420
|
* key was found and the node was replaced instead of inserted.
|
|
421
421
|
*/
|
|
422
422
|
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean {
|
|
423
|
-
const [newNode, newValue] = this.
|
|
423
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
424
424
|
if (newNode === undefined) return false;
|
|
425
425
|
|
|
426
426
|
// If the tree is empty, directly set the new node as the root node
|