avl-tree-typed 2.0.5 → 2.1.0
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 +1 -1
- package/dist/constants/index.js +1 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/data-structures/base/iterable-element-base.js +149 -107
- package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/data-structures/base/linear-base.d.ts +250 -192
- package/dist/data-structures/base/linear-base.js +137 -274
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/data-structures/binary-tree/binary-tree.js +598 -869
- package/dist/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/data-structures/binary-tree/bst.js +505 -481
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/data-structures/graph/abstract-graph.js +267 -237
- package/dist/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/data-structures/graph/directed-graph.js +146 -233
- package/dist/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/data-structures/graph/map-graph.js +56 -59
- package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/data-structures/graph/undirected-graph.js +129 -149
- package/dist/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/data-structures/hash/hash-map.js +270 -457
- package/dist/data-structures/heap/heap.d.ts +214 -289
- package/dist/data-structures/heap/heap.js +340 -349
- package/dist/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/data-structures/heap/max-heap.js +11 -66
- package/dist/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/data-structures/heap/min-heap.js +11 -66
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/data-structures/queue/deque.d.ts +227 -254
- package/dist/data-structures/queue/deque.js +309 -348
- package/dist/data-structures/queue/queue.d.ts +180 -201
- package/dist/data-structures/queue/queue.js +265 -248
- package/dist/data-structures/stack/stack.d.ts +124 -102
- package/dist/data-structures/stack/stack.js +181 -125
- package/dist/data-structures/trie/trie.d.ts +164 -165
- package/dist/data-structures/trie/trie.js +189 -172
- package/dist/interfaces/binary-tree.d.ts +56 -6
- package/dist/interfaces/graph.d.ts +16 -0
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/types/utils/utils.d.ts +1 -0
- package/dist/utils/number.js +1 -2
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.js +9 -8
- package/package.json +15 -15
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +664 -893
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +2 -0
- package/src/utils/utils.ts +9 -14
package/dist/common/index.js
CHANGED
|
@@ -6,7 +6,7 @@ var DFSOperation;
|
|
|
6
6
|
(function (DFSOperation) {
|
|
7
7
|
DFSOperation[DFSOperation["VISIT"] = 0] = "VISIT";
|
|
8
8
|
DFSOperation[DFSOperation["PROCESS"] = 1] = "PROCESS";
|
|
9
|
-
})(DFSOperation
|
|
9
|
+
})(DFSOperation || (exports.DFSOperation = DFSOperation = {}));
|
|
10
10
|
class Range {
|
|
11
11
|
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
12
12
|
this.low = low;
|
package/dist/constants/index.js
CHANGED
|
@@ -5,4 +5,4 @@ var DFSOperation;
|
|
|
5
5
|
(function (DFSOperation) {
|
|
6
6
|
DFSOperation[DFSOperation["VISIT"] = 0] = "VISIT";
|
|
7
7
|
DFSOperation[DFSOperation["PROCESS"] = 1] = "PROCESS";
|
|
8
|
-
})(DFSOperation
|
|
8
|
+
})(DFSOperation || (exports.DFSOperation = DFSOperation = {}));
|
|
@@ -1,116 +1,219 @@
|
|
|
1
|
-
import { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
|
|
2
|
-
|
|
1
|
+
import type { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Base class that makes a data structure iterable and provides common
|
|
4
|
+
* element-wise utilities (e.g., map/filter/reduce/find).
|
|
5
|
+
*
|
|
6
|
+
* @template E The public element type yielded by the structure.
|
|
7
|
+
* @template R The underlying "raw" element type used internally or by converters.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This class implements the JavaScript iteration protocol (via `Symbol.iterator`)
|
|
11
|
+
* and offers array-like helpers with predictable time/space complexity.
|
|
12
|
+
*/
|
|
13
|
+
export declare abstract class IterableElementBase<E, R> implements Iterable<E> {
|
|
3
14
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @param
|
|
15
|
+
* Create a new iterable base.
|
|
16
|
+
*
|
|
17
|
+
* @param options Optional behavior overrides. When provided, a `toElementFn`
|
|
18
|
+
* is used to convert a raw element (`R`) into a public element (`E`).
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* Time O(1), Space O(1).
|
|
7
22
|
*/
|
|
8
23
|
protected constructor(options?: IterableElementBaseOptions<E, R>);
|
|
24
|
+
/**
|
|
25
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* Time O(1), Space O(1).
|
|
29
|
+
*/
|
|
9
30
|
protected _toElementFn?: (rawElement: R) => E;
|
|
31
|
+
/**
|
|
32
|
+
* Exposes the current `toElementFn`, if configured.
|
|
33
|
+
*
|
|
34
|
+
* @returns The converter function or `undefined` when not set.
|
|
35
|
+
* @remarks
|
|
36
|
+
* Time O(1), Space O(1).
|
|
37
|
+
*/
|
|
10
38
|
get toElementFn(): ((rawElement: R) => E) | undefined;
|
|
11
39
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
40
|
+
* Returns an iterator over the structure's elements.
|
|
41
|
+
*
|
|
42
|
+
* @param args Optional iterator arguments forwarded to the internal iterator.
|
|
43
|
+
* @returns An `IterableIterator<E>` that yields the elements in traversal order.
|
|
14
44
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
18
|
-
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
45
|
+
* @remarks
|
|
46
|
+
* Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
|
|
19
47
|
*/
|
|
20
|
-
[Symbol.iterator](...args:
|
|
48
|
+
[Symbol.iterator](...args: unknown[]): IterableIterator<E>;
|
|
21
49
|
/**
|
|
22
|
-
*
|
|
23
|
-
* Space Complexity: O(n)
|
|
50
|
+
* Returns an iterator over the values (alias of the default iterator).
|
|
24
51
|
*
|
|
25
|
-
*
|
|
52
|
+
* @returns An `IterableIterator<E>` over all elements.
|
|
53
|
+
* @remarks
|
|
54
|
+
* Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
|
|
26
55
|
*/
|
|
27
56
|
values(): IterableIterator<E>;
|
|
28
57
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* @param
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* @
|
|
37
|
-
*
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* @
|
|
55
|
-
*
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
* Time
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* element
|
|
67
|
-
* @
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
58
|
+
* Tests whether all elements satisfy the predicate.
|
|
59
|
+
*
|
|
60
|
+
* @template TReturn
|
|
61
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
62
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
63
|
+
* @returns `true` if every element passes; otherwise `false`.
|
|
64
|
+
*
|
|
65
|
+
* @remarks
|
|
66
|
+
* Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
|
|
67
|
+
*/
|
|
68
|
+
every(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Tests whether at least one element satisfies the predicate.
|
|
71
|
+
*
|
|
72
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
73
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
74
|
+
* @returns `true` if any element passes; otherwise `false`.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
77
|
+
* Time O(n) in the worst case; may exit early on first success. Space O(1).
|
|
78
|
+
*/
|
|
79
|
+
some(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Invokes a callback for each element in iteration order.
|
|
82
|
+
*
|
|
83
|
+
* @param callbackfn Function invoked per element with signature `(value, index, self)`.
|
|
84
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
85
|
+
* @returns `void`.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* Time O(n), Space O(1).
|
|
89
|
+
*/
|
|
90
|
+
forEach(callbackfn: ElementCallback<E, R, void>, thisArg?: unknown): void;
|
|
91
|
+
/**
|
|
92
|
+
* Finds the first element that satisfies the predicate and returns it.
|
|
93
|
+
*
|
|
94
|
+
* @overload
|
|
95
|
+
* Finds the first element of type `S` (a subtype of `E`) that satisfies the predicate and returns it.
|
|
96
|
+
* @template S
|
|
97
|
+
* @param predicate Type-guard predicate: `(value, index, self) => value is S`.
|
|
98
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
99
|
+
* @returns The matched element typed as `S`, or `undefined` if not found.
|
|
100
|
+
*
|
|
101
|
+
* @overload
|
|
102
|
+
* @param predicate Boolean predicate: `(value, index, self) => boolean`.
|
|
103
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
104
|
+
* @returns The first matching element as `E`, or `undefined` if not found.
|
|
105
|
+
*
|
|
106
|
+
* @remarks
|
|
107
|
+
* Time O(n) in the worst case; may exit early on the first match. Space O(1).
|
|
108
|
+
*/
|
|
109
|
+
find<S extends E>(predicate: ElementCallback<E, R, S>, thisArg?: unknown): S | undefined;
|
|
110
|
+
find(predicate: ElementCallback<E, R, unknown>, thisArg?: unknown): E | undefined;
|
|
111
|
+
/**
|
|
112
|
+
* Checks whether a strictly-equal element exists in the structure.
|
|
113
|
+
*
|
|
114
|
+
* @param element The element to test with `===` equality.
|
|
115
|
+
* @returns `true` if an equal element is found; otherwise `false`.
|
|
116
|
+
*
|
|
117
|
+
* @remarks
|
|
118
|
+
* Time O(n) in the worst case. Space O(1).
|
|
83
119
|
*/
|
|
84
120
|
has(element: E): boolean;
|
|
85
121
|
reduce(callbackfn: ReduceElementCallback<E, R>): E;
|
|
86
122
|
reduce(callbackfn: ReduceElementCallback<E, R>, initialValue: E): E;
|
|
87
123
|
reduce<U>(callbackfn: ReduceElementCallback<E, R, U>, initialValue: U): U;
|
|
88
124
|
/**
|
|
89
|
-
*
|
|
90
|
-
* Space Complexity: O(n)
|
|
125
|
+
* Materializes the elements into a new array.
|
|
91
126
|
*
|
|
92
|
-
*
|
|
93
|
-
* @
|
|
127
|
+
* @returns A shallow array copy of the iteration order.
|
|
128
|
+
* @remarks
|
|
129
|
+
* Time O(n), Space O(n).
|
|
94
130
|
*/
|
|
95
131
|
toArray(): E[];
|
|
96
132
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
133
|
+
* Returns a representation of the structure suitable for quick visualization.
|
|
134
|
+
* Defaults to an array of elements; subclasses may override to provide richer visuals.
|
|
99
135
|
*
|
|
100
|
-
*
|
|
136
|
+
* @returns A visual representation (array by default).
|
|
137
|
+
* @remarks
|
|
138
|
+
* Time O(n), Space O(n).
|
|
101
139
|
*/
|
|
102
140
|
toVisual(): E[];
|
|
103
141
|
/**
|
|
104
|
-
*
|
|
105
|
-
* Space Complexity: O(n)
|
|
142
|
+
* Prints `toVisual()` to the console. Intended for quick debugging.
|
|
106
143
|
*
|
|
107
|
-
*
|
|
144
|
+
* @returns `void`.
|
|
145
|
+
* @remarks
|
|
146
|
+
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
108
147
|
*/
|
|
109
148
|
print(): void;
|
|
149
|
+
/**
|
|
150
|
+
* Indicates whether the structure currently contains no elements.
|
|
151
|
+
*
|
|
152
|
+
* @returns `true` if empty; otherwise `false`.
|
|
153
|
+
* @remarks
|
|
154
|
+
* Expected Time O(1), Space O(1) for most implementations.
|
|
155
|
+
*/
|
|
110
156
|
abstract isEmpty(): boolean;
|
|
157
|
+
/**
|
|
158
|
+
* Removes all elements from the structure.
|
|
159
|
+
*
|
|
160
|
+
* @returns `void`.
|
|
161
|
+
* @remarks
|
|
162
|
+
* Expected Time O(1) or O(n) depending on the implementation; Space O(1).
|
|
163
|
+
*/
|
|
111
164
|
abstract clear(): void;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Creates a structural copy with the same element values and configuration.
|
|
167
|
+
*
|
|
168
|
+
* @returns A clone of the current instance (same concrete type).
|
|
169
|
+
* @remarks
|
|
170
|
+
* Expected Time O(n) to copy elements; Space O(n).
|
|
171
|
+
*/
|
|
172
|
+
abstract clone(): this;
|
|
173
|
+
/**
|
|
174
|
+
* Maps each element to a new element and returns a new iterable structure.
|
|
175
|
+
*
|
|
176
|
+
* @template EM The mapped element type.
|
|
177
|
+
* @template RM The mapped raw element type used internally by the target structure.
|
|
178
|
+
* @param callback Function with signature `(value, index, self) => mapped`.
|
|
179
|
+
* @param options Optional options for the returned structure, including its `toElementFn`.
|
|
180
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
181
|
+
* @returns A new `IterableElementBase<EM, RM>` containing mapped elements.
|
|
182
|
+
*
|
|
183
|
+
* @remarks
|
|
184
|
+
* Time O(n), Space O(n).
|
|
185
|
+
*/
|
|
186
|
+
abstract map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: IterableElementBaseOptions<EM, RM>, thisArg?: unknown): IterableElementBase<EM, RM>;
|
|
187
|
+
/**
|
|
188
|
+
* Maps each element to the same element type and returns the same concrete structure type.
|
|
189
|
+
*
|
|
190
|
+
* @param callback Function with signature `(value, index, self) => mappedValue`.
|
|
191
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
192
|
+
* @returns A new instance of the same concrete type with mapped elements.
|
|
193
|
+
*
|
|
194
|
+
* @remarks
|
|
195
|
+
* Time O(n), Space O(n).
|
|
196
|
+
*/
|
|
197
|
+
abstract mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this;
|
|
198
|
+
/**
|
|
199
|
+
* Filters elements using the provided predicate and returns the same concrete structure type.
|
|
200
|
+
*
|
|
201
|
+
* @param predicate Function with signature `(value, index, self) => boolean`.
|
|
202
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
203
|
+
* @returns A new instance of the same concrete type containing only elements that pass the predicate.
|
|
204
|
+
*
|
|
205
|
+
* @remarks
|
|
206
|
+
* Time O(n), Space O(k) where `k` is the number of kept elements.
|
|
207
|
+
*/
|
|
208
|
+
abstract filter(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): this;
|
|
209
|
+
/**
|
|
210
|
+
* Internal iterator factory used by the default iterator.
|
|
211
|
+
*
|
|
212
|
+
* @param args Optional iterator arguments.
|
|
213
|
+
* @returns An iterator over elements.
|
|
214
|
+
*
|
|
215
|
+
* @remarks
|
|
216
|
+
* Implementations should yield in O(1) per element with O(1) extra space when possible.
|
|
217
|
+
*/
|
|
218
|
+
protected abstract _getIterator(...args: unknown[]): IterableIterator<E>;
|
|
116
219
|
}
|