data-structure-typed 1.49.5 → 1.49.7
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 +17 -23
- package/README_zh-CN.md +2 -0
- package/benchmark/report.html +14 -23
- package/benchmark/report.json +158 -251
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +194 -153
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/cjs/data-structures/binary-tree/bst.js +114 -91
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/cjs/data-structures/hash/hash-map.js +35 -35
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/index.d.ts +0 -1
- package/dist/cjs/data-structures/hash/index.js +0 -1
- package/dist/cjs/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +2 -1
- package/dist/cjs/data-structures/heap/heap.js +13 -13
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +1 -3
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/cjs/data-structures/matrix/matrix.js +0 -7
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -11
- package/dist/cjs/data-structures/queue/deque.js +9 -13
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/cjs/data-structures/queue/queue.js +29 -25
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.js +2 -3
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/cjs/data-structures/trie/trie.js +9 -5
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +3 -3
- package/dist/cjs/types/common.js +2 -2
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/cjs/types/data-structures/hash/index.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/index.js +0 -1
- package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/linked-list/index.js +1 -0
- package/dist/cjs/types/data-structures/linked-list/index.js.map +1 -1
- package/dist/cjs/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/cjs/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/matrix/index.js +1 -0
- package/dist/cjs/types/data-structures/matrix/index.js.map +1 -1
- package/dist/cjs/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/cjs/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +194 -153
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/mjs/data-structures/binary-tree/bst.js +114 -91
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/mjs/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/mjs/data-structures/hash/hash-map.js +35 -37
- package/dist/mjs/data-structures/hash/index.d.ts +0 -1
- package/dist/mjs/data-structures/hash/index.js +0 -1
- package/dist/mjs/data-structures/heap/heap.d.ts +2 -1
- package/dist/mjs/data-structures/heap/heap.js +19 -20
- package/dist/mjs/data-structures/heap/max-heap.js +1 -1
- package/dist/mjs/data-structures/heap/min-heap.js +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +2 -5
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +16 -23
- package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/mjs/data-structures/matrix/matrix.js +0 -7
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -11
- package/dist/mjs/data-structures/queue/deque.js +9 -14
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -13
- package/dist/mjs/data-structures/queue/queue.js +30 -28
- package/dist/mjs/data-structures/stack/stack.js +3 -5
- package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/mjs/data-structures/trie/trie.js +10 -9
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +3 -3
- package/dist/mjs/types/common.js +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/mjs/types/data-structures/hash/index.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/index.js +0 -1
- package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/linked-list/index.js +1 -0
- package/dist/mjs/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/mjs/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/matrix/index.js +1 -0
- package/dist/mjs/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/mjs/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -1
- package/dist/umd/data-structure-typed.js +620 -823
- 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/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +255 -211
- package/src/data-structures/binary-tree/bst.ts +126 -107
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -15
- package/test/performance/data-structures/comparison/comparison.test.ts +13 -36
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
- package/test/performance/data-structures/hash/hash-map.test.ts +11 -34
- package/test/performance/data-structures/heap/heap.test.ts +5 -18
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
- package/test/performance/data-structures/queue/queue.test.ts +8 -25
- package/test/performance/data-structures/stack/stack.test.ts +6 -18
- package/test/performance/data-structures/trie/trie.test.ts +2 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -5
- package/test/unit/data-structures/binary-tree/bst.test.ts +17 -1
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +5 -5
- package/test/unit/data-structures/trie/trie.test.ts +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/cjs/data-structures/hash/hash-table.js +0 -282
- package/dist/cjs/data-structures/hash/hash-table.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/hash-table.js +0 -3
- package/dist/cjs/types/data-structures/hash/hash-table.js.map +0 -1
- package/dist/cjs/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/cjs/types/data-structures/matrix/matrix2d.js +0 -3
- package/dist/cjs/types/data-structures/matrix/matrix2d.js.map +0 -1
- package/dist/cjs/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/cjs/types/data-structures/matrix/vector2d.js +0 -3
- package/dist/cjs/types/data-structures/matrix/vector2d.js.map +0 -1
- package/dist/mjs/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/mjs/data-structures/hash/hash-table.js +0 -283
- package/dist/mjs/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/hash-table.js +0 -1
- package/dist/mjs/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/mjs/types/data-structures/matrix/matrix2d.js +0 -1
- package/dist/mjs/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/mjs/types/data-structures/matrix/vector2d.js +0 -1
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -238
|
@@ -124,8 +124,6 @@ var dataStructureTyped = (() => {
|
|
|
124
124
|
FibonacciHeap: () => FibonacciHeap,
|
|
125
125
|
FibonacciHeapNode: () => FibonacciHeapNode,
|
|
126
126
|
HashMap: () => HashMap,
|
|
127
|
-
HashTable: () => HashTable,
|
|
128
|
-
HashTableNode: () => HashTableNode,
|
|
129
127
|
Heap: () => Heap,
|
|
130
128
|
IterableElementBase: () => IterableElementBase,
|
|
131
129
|
IterableEntryBase: () => IterableEntryBase,
|
|
@@ -176,273 +174,6 @@ var dataStructureTyped = (() => {
|
|
|
176
174
|
uuidV4: () => uuidV4
|
|
177
175
|
});
|
|
178
176
|
|
|
179
|
-
// src/data-structures/hash/hash-table.ts
|
|
180
|
-
var HashTableNode = class {
|
|
181
|
-
constructor(key, value) {
|
|
182
|
-
__publicField(this, "key");
|
|
183
|
-
__publicField(this, "value");
|
|
184
|
-
__publicField(this, "next");
|
|
185
|
-
this.key = key;
|
|
186
|
-
this.value = value;
|
|
187
|
-
this.next = void 0;
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
var _HashTable = class _HashTable {
|
|
191
|
-
constructor(capacity = _HashTable.DEFAULT_CAPACITY, hashFn) {
|
|
192
|
-
__publicField(this, "_capacity");
|
|
193
|
-
__publicField(this, "_size");
|
|
194
|
-
__publicField(this, "_buckets");
|
|
195
|
-
__publicField(this, "_hashFn");
|
|
196
|
-
this._hashFn = hashFn || this._defaultHashFn;
|
|
197
|
-
this._capacity = Math.max(capacity, _HashTable.DEFAULT_CAPACITY);
|
|
198
|
-
this._size = 0;
|
|
199
|
-
this._buckets = new Array(this._capacity).fill(void 0);
|
|
200
|
-
}
|
|
201
|
-
get capacity() {
|
|
202
|
-
return this._capacity;
|
|
203
|
-
}
|
|
204
|
-
get size() {
|
|
205
|
-
return this._size;
|
|
206
|
-
}
|
|
207
|
-
get buckets() {
|
|
208
|
-
return this._buckets;
|
|
209
|
-
}
|
|
210
|
-
get hashFn() {
|
|
211
|
-
return this._hashFn;
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
215
|
-
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
216
|
-
* table. It is of type K, which is a generic type representing the key's data type.
|
|
217
|
-
* @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
|
|
218
|
-
* table.
|
|
219
|
-
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
220
|
-
* value.
|
|
221
|
-
*/
|
|
222
|
-
set(key, value) {
|
|
223
|
-
const index = this._hash(key);
|
|
224
|
-
const newNode = new HashTableNode(key, value);
|
|
225
|
-
if (!this._buckets[index]) {
|
|
226
|
-
this._buckets[index] = newNode;
|
|
227
|
-
} else {
|
|
228
|
-
let currentNode = this._buckets[index];
|
|
229
|
-
while (currentNode) {
|
|
230
|
-
if (currentNode.key === key) {
|
|
231
|
-
currentNode.value = value;
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
if (!currentNode.next) {
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
|
-
currentNode = currentNode.next;
|
|
238
|
-
}
|
|
239
|
-
currentNode.next = newNode;
|
|
240
|
-
}
|
|
241
|
-
this._size++;
|
|
242
|
-
if (this._size / this._capacity >= _HashTable.LOAD_FACTOR) {
|
|
243
|
-
this._expand();
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
248
|
-
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
249
|
-
* structure.
|
|
250
|
-
* @returns The method is returning the value associated with the given key if it exists in the hash table. If the key is
|
|
251
|
-
* not found, it returns `undefined`.
|
|
252
|
-
*/
|
|
253
|
-
get(key) {
|
|
254
|
-
const index = this._hash(key);
|
|
255
|
-
let currentNode = this._buckets[index];
|
|
256
|
-
while (currentNode) {
|
|
257
|
-
if (currentNode.key === key) {
|
|
258
|
-
return currentNode.value;
|
|
259
|
-
}
|
|
260
|
-
currentNode = currentNode.next;
|
|
261
|
-
}
|
|
262
|
-
return void 0;
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* The delete function removes a key-value pair from a hash table.
|
|
266
|
-
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
267
|
-
* table.
|
|
268
|
-
* @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
|
|
269
|
-
* any value.
|
|
270
|
-
*/
|
|
271
|
-
delete(key) {
|
|
272
|
-
const index = this._hash(key);
|
|
273
|
-
let currentNode = this._buckets[index];
|
|
274
|
-
let prevNode = void 0;
|
|
275
|
-
while (currentNode) {
|
|
276
|
-
if (currentNode.key === key) {
|
|
277
|
-
if (prevNode) {
|
|
278
|
-
prevNode.next = currentNode.next;
|
|
279
|
-
} else {
|
|
280
|
-
this._buckets[index] = currentNode.next;
|
|
281
|
-
}
|
|
282
|
-
this._size--;
|
|
283
|
-
currentNode.next = void 0;
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
286
|
-
prevNode = currentNode;
|
|
287
|
-
currentNode = currentNode.next;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
*[Symbol.iterator]() {
|
|
291
|
-
for (const bucket of this._buckets) {
|
|
292
|
-
let currentNode = bucket;
|
|
293
|
-
while (currentNode) {
|
|
294
|
-
yield [currentNode.key, currentNode.value];
|
|
295
|
-
currentNode = currentNode.next;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
forEach(callback) {
|
|
300
|
-
let index = 0;
|
|
301
|
-
for (const entry of this) {
|
|
302
|
-
callback(entry, index, this);
|
|
303
|
-
index++;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
filter(predicate) {
|
|
307
|
-
const newTable = new _HashTable();
|
|
308
|
-
let index = 0;
|
|
309
|
-
for (const [key, value] of this) {
|
|
310
|
-
if (predicate([key, value], index, this)) {
|
|
311
|
-
newTable.set(key, value);
|
|
312
|
-
}
|
|
313
|
-
index++;
|
|
314
|
-
}
|
|
315
|
-
return newTable;
|
|
316
|
-
}
|
|
317
|
-
map(callback) {
|
|
318
|
-
const newTable = new _HashTable();
|
|
319
|
-
let index = 0;
|
|
320
|
-
for (const [key, value] of this) {
|
|
321
|
-
newTable.set(key, callback([key, value], index, this));
|
|
322
|
-
index++;
|
|
323
|
-
}
|
|
324
|
-
return newTable;
|
|
325
|
-
}
|
|
326
|
-
reduce(callback, initialValue) {
|
|
327
|
-
let accumulator = initialValue;
|
|
328
|
-
let index = 0;
|
|
329
|
-
for (const entry of this) {
|
|
330
|
-
accumulator = callback(accumulator, entry, index, this);
|
|
331
|
-
index++;
|
|
332
|
-
}
|
|
333
|
-
return accumulator;
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
337
|
-
* capacity of the data structure.
|
|
338
|
-
* @param {K} key - The `key` parameter is the input value that needs to be hashed. It can be of any type, but in this
|
|
339
|
-
* code snippet, it is checked whether the key is a string or an object. If it is a string, the `_murmurStringHashFn`
|
|
340
|
-
* function is used to
|
|
341
|
-
* @returns the hash value of the key modulo the capacity of the data structure.
|
|
342
|
-
*/
|
|
343
|
-
_defaultHashFn(key) {
|
|
344
|
-
const hashValue = typeof key === "string" ? this._murmurStringHashFn(key) : this._objectHash(key);
|
|
345
|
-
return hashValue % this._capacity;
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* The `_multiplicativeStringHashFn` function calculates a hash value for a given string key using the multiplicative
|
|
349
|
-
* string hash function.
|
|
350
|
-
* @param {K} key - The `key` parameter is the input value for which we want to calculate the hash. It can be of any
|
|
351
|
-
* type, as it is generic (`K`). The function converts the `key` to a string using the `String()` function.
|
|
352
|
-
* @returns a number, which is the result of the multiplicative string hash function applied to the input key.
|
|
353
|
-
*/
|
|
354
|
-
_multiplicativeStringHashFn(key) {
|
|
355
|
-
const keyString = String(key);
|
|
356
|
-
let hash = 0;
|
|
357
|
-
for (let i = 0; i < keyString.length; i++) {
|
|
358
|
-
const charCode = keyString.charCodeAt(i);
|
|
359
|
-
const A = 0.618033988749895;
|
|
360
|
-
const M = 1 << 30;
|
|
361
|
-
hash = (hash * A + charCode) % M;
|
|
362
|
-
}
|
|
363
|
-
return Math.abs(hash);
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* The function `_murmurStringHashFn` calculates a hash value for a given string key using the MurmurHash algorithm.
|
|
367
|
-
* @param {K} key - The `key` parameter is the input value for which you want to calculate the hash. It can be of any
|
|
368
|
-
* type, but it will be converted to a string using the `String()` function before calculating the hash.
|
|
369
|
-
* @returns a number, which is the hash value calculated for the given key.
|
|
370
|
-
*/
|
|
371
|
-
_murmurStringHashFn(key) {
|
|
372
|
-
const keyString = String(key);
|
|
373
|
-
const seed = 0;
|
|
374
|
-
let hash = seed;
|
|
375
|
-
for (let i = 0; i < keyString.length; i++) {
|
|
376
|
-
const char = keyString.charCodeAt(i);
|
|
377
|
-
hash = (hash ^ char) * 1540483477;
|
|
378
|
-
hash = (hash ^ hash >>> 15) * 668265261;
|
|
379
|
-
hash = hash ^ hash >>> 15;
|
|
380
|
-
}
|
|
381
|
-
return Math.abs(hash);
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* The _hash function takes a key and returns a number.
|
|
385
|
-
* @param {K} key - The parameter "key" is of type K, which represents the type of the key that will be hashed.
|
|
386
|
-
* @returns The hash function is returning a number.
|
|
387
|
-
*/
|
|
388
|
-
_hash(key) {
|
|
389
|
-
return this.hashFn(key);
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* The function calculates a hash value for a given string using the djb2 algorithm.
|
|
393
|
-
* @param {string} key - The `key` parameter in the `stringHash` function is a string value that represents the input for
|
|
394
|
-
* which we want to calculate the hash value.
|
|
395
|
-
* @returns a number, which is the hash value of the input string.
|
|
396
|
-
*/
|
|
397
|
-
_stringHash(key) {
|
|
398
|
-
let hash = 0;
|
|
399
|
-
for (let i = 0; i < key.length; i++) {
|
|
400
|
-
hash = hash * 31 + key.charCodeAt(i) & 4294967295;
|
|
401
|
-
}
|
|
402
|
-
return hash;
|
|
403
|
-
}
|
|
404
|
-
/**
|
|
405
|
-
* The function `_objectHash` takes a key and returns a hash value, using a custom hash function for objects.
|
|
406
|
-
* @param {K} key - The parameter "key" is of type "K", which means it can be any type. It could be a string, number,
|
|
407
|
-
* boolean, object, or any other type of value. The purpose of the objectHash function is to generate a hash value for
|
|
408
|
-
* the key, which can be used for
|
|
409
|
-
* @returns a number, which is the hash value of the key.
|
|
410
|
-
*/
|
|
411
|
-
_objectHash(key) {
|
|
412
|
-
return this._stringHash(JSON.stringify(key));
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* The `expand` function increases the capacity of a hash table by creating a new array of buckets with double the
|
|
416
|
-
* capacity and rehashing all the existing key-value pairs into the new buckets.
|
|
417
|
-
*/
|
|
418
|
-
_expand() {
|
|
419
|
-
const newCapacity = this._capacity * 2;
|
|
420
|
-
const newBuckets = new Array(newCapacity).fill(void 0);
|
|
421
|
-
for (const bucket of this._buckets) {
|
|
422
|
-
let currentNode = bucket;
|
|
423
|
-
while (currentNode) {
|
|
424
|
-
const newIndex = this._hash(currentNode.key);
|
|
425
|
-
const newNode = new HashTableNode(currentNode.key, currentNode.value);
|
|
426
|
-
if (!newBuckets[newIndex]) {
|
|
427
|
-
newBuckets[newIndex] = newNode;
|
|
428
|
-
} else {
|
|
429
|
-
let currentNewNode = newBuckets[newIndex];
|
|
430
|
-
while (currentNewNode.next) {
|
|
431
|
-
currentNewNode = currentNewNode.next;
|
|
432
|
-
}
|
|
433
|
-
currentNewNode.next = newNode;
|
|
434
|
-
}
|
|
435
|
-
currentNode = currentNode.next;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
this._buckets = newBuckets;
|
|
439
|
-
this._capacity = newCapacity;
|
|
440
|
-
}
|
|
441
|
-
};
|
|
442
|
-
__publicField(_HashTable, "DEFAULT_CAPACITY", 16);
|
|
443
|
-
__publicField(_HashTable, "LOAD_FACTOR", 0.75);
|
|
444
|
-
var HashTable = _HashTable;
|
|
445
|
-
|
|
446
177
|
// src/data-structures/base/iterable-base.ts
|
|
447
178
|
var IterableEntryBase = class {
|
|
448
179
|
/**
|
|
@@ -854,14 +585,14 @@ var dataStructureTyped = (() => {
|
|
|
854
585
|
// src/data-structures/hash/hash-map.ts
|
|
855
586
|
var HashMap = class _HashMap extends IterableEntryBase {
|
|
856
587
|
/**
|
|
857
|
-
* The constructor function initializes a new instance of a class with optional
|
|
858
|
-
* @param
|
|
588
|
+
* The constructor function initializes a new instance of a class with optional entries and options.
|
|
589
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
859
590
|
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
860
591
|
* key-value pairs.
|
|
861
592
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
862
593
|
* configuration options for the constructor. In this case, it has one property:
|
|
863
594
|
*/
|
|
864
|
-
constructor(
|
|
595
|
+
constructor(entries = [], options) {
|
|
865
596
|
super();
|
|
866
597
|
__publicField(this, "_store", {});
|
|
867
598
|
__publicField(this, "_objMap", /* @__PURE__ */ new Map());
|
|
@@ -873,8 +604,8 @@ var dataStructureTyped = (() => {
|
|
|
873
604
|
this._hashFn = hashFn;
|
|
874
605
|
}
|
|
875
606
|
}
|
|
876
|
-
if (
|
|
877
|
-
this.setMany(
|
|
607
|
+
if (entries) {
|
|
608
|
+
this.setMany(entries);
|
|
878
609
|
}
|
|
879
610
|
}
|
|
880
611
|
get size() {
|
|
@@ -914,12 +645,12 @@ var dataStructureTyped = (() => {
|
|
|
914
645
|
}
|
|
915
646
|
/**
|
|
916
647
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
917
|
-
* @param
|
|
918
|
-
* key-value pair is represented as an array with two
|
|
648
|
+
* @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
|
|
649
|
+
* key-value pair is represented as an array with two entries: the key and the value.
|
|
919
650
|
*/
|
|
920
|
-
setMany(
|
|
651
|
+
setMany(entries) {
|
|
921
652
|
const results = [];
|
|
922
|
-
for (const [key, value] of
|
|
653
|
+
for (const [key, value] of entries)
|
|
923
654
|
results.push(this.set(key, value));
|
|
924
655
|
return results;
|
|
925
656
|
}
|
|
@@ -1033,9 +764,6 @@ var dataStructureTyped = (() => {
|
|
|
1033
764
|
}
|
|
1034
765
|
return filteredMap;
|
|
1035
766
|
}
|
|
1036
|
-
print() {
|
|
1037
|
-
console.log([...this.entries()]);
|
|
1038
|
-
}
|
|
1039
767
|
put(key, value) {
|
|
1040
768
|
return this.set(key, value);
|
|
1041
769
|
}
|
|
@@ -1071,26 +799,31 @@ var dataStructureTyped = (() => {
|
|
|
1071
799
|
}
|
|
1072
800
|
};
|
|
1073
801
|
var LinkedHashMap = class _LinkedHashMap extends IterableEntryBase {
|
|
1074
|
-
constructor(
|
|
1075
|
-
hashFn: (key) => String(key),
|
|
1076
|
-
objHashFn: (key) => key
|
|
1077
|
-
}) {
|
|
802
|
+
constructor(entries, options) {
|
|
1078
803
|
super();
|
|
1079
804
|
__publicField(this, "_noObjMap", {});
|
|
1080
805
|
__publicField(this, "_objMap", /* @__PURE__ */ new WeakMap());
|
|
1081
806
|
__publicField(this, "_head");
|
|
1082
807
|
__publicField(this, "_tail");
|
|
1083
808
|
__publicField(this, "_sentinel");
|
|
1084
|
-
__publicField(this, "_hashFn");
|
|
1085
|
-
__publicField(this, "_objHashFn");
|
|
1086
809
|
__publicField(this, "_size", 0);
|
|
810
|
+
/**
|
|
811
|
+
* Time Complexity: O(n)
|
|
812
|
+
* Space Complexity: O(n)
|
|
813
|
+
*/
|
|
814
|
+
__publicField(this, "_hashFn", (key) => String(key));
|
|
815
|
+
__publicField(this, "_objHashFn", (key) => key);
|
|
1087
816
|
this._sentinel = {};
|
|
1088
817
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
818
|
+
if (options) {
|
|
819
|
+
const { hashFn, objHashFn } = options;
|
|
820
|
+
if (hashFn)
|
|
821
|
+
this._hashFn = hashFn;
|
|
822
|
+
if (objHashFn)
|
|
823
|
+
this._objHashFn = objHashFn;
|
|
824
|
+
}
|
|
825
|
+
if (entries) {
|
|
826
|
+
for (const el of entries) {
|
|
1094
827
|
this.set(el[0], el[1]);
|
|
1095
828
|
}
|
|
1096
829
|
}
|
|
@@ -1314,7 +1047,7 @@ var dataStructureTyped = (() => {
|
|
|
1314
1047
|
* Time Complexity: O(1)
|
|
1315
1048
|
* Space Complexity: O(1)
|
|
1316
1049
|
*
|
|
1317
|
-
* The `clear` function clears all the
|
|
1050
|
+
* The `clear` function clears all the entries in a data structure and resets its properties.
|
|
1318
1051
|
*/
|
|
1319
1052
|
clear() {
|
|
1320
1053
|
this._noObjMap = {};
|
|
@@ -1329,10 +1062,6 @@ var dataStructureTyped = (() => {
|
|
|
1329
1062
|
}
|
|
1330
1063
|
return cloned;
|
|
1331
1064
|
}
|
|
1332
|
-
/**
|
|
1333
|
-
* Time Complexity: O(n)
|
|
1334
|
-
* Space Complexity: O(n)
|
|
1335
|
-
*/
|
|
1336
1065
|
/**
|
|
1337
1066
|
* Time Complexity: O(n)
|
|
1338
1067
|
* Space Complexity: O(n)
|
|
@@ -1359,10 +1088,6 @@ var dataStructureTyped = (() => {
|
|
|
1359
1088
|
}
|
|
1360
1089
|
return filteredMap;
|
|
1361
1090
|
}
|
|
1362
|
-
/**
|
|
1363
|
-
* Time Complexity: O(n)
|
|
1364
|
-
* Space Complexity: O(n)
|
|
1365
|
-
*/
|
|
1366
1091
|
/**
|
|
1367
1092
|
* Time Complexity: O(n)
|
|
1368
1093
|
* Space Complexity: O(n)
|
|
@@ -1390,11 +1115,15 @@ var dataStructureTyped = (() => {
|
|
|
1390
1115
|
}
|
|
1391
1116
|
return mappedMap;
|
|
1392
1117
|
}
|
|
1118
|
+
/**
|
|
1119
|
+
* Time Complexity: O(n)
|
|
1120
|
+
* Space Complexity: O(n)
|
|
1121
|
+
*/
|
|
1393
1122
|
put(key, value) {
|
|
1394
1123
|
return this.set(key, value);
|
|
1395
1124
|
}
|
|
1396
1125
|
/**
|
|
1397
|
-
* Time Complexity: O(n), where n is the number of
|
|
1126
|
+
* Time Complexity: O(n), where n is the number of entries in the LinkedHashMap.
|
|
1398
1127
|
* Space Complexity: O(1)
|
|
1399
1128
|
*
|
|
1400
1129
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
@@ -1449,14 +1178,11 @@ var dataStructureTyped = (() => {
|
|
|
1449
1178
|
/**
|
|
1450
1179
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
1451
1180
|
*/
|
|
1452
|
-
constructor(elements) {
|
|
1181
|
+
constructor(elements = []) {
|
|
1453
1182
|
super();
|
|
1454
1183
|
__publicField(this, "_head");
|
|
1455
1184
|
__publicField(this, "_tail");
|
|
1456
|
-
__publicField(this, "_size");
|
|
1457
|
-
this._head = void 0;
|
|
1458
|
-
this._tail = void 0;
|
|
1459
|
-
this._size = 0;
|
|
1185
|
+
__publicField(this, "_size", 0);
|
|
1460
1186
|
if (elements) {
|
|
1461
1187
|
for (const el of elements)
|
|
1462
1188
|
this.push(el);
|
|
@@ -2126,7 +1852,7 @@ var dataStructureTyped = (() => {
|
|
|
2126
1852
|
/**
|
|
2127
1853
|
* The constructor initializes the linked list with an empty head, tail, and size.
|
|
2128
1854
|
*/
|
|
2129
|
-
constructor(elements) {
|
|
1855
|
+
constructor(elements = []) {
|
|
2130
1856
|
super();
|
|
2131
1857
|
__publicField(this, "_head");
|
|
2132
1858
|
__publicField(this, "_tail");
|
|
@@ -2852,22 +2578,22 @@ var dataStructureTyped = (() => {
|
|
|
2852
2578
|
}
|
|
2853
2579
|
};
|
|
2854
2580
|
var SkipList = class {
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2581
|
+
constructor(elements = [], options) {
|
|
2582
|
+
__publicField(this, "_head", new SkipListNode(void 0, void 0, this.maxLevel));
|
|
2583
|
+
__publicField(this, "_level", 0);
|
|
2584
|
+
__publicField(this, "_maxLevel", 16);
|
|
2585
|
+
__publicField(this, "_probability", 0.5);
|
|
2586
|
+
if (options) {
|
|
2587
|
+
const { maxLevel, probability } = options;
|
|
2588
|
+
if (typeof maxLevel === "number")
|
|
2589
|
+
this._maxLevel = maxLevel;
|
|
2590
|
+
if (typeof probability === "number")
|
|
2591
|
+
this._probability = probability;
|
|
2592
|
+
}
|
|
2593
|
+
if (elements) {
|
|
2594
|
+
for (const [key, value] of elements)
|
|
2595
|
+
this.add(key, value);
|
|
2596
|
+
}
|
|
2871
2597
|
}
|
|
2872
2598
|
get head() {
|
|
2873
2599
|
return this._head;
|
|
@@ -3096,14 +2822,12 @@ var dataStructureTyped = (() => {
|
|
|
3096
2822
|
* of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
3097
2823
|
* is provided and is an array, it is assigned to the `_elements
|
|
3098
2824
|
*/
|
|
3099
|
-
constructor(elements) {
|
|
2825
|
+
constructor(elements = []) {
|
|
3100
2826
|
super();
|
|
3101
|
-
__publicField(this, "_elements");
|
|
3102
|
-
this._elements = [];
|
|
2827
|
+
__publicField(this, "_elements", []);
|
|
3103
2828
|
if (elements) {
|
|
3104
|
-
for (const el of elements)
|
|
2829
|
+
for (const el of elements)
|
|
3105
2830
|
this.push(el);
|
|
3106
|
-
}
|
|
3107
2831
|
}
|
|
3108
2832
|
}
|
|
3109
2833
|
get elements() {
|
|
@@ -3295,18 +3019,20 @@ var dataStructureTyped = (() => {
|
|
|
3295
3019
|
/**
|
|
3296
3020
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
3297
3021
|
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
3298
|
-
* will be used to initialize the `
|
|
3022
|
+
* will be used to initialize the `_elements` property of the class. If not provided, the `_elements` property will be
|
|
3299
3023
|
* initialized as an empty array.
|
|
3300
3024
|
*/
|
|
3301
|
-
constructor(elements) {
|
|
3025
|
+
constructor(elements = []) {
|
|
3302
3026
|
super();
|
|
3303
|
-
__publicField(this, "
|
|
3304
|
-
__publicField(this, "_offset");
|
|
3305
|
-
|
|
3306
|
-
|
|
3027
|
+
__publicField(this, "_elements", []);
|
|
3028
|
+
__publicField(this, "_offset", 0);
|
|
3029
|
+
if (elements) {
|
|
3030
|
+
for (const el of elements)
|
|
3031
|
+
this.push(el);
|
|
3032
|
+
}
|
|
3307
3033
|
}
|
|
3308
|
-
get
|
|
3309
|
-
return this.
|
|
3034
|
+
get elements() {
|
|
3035
|
+
return this._elements;
|
|
3310
3036
|
}
|
|
3311
3037
|
get offset() {
|
|
3312
3038
|
return this._offset;
|
|
@@ -3316,18 +3042,18 @@ var dataStructureTyped = (() => {
|
|
|
3316
3042
|
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
3317
3043
|
*/
|
|
3318
3044
|
get size() {
|
|
3319
|
-
return this.
|
|
3045
|
+
return this.elements.length - this.offset;
|
|
3320
3046
|
}
|
|
3321
3047
|
/**
|
|
3322
3048
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
3323
3049
|
* Space Complexity: O(1) - no additional space is used.
|
|
3324
3050
|
*
|
|
3325
|
-
* The `first` function returns the first element of the array `
|
|
3326
|
-
* @returns The `get first()` method returns the first element of the data structure, represented by the `
|
|
3051
|
+
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
3052
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
3327
3053
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
3328
3054
|
*/
|
|
3329
3055
|
get first() {
|
|
3330
|
-
return this.size > 0 ? this.
|
|
3056
|
+
return this.size > 0 ? this.elements[this.offset] : void 0;
|
|
3331
3057
|
}
|
|
3332
3058
|
/**
|
|
3333
3059
|
* Time Complexity: O(1) - constant time as it adds an element to the end of the array.
|
|
@@ -3338,11 +3064,11 @@ var dataStructureTyped = (() => {
|
|
|
3338
3064
|
* Space Complexity: O(1) - no additional space is used.
|
|
3339
3065
|
*
|
|
3340
3066
|
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
3341
|
-
* @returns The method `get last()` returns the last element of the `
|
|
3067
|
+
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
3342
3068
|
* array is empty, it returns `undefined`.
|
|
3343
3069
|
*/
|
|
3344
3070
|
get last() {
|
|
3345
|
-
return this.size > 0 ? this.
|
|
3071
|
+
return this.size > 0 ? this.elements[this.elements.length - 1] : void 0;
|
|
3346
3072
|
}
|
|
3347
3073
|
/**
|
|
3348
3074
|
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
|
|
@@ -3372,7 +3098,7 @@ var dataStructureTyped = (() => {
|
|
|
3372
3098
|
* @returns The `add` method is returning a `Queue<E>` object.
|
|
3373
3099
|
*/
|
|
3374
3100
|
push(element) {
|
|
3375
|
-
this.
|
|
3101
|
+
this.elements.push(element);
|
|
3376
3102
|
return true;
|
|
3377
3103
|
}
|
|
3378
3104
|
/**
|
|
@@ -3392,9 +3118,9 @@ var dataStructureTyped = (() => {
|
|
|
3392
3118
|
return void 0;
|
|
3393
3119
|
const first = this.first;
|
|
3394
3120
|
this._offset += 1;
|
|
3395
|
-
if (this.offset * 2 < this.
|
|
3121
|
+
if (this.offset * 2 < this.elements.length)
|
|
3396
3122
|
return first;
|
|
3397
|
-
this.
|
|
3123
|
+
this._elements = this.elements.slice(this.offset);
|
|
3398
3124
|
this._offset = 0;
|
|
3399
3125
|
return first;
|
|
3400
3126
|
}
|
|
@@ -3406,8 +3132,8 @@ var dataStructureTyped = (() => {
|
|
|
3406
3132
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
3407
3133
|
* Space Complexity: O(1) - no additional space is used.
|
|
3408
3134
|
*
|
|
3409
|
-
* The `peek` function returns the first element of the array `
|
|
3410
|
-
* @returns The `peek()` method returns the first element of the data structure, represented by the `
|
|
3135
|
+
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
3136
|
+
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
3411
3137
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
3412
3138
|
*/
|
|
3413
3139
|
peek() {
|
|
@@ -3422,7 +3148,7 @@ var dataStructureTyped = (() => {
|
|
|
3422
3148
|
* Space Complexity: O(1) - no additional space is used.
|
|
3423
3149
|
*
|
|
3424
3150
|
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
3425
|
-
* @returns The method `peekLast()` returns the last element of the `
|
|
3151
|
+
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
3426
3152
|
* array is empty, it returns `undefined`.
|
|
3427
3153
|
*/
|
|
3428
3154
|
peekLast() {
|
|
@@ -3467,7 +3193,7 @@ var dataStructureTyped = (() => {
|
|
|
3467
3193
|
* @param index
|
|
3468
3194
|
*/
|
|
3469
3195
|
getAt(index) {
|
|
3470
|
-
return this.
|
|
3196
|
+
return this.elements[index];
|
|
3471
3197
|
}
|
|
3472
3198
|
/**
|
|
3473
3199
|
* Time Complexity: O(1) - constant time as it retrieves the value at the specified index.
|
|
@@ -3491,17 +3217,17 @@ var dataStructureTyped = (() => {
|
|
|
3491
3217
|
* Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array.
|
|
3492
3218
|
* Space Complexity: O(n) - where n is the number of elements in the queue.
|
|
3493
3219
|
*
|
|
3494
|
-
* The toArray() function returns an array of elements from the current offset to the end of the
|
|
3220
|
+
* The toArray() function returns an array of elements from the current offset to the end of the _elements array.
|
|
3495
3221
|
* @returns An array of type E is being returned.
|
|
3496
3222
|
*/
|
|
3497
3223
|
toArray() {
|
|
3498
|
-
return this.
|
|
3224
|
+
return this.elements.slice(this.offset);
|
|
3499
3225
|
}
|
|
3500
3226
|
/**
|
|
3501
|
-
* The clear function resets the
|
|
3227
|
+
* The clear function resets the elements array and offset to their initial values.
|
|
3502
3228
|
*/
|
|
3503
3229
|
clear() {
|
|
3504
|
-
this.
|
|
3230
|
+
this._elements = [];
|
|
3505
3231
|
this._offset = 0;
|
|
3506
3232
|
}
|
|
3507
3233
|
/**
|
|
@@ -3516,7 +3242,7 @@ var dataStructureTyped = (() => {
|
|
|
3516
3242
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
3517
3243
|
*/
|
|
3518
3244
|
clone() {
|
|
3519
|
-
return new _Queue(this.
|
|
3245
|
+
return new _Queue(this.elements.slice(this.offset));
|
|
3520
3246
|
}
|
|
3521
3247
|
/**
|
|
3522
3248
|
* Time Complexity: O(n)
|
|
@@ -3581,7 +3307,7 @@ var dataStructureTyped = (() => {
|
|
|
3581
3307
|
* Space Complexity: O(n)
|
|
3582
3308
|
*/
|
|
3583
3309
|
*_getIterator() {
|
|
3584
|
-
for (const item of this.
|
|
3310
|
+
for (const item of this.elements) {
|
|
3585
3311
|
yield item;
|
|
3586
3312
|
}
|
|
3587
3313
|
}
|
|
@@ -3620,25 +3346,21 @@ var dataStructureTyped = (() => {
|
|
|
3620
3346
|
|
|
3621
3347
|
// src/data-structures/queue/deque.ts
|
|
3622
3348
|
var Deque = class _Deque extends IterableElementBase {
|
|
3623
|
-
|
|
3624
|
-
* The constructor initializes a data structure with a specified bucket size and populates it with
|
|
3625
|
-
* elements from an iterable.
|
|
3626
|
-
* @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that
|
|
3627
|
-
* contains the initial elements to be stored in the data structure. It can also be an object with a
|
|
3628
|
-
* `length` property or a `size` property, which represents the number of elements in the iterable.
|
|
3629
|
-
* @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be
|
|
3630
|
-
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
3631
|
-
*/
|
|
3632
|
-
constructor(elements = [], bucketSize = 1 << 12) {
|
|
3349
|
+
constructor(elements = [], options) {
|
|
3633
3350
|
super();
|
|
3634
3351
|
__publicField(this, "_bucketFirst", 0);
|
|
3635
3352
|
__publicField(this, "_firstInBucket", 0);
|
|
3636
3353
|
__publicField(this, "_bucketLast", 0);
|
|
3637
3354
|
__publicField(this, "_lastInBucket", 0);
|
|
3638
3355
|
__publicField(this, "_bucketCount", 0);
|
|
3639
|
-
__publicField(this, "_bucketSize");
|
|
3356
|
+
__publicField(this, "_bucketSize", 1 << 12);
|
|
3640
3357
|
__publicField(this, "_buckets", []);
|
|
3641
3358
|
__publicField(this, "_size", 0);
|
|
3359
|
+
if (options) {
|
|
3360
|
+
const { bucketSize } = options;
|
|
3361
|
+
if (typeof bucketSize === "number")
|
|
3362
|
+
this._bucketSize = bucketSize;
|
|
3363
|
+
}
|
|
3642
3364
|
let _size;
|
|
3643
3365
|
if ("length" in elements) {
|
|
3644
3366
|
if (elements.length instanceof Function)
|
|
@@ -3651,7 +3373,6 @@ var dataStructureTyped = (() => {
|
|
|
3651
3373
|
else
|
|
3652
3374
|
_size = elements.size;
|
|
3653
3375
|
}
|
|
3654
|
-
this._bucketSize = bucketSize;
|
|
3655
3376
|
this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
|
|
3656
3377
|
for (let i = 0; i < this._bucketCount; ++i) {
|
|
3657
3378
|
this._buckets.push(new Array(this._bucketSize));
|
|
@@ -4210,7 +3931,7 @@ var dataStructureTyped = (() => {
|
|
|
4210
3931
|
* satisfy the given predicate function.
|
|
4211
3932
|
*/
|
|
4212
3933
|
filter(predicate, thisArg) {
|
|
4213
|
-
const newDeque = new _Deque([], this._bucketSize);
|
|
3934
|
+
const newDeque = new _Deque([], { bucketSize: this._bucketSize });
|
|
4214
3935
|
let index = 0;
|
|
4215
3936
|
for (const el of this) {
|
|
4216
3937
|
if (predicate.call(thisArg, el, index, this)) {
|
|
@@ -4238,7 +3959,7 @@ var dataStructureTyped = (() => {
|
|
|
4238
3959
|
* @returns a new Deque object with the mapped values.
|
|
4239
3960
|
*/
|
|
4240
3961
|
map(callback, thisArg) {
|
|
4241
|
-
const newDeque = new _Deque([], this._bucketSize);
|
|
3962
|
+
const newDeque = new _Deque([], { bucketSize: this._bucketSize });
|
|
4242
3963
|
let index = 0;
|
|
4243
3964
|
for (const el of this) {
|
|
4244
3965
|
newDeque.push(callback.call(thisArg, el, index, this));
|
|
@@ -4374,23 +4095,20 @@ var dataStructureTyped = (() => {
|
|
|
4374
4095
|
|
|
4375
4096
|
// src/data-structures/heap/heap.ts
|
|
4376
4097
|
var Heap = class _Heap extends IterableElementBase {
|
|
4377
|
-
constructor(elements, options) {
|
|
4098
|
+
constructor(elements = [], options) {
|
|
4378
4099
|
super();
|
|
4379
|
-
__publicField(this, "
|
|
4380
|
-
__publicField(this, "_elements", []);
|
|
4381
|
-
const defaultComparator = (a, b) => {
|
|
4100
|
+
__publicField(this, "_comparator", (a, b) => {
|
|
4382
4101
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
4383
4102
|
throw new Error("The a, b params of compare function must be number");
|
|
4384
4103
|
} else {
|
|
4385
4104
|
return a - b;
|
|
4386
4105
|
}
|
|
4387
|
-
};
|
|
4106
|
+
});
|
|
4107
|
+
__publicField(this, "_elements", []);
|
|
4388
4108
|
if (options) {
|
|
4389
|
-
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
comparator: defaultComparator
|
|
4393
|
-
};
|
|
4109
|
+
const { comparator } = options;
|
|
4110
|
+
if (comparator)
|
|
4111
|
+
this._comparator = comparator;
|
|
4394
4112
|
}
|
|
4395
4113
|
if (elements) {
|
|
4396
4114
|
for (const el of elements) {
|
|
@@ -4398,6 +4116,9 @@ var dataStructureTyped = (() => {
|
|
|
4398
4116
|
}
|
|
4399
4117
|
}
|
|
4400
4118
|
}
|
|
4119
|
+
get comparator() {
|
|
4120
|
+
return this._comparator;
|
|
4121
|
+
}
|
|
4401
4122
|
get elements() {
|
|
4402
4123
|
return this._elements;
|
|
4403
4124
|
}
|
|
@@ -4602,7 +4323,7 @@ var dataStructureTyped = (() => {
|
|
|
4602
4323
|
* @returns A new Heap instance containing the same elements.
|
|
4603
4324
|
*/
|
|
4604
4325
|
clone() {
|
|
4605
|
-
const clonedHeap = new _Heap([], this.
|
|
4326
|
+
const clonedHeap = new _Heap([], { comparator: this.comparator });
|
|
4606
4327
|
clonedHeap._elements = [...this.elements];
|
|
4607
4328
|
return clonedHeap;
|
|
4608
4329
|
}
|
|
@@ -4728,7 +4449,7 @@ var dataStructureTyped = (() => {
|
|
|
4728
4449
|
while (index > 0) {
|
|
4729
4450
|
const parent = index - 1 >> 1;
|
|
4730
4451
|
const parentItem = this.elements[parent];
|
|
4731
|
-
if (this.
|
|
4452
|
+
if (this.comparator(parentItem, element) <= 0)
|
|
4732
4453
|
break;
|
|
4733
4454
|
this.elements[index] = parentItem;
|
|
4734
4455
|
index = parent;
|
|
@@ -4750,11 +4471,11 @@ var dataStructureTyped = (() => {
|
|
|
4750
4471
|
let left = index << 1 | 1;
|
|
4751
4472
|
const right = left + 1;
|
|
4752
4473
|
let minItem = this.elements[left];
|
|
4753
|
-
if (right < this.elements.length && this.
|
|
4474
|
+
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
|
|
4754
4475
|
left = right;
|
|
4755
4476
|
minItem = this.elements[right];
|
|
4756
4477
|
}
|
|
4757
|
-
if (this.
|
|
4478
|
+
if (this.comparator(minItem, element) >= 0)
|
|
4758
4479
|
break;
|
|
4759
4480
|
this.elements[index] = minItem;
|
|
4760
4481
|
index = left;
|
|
@@ -5113,7 +4834,7 @@ var dataStructureTyped = (() => {
|
|
|
5113
4834
|
|
|
5114
4835
|
// src/data-structures/heap/max-heap.ts
|
|
5115
4836
|
var MaxHeap = class extends Heap {
|
|
5116
|
-
constructor(elements, options = {
|
|
4837
|
+
constructor(elements = [], options = {
|
|
5117
4838
|
comparator: (a, b) => {
|
|
5118
4839
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
5119
4840
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -5128,7 +4849,7 @@ var dataStructureTyped = (() => {
|
|
|
5128
4849
|
|
|
5129
4850
|
// src/data-structures/heap/min-heap.ts
|
|
5130
4851
|
var MinHeap = class extends Heap {
|
|
5131
|
-
constructor(elements, options = {
|
|
4852
|
+
constructor(elements = [], options = {
|
|
5132
4853
|
comparator: (a, b) => {
|
|
5133
4854
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
5134
4855
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -7241,8 +6962,8 @@ var dataStructureTyped = (() => {
|
|
|
7241
6962
|
|
|
7242
6963
|
// src/types/common.ts
|
|
7243
6964
|
var BSTVariant = /* @__PURE__ */ ((BSTVariant2) => {
|
|
7244
|
-
BSTVariant2["
|
|
7245
|
-
BSTVariant2["
|
|
6965
|
+
BSTVariant2["STANDARD"] = "STANDARD";
|
|
6966
|
+
BSTVariant2["INVERSE"] = "INVERSE";
|
|
7246
6967
|
return BSTVariant2;
|
|
7247
6968
|
})(BSTVariant || {});
|
|
7248
6969
|
var CP = /* @__PURE__ */ ((CP2) => {
|
|
@@ -7315,15 +7036,15 @@ var dataStructureTyped = (() => {
|
|
|
7315
7036
|
};
|
|
7316
7037
|
var BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
7317
7038
|
/**
|
|
7318
|
-
* The constructor function initializes a binary tree object with optional
|
|
7319
|
-
* @param [
|
|
7320
|
-
*
|
|
7039
|
+
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
|
|
7040
|
+
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
7041
|
+
* nodes to be added to the binary tree.
|
|
7321
7042
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
7322
7043
|
* configuration options for the binary tree. In this case, it is of type
|
|
7323
7044
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
7324
7045
|
* required.
|
|
7325
7046
|
*/
|
|
7326
|
-
constructor(
|
|
7047
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
7327
7048
|
super();
|
|
7328
7049
|
__publicField(this, "iterationType", "ITERATIVE" /* ITERATIVE */);
|
|
7329
7050
|
__publicField(this, "_extractor", (key) => Number(key));
|
|
@@ -7332,16 +7053,14 @@ var dataStructureTyped = (() => {
|
|
|
7332
7053
|
__publicField(this, "_defaultOneParamCallback", (node) => node ? node.key : void 0);
|
|
7333
7054
|
if (options) {
|
|
7334
7055
|
const { iterationType, extractor } = options;
|
|
7335
|
-
if (iterationType)
|
|
7056
|
+
if (iterationType)
|
|
7336
7057
|
this.iterationType = iterationType;
|
|
7337
|
-
|
|
7338
|
-
if (extractor) {
|
|
7058
|
+
if (extractor)
|
|
7339
7059
|
this._extractor = extractor;
|
|
7340
|
-
}
|
|
7341
7060
|
}
|
|
7342
7061
|
this._size = 0;
|
|
7343
|
-
if (
|
|
7344
|
-
this.addMany(
|
|
7062
|
+
if (keysOrNodesOrEntries)
|
|
7063
|
+
this.addMany(keysOrNodesOrEntries);
|
|
7345
7064
|
}
|
|
7346
7065
|
get extractor() {
|
|
7347
7066
|
return this._extractor;
|
|
@@ -7372,29 +7091,21 @@ var dataStructureTyped = (() => {
|
|
|
7372
7091
|
return new _BinaryTree([], __spreadValues({ iterationType: this.iterationType }, options));
|
|
7373
7092
|
}
|
|
7374
7093
|
/**
|
|
7375
|
-
* The function
|
|
7376
|
-
* @param
|
|
7377
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
|
7378
|
-
*/
|
|
7379
|
-
isNode(exemplar) {
|
|
7380
|
-
return exemplar instanceof BinaryTreeNode;
|
|
7381
|
-
}
|
|
7382
|
-
/**
|
|
7383
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
7384
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
7094
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
7095
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
7385
7096
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
7386
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
7097
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
|
|
7387
7098
|
* is provided, it will be `undefined`.
|
|
7388
7099
|
* @returns a value of type N (node), or null, or undefined.
|
|
7389
7100
|
*/
|
|
7390
|
-
exemplarToNode(
|
|
7391
|
-
if (
|
|
7101
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
7102
|
+
if (keyOrNodeOrEntry === void 0)
|
|
7392
7103
|
return;
|
|
7393
7104
|
let node;
|
|
7394
|
-
if (
|
|
7105
|
+
if (keyOrNodeOrEntry === null) {
|
|
7395
7106
|
node = null;
|
|
7396
|
-
} else if (this.isEntry(
|
|
7397
|
-
const [key, value2] =
|
|
7107
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
7108
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
7398
7109
|
if (key === void 0) {
|
|
7399
7110
|
return;
|
|
7400
7111
|
} else if (key === null) {
|
|
@@ -7402,23 +7113,104 @@ var dataStructureTyped = (() => {
|
|
|
7402
7113
|
} else {
|
|
7403
7114
|
node = this.createNode(key, value2);
|
|
7404
7115
|
}
|
|
7405
|
-
} else if (this.isNode(
|
|
7406
|
-
node =
|
|
7407
|
-
} else if (this.isNotNodeInstance(
|
|
7408
|
-
node = this.createNode(
|
|
7116
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
7117
|
+
node = keyOrNodeOrEntry;
|
|
7118
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
7119
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
7409
7120
|
} else {
|
|
7410
7121
|
return;
|
|
7411
7122
|
}
|
|
7412
7123
|
return node;
|
|
7413
7124
|
}
|
|
7125
|
+
/**
|
|
7126
|
+
* Time Complexity: O(n)
|
|
7127
|
+
* Space Complexity: O(log n)
|
|
7128
|
+
*/
|
|
7129
|
+
/**
|
|
7130
|
+
* Time Complexity: O(n)
|
|
7131
|
+
* Space Complexity: O(log n)
|
|
7132
|
+
*
|
|
7133
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
7134
|
+
* key, otherwise it returns the key itself.
|
|
7135
|
+
* @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`,
|
|
7136
|
+
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
7137
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
7138
|
+
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
7139
|
+
* `IterationType.ITERATIVE`.
|
|
7140
|
+
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
7141
|
+
* itself if it is not a valid node key.
|
|
7142
|
+
*/
|
|
7143
|
+
ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
7144
|
+
let res;
|
|
7145
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
7146
|
+
res = keyOrNodeOrEntry;
|
|
7147
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
7148
|
+
if (keyOrNodeOrEntry[0] === null)
|
|
7149
|
+
res = null;
|
|
7150
|
+
else if (keyOrNodeOrEntry[0] !== void 0)
|
|
7151
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
7152
|
+
} else {
|
|
7153
|
+
if (keyOrNodeOrEntry === null)
|
|
7154
|
+
res = null;
|
|
7155
|
+
else if (keyOrNodeOrEntry !== void 0)
|
|
7156
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
7157
|
+
}
|
|
7158
|
+
return res;
|
|
7159
|
+
}
|
|
7160
|
+
/**
|
|
7161
|
+
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
7162
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,N>`.
|
|
7163
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N.
|
|
7164
|
+
*/
|
|
7165
|
+
isNode(keyOrNodeOrEntry) {
|
|
7166
|
+
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
7167
|
+
}
|
|
7414
7168
|
/**
|
|
7415
7169
|
* The function checks if a given value is an entry in a binary tree node.
|
|
7416
|
-
* @param
|
|
7170
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,N> - A generic type representing a node in a binary tree. It has
|
|
7417
7171
|
* two type parameters V and N, representing the value and node type respectively.
|
|
7418
7172
|
* @returns a boolean value.
|
|
7419
7173
|
*/
|
|
7420
|
-
isEntry(
|
|
7421
|
-
return Array.isArray(
|
|
7174
|
+
isEntry(keyOrNodeOrEntry) {
|
|
7175
|
+
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
7176
|
+
}
|
|
7177
|
+
/**
|
|
7178
|
+
* Time complexity: O(n)
|
|
7179
|
+
* Space complexity: O(log n)
|
|
7180
|
+
*/
|
|
7181
|
+
/**
|
|
7182
|
+
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
7183
|
+
* BinaryTreeNode and its key is not NaN.
|
|
7184
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7185
|
+
* @returns a boolean value.
|
|
7186
|
+
*/
|
|
7187
|
+
isRealNode(node) {
|
|
7188
|
+
return node instanceof BinaryTreeNode && String(node.key) !== "NaN";
|
|
7189
|
+
}
|
|
7190
|
+
/**
|
|
7191
|
+
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
7192
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7193
|
+
* @returns a boolean value.
|
|
7194
|
+
*/
|
|
7195
|
+
isNIL(node) {
|
|
7196
|
+
return node instanceof BinaryTreeNode && String(node.key) === "NaN";
|
|
7197
|
+
}
|
|
7198
|
+
/**
|
|
7199
|
+
* The function checks if a given node is a real node or null.
|
|
7200
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7201
|
+
* @returns a boolean value.
|
|
7202
|
+
*/
|
|
7203
|
+
isNodeOrNull(node) {
|
|
7204
|
+
return this.isRealNode(node) || node === null;
|
|
7205
|
+
}
|
|
7206
|
+
/**
|
|
7207
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
7208
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
7209
|
+
* data type.
|
|
7210
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
7211
|
+
*/
|
|
7212
|
+
isNotNodeInstance(potentialKey) {
|
|
7213
|
+
return !(potentialKey instanceof BinaryTreeNode);
|
|
7422
7214
|
}
|
|
7423
7215
|
/**
|
|
7424
7216
|
* Time Complexity O(log n) - O(n)
|
|
@@ -7437,11 +7229,11 @@ var dataStructureTyped = (() => {
|
|
|
7437
7229
|
add(keyOrNodeOrEntry, value) {
|
|
7438
7230
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
7439
7231
|
if (newNode === void 0)
|
|
7440
|
-
return;
|
|
7232
|
+
return false;
|
|
7441
7233
|
if (!this.root) {
|
|
7442
7234
|
this._root = newNode;
|
|
7443
7235
|
this._size = 1;
|
|
7444
|
-
return
|
|
7236
|
+
return true;
|
|
7445
7237
|
}
|
|
7446
7238
|
const queue = new Queue([this.root]);
|
|
7447
7239
|
let potentialParent;
|
|
@@ -7451,7 +7243,7 @@ var dataStructureTyped = (() => {
|
|
|
7451
7243
|
continue;
|
|
7452
7244
|
if (newNode !== null && cur.key === newNode.key) {
|
|
7453
7245
|
this._replaceNode(cur, newNode);
|
|
7454
|
-
return
|
|
7246
|
+
return true;
|
|
7455
7247
|
}
|
|
7456
7248
|
if (potentialParent === void 0 && (cur.left === void 0 || cur.right === void 0)) {
|
|
7457
7249
|
potentialParent = cur;
|
|
@@ -7470,9 +7262,9 @@ var dataStructureTyped = (() => {
|
|
|
7470
7262
|
potentialParent.right = newNode;
|
|
7471
7263
|
}
|
|
7472
7264
|
this._size++;
|
|
7473
|
-
return
|
|
7265
|
+
return true;
|
|
7474
7266
|
}
|
|
7475
|
-
return
|
|
7267
|
+
return false;
|
|
7476
7268
|
}
|
|
7477
7269
|
/**
|
|
7478
7270
|
* Time Complexity: O(k log n) - O(k * n)
|
|
@@ -7483,19 +7275,19 @@ var dataStructureTyped = (() => {
|
|
|
7483
7275
|
* Time Complexity: O(k log n) - O(k * n)
|
|
7484
7276
|
* Space Complexity: O(1)
|
|
7485
7277
|
*
|
|
7486
|
-
* The `addMany` function takes in a collection of
|
|
7278
|
+
* The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
|
|
7487
7279
|
* adds each node with its corresponding value to the data structure.
|
|
7488
|
-
* @param
|
|
7280
|
+
* @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
|
|
7489
7281
|
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
7490
7282
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
7491
7283
|
*/
|
|
7492
|
-
addMany(
|
|
7284
|
+
addMany(keysOrNodesOrEntries, values) {
|
|
7493
7285
|
const inserted = [];
|
|
7494
7286
|
let valuesIterator;
|
|
7495
7287
|
if (values) {
|
|
7496
7288
|
valuesIterator = values[Symbol.iterator]();
|
|
7497
7289
|
}
|
|
7498
|
-
for (const
|
|
7290
|
+
for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
|
|
7499
7291
|
let value = void 0;
|
|
7500
7292
|
if (valuesIterator) {
|
|
7501
7293
|
const valueResult = valuesIterator.next();
|
|
@@ -7503,17 +7295,30 @@ var dataStructureTyped = (() => {
|
|
|
7503
7295
|
value = valueResult.value;
|
|
7504
7296
|
}
|
|
7505
7297
|
}
|
|
7506
|
-
inserted.push(this.add(
|
|
7298
|
+
inserted.push(this.add(keyOrNodeOrEntry, value));
|
|
7507
7299
|
}
|
|
7508
7300
|
return inserted;
|
|
7509
7301
|
}
|
|
7510
7302
|
/**
|
|
7511
|
-
* Time Complexity: O(k * n)
|
|
7303
|
+
* Time Complexity: O(k * n)
|
|
7304
|
+
* Space Complexity: O(1)
|
|
7305
|
+
* "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
7306
|
+
*/
|
|
7307
|
+
/**
|
|
7308
|
+
* Time Complexity: O(k * n)
|
|
7512
7309
|
* Space Complexity: O(1)
|
|
7310
|
+
*
|
|
7311
|
+
* The `refill` function clears the current data and adds new key-value pairs to the data structure.
|
|
7312
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
|
|
7313
|
+
* KeyOrNodeOrEntry<K, V, N>.
|
|
7314
|
+
* @param [values] - The `values` parameter is an optional iterable that contains the values to be
|
|
7315
|
+
* associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
|
|
7316
|
+
* the values will be associated with the corresponding keys or nodes or entries in the
|
|
7317
|
+
* `keysOrNodesOrEntries` iterable
|
|
7513
7318
|
*/
|
|
7514
|
-
refill(
|
|
7319
|
+
refill(keysOrNodesOrEntries, values) {
|
|
7515
7320
|
this.clear();
|
|
7516
|
-
this.addMany(
|
|
7321
|
+
this.addMany(keysOrNodesOrEntries, values);
|
|
7517
7322
|
}
|
|
7518
7323
|
/**
|
|
7519
7324
|
* Time Complexity: O(n)
|
|
@@ -7583,24 +7388,24 @@ var dataStructureTyped = (() => {
|
|
|
7583
7388
|
* Space Complexity: O(1)
|
|
7584
7389
|
*
|
|
7585
7390
|
* The function calculates the depth of a given node in a binary tree.
|
|
7586
|
-
* @param {K | N | null | undefined}
|
|
7391
|
+
* @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
|
|
7587
7392
|
* the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
|
|
7588
7393
|
* `undefined`.
|
|
7589
7394
|
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
|
|
7590
7395
|
* from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
|
|
7591
7396
|
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
|
7592
|
-
* @returns the depth of the `
|
|
7397
|
+
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
7593
7398
|
*/
|
|
7594
|
-
getDepth(
|
|
7595
|
-
|
|
7399
|
+
getDepth(dist, beginRoot = this.root) {
|
|
7400
|
+
dist = this.ensureNode(dist);
|
|
7596
7401
|
beginRoot = this.ensureNode(beginRoot);
|
|
7597
7402
|
let depth = 0;
|
|
7598
|
-
while (
|
|
7599
|
-
if (
|
|
7403
|
+
while (dist == null ? void 0 : dist.parent) {
|
|
7404
|
+
if (dist === beginRoot) {
|
|
7600
7405
|
return depth;
|
|
7601
7406
|
}
|
|
7602
7407
|
depth++;
|
|
7603
|
-
|
|
7408
|
+
dist = dist.parent;
|
|
7604
7409
|
}
|
|
7605
7410
|
return depth;
|
|
7606
7411
|
}
|
|
@@ -7792,6 +7597,7 @@ var dataStructureTyped = (() => {
|
|
|
7792
7597
|
}
|
|
7793
7598
|
/**
|
|
7794
7599
|
* Time Complexity: O(n)
|
|
7600
|
+
* Space Complexity: O(log n).
|
|
7795
7601
|
*
|
|
7796
7602
|
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
|
|
7797
7603
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
@@ -7888,24 +7694,6 @@ var dataStructureTyped = (() => {
|
|
|
7888
7694
|
}
|
|
7889
7695
|
}
|
|
7890
7696
|
}
|
|
7891
|
-
/**
|
|
7892
|
-
* Time Complexity: O(n)
|
|
7893
|
-
* Space Complexity: O(log n)
|
|
7894
|
-
*/
|
|
7895
|
-
/**
|
|
7896
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
7897
|
-
* key, otherwise it returns the key itself.
|
|
7898
|
-
* @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
|
|
7899
|
-
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
7900
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
7901
|
-
* type of iteration to be used when searching for a node by key. It has a default value of
|
|
7902
|
-
* `IterationType.ITERATIVE`.
|
|
7903
|
-
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
7904
|
-
* itself if it is not a valid node key.
|
|
7905
|
-
*/
|
|
7906
|
-
ensureNode(key, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
7907
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
7908
|
-
}
|
|
7909
7697
|
/**
|
|
7910
7698
|
* Time Complexity: O(n)
|
|
7911
7699
|
* Space Complexity: O(log n)
|
|
@@ -7935,10 +7723,13 @@ var dataStructureTyped = (() => {
|
|
|
7935
7723
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value) != null ? _b : void 0;
|
|
7936
7724
|
}
|
|
7937
7725
|
/**
|
|
7938
|
-
* Time Complexity: O(
|
|
7939
|
-
* Space Complexity: O(
|
|
7726
|
+
* Time Complexity: O(1)
|
|
7727
|
+
* Space Complexity: O(1)
|
|
7940
7728
|
*/
|
|
7941
7729
|
/**
|
|
7730
|
+
* Time Complexity: O(1)
|
|
7731
|
+
* Space Complexity: O(1)
|
|
7732
|
+
*
|
|
7942
7733
|
* Clear the binary tree, removing all nodes.
|
|
7943
7734
|
*/
|
|
7944
7735
|
clear() {
|
|
@@ -7946,6 +7737,13 @@ var dataStructureTyped = (() => {
|
|
|
7946
7737
|
this._size = 0;
|
|
7947
7738
|
}
|
|
7948
7739
|
/**
|
|
7740
|
+
* Time Complexity: O(1)
|
|
7741
|
+
* Space Complexity: O(1)
|
|
7742
|
+
*/
|
|
7743
|
+
/**
|
|
7744
|
+
* Time Complexity: O(1)
|
|
7745
|
+
* Space Complexity: O(1)
|
|
7746
|
+
*
|
|
7949
7747
|
* Check if the binary tree is empty.
|
|
7950
7748
|
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
7951
7749
|
*/
|
|
@@ -7980,7 +7778,7 @@ var dataStructureTyped = (() => {
|
|
|
7980
7778
|
}
|
|
7981
7779
|
/**
|
|
7982
7780
|
* Time Complexity: O(log n)
|
|
7983
|
-
* Space Complexity: O(
|
|
7781
|
+
* Space Complexity: O(1)
|
|
7984
7782
|
*/
|
|
7985
7783
|
/**
|
|
7986
7784
|
* Time Complexity: O(log n)
|
|
@@ -8071,7 +7869,7 @@ var dataStructureTyped = (() => {
|
|
|
8071
7869
|
* possible values:
|
|
8072
7870
|
* @returns a boolean value.
|
|
8073
7871
|
*/
|
|
8074
|
-
|
|
7872
|
+
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
8075
7873
|
beginRoot = this.ensureNode(beginRoot);
|
|
8076
7874
|
if (!beginRoot)
|
|
8077
7875
|
return true;
|
|
@@ -8084,45 +7882,32 @@ var dataStructureTyped = (() => {
|
|
|
8084
7882
|
return false;
|
|
8085
7883
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
8086
7884
|
};
|
|
8087
|
-
|
|
7885
|
+
const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
7886
|
+
const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
|
|
7887
|
+
return isStandardBST || isInverseBST;
|
|
8088
7888
|
} else {
|
|
8089
|
-
const
|
|
8090
|
-
|
|
8091
|
-
|
|
8092
|
-
|
|
8093
|
-
|
|
8094
|
-
curr
|
|
7889
|
+
const checkBST = (checkMax = false) => {
|
|
7890
|
+
const stack = [];
|
|
7891
|
+
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
7892
|
+
let curr = beginRoot;
|
|
7893
|
+
while (curr || stack.length > 0) {
|
|
7894
|
+
while (curr) {
|
|
7895
|
+
stack.push(curr);
|
|
7896
|
+
curr = curr.left;
|
|
7897
|
+
}
|
|
7898
|
+
curr = stack.pop();
|
|
7899
|
+
const numKey = this.extractor(curr.key);
|
|
7900
|
+
if (!curr || !checkMax && prev >= numKey || checkMax && prev <= numKey)
|
|
7901
|
+
return false;
|
|
7902
|
+
prev = numKey;
|
|
7903
|
+
curr = curr.right;
|
|
8095
7904
|
}
|
|
8096
|
-
|
|
8097
|
-
|
|
8098
|
-
|
|
8099
|
-
|
|
8100
|
-
prev = numKey;
|
|
8101
|
-
curr = curr.right;
|
|
8102
|
-
}
|
|
8103
|
-
return true;
|
|
7905
|
+
return true;
|
|
7906
|
+
};
|
|
7907
|
+
const isStandardBST = checkBST(false), isInverseBST = checkBST(true);
|
|
7908
|
+
return isStandardBST || isInverseBST;
|
|
8104
7909
|
}
|
|
8105
7910
|
}
|
|
8106
|
-
/**
|
|
8107
|
-
* Time Complexity: O(n)
|
|
8108
|
-
* Space Complexity: O(1)
|
|
8109
|
-
*/
|
|
8110
|
-
/**
|
|
8111
|
-
* Time Complexity: O(n)
|
|
8112
|
-
* Space Complexity: O(1)
|
|
8113
|
-
*
|
|
8114
|
-
* The function checks if a binary tree is a binary search tree.
|
|
8115
|
-
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
8116
|
-
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
8117
|
-
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is
|
|
8118
|
-
* expected to be
|
|
8119
|
-
* @returns a boolean value.
|
|
8120
|
-
*/
|
|
8121
|
-
isBST(iterationType = this.iterationType) {
|
|
8122
|
-
if (this.root === null)
|
|
8123
|
-
return true;
|
|
8124
|
-
return this.isSubtreeBST(this.root, iterationType);
|
|
8125
|
-
}
|
|
8126
7911
|
/**
|
|
8127
7912
|
* Time complexity: O(n)
|
|
8128
7913
|
* Space complexity: O(log n)
|
|
@@ -8142,7 +7927,7 @@ var dataStructureTyped = (() => {
|
|
|
8142
7927
|
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
8143
7928
|
* traversal will include null values, otherwise it will skip them.
|
|
8144
7929
|
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
8145
|
-
* the `callback` function on each node in the subtree. The type of the array
|
|
7930
|
+
* the `callback` function on each node in the subtree. The type of the array nodes is determined
|
|
8146
7931
|
* by the return type of the `callback` function.
|
|
8147
7932
|
*/
|
|
8148
7933
|
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
@@ -8182,44 +7967,6 @@ var dataStructureTyped = (() => {
|
|
|
8182
7967
|
}
|
|
8183
7968
|
return ans;
|
|
8184
7969
|
}
|
|
8185
|
-
/**
|
|
8186
|
-
* Time complexity: O(n)
|
|
8187
|
-
* Space complexity: O(log n)
|
|
8188
|
-
*/
|
|
8189
|
-
/**
|
|
8190
|
-
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
8191
|
-
* BinaryTreeNode and its key is not NaN.
|
|
8192
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
8193
|
-
* @returns a boolean value.
|
|
8194
|
-
*/
|
|
8195
|
-
isRealNode(node) {
|
|
8196
|
-
return node instanceof BinaryTreeNode && String(node.key) !== "NaN";
|
|
8197
|
-
}
|
|
8198
|
-
/**
|
|
8199
|
-
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
8200
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
8201
|
-
* @returns a boolean value.
|
|
8202
|
-
*/
|
|
8203
|
-
isNIL(node) {
|
|
8204
|
-
return node instanceof BinaryTreeNode && String(node.key) === "NaN";
|
|
8205
|
-
}
|
|
8206
|
-
/**
|
|
8207
|
-
* The function checks if a given node is a real node or null.
|
|
8208
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
8209
|
-
* @returns a boolean value.
|
|
8210
|
-
*/
|
|
8211
|
-
isNodeOrNull(node) {
|
|
8212
|
-
return this.isRealNode(node) || node === null;
|
|
8213
|
-
}
|
|
8214
|
-
/**
|
|
8215
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
8216
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
8217
|
-
* data type.
|
|
8218
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
8219
|
-
*/
|
|
8220
|
-
isNotNodeInstance(potentialKey) {
|
|
8221
|
-
return !(potentialKey instanceof BinaryTreeNode);
|
|
8222
|
-
}
|
|
8223
7970
|
/**
|
|
8224
7971
|
* Time complexity: O(n)
|
|
8225
7972
|
* Space complexity: O(n)
|
|
@@ -8527,7 +8274,12 @@ var dataStructureTyped = (() => {
|
|
|
8527
8274
|
}
|
|
8528
8275
|
/**
|
|
8529
8276
|
* Time complexity: O(n)
|
|
8530
|
-
* Space complexity: O(
|
|
8277
|
+
* Space complexity: O(n)
|
|
8278
|
+
*/
|
|
8279
|
+
/**
|
|
8280
|
+
* Time complexity: O(n)
|
|
8281
|
+
* Space complexity: O(n)
|
|
8282
|
+
*
|
|
8531
8283
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
8532
8284
|
* algorithm.
|
|
8533
8285
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
@@ -8540,7 +8292,7 @@ var dataStructureTyped = (() => {
|
|
|
8540
8292
|
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
|
|
8541
8293
|
* the root of the tree. If no value is provided, the default value is the root of the tree.
|
|
8542
8294
|
* @returns The function `morris` returns an array of values that are the result of invoking the
|
|
8543
|
-
* `callback` function on each node in the binary tree. The type of the array
|
|
8295
|
+
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
8544
8296
|
* by the return type of the `callback` function.
|
|
8545
8297
|
*/
|
|
8546
8298
|
morris(callback = this._defaultOneParamCallback, pattern = "in", beginRoot = this.root) {
|
|
@@ -8649,8 +8401,8 @@ var dataStructureTyped = (() => {
|
|
|
8649
8401
|
* Time Complexity: O(n)
|
|
8650
8402
|
* Space Complexity: O(n)
|
|
8651
8403
|
*
|
|
8652
|
-
* The `filter` function creates a new tree by iterating over the
|
|
8653
|
-
* adding only the
|
|
8404
|
+
* The `filter` function creates a new tree by iterating over the nodes of the current tree and
|
|
8405
|
+
* adding only the nodes that satisfy the given predicate function.
|
|
8654
8406
|
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
|
|
8655
8407
|
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
|
|
8656
8408
|
* included in the filtered tree or not.
|
|
@@ -8707,6 +8459,13 @@ var dataStructureTyped = (() => {
|
|
|
8707
8459
|
// // }
|
|
8708
8460
|
//
|
|
8709
8461
|
/**
|
|
8462
|
+
* Time Complexity: O(n)
|
|
8463
|
+
* Space Complexity: O(n)
|
|
8464
|
+
*/
|
|
8465
|
+
/**
|
|
8466
|
+
* Time Complexity: O(n)
|
|
8467
|
+
* Space Complexity: O(n)
|
|
8468
|
+
*
|
|
8710
8469
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
8711
8470
|
* @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
|
|
8712
8471
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
@@ -8909,25 +8668,24 @@ var dataStructureTyped = (() => {
|
|
|
8909
8668
|
var BST = class _BST extends BinaryTree {
|
|
8910
8669
|
/**
|
|
8911
8670
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
8912
|
-
* the tree with optional
|
|
8913
|
-
* @param [
|
|
8671
|
+
* the tree with optional keysOrNodesOrEntries and options.
|
|
8672
|
+
* @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
8914
8673
|
* binary search tree.
|
|
8915
8674
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
8916
8675
|
* configuration options for the binary search tree. It can have the following properties:
|
|
8917
8676
|
*/
|
|
8918
|
-
constructor(
|
|
8677
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
8919
8678
|
super([], options);
|
|
8920
8679
|
__publicField(this, "_root");
|
|
8921
|
-
__publicField(this, "_variant", "
|
|
8680
|
+
__publicField(this, "_variant", "STANDARD" /* STANDARD */);
|
|
8922
8681
|
if (options) {
|
|
8923
8682
|
const { variant } = options;
|
|
8924
|
-
if (variant)
|
|
8683
|
+
if (variant)
|
|
8925
8684
|
this._variant = variant;
|
|
8926
|
-
}
|
|
8927
8685
|
}
|
|
8928
8686
|
this._root = void 0;
|
|
8929
|
-
if (
|
|
8930
|
-
this.addMany(
|
|
8687
|
+
if (keysOrNodesOrEntries)
|
|
8688
|
+
this.addMany(keysOrNodesOrEntries);
|
|
8931
8689
|
}
|
|
8932
8690
|
get root() {
|
|
8933
8691
|
return this._root;
|
|
@@ -8960,48 +8718,88 @@ var dataStructureTyped = (() => {
|
|
|
8960
8718
|
}, options));
|
|
8961
8719
|
}
|
|
8962
8720
|
/**
|
|
8963
|
-
* The function
|
|
8964
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
8965
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
8966
|
-
*/
|
|
8967
|
-
isNode(exemplar) {
|
|
8968
|
-
return exemplar instanceof BSTNode;
|
|
8969
|
-
}
|
|
8970
|
-
/**
|
|
8971
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
8721
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
8972
8722
|
* otherwise it returns undefined.
|
|
8973
|
-
* @param
|
|
8723
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
8974
8724
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
8975
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
8725
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
8976
8726
|
* @returns a node of type N or undefined.
|
|
8977
8727
|
*/
|
|
8978
|
-
exemplarToNode(
|
|
8728
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
8979
8729
|
let node;
|
|
8980
|
-
if (
|
|
8730
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
8981
8731
|
return;
|
|
8982
|
-
} else if (this.isNode(
|
|
8983
|
-
node =
|
|
8984
|
-
} else if (this.isEntry(
|
|
8985
|
-
const [key, value2] =
|
|
8732
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
8733
|
+
node = keyOrNodeOrEntry;
|
|
8734
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
8735
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
8986
8736
|
if (key === void 0 || key === null) {
|
|
8987
8737
|
return;
|
|
8988
8738
|
} else {
|
|
8989
8739
|
node = this.createNode(key, value2);
|
|
8990
8740
|
}
|
|
8991
|
-
} else if (this.isNotNodeInstance(
|
|
8992
|
-
node = this.createNode(
|
|
8741
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
8742
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
8993
8743
|
} else {
|
|
8994
8744
|
return;
|
|
8995
8745
|
}
|
|
8996
8746
|
return node;
|
|
8997
8747
|
}
|
|
8998
8748
|
/**
|
|
8999
|
-
* Time Complexity: O(log n)
|
|
9000
|
-
* Space Complexity: O(
|
|
8749
|
+
* Time Complexity: O(log n)
|
|
8750
|
+
* Space Complexity: O(log n)
|
|
8751
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
9001
8752
|
*/
|
|
9002
8753
|
/**
|
|
9003
|
-
* Time Complexity: O(log n)
|
|
9004
|
-
* Space Complexity: O(
|
|
8754
|
+
* Time Complexity: O(log n)
|
|
8755
|
+
* Space Complexity: O(log n)
|
|
8756
|
+
*
|
|
8757
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
8758
|
+
* otherwise it returns the key itself.
|
|
8759
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
8760
|
+
* `undefined`.
|
|
8761
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
8762
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
8763
|
+
* @returns either a node object (N) or undefined.
|
|
8764
|
+
*/
|
|
8765
|
+
ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE" /* ITERATIVE */) {
|
|
8766
|
+
let res;
|
|
8767
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
8768
|
+
res = keyOrNodeOrEntry;
|
|
8769
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
8770
|
+
if (keyOrNodeOrEntry[0])
|
|
8771
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
8772
|
+
} else {
|
|
8773
|
+
if (keyOrNodeOrEntry)
|
|
8774
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
8775
|
+
}
|
|
8776
|
+
return res;
|
|
8777
|
+
}
|
|
8778
|
+
/**
|
|
8779
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
8780
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
8781
|
+
* data type.
|
|
8782
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
8783
|
+
*/
|
|
8784
|
+
isNotNodeInstance(potentialKey) {
|
|
8785
|
+
return !(potentialKey instanceof BSTNode);
|
|
8786
|
+
}
|
|
8787
|
+
/**
|
|
8788
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
8789
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
8790
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
8791
|
+
*/
|
|
8792
|
+
isNode(keyOrNodeOrEntry) {
|
|
8793
|
+
return keyOrNodeOrEntry instanceof BSTNode;
|
|
8794
|
+
}
|
|
8795
|
+
/**
|
|
8796
|
+
* Time Complexity: O(log n)
|
|
8797
|
+
* Space Complexity: O(1)
|
|
8798
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
8799
|
+
*/
|
|
8800
|
+
/**
|
|
8801
|
+
* Time Complexity: O(log n)
|
|
8802
|
+
* Space Complexity: O(1)
|
|
9005
8803
|
*
|
|
9006
8804
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
9007
8805
|
* or inserting a new node if the key is unique.
|
|
@@ -9014,23 +8812,23 @@ var dataStructureTyped = (() => {
|
|
|
9014
8812
|
add(keyOrNodeOrEntry, value) {
|
|
9015
8813
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
9016
8814
|
if (newNode === void 0)
|
|
9017
|
-
return;
|
|
8815
|
+
return false;
|
|
9018
8816
|
if (this.root === void 0) {
|
|
9019
8817
|
this._setRoot(newNode);
|
|
9020
8818
|
this._size++;
|
|
9021
|
-
return
|
|
8819
|
+
return true;
|
|
9022
8820
|
}
|
|
9023
8821
|
let current = this.root;
|
|
9024
8822
|
while (current !== void 0) {
|
|
9025
8823
|
if (this._compare(current.key, newNode.key) === "eq" /* eq */) {
|
|
9026
8824
|
this._replaceNode(current, newNode);
|
|
9027
|
-
return
|
|
8825
|
+
return true;
|
|
9028
8826
|
} else if (this._compare(current.key, newNode.key) === "gt" /* gt */) {
|
|
9029
8827
|
if (current.left === void 0) {
|
|
9030
8828
|
current.left = newNode;
|
|
9031
8829
|
newNode.parent = current;
|
|
9032
8830
|
this._size++;
|
|
9033
|
-
return
|
|
8831
|
+
return true;
|
|
9034
8832
|
}
|
|
9035
8833
|
current = current.left;
|
|
9036
8834
|
} else {
|
|
@@ -9038,20 +8836,21 @@ var dataStructureTyped = (() => {
|
|
|
9038
8836
|
current.right = newNode;
|
|
9039
8837
|
newNode.parent = current;
|
|
9040
8838
|
this._size++;
|
|
9041
|
-
return
|
|
8839
|
+
return true;
|
|
9042
8840
|
}
|
|
9043
8841
|
current = current.right;
|
|
9044
8842
|
}
|
|
9045
8843
|
}
|
|
9046
|
-
return
|
|
8844
|
+
return false;
|
|
9047
8845
|
}
|
|
9048
8846
|
/**
|
|
9049
|
-
* Time Complexity: O(k log n)
|
|
9050
|
-
* Space Complexity: O(k)
|
|
8847
|
+
* Time Complexity: O(k log n)
|
|
8848
|
+
* Space Complexity: O(k)
|
|
8849
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
9051
8850
|
*/
|
|
9052
8851
|
/**
|
|
9053
|
-
* Time Complexity: O(k log n)
|
|
9054
|
-
* Space Complexity: O(k)
|
|
8852
|
+
* Time Complexity: O(k log n)
|
|
8853
|
+
* Space Complexity: O(k)
|
|
9055
8854
|
*
|
|
9056
8855
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
9057
8856
|
* balancing the tree after each addition.
|
|
@@ -9062,7 +8861,7 @@ var dataStructureTyped = (() => {
|
|
|
9062
8861
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
9063
8862
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
9064
8863
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
9065
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
8864
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
9066
8865
|
* in the order they appear in the input.
|
|
9067
8866
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
9068
8867
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
@@ -9143,20 +8942,19 @@ var dataStructureTyped = (() => {
|
|
|
9143
8942
|
return inserted;
|
|
9144
8943
|
}
|
|
9145
8944
|
/**
|
|
9146
|
-
* Time Complexity: O(n log n)
|
|
9147
|
-
* Space Complexity: O(n)
|
|
8945
|
+
* Time Complexity: O(n log n)
|
|
8946
|
+
* Space Complexity: O(n)
|
|
8947
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
9148
8948
|
*/
|
|
9149
8949
|
/**
|
|
9150
|
-
* Time Complexity: O(log n)
|
|
9151
|
-
* Space Complexity: O(
|
|
8950
|
+
* Time Complexity: O(n log n)
|
|
8951
|
+
* Space Complexity: O(n)
|
|
9152
8952
|
*
|
|
9153
8953
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
9154
8954
|
* leftmost node if the comparison result is greater than.
|
|
9155
8955
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
9156
8956
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
9157
8957
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
9158
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
9159
|
-
* be performed. It can have one of the following values:
|
|
9160
8958
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
9161
8959
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
9162
8960
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
@@ -9165,7 +8963,7 @@ var dataStructureTyped = (() => {
|
|
|
9165
8963
|
let current = this.ensureNode(beginRoot);
|
|
9166
8964
|
if (!current)
|
|
9167
8965
|
return void 0;
|
|
9168
|
-
if (this._variant === "
|
|
8966
|
+
if (this._variant === "STANDARD" /* STANDARD */) {
|
|
9169
8967
|
while (current.right !== void 0) {
|
|
9170
8968
|
current = current.right;
|
|
9171
8969
|
}
|
|
@@ -9177,12 +8975,12 @@ var dataStructureTyped = (() => {
|
|
|
9177
8975
|
return current.key;
|
|
9178
8976
|
}
|
|
9179
8977
|
/**
|
|
9180
|
-
* Time Complexity: O(log n)
|
|
9181
|
-
* Space Complexity: O(1)
|
|
8978
|
+
* Time Complexity: O(log n)
|
|
8979
|
+
* Space Complexity: O(1)
|
|
9182
8980
|
*/
|
|
9183
8981
|
/**
|
|
9184
|
-
* Time Complexity: O(log n)
|
|
9185
|
-
* Space Complexity: O(
|
|
8982
|
+
* Time Complexity: O(log n)
|
|
8983
|
+
* Space Complexity: O(1)
|
|
9186
8984
|
*
|
|
9187
8985
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
9188
8986
|
* either recursive or iterative methods.
|
|
@@ -9225,53 +9023,34 @@ var dataStructureTyped = (() => {
|
|
|
9225
9023
|
}
|
|
9226
9024
|
}
|
|
9227
9025
|
/**
|
|
9228
|
-
|
|
9229
|
-
|
|
9230
|
-
|
|
9231
|
-
|
|
9232
|
-
|
|
9233
|
-
|
|
9234
|
-
|
|
9235
|
-
|
|
9236
|
-
|
|
9237
|
-
|
|
9238
|
-
|
|
9239
|
-
|
|
9240
|
-
|
|
9241
|
-
|
|
9242
|
-
|
|
9243
|
-
|
|
9244
|
-
|
|
9245
|
-
|
|
9246
|
-
|
|
9247
|
-
|
|
9248
|
-
|
|
9249
|
-
|
|
9250
|
-
|
|
9251
|
-
|
|
9252
|
-
|
|
9253
|
-
|
|
9254
|
-
|
|
9255
|
-
|
|
9256
|
-
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
9257
|
-
* recursive or iterative approach.
|
|
9258
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
9259
|
-
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
9260
|
-
* callback function `C`.
|
|
9261
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
|
|
9262
|
-
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
9263
|
-
* function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
|
|
9264
|
-
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
9265
|
-
* first node that matches the identifier. If set to true, the function will return an array
|
|
9266
|
-
* containing only the first matching node. If set to false (default), the function will continue
|
|
9267
|
-
* searching for all nodes that match the identifier and return an array containing
|
|
9268
|
-
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
9269
|
-
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
9270
|
-
* traversal will start from the root of the tree.
|
|
9271
|
-
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
9272
|
-
* performed on the binary tree. It can have two possible values:
|
|
9273
|
-
* @returns The method returns an array of nodes (`N[]`).
|
|
9274
|
-
*/
|
|
9026
|
+
* Time Complexity: O(log n)
|
|
9027
|
+
* Space Complexity: O(log n)
|
|
9028
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
9029
|
+
* /
|
|
9030
|
+
|
|
9031
|
+
/**
|
|
9032
|
+
* Time Complexity: O(log n)
|
|
9033
|
+
* Space Complexity: O(log n)
|
|
9034
|
+
*
|
|
9035
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
9036
|
+
* recursive or iterative approach.
|
|
9037
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
9038
|
+
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
9039
|
+
* callback function `C`.
|
|
9040
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
|
|
9041
|
+
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
9042
|
+
* function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
|
|
9043
|
+
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
9044
|
+
* first node that matches the identifier. If set to true, the function will return an array
|
|
9045
|
+
* containing only the first matching node. If set to false (default), the function will continue
|
|
9046
|
+
* searching for all nodes that match the identifier and return an array containing
|
|
9047
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
9048
|
+
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
9049
|
+
* traversal will start from the root of the tree.
|
|
9050
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
9051
|
+
* performed on the binary tree. It can have two possible values:
|
|
9052
|
+
* @returns The method returns an array of nodes (`N[]`).
|
|
9053
|
+
*/
|
|
9275
9054
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9276
9055
|
beginRoot = this.ensureNode(beginRoot);
|
|
9277
9056
|
if (!beginRoot)
|
|
@@ -9324,12 +9103,13 @@ var dataStructureTyped = (() => {
|
|
|
9324
9103
|
return ans;
|
|
9325
9104
|
}
|
|
9326
9105
|
/**
|
|
9327
|
-
* Time Complexity: O(log n)
|
|
9328
|
-
* Space Complexity: O(log n)
|
|
9106
|
+
* Time Complexity: O(log n)
|
|
9107
|
+
* Space Complexity: O(log n)
|
|
9108
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
9329
9109
|
*/
|
|
9330
9110
|
/**
|
|
9331
|
-
* Time Complexity: O(log n)
|
|
9332
|
-
* Space Complexity: O(log n)
|
|
9111
|
+
* Time Complexity: O(log n)
|
|
9112
|
+
* Space Complexity: O(log n)
|
|
9333
9113
|
*
|
|
9334
9114
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
9335
9115
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -9388,12 +9168,12 @@ var dataStructureTyped = (() => {
|
|
|
9388
9168
|
}
|
|
9389
9169
|
}
|
|
9390
9170
|
/**
|
|
9391
|
-
* Time Complexity: O(log n)
|
|
9392
|
-
* Space Complexity: O(log n)
|
|
9171
|
+
* Time Complexity: O(log n)
|
|
9172
|
+
* Space Complexity: O(log n)
|
|
9393
9173
|
*/
|
|
9394
9174
|
/**
|
|
9395
|
-
* Time Complexity: O(n)
|
|
9396
|
-
* Space Complexity: O(n)
|
|
9175
|
+
* Time Complexity: O(log n)
|
|
9176
|
+
* Space Complexity: O(log n)
|
|
9397
9177
|
*
|
|
9398
9178
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
9399
9179
|
* ensures the tree is perfectly balanced.
|
|
@@ -9451,8 +9231,8 @@ var dataStructureTyped = (() => {
|
|
|
9451
9231
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
9452
9232
|
*/
|
|
9453
9233
|
/**
|
|
9454
|
-
* Time Complexity: O(n)
|
|
9455
|
-
* Space Complexity: O(log n)
|
|
9234
|
+
* Time Complexity: O(n)
|
|
9235
|
+
* Space Complexity: O(log n)
|
|
9456
9236
|
*
|
|
9457
9237
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
9458
9238
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -9519,7 +9299,7 @@ var dataStructureTyped = (() => {
|
|
|
9519
9299
|
_compare(a, b) {
|
|
9520
9300
|
const extractedA = this.extractor(a);
|
|
9521
9301
|
const extractedB = this.extractor(b);
|
|
9522
|
-
const compared = this.variant === "
|
|
9302
|
+
const compared = this.variant === "STANDARD" /* STANDARD */ ? extractedA - extractedB : extractedB - extractedA;
|
|
9523
9303
|
return compared > 0 ? "gt" /* gt */ : compared < 0 ? "lt" /* lt */ : "eq" /* eq */;
|
|
9524
9304
|
}
|
|
9525
9305
|
};
|
|
@@ -9960,18 +9740,18 @@ var dataStructureTyped = (() => {
|
|
|
9960
9740
|
};
|
|
9961
9741
|
var AVLTree = class _AVLTree extends BST {
|
|
9962
9742
|
/**
|
|
9963
|
-
* The constructor function initializes an AVLTree object with optional
|
|
9964
|
-
* @param [
|
|
9965
|
-
* objects. It represents a collection of
|
|
9743
|
+
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
|
|
9744
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
9745
|
+
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
9966
9746
|
* initialization.
|
|
9967
9747
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
9968
9748
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
9969
9749
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
9970
9750
|
*/
|
|
9971
|
-
constructor(
|
|
9751
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
9972
9752
|
super([], options);
|
|
9973
|
-
if (
|
|
9974
|
-
super.addMany(
|
|
9753
|
+
if (keysOrNodesOrEntries)
|
|
9754
|
+
super.addMany(keysOrNodesOrEntries);
|
|
9975
9755
|
}
|
|
9976
9756
|
/**
|
|
9977
9757
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -9999,12 +9779,12 @@ var dataStructureTyped = (() => {
|
|
|
9999
9779
|
}, options));
|
|
10000
9780
|
}
|
|
10001
9781
|
/**
|
|
10002
|
-
* The function checks if an
|
|
10003
|
-
* @param
|
|
10004
|
-
* @returns a boolean value indicating whether the
|
|
9782
|
+
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
9783
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
9784
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
10005
9785
|
*/
|
|
10006
|
-
isNode(
|
|
10007
|
-
return
|
|
9786
|
+
isNode(keyOrNodeOrEntry) {
|
|
9787
|
+
return keyOrNodeOrEntry instanceof AVLTreeNode;
|
|
10008
9788
|
}
|
|
10009
9789
|
/**
|
|
10010
9790
|
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
@@ -10016,12 +9796,13 @@ var dataStructureTyped = (() => {
|
|
|
10016
9796
|
return !(potentialKey instanceof AVLTreeNode);
|
|
10017
9797
|
}
|
|
10018
9798
|
/**
|
|
10019
|
-
* Time Complexity: O(log n)
|
|
10020
|
-
* Space Complexity: O(1)
|
|
9799
|
+
* Time Complexity: O(log n)
|
|
9800
|
+
* Space Complexity: O(1)
|
|
9801
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10021
9802
|
*/
|
|
10022
9803
|
/**
|
|
10023
|
-
* Time Complexity: O(log n)
|
|
10024
|
-
* Space Complexity: O(1)
|
|
9804
|
+
* Time Complexity: O(log n)
|
|
9805
|
+
* Space Complexity: O(1)
|
|
10025
9806
|
*
|
|
10026
9807
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
10027
9808
|
* a new node.
|
|
@@ -10033,19 +9814,19 @@ var dataStructureTyped = (() => {
|
|
|
10033
9814
|
*/
|
|
10034
9815
|
add(keyOrNodeOrEntry, value) {
|
|
10035
9816
|
if (keyOrNodeOrEntry === null)
|
|
10036
|
-
return
|
|
9817
|
+
return false;
|
|
10037
9818
|
const inserted = super.add(keyOrNodeOrEntry, value);
|
|
10038
9819
|
if (inserted)
|
|
10039
|
-
this._balancePath(
|
|
9820
|
+
this._balancePath(keyOrNodeOrEntry);
|
|
10040
9821
|
return inserted;
|
|
10041
9822
|
}
|
|
10042
9823
|
/**
|
|
10043
|
-
* Time Complexity: O(log n)
|
|
10044
|
-
* Space Complexity: O(1)
|
|
9824
|
+
* Time Complexity: O(log n)
|
|
9825
|
+
* Space Complexity: O(1)
|
|
10045
9826
|
*/
|
|
10046
9827
|
/**
|
|
10047
|
-
* Time Complexity: O(log n)
|
|
10048
|
-
* Space Complexity: O(1)
|
|
9828
|
+
* Time Complexity: O(log n)
|
|
9829
|
+
* Space Complexity: O(1)
|
|
10049
9830
|
*
|
|
10050
9831
|
* The function overrides the delete method of a binary tree, performs the deletion, and then
|
|
10051
9832
|
* balances the tree if necessary.
|
|
@@ -10099,12 +9880,13 @@ var dataStructureTyped = (() => {
|
|
|
10099
9880
|
return void 0;
|
|
10100
9881
|
}
|
|
10101
9882
|
/**
|
|
10102
|
-
* Time Complexity: O(1)
|
|
10103
|
-
* Space Complexity: O(1)
|
|
9883
|
+
* Time Complexity: O(1)
|
|
9884
|
+
* Space Complexity: O(1)
|
|
9885
|
+
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
10104
9886
|
*/
|
|
10105
9887
|
/**
|
|
10106
|
-
* Time Complexity: O(1)
|
|
10107
|
-
* Space Complexity: O(1)
|
|
9888
|
+
* Time Complexity: O(1)
|
|
9889
|
+
* Space Complexity: O(1)
|
|
10108
9890
|
*
|
|
10109
9891
|
* The function calculates the balance factor of a node in a binary tree.
|
|
10110
9892
|
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
|
|
@@ -10120,12 +9902,13 @@ var dataStructureTyped = (() => {
|
|
|
10120
9902
|
return node.right.height - node.left.height;
|
|
10121
9903
|
}
|
|
10122
9904
|
/**
|
|
10123
|
-
* Time Complexity: O(1)
|
|
10124
|
-
* Space Complexity: O(1)
|
|
9905
|
+
* Time Complexity: O(1)
|
|
9906
|
+
* Space Complexity: O(1)
|
|
9907
|
+
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
10125
9908
|
*/
|
|
10126
9909
|
/**
|
|
10127
|
-
* Time Complexity: O(1)
|
|
10128
|
-
* Space Complexity: O(1)
|
|
9910
|
+
* Time Complexity: O(1)
|
|
9911
|
+
* Space Complexity: O(1)
|
|
10129
9912
|
*
|
|
10130
9913
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
10131
9914
|
* right children.
|
|
@@ -10143,12 +9926,13 @@ var dataStructureTyped = (() => {
|
|
|
10143
9926
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
10144
9927
|
}
|
|
10145
9928
|
/**
|
|
10146
|
-
* Time Complexity: O(log n)
|
|
10147
|
-
* Space Complexity: O(1)
|
|
9929
|
+
* Time Complexity: O(log n)
|
|
9930
|
+
* Space Complexity: O(1)
|
|
9931
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10148
9932
|
*/
|
|
10149
9933
|
/**
|
|
10150
|
-
* Time Complexity: O(log n)
|
|
10151
|
-
* Space Complexity: O(1)
|
|
9934
|
+
* Time Complexity: O(log n)
|
|
9935
|
+
* Space Complexity: O(1)
|
|
10152
9936
|
*
|
|
10153
9937
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
10154
9938
|
* to restore balance in an AVL tree after inserting a node.
|
|
@@ -10156,6 +9940,7 @@ var dataStructureTyped = (() => {
|
|
|
10156
9940
|
* AVL tree that needs to be balanced.
|
|
10157
9941
|
*/
|
|
10158
9942
|
_balancePath(node) {
|
|
9943
|
+
node = this.ensureNode(node);
|
|
10159
9944
|
const path = this.getPathToRoot(node, false);
|
|
10160
9945
|
for (let i = 0; i < path.length; i++) {
|
|
10161
9946
|
const A = path[i];
|
|
@@ -10182,12 +9967,13 @@ var dataStructureTyped = (() => {
|
|
|
10182
9967
|
}
|
|
10183
9968
|
}
|
|
10184
9969
|
/**
|
|
10185
|
-
* Time Complexity: O(1)
|
|
10186
|
-
* Space Complexity: O(1)
|
|
9970
|
+
* Time Complexity: O(1)
|
|
9971
|
+
* Space Complexity: O(1)
|
|
9972
|
+
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
10187
9973
|
*/
|
|
10188
9974
|
/**
|
|
10189
|
-
* Time Complexity: O(1)
|
|
10190
|
-
* Space Complexity: O(1)
|
|
9975
|
+
* Time Complexity: O(1)
|
|
9976
|
+
* Space Complexity: O(1)
|
|
10191
9977
|
*
|
|
10192
9978
|
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
10193
9979
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10221,12 +10007,12 @@ var dataStructureTyped = (() => {
|
|
|
10221
10007
|
this._updateHeight(B);
|
|
10222
10008
|
}
|
|
10223
10009
|
/**
|
|
10224
|
-
* Time Complexity: O(1)
|
|
10225
|
-
* Space Complexity: O(1)
|
|
10010
|
+
* Time Complexity: O(1)
|
|
10011
|
+
* Space Complexity: O(1)
|
|
10226
10012
|
*/
|
|
10227
10013
|
/**
|
|
10228
|
-
* Time Complexity: O(1)
|
|
10229
|
-
* Space Complexity: O(1)
|
|
10014
|
+
* Time Complexity: O(1)
|
|
10015
|
+
* Space Complexity: O(1)
|
|
10230
10016
|
*
|
|
10231
10017
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
10232
10018
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10275,12 +10061,12 @@ var dataStructureTyped = (() => {
|
|
|
10275
10061
|
C && this._updateHeight(C);
|
|
10276
10062
|
}
|
|
10277
10063
|
/**
|
|
10278
|
-
* Time Complexity: O(1)
|
|
10279
|
-
* Space Complexity: O(1)
|
|
10064
|
+
* Time Complexity: O(1)
|
|
10065
|
+
* Space Complexity: O(1)
|
|
10280
10066
|
*/
|
|
10281
10067
|
/**
|
|
10282
|
-
* Time Complexity: O(1)
|
|
10283
|
-
* Space Complexity: O(1)
|
|
10068
|
+
* Time Complexity: O(1)
|
|
10069
|
+
* Space Complexity: O(1)
|
|
10284
10070
|
*
|
|
10285
10071
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
10286
10072
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10315,12 +10101,12 @@ var dataStructureTyped = (() => {
|
|
|
10315
10101
|
B && this._updateHeight(B);
|
|
10316
10102
|
}
|
|
10317
10103
|
/**
|
|
10318
|
-
* Time Complexity: O(1)
|
|
10319
|
-
* Space Complexity: O(1)
|
|
10104
|
+
* Time Complexity: O(1)
|
|
10105
|
+
* Space Complexity: O(1)
|
|
10320
10106
|
*/
|
|
10321
10107
|
/**
|
|
10322
|
-
* Time Complexity: O(1)
|
|
10323
|
-
* Space Complexity: O(1)
|
|
10108
|
+
* Time Complexity: O(1)
|
|
10109
|
+
* Space Complexity: O(1)
|
|
10324
10110
|
*
|
|
10325
10111
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
10326
10112
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -10385,23 +10171,23 @@ var dataStructureTyped = (() => {
|
|
|
10385
10171
|
var RedBlackTree = class _RedBlackTree extends BST {
|
|
10386
10172
|
/**
|
|
10387
10173
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
10388
|
-
* initializes the tree with optional
|
|
10389
|
-
* @param [
|
|
10390
|
-
* objects. It represents the initial
|
|
10174
|
+
* initializes the tree with optional nodes and options.
|
|
10175
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
10176
|
+
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
10391
10177
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
10392
|
-
*
|
|
10178
|
+
* nodes to the
|
|
10393
10179
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
10394
10180
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
10395
10181
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
10396
10182
|
*/
|
|
10397
|
-
constructor(
|
|
10183
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
10398
10184
|
super([], options);
|
|
10399
10185
|
__publicField(this, "Sentinel", new RedBlackTreeNode(NaN));
|
|
10400
10186
|
__publicField(this, "_root");
|
|
10401
10187
|
__publicField(this, "_size", 0);
|
|
10402
10188
|
this._root = this.Sentinel;
|
|
10403
|
-
if (
|
|
10404
|
-
super.addMany(
|
|
10189
|
+
if (keysOrNodesOrEntries)
|
|
10190
|
+
super.addMany(keysOrNodesOrEntries);
|
|
10405
10191
|
}
|
|
10406
10192
|
get root() {
|
|
10407
10193
|
return this._root;
|
|
@@ -10438,57 +10224,67 @@ var dataStructureTyped = (() => {
|
|
|
10438
10224
|
}, options));
|
|
10439
10225
|
}
|
|
10440
10226
|
/**
|
|
10441
|
-
* The function
|
|
10442
|
-
* @param
|
|
10443
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
10444
|
-
* class.
|
|
10445
|
-
*/
|
|
10446
|
-
isNode(exemplar) {
|
|
10447
|
-
return exemplar instanceof RedBlackTreeNode;
|
|
10448
|
-
}
|
|
10449
|
-
/**
|
|
10450
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10451
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10452
|
-
* data type.
|
|
10453
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10454
|
-
*/
|
|
10455
|
-
isNotNodeInstance(potentialKey) {
|
|
10456
|
-
return !(potentialKey instanceof RedBlackTreeNode);
|
|
10457
|
-
}
|
|
10458
|
-
/**
|
|
10459
|
-
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
10460
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
10227
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
10228
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
10461
10229
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
10462
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
10230
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
10463
10231
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
10464
10232
|
* @returns a node of type N or undefined.
|
|
10465
10233
|
*/
|
|
10466
|
-
exemplarToNode(
|
|
10234
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
10467
10235
|
let node;
|
|
10468
|
-
if (
|
|
10236
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0) {
|
|
10469
10237
|
return;
|
|
10470
|
-
} else if (this.isNode(
|
|
10471
|
-
node =
|
|
10472
|
-
} else if (this.isEntry(
|
|
10473
|
-
const [key, value2] =
|
|
10238
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
10239
|
+
node = keyOrNodeOrEntry;
|
|
10240
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
10241
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
10474
10242
|
if (key === void 0 || key === null) {
|
|
10475
10243
|
return;
|
|
10476
10244
|
} else {
|
|
10477
10245
|
node = this.createNode(key, value2, 1 /* RED */);
|
|
10478
10246
|
}
|
|
10479
|
-
} else if (this.isNotNodeInstance(
|
|
10480
|
-
node = this.createNode(
|
|
10247
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
10248
|
+
node = this.createNode(keyOrNodeOrEntry, value, 1 /* RED */);
|
|
10481
10249
|
} else {
|
|
10482
10250
|
return;
|
|
10483
10251
|
}
|
|
10484
10252
|
return node;
|
|
10485
10253
|
}
|
|
10254
|
+
/**
|
|
10255
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
10256
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
10257
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
10258
|
+
* class.
|
|
10259
|
+
*/
|
|
10260
|
+
isNode(keyOrNodeOrEntry) {
|
|
10261
|
+
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
10262
|
+
}
|
|
10486
10263
|
/**
|
|
10487
10264
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10488
10265
|
* Space Complexity: O(1)
|
|
10489
10266
|
*/
|
|
10267
|
+
isRealNode(node) {
|
|
10268
|
+
if (node === this.Sentinel || node === void 0)
|
|
10269
|
+
return false;
|
|
10270
|
+
return node instanceof RedBlackTreeNode;
|
|
10271
|
+
}
|
|
10490
10272
|
/**
|
|
10491
|
-
*
|
|
10273
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10274
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10275
|
+
* data type.
|
|
10276
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10277
|
+
*/
|
|
10278
|
+
isNotNodeInstance(potentialKey) {
|
|
10279
|
+
return !(potentialKey instanceof RedBlackTreeNode);
|
|
10280
|
+
}
|
|
10281
|
+
/**
|
|
10282
|
+
* Time Complexity: O(log n)
|
|
10283
|
+
* Space Complexity: O(1)
|
|
10284
|
+
* on average (where n is the number of nodes in the tree)
|
|
10285
|
+
*/
|
|
10286
|
+
/**
|
|
10287
|
+
* Time Complexity: O(log n)
|
|
10492
10288
|
* Space Complexity: O(1)
|
|
10493
10289
|
*
|
|
10494
10290
|
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
@@ -10502,7 +10298,7 @@ var dataStructureTyped = (() => {
|
|
|
10502
10298
|
add(keyOrNodeOrEntry, value) {
|
|
10503
10299
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
10504
10300
|
if (newNode === void 0)
|
|
10505
|
-
return;
|
|
10301
|
+
return false;
|
|
10506
10302
|
newNode.left = this.Sentinel;
|
|
10507
10303
|
newNode.right = this.Sentinel;
|
|
10508
10304
|
let y = void 0;
|
|
@@ -10518,7 +10314,7 @@ var dataStructureTyped = (() => {
|
|
|
10518
10314
|
if (newNode !== x) {
|
|
10519
10315
|
this._replaceNode(x, newNode);
|
|
10520
10316
|
}
|
|
10521
|
-
return;
|
|
10317
|
+
return false;
|
|
10522
10318
|
}
|
|
10523
10319
|
}
|
|
10524
10320
|
}
|
|
@@ -10533,21 +10329,23 @@ var dataStructureTyped = (() => {
|
|
|
10533
10329
|
if (newNode.parent === void 0) {
|
|
10534
10330
|
newNode.color = 0 /* BLACK */;
|
|
10535
10331
|
this._size++;
|
|
10536
|
-
return;
|
|
10332
|
+
return false;
|
|
10537
10333
|
}
|
|
10538
10334
|
if (newNode.parent.parent === void 0) {
|
|
10539
10335
|
this._size++;
|
|
10540
|
-
return;
|
|
10336
|
+
return false;
|
|
10541
10337
|
}
|
|
10542
10338
|
this._fixInsert(newNode);
|
|
10543
10339
|
this._size++;
|
|
10340
|
+
return true;
|
|
10544
10341
|
}
|
|
10545
10342
|
/**
|
|
10546
|
-
* Time Complexity: O(log n)
|
|
10343
|
+
* Time Complexity: O(log n)
|
|
10547
10344
|
* Space Complexity: O(1)
|
|
10345
|
+
* on average (where n is the number of nodes in the tree)
|
|
10548
10346
|
*/
|
|
10549
10347
|
/**
|
|
10550
|
-
* Time Complexity: O(log n)
|
|
10348
|
+
* Time Complexity: O(log n)
|
|
10551
10349
|
* Space Complexity: O(1)
|
|
10552
10350
|
*
|
|
10553
10351
|
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
@@ -10615,20 +10413,11 @@ var dataStructureTyped = (() => {
|
|
|
10615
10413
|
return ans;
|
|
10616
10414
|
}
|
|
10617
10415
|
/**
|
|
10618
|
-
* Time Complexity: O(log n)
|
|
10619
|
-
* Space Complexity: O(1)
|
|
10620
|
-
*/
|
|
10621
|
-
isRealNode(node) {
|
|
10622
|
-
if (node === this.Sentinel || node === void 0)
|
|
10623
|
-
return false;
|
|
10624
|
-
return node instanceof RedBlackTreeNode;
|
|
10625
|
-
}
|
|
10626
|
-
/**
|
|
10627
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
10416
|
+
* Time Complexity: O(log n)
|
|
10628
10417
|
* Space Complexity: O(1)
|
|
10629
10418
|
*/
|
|
10630
10419
|
/**
|
|
10631
|
-
* Time Complexity: O(log n)
|
|
10420
|
+
* Time Complexity: O(log n)
|
|
10632
10421
|
* Space Complexity: O(1)
|
|
10633
10422
|
*
|
|
10634
10423
|
* The function `getNode` retrieves a single node from a binary tree based on a given identifier and
|
|
@@ -10680,7 +10469,7 @@ var dataStructureTyped = (() => {
|
|
|
10680
10469
|
return y;
|
|
10681
10470
|
}
|
|
10682
10471
|
/**
|
|
10683
|
-
* Time Complexity: O(
|
|
10472
|
+
* Time Complexity: O(1)
|
|
10684
10473
|
* Space Complexity: O(1)
|
|
10685
10474
|
*/
|
|
10686
10475
|
clear() {
|
|
@@ -10757,11 +10546,11 @@ var dataStructureTyped = (() => {
|
|
|
10757
10546
|
}
|
|
10758
10547
|
}
|
|
10759
10548
|
/**
|
|
10760
|
-
* Time Complexity: O(log n)
|
|
10549
|
+
* Time Complexity: O(log n)
|
|
10761
10550
|
* Space Complexity: O(1)
|
|
10762
10551
|
*/
|
|
10763
10552
|
/**
|
|
10764
|
-
* Time Complexity: O(log n)
|
|
10553
|
+
* Time Complexity: O(log n)
|
|
10765
10554
|
* Space Complexity: O(1)
|
|
10766
10555
|
*
|
|
10767
10556
|
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
@@ -10851,11 +10640,11 @@ var dataStructureTyped = (() => {
|
|
|
10851
10640
|
v.parent = u.parent;
|
|
10852
10641
|
}
|
|
10853
10642
|
/**
|
|
10854
|
-
* Time Complexity: O(log n)
|
|
10643
|
+
* Time Complexity: O(log n)
|
|
10855
10644
|
* Space Complexity: O(1)
|
|
10856
10645
|
*/
|
|
10857
10646
|
/**
|
|
10858
|
-
* Time Complexity: O(log n)
|
|
10647
|
+
* Time Complexity: O(log n)
|
|
10859
10648
|
* Space Complexity: O(1)
|
|
10860
10649
|
*
|
|
10861
10650
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
@@ -10938,11 +10727,11 @@ var dataStructureTyped = (() => {
|
|
|
10938
10727
|
}
|
|
10939
10728
|
};
|
|
10940
10729
|
var TreeMultimap = class _TreeMultimap extends AVLTree {
|
|
10941
|
-
constructor(
|
|
10730
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
10942
10731
|
super([], options);
|
|
10943
10732
|
__publicField(this, "_count", 0);
|
|
10944
|
-
if (
|
|
10945
|
-
this.addMany(
|
|
10733
|
+
if (keysOrNodesOrEntries)
|
|
10734
|
+
this.addMany(keysOrNodesOrEntries);
|
|
10946
10735
|
}
|
|
10947
10736
|
// TODO the _count is not accurate after nodes count modified
|
|
10948
10737
|
get count() {
|
|
@@ -10969,26 +10758,8 @@ var dataStructureTyped = (() => {
|
|
|
10969
10758
|
}, options));
|
|
10970
10759
|
}
|
|
10971
10760
|
/**
|
|
10972
|
-
* The function
|
|
10973
|
-
* @param
|
|
10974
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
10975
|
-
* class.
|
|
10976
|
-
*/
|
|
10977
|
-
isNode(exemplar) {
|
|
10978
|
-
return exemplar instanceof TreeMultimapNode;
|
|
10979
|
-
}
|
|
10980
|
-
/**
|
|
10981
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10982
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10983
|
-
* data type.
|
|
10984
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10985
|
-
*/
|
|
10986
|
-
isNotNodeInstance(potentialKey) {
|
|
10987
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
10988
|
-
}
|
|
10989
|
-
/**
|
|
10990
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
10991
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
10761
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
10762
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
10992
10763
|
* can be one of the following:
|
|
10993
10764
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
10994
10765
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -10997,33 +10768,52 @@ var dataStructureTyped = (() => {
|
|
|
10997
10768
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
10998
10769
|
* @returns a node of type `N` or `undefined`.
|
|
10999
10770
|
*/
|
|
11000
|
-
exemplarToNode(
|
|
10771
|
+
exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
11001
10772
|
let node;
|
|
11002
|
-
if (
|
|
10773
|
+
if (keyOrNodeOrEntry === void 0 || keyOrNodeOrEntry === null) {
|
|
11003
10774
|
return;
|
|
11004
|
-
} else if (this.isNode(
|
|
11005
|
-
node =
|
|
11006
|
-
} else if (this.isEntry(
|
|
11007
|
-
const [key, value2] =
|
|
10775
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
10776
|
+
node = keyOrNodeOrEntry;
|
|
10777
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
10778
|
+
const [key, value2] = keyOrNodeOrEntry;
|
|
11008
10779
|
if (key === void 0 || key === null) {
|
|
11009
10780
|
return;
|
|
11010
10781
|
} else {
|
|
11011
10782
|
node = this.createNode(key, value2, count);
|
|
11012
10783
|
}
|
|
11013
|
-
} else if (this.isNotNodeInstance(
|
|
11014
|
-
node = this.createNode(
|
|
10784
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
10785
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
11015
10786
|
} else {
|
|
11016
10787
|
return;
|
|
11017
10788
|
}
|
|
11018
10789
|
return node;
|
|
11019
10790
|
}
|
|
11020
10791
|
/**
|
|
11021
|
-
*
|
|
11022
|
-
*
|
|
10792
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
10793
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
10794
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
10795
|
+
* class.
|
|
11023
10796
|
*/
|
|
10797
|
+
isNode(keyOrNodeOrEntry) {
|
|
10798
|
+
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
10799
|
+
}
|
|
11024
10800
|
/**
|
|
11025
|
-
*
|
|
11026
|
-
*
|
|
10801
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
10802
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
10803
|
+
* data type.
|
|
10804
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
10805
|
+
*/
|
|
10806
|
+
isNotNodeInstance(potentialKey) {
|
|
10807
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
10808
|
+
}
|
|
10809
|
+
/**
|
|
10810
|
+
* Time Complexity: O(log n)
|
|
10811
|
+
* Space Complexity: O(1)
|
|
10812
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
10813
|
+
*/
|
|
10814
|
+
/**
|
|
10815
|
+
* Time Complexity: O(log n)
|
|
10816
|
+
* Space Complexity: O(1)
|
|
11027
10817
|
*
|
|
11028
10818
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
11029
10819
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -11040,21 +10830,22 @@ var dataStructureTyped = (() => {
|
|
|
11040
10830
|
add(keyOrNodeOrEntry, value, count = 1) {
|
|
11041
10831
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
11042
10832
|
if (newNode === void 0)
|
|
11043
|
-
return;
|
|
10833
|
+
return false;
|
|
11044
10834
|
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
11045
10835
|
const inserted = super.add(newNode);
|
|
11046
10836
|
if (inserted) {
|
|
11047
10837
|
this._count += orgNodeCount;
|
|
11048
10838
|
}
|
|
11049
|
-
return
|
|
10839
|
+
return true;
|
|
11050
10840
|
}
|
|
11051
10841
|
/**
|
|
11052
|
-
* Time Complexity: O(k log n)
|
|
11053
|
-
* Space Complexity: O(1)
|
|
10842
|
+
* Time Complexity: O(k log n)
|
|
10843
|
+
* Space Complexity: O(1)
|
|
10844
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
11054
10845
|
*/
|
|
11055
10846
|
/**
|
|
11056
|
-
* Time Complexity: O(k log n)
|
|
11057
|
-
* Space Complexity: O(1)
|
|
10847
|
+
* Time Complexity: O(k log n)
|
|
10848
|
+
* Space Complexity: O(1)
|
|
11058
10849
|
*
|
|
11059
10850
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
11060
10851
|
* structure.
|
|
@@ -11066,12 +10857,13 @@ var dataStructureTyped = (() => {
|
|
|
11066
10857
|
return super.addMany(keysOrNodesOrEntries);
|
|
11067
10858
|
}
|
|
11068
10859
|
/**
|
|
11069
|
-
* Time Complexity: O(
|
|
11070
|
-
* Space Complexity: O(
|
|
10860
|
+
* Time Complexity: O(n log n)
|
|
10861
|
+
* Space Complexity: O(n)
|
|
10862
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
|
|
11071
10863
|
*/
|
|
11072
10864
|
/**
|
|
11073
|
-
* Time Complexity: O(n log n)
|
|
11074
|
-
* Space Complexity: O(n)
|
|
10865
|
+
* Time Complexity: O(n log n)
|
|
10866
|
+
* Space Complexity: O(n)
|
|
11075
10867
|
*
|
|
11076
10868
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
11077
10869
|
* tree using either a recursive or iterative approach.
|
|
@@ -11116,12 +10908,13 @@ var dataStructureTyped = (() => {
|
|
|
11116
10908
|
}
|
|
11117
10909
|
}
|
|
11118
10910
|
/**
|
|
11119
|
-
* Time Complexity: O(k log n)
|
|
11120
|
-
* Space Complexity: O(1)
|
|
10911
|
+
* Time Complexity: O(k log n)
|
|
10912
|
+
* Space Complexity: O(1)
|
|
10913
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
|
|
11121
10914
|
*/
|
|
11122
10915
|
/**
|
|
11123
|
-
* Time Complexity: O(log n)
|
|
11124
|
-
* Space Complexity: O(1)
|
|
10916
|
+
* Time Complexity: O(k log n)
|
|
10917
|
+
* Space Complexity: O(1)
|
|
11125
10918
|
*
|
|
11126
10919
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
11127
10920
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -11191,10 +10984,13 @@ var dataStructureTyped = (() => {
|
|
|
11191
10984
|
return deletedResult;
|
|
11192
10985
|
}
|
|
11193
10986
|
/**
|
|
11194
|
-
* Time Complexity: O(
|
|
11195
|
-
* Space Complexity: O(
|
|
10987
|
+
* Time Complexity: O(1)
|
|
10988
|
+
* Space Complexity: O(1)
|
|
11196
10989
|
*/
|
|
11197
10990
|
/**
|
|
10991
|
+
* Time Complexity: O(1)
|
|
10992
|
+
* Space Complexity: O(1)
|
|
10993
|
+
*
|
|
11198
10994
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
11199
10995
|
*/
|
|
11200
10996
|
clear() {
|
|
@@ -11295,14 +11091,14 @@ var dataStructureTyped = (() => {
|
|
|
11295
11091
|
|
|
11296
11092
|
// src/data-structures/priority-queue/priority-queue.ts
|
|
11297
11093
|
var PriorityQueue = class extends Heap {
|
|
11298
|
-
constructor(elements, options) {
|
|
11094
|
+
constructor(elements = [], options) {
|
|
11299
11095
|
super(elements, options);
|
|
11300
11096
|
}
|
|
11301
11097
|
};
|
|
11302
11098
|
|
|
11303
11099
|
// src/data-structures/priority-queue/min-priority-queue.ts
|
|
11304
11100
|
var MinPriorityQueue = class extends PriorityQueue {
|
|
11305
|
-
constructor(elements, options = {
|
|
11101
|
+
constructor(elements = [], options = {
|
|
11306
11102
|
comparator: (a, b) => {
|
|
11307
11103
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
11308
11104
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -11317,7 +11113,7 @@ var dataStructureTyped = (() => {
|
|
|
11317
11113
|
|
|
11318
11114
|
// src/data-structures/priority-queue/max-priority-queue.ts
|
|
11319
11115
|
var MaxPriorityQueue = class extends PriorityQueue {
|
|
11320
|
-
constructor(elements, options = {
|
|
11116
|
+
constructor(elements = [], options = {
|
|
11321
11117
|
comparator: (a, b) => {
|
|
11322
11118
|
if (!(typeof a === "number" && typeof b === "number")) {
|
|
11323
11119
|
throw new Error("The a, b params of compare function must be number");
|
|
@@ -11846,18 +11642,19 @@ var dataStructureTyped = (() => {
|
|
|
11846
11642
|
}
|
|
11847
11643
|
};
|
|
11848
11644
|
var Trie = class _Trie extends IterableElementBase {
|
|
11849
|
-
constructor(words
|
|
11645
|
+
constructor(words = [], options) {
|
|
11850
11646
|
super();
|
|
11851
|
-
__publicField(this, "_size");
|
|
11852
|
-
__publicField(this, "_caseSensitive");
|
|
11853
|
-
__publicField(this, "_root");
|
|
11854
|
-
|
|
11855
|
-
|
|
11856
|
-
|
|
11647
|
+
__publicField(this, "_size", 0);
|
|
11648
|
+
__publicField(this, "_caseSensitive", true);
|
|
11649
|
+
__publicField(this, "_root", new TrieNode(""));
|
|
11650
|
+
if (options) {
|
|
11651
|
+
const { caseSensitive } = options;
|
|
11652
|
+
if (caseSensitive !== void 0)
|
|
11653
|
+
this._caseSensitive = caseSensitive;
|
|
11654
|
+
}
|
|
11857
11655
|
if (words) {
|
|
11858
|
-
for (const word of words)
|
|
11656
|
+
for (const word of words)
|
|
11859
11657
|
this.add(word);
|
|
11860
|
-
}
|
|
11861
11658
|
}
|
|
11862
11659
|
}
|
|
11863
11660
|
get size() {
|