heap-typed 1.48.1 → 1.48.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/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/avl-tree.d.ts +16 -16
- package/dist/data-structures/binary-tree/avl-tree.js +7 -7
- package/dist/data-structures/binary-tree/binary-tree.d.ts +121 -152
- package/dist/data-structures/binary-tree/binary-tree.js +140 -182
- package/dist/data-structures/binary-tree/bst.d.ts +28 -47
- package/dist/data-structures/binary-tree/bst.js +54 -57
- package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
- package/dist/data-structures/binary-tree/rb-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
- package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
- 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/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +11 -8
- package/dist/types/common.js +6 -1
- 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/binary-tree/avl-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- 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/avl-tree.ts +20 -21
- package/src/data-structures/binary-tree/binary-tree.ts +222 -267
- package/src/data-structures/binary-tree/bst.ts +86 -82
- package/src/data-structures/binary-tree/rb-tree.ts +25 -26
- package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
- 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/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- 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/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
- package/src/types/data-structures/index.ts +1 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TreeMultimap, TreeMultimapNode } from '../../../data-structures';
|
|
2
2
|
import { AVLTreeOptions } from './avl-tree';
|
|
3
|
-
export type TreeMultimapNodeNested<
|
|
4
|
-
export type TreeMultimapNested<
|
|
5
|
-
export type TreeMultimapOptions = Omit<AVLTreeOptions
|
|
3
|
+
export type TreeMultimapNodeNested<K, V> = TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
|
+
export type TreeMultimapNested<K, V, N extends TreeMultimapNode<K, V, N>> = TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
|
+
export type TreeMultimapOptions<K> = Omit<AVLTreeOptions<K>, 'isMergeDuplicatedNodeByKey'> & {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "heap-typed",
|
|
3
|
-
"version": "1.48.
|
|
3
|
+
"version": "1.48.3",
|
|
4
4
|
"description": "Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -131,6 +131,6 @@
|
|
|
131
131
|
"typescript": "^4.9.5"
|
|
132
132
|
},
|
|
133
133
|
"dependencies": {
|
|
134
|
-
"data-structure-typed": "^1.48.
|
|
134
|
+
"data-structure-typed": "^1.48.3"
|
|
135
135
|
}
|
|
136
136
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './iterable-base';
|
|
@@ -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
|
+
}
|
|
@@ -12,16 +12,15 @@ import type {
|
|
|
12
12
|
AVLTreeOptions,
|
|
13
13
|
BiTreeDeleteResult,
|
|
14
14
|
BSTNodeKeyOrNode,
|
|
15
|
-
BTNKey,
|
|
16
15
|
BTNodeExemplar
|
|
17
16
|
} from '../../types';
|
|
18
17
|
import { BTNCallback } from '../../types';
|
|
19
18
|
import { IBinaryTree } from '../../interfaces';
|
|
20
19
|
|
|
21
|
-
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
20
|
+
export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
22
21
|
height: number;
|
|
23
22
|
|
|
24
|
-
constructor(key:
|
|
23
|
+
constructor(key: K, value?: V) {
|
|
25
24
|
super(key, value);
|
|
26
25
|
this.height = 0;
|
|
27
26
|
}
|
|
@@ -37,35 +36,35 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|
|
37
36
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
38
37
|
* 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
|
|
39
38
|
*/
|
|
40
|
-
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>, TREE extends AVLTree<V, N, TREE> = AVLTree<V, N, AVLTreeNested<V, N>>>
|
|
41
|
-
extends BST<V, N, TREE>
|
|
42
|
-
implements IBinaryTree<V, N, TREE> {
|
|
39
|
+
export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>>
|
|
40
|
+
extends BST<K, V, N, TREE>
|
|
41
|
+
implements IBinaryTree<K, V, N, TREE> {
|
|
43
42
|
|
|
44
43
|
/**
|
|
45
44
|
* The constructor function initializes an AVLTree object with optional elements and options.
|
|
46
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
45
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
|
|
47
46
|
* objects. It represents a collection of elements that will be added to the AVL tree during
|
|
48
47
|
* initialization.
|
|
49
48
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
50
49
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
51
50
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
52
51
|
*/
|
|
53
|
-
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions
|
|
52
|
+
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) {
|
|
54
53
|
super([], options);
|
|
55
54
|
if (elements) super.addMany(elements);
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
/**
|
|
59
58
|
* The function creates a new AVL tree node with the specified key and value.
|
|
60
|
-
* @param {
|
|
59
|
+
* @param {K} key - The key parameter is the key value that will be associated with
|
|
61
60
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
62
61
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
63
62
|
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
64
63
|
* node type `N`.
|
|
65
64
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
66
65
|
*/
|
|
67
|
-
override createNode(key:
|
|
68
|
-
return new AVLTreeNode<V, N>(key, value) as N;
|
|
66
|
+
override createNode(key: K, value?: V): N {
|
|
67
|
+
return new AVLTreeNode<K, V, N>(key, value) as N;
|
|
69
68
|
}
|
|
70
69
|
|
|
71
70
|
/**
|
|
@@ -75,19 +74,19 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
75
74
|
* being created.
|
|
76
75
|
* @returns a new AVLTree object.
|
|
77
76
|
*/
|
|
78
|
-
override createTree(options?: AVLTreeOptions): TREE {
|
|
79
|
-
return new AVLTree<V, N, TREE>([], {
|
|
77
|
+
override createTree(options?: AVLTreeOptions<K>): TREE {
|
|
78
|
+
return new AVLTree<K, V, N, TREE>([], {
|
|
80
79
|
iterationType: this.iterationType,
|
|
81
|
-
|
|
80
|
+
variant: this.variant, ...options
|
|
82
81
|
}) as TREE;
|
|
83
82
|
}
|
|
84
83
|
|
|
85
84
|
/**
|
|
86
85
|
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
87
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
86
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
88
87
|
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
89
88
|
*/
|
|
90
|
-
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N {
|
|
89
|
+
override isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N {
|
|
91
90
|
return exemplar instanceof AVLTreeNode;
|
|
92
91
|
}
|
|
93
92
|
|
|
@@ -106,7 +105,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
106
105
|
* entry.
|
|
107
106
|
* @returns The method is returning either the inserted node or `undefined`.
|
|
108
107
|
*/
|
|
109
|
-
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
|
|
108
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined {
|
|
110
109
|
if (keyOrNodeOrEntry === null) return undefined;
|
|
111
110
|
const inserted = super.add(keyOrNodeOrEntry);
|
|
112
111
|
if (inserted) this._balancePath(inserted);
|
|
@@ -151,14 +150,14 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
151
150
|
/**
|
|
152
151
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
153
152
|
* tree.
|
|
154
|
-
* @param {
|
|
155
|
-
* needs to be swapped with the destination node. It can be of type `
|
|
156
|
-
* @param {
|
|
153
|
+
* @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
154
|
+
* needs to be swapped with the destination node. It can be of type `K`, `N`, or `undefined`.
|
|
155
|
+
* @param {K | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
157
156
|
* node where the values from the source node will be swapped to.
|
|
158
157
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
159
158
|
* if either `srcNode` or `destNode` is undefined.
|
|
160
159
|
*/
|
|
161
|
-
protected override _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined {
|
|
160
|
+
protected override _swapProperties(srcNode: BSTNodeKeyOrNode<K, N>, destNode: BSTNodeKeyOrNode<K, N>): N | undefined {
|
|
162
161
|
srcNode = this.ensureNode(srcNode);
|
|
163
162
|
destNode = this.ensureNode(destNode);
|
|
164
163
|
|