data-structure-typed 1.36.7 → 1.36.9
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/.eslintrc.js +1 -1
- package/CHANGELOG.md +3 -1
- package/README.md +8 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -5
- package/dist/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -85
- package/dist/data-structures/binary-tree/binary-tree.js +76 -128
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -16
- package/dist/data-structures/binary-tree/tree-multiset.js +10 -20
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +3 -3
- package/dist/data-structures/hash/hash-table.js +3 -3
- package/dist/data-structures/heap/heap.d.ts +136 -11
- package/dist/data-structures/heap/heap.js +293 -13
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -3
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -3
- package/dist/data-structures/queue/deque.d.ts +2 -2
- package/dist/data-structures/queue/deque.js +2 -2
- package/dist/data-structures/queue/queue.js +1 -1
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +5 -5
- package/lib/data-structures/binary-tree/avl-tree.js +6 -6
- package/lib/data-structures/binary-tree/binary-tree.d.ts +18 -85
- package/lib/data-structures/binary-tree/binary-tree.js +76 -128
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +9 -16
- package/lib/data-structures/binary-tree/tree-multiset.js +10 -20
- package/lib/data-structures/hash/hash-map.d.ts +1 -1
- package/lib/data-structures/hash/hash-map.js +1 -1
- package/lib/data-structures/hash/hash-table.d.ts +3 -3
- package/lib/data-structures/hash/hash-table.js +3 -3
- package/lib/data-structures/heap/heap.d.ts +136 -11
- package/lib/data-structures/heap/heap.js +290 -12
- package/lib/data-structures/linked-list/skip-linked-list.d.ts +3 -3
- package/lib/data-structures/linked-list/skip-linked-list.js +3 -3
- package/lib/data-structures/queue/deque.d.ts +2 -2
- package/lib/data-structures/queue/deque.js +2 -2
- package/lib/data-structures/queue/queue.js +1 -1
- package/lib/data-structures/trie/trie.d.ts +2 -2
- package/lib/data-structures/trie/trie.js +2 -2
- package/lib/interfaces/binary-tree.d.ts +1 -1
- package/package.json +7 -6
- package/src/data-structures/binary-tree/avl-tree.ts +6 -6
- package/src/data-structures/binary-tree/binary-tree.ts +79 -214
- package/src/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +10 -21
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +340 -16
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -3
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/test/types/index.ts +1 -0
- package/test/types/utils/big-o.ts +1 -0
- package/test/types/utils/index.ts +1 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -14
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +28 -28
- package/test/unit/data-structures/binary-tree/overall.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +28 -28
- package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-table.test.ts +5 -5
- package/test/unit/data-structures/heap/heap.test.ts +192 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +7 -7
- package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +3 -3
- package/test/unit/data-structures/trie/trie.test.ts +5 -5
- package/test/utils/big-o.ts +199 -0
- package/test/utils/index.ts +1 -1
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
- package/test/utils/magnitude.ts +0 -21
|
@@ -213,13 +213,13 @@ export class HashTable<K, V> {
|
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
/**
|
|
216
|
-
* The
|
|
216
|
+
* The delete function removes a key-value pair from a hash table.
|
|
217
217
|
* @param {K} key - The `key` parameter represents the key of the key-value pair that needs to be removed from the hash
|
|
218
218
|
* table.
|
|
219
|
-
* @returns Nothing is being returned. The `
|
|
219
|
+
* @returns Nothing is being returned. The `delete` method has a return type of `void`, which means it does not return
|
|
220
220
|
* any value.
|
|
221
221
|
*/
|
|
222
|
-
|
|
222
|
+
delete(key: K): void {
|
|
223
223
|
const index = this._hash(key);
|
|
224
224
|
let currentNode = this._buckets[index];
|
|
225
225
|
let prevNode: HashTableNode<K, V> | null = null;
|
|
@@ -10,7 +10,7 @@ import {DFSOrderPattern} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class Heap<E> {
|
|
12
12
|
protected nodes: E[] = [];
|
|
13
|
-
|
|
13
|
+
protected readonly comparator: Comparator<E>;
|
|
14
14
|
|
|
15
15
|
constructor(comparator: Comparator<E>) {
|
|
16
16
|
this.comparator = comparator;
|
|
@@ -18,21 +18,29 @@ export class Heap<E> {
|
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Insert an element into the heap and maintain the heap properties.
|
|
21
|
-
* @param
|
|
21
|
+
* @param element - The element to be inserted.
|
|
22
22
|
*/
|
|
23
|
-
add(
|
|
24
|
-
this.
|
|
23
|
+
add(element: E): Heap<E> {
|
|
24
|
+
return this.push(element);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
29
|
+
* @param element - The element to be inserted.
|
|
30
|
+
*/
|
|
31
|
+
push(element: E): Heap<E> {
|
|
32
|
+
this.nodes.push(element);
|
|
25
33
|
this.bubbleUp(this.nodes.length - 1);
|
|
26
34
|
return this;
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
/**
|
|
30
38
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
31
|
-
* @returns The top element or
|
|
39
|
+
* @returns The top element or undefined if the heap is empty.
|
|
32
40
|
*/
|
|
33
|
-
poll(): E |
|
|
41
|
+
poll(): E | undefined {
|
|
34
42
|
if (this.nodes.length === 0) {
|
|
35
|
-
return
|
|
43
|
+
return undefined;
|
|
36
44
|
}
|
|
37
45
|
if (this.nodes.length === 1) {
|
|
38
46
|
return this.nodes.pop() as E;
|
|
@@ -44,6 +52,14 @@ export class Heap<E> {
|
|
|
44
52
|
return topValue;
|
|
45
53
|
}
|
|
46
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
57
|
+
* @returns The top element or undefined if the heap is empty.
|
|
58
|
+
*/
|
|
59
|
+
pop(): E | undefined {
|
|
60
|
+
return this.poll();
|
|
61
|
+
}
|
|
62
|
+
|
|
47
63
|
/**
|
|
48
64
|
* Float operation to maintain heap properties after adding an element.
|
|
49
65
|
* @param index - The index of the newly added element.
|
|
@@ -97,11 +113,11 @@ export class Heap<E> {
|
|
|
97
113
|
|
|
98
114
|
/**
|
|
99
115
|
* Peek at the top element of the heap without removing it.
|
|
100
|
-
* @returns The top element or
|
|
116
|
+
* @returns The top element or undefined if the heap is empty.
|
|
101
117
|
*/
|
|
102
|
-
peek(): E |
|
|
118
|
+
peek(): E | undefined {
|
|
103
119
|
if (this.nodes.length === 0) {
|
|
104
|
-
return
|
|
120
|
+
return undefined;
|
|
105
121
|
}
|
|
106
122
|
return this.nodes[0];
|
|
107
123
|
}
|
|
@@ -115,10 +131,10 @@ export class Heap<E> {
|
|
|
115
131
|
|
|
116
132
|
/**
|
|
117
133
|
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
118
|
-
* @returns The last element or
|
|
134
|
+
* @returns The last element or undefined if the heap is empty.
|
|
119
135
|
*/
|
|
120
|
-
get leaf(): E |
|
|
121
|
-
return this.nodes[this.size - 1] ??
|
|
136
|
+
get leaf(): E | undefined {
|
|
137
|
+
return this.nodes[this.size - 1] ?? undefined;
|
|
122
138
|
}
|
|
123
139
|
|
|
124
140
|
/**
|
|
@@ -147,11 +163,11 @@ export class Heap<E> {
|
|
|
147
163
|
|
|
148
164
|
/**
|
|
149
165
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
150
|
-
* @param
|
|
166
|
+
* @param element - the element to check.
|
|
151
167
|
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
152
168
|
*/
|
|
153
|
-
has(
|
|
154
|
-
return this.nodes.includes(
|
|
169
|
+
has(element: E): boolean {
|
|
170
|
+
return this.nodes.includes(element);
|
|
155
171
|
}
|
|
156
172
|
|
|
157
173
|
/**
|
|
@@ -235,3 +251,311 @@ export class Heap<E> {
|
|
|
235
251
|
return binaryHeap;
|
|
236
252
|
}
|
|
237
253
|
}
|
|
254
|
+
|
|
255
|
+
export class FibonacciHeapNode<E> {
|
|
256
|
+
element: E;
|
|
257
|
+
degree: number;
|
|
258
|
+
left?: FibonacciHeapNode<E>;
|
|
259
|
+
right?: FibonacciHeapNode<E>;
|
|
260
|
+
child?: FibonacciHeapNode<E>;
|
|
261
|
+
parent?: FibonacciHeapNode<E>;
|
|
262
|
+
marked: boolean;
|
|
263
|
+
constructor(element: E, degree = 0) {
|
|
264
|
+
this.element = element;
|
|
265
|
+
this.degree = degree;
|
|
266
|
+
this.marked = false;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export class FibonacciHeap<E> {
|
|
271
|
+
root?: FibonacciHeapNode<E>;
|
|
272
|
+
protected min?: FibonacciHeapNode<E>;
|
|
273
|
+
size: number = 0;
|
|
274
|
+
protected readonly comparator: Comparator<E>;
|
|
275
|
+
|
|
276
|
+
constructor(comparator?: Comparator<E>) {
|
|
277
|
+
this.clear();
|
|
278
|
+
this.comparator = comparator || this.defaultComparator;
|
|
279
|
+
|
|
280
|
+
if (typeof this.comparator !== 'function') {
|
|
281
|
+
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Default comparator function used by the heap.
|
|
287
|
+
* @param {E} a
|
|
288
|
+
* @param {E} b
|
|
289
|
+
* @protected
|
|
290
|
+
*/
|
|
291
|
+
protected defaultComparator(a: E, b: E): number {
|
|
292
|
+
if (a < b) return -1;
|
|
293
|
+
if (a > b) return 1;
|
|
294
|
+
return 0;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Get the size (number of elements) of the heap.
|
|
299
|
+
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
300
|
+
*/
|
|
301
|
+
clear(): void {
|
|
302
|
+
this.root = undefined;
|
|
303
|
+
this.min = undefined;
|
|
304
|
+
this.size = 0;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Create a new node.
|
|
309
|
+
* @param element
|
|
310
|
+
* @protected
|
|
311
|
+
*/
|
|
312
|
+
protected createNode(element: E): FibonacciHeapNode<E> {
|
|
313
|
+
return new FibonacciHeapNode<E>(element);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Merge the given node with the root list.
|
|
318
|
+
* @param node - The node to be merged.
|
|
319
|
+
*/
|
|
320
|
+
protected mergeWithRoot(node: FibonacciHeapNode<E>): void {
|
|
321
|
+
if (!this.root) {
|
|
322
|
+
this.root = node;
|
|
323
|
+
} else {
|
|
324
|
+
node.right = this.root.right;
|
|
325
|
+
node.left = this.root;
|
|
326
|
+
this.root.right!.left = node;
|
|
327
|
+
this.root.right = node;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* O(1) time operation.
|
|
333
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
334
|
+
* @param element
|
|
335
|
+
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
336
|
+
*/
|
|
337
|
+
add(element: E): FibonacciHeap<E> {
|
|
338
|
+
return this.push(element);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* O(1) time operation.
|
|
343
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
344
|
+
* @param element
|
|
345
|
+
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
346
|
+
*/
|
|
347
|
+
push(element: E): FibonacciHeap<E> {
|
|
348
|
+
const node = this.createNode(element);
|
|
349
|
+
node.left = node;
|
|
350
|
+
node.right = node;
|
|
351
|
+
this.mergeWithRoot(node);
|
|
352
|
+
|
|
353
|
+
if (!this.min || this.comparator(node.element, this.min.element) <= 0) {
|
|
354
|
+
this.min = node;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
this.size++;
|
|
358
|
+
return this;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* O(1) time operation.
|
|
363
|
+
* Peek at the top element of the heap without removing it.
|
|
364
|
+
* @returns The top element or undefined if the heap is empty.
|
|
365
|
+
* @protected
|
|
366
|
+
*/
|
|
367
|
+
peek(): E | undefined {
|
|
368
|
+
return this.min ? this.min.element : undefined;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* O(1) time operation.
|
|
373
|
+
* Get the size (number of elements) of the heap.
|
|
374
|
+
* @param {FibonacciHeapNode<E>} head - The head of the linked list.
|
|
375
|
+
* @protected
|
|
376
|
+
* @returns FibonacciHeapNode<E>[] - An array containing the nodes of the linked list.
|
|
377
|
+
*/
|
|
378
|
+
consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[] {
|
|
379
|
+
const nodes: FibonacciHeapNode<E>[] = [];
|
|
380
|
+
if (!head) return nodes;
|
|
381
|
+
|
|
382
|
+
let node: FibonacciHeapNode<E> | undefined = head;
|
|
383
|
+
let flag = false;
|
|
384
|
+
|
|
385
|
+
while (true) {
|
|
386
|
+
if (node === head && flag) break;
|
|
387
|
+
else if (node === head) flag = true;
|
|
388
|
+
|
|
389
|
+
if (node) {
|
|
390
|
+
nodes.push(node);
|
|
391
|
+
node = node.right;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return nodes;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* O(log n) time operation.
|
|
400
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
401
|
+
* @param node - The node to be removed.
|
|
402
|
+
* @protected
|
|
403
|
+
*/
|
|
404
|
+
protected removeFromRoot(node: FibonacciHeapNode<E>): void {
|
|
405
|
+
if (this.root === node) this.root = node.right;
|
|
406
|
+
if (node.left) node.left.right = node.right;
|
|
407
|
+
if (node.right) node.right.left = node.left;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* O(log n) time operation.
|
|
412
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
413
|
+
* @param parent
|
|
414
|
+
* @param node
|
|
415
|
+
*/
|
|
416
|
+
mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void {
|
|
417
|
+
if (!parent.child) {
|
|
418
|
+
parent.child = node;
|
|
419
|
+
} else {
|
|
420
|
+
node.right = parent.child.right;
|
|
421
|
+
node.left = parent.child;
|
|
422
|
+
parent.child.right!.left = node;
|
|
423
|
+
parent.child.right = node;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* O(log n) time operation.
|
|
429
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
430
|
+
* @param y
|
|
431
|
+
* @param x
|
|
432
|
+
* @protected
|
|
433
|
+
*/
|
|
434
|
+
protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void {
|
|
435
|
+
this.removeFromRoot(y);
|
|
436
|
+
y.left = y;
|
|
437
|
+
y.right = y;
|
|
438
|
+
this.mergeWithChild(x, y);
|
|
439
|
+
x.degree++;
|
|
440
|
+
y.parent = x;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* O(log n) time operation.
|
|
445
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
446
|
+
* @protected
|
|
447
|
+
*/
|
|
448
|
+
protected consolidate(): void {
|
|
449
|
+
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.size);
|
|
450
|
+
const nodes = this.consumeLinkedList(this.root);
|
|
451
|
+
let x: FibonacciHeapNode<E> | undefined,
|
|
452
|
+
y: FibonacciHeapNode<E> | undefined,
|
|
453
|
+
d: number,
|
|
454
|
+
t: FibonacciHeapNode<E> | undefined;
|
|
455
|
+
|
|
456
|
+
for (const node of nodes) {
|
|
457
|
+
x = node;
|
|
458
|
+
d = x.degree;
|
|
459
|
+
|
|
460
|
+
while (A[d]) {
|
|
461
|
+
y = A[d] as FibonacciHeapNode<E>;
|
|
462
|
+
|
|
463
|
+
if (this.comparator(x.element, y.element) > 0) {
|
|
464
|
+
t = x;
|
|
465
|
+
x = y;
|
|
466
|
+
y = t;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
this.link(y, x);
|
|
470
|
+
A[d] = undefined;
|
|
471
|
+
d++;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
A[d] = x;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
for (let i = 0; i < this.size; i++) {
|
|
478
|
+
if (A[i] && this.comparator(A[i]!.element, this.min!.element) <= 0) {
|
|
479
|
+
this.min = A[i]!;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* O(log n) time operation.
|
|
486
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
487
|
+
* @returns The top element or undefined if the heap is empty.
|
|
488
|
+
*/
|
|
489
|
+
poll(): E | undefined {
|
|
490
|
+
return this.pop();
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* O(log n) time operation.
|
|
495
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
496
|
+
* @returns The top element or undefined if the heap is empty.
|
|
497
|
+
*/
|
|
498
|
+
pop(): E | undefined {
|
|
499
|
+
if (this.size === 0) return undefined;
|
|
500
|
+
|
|
501
|
+
const z = this.min!;
|
|
502
|
+
if (z.child) {
|
|
503
|
+
const nodes = this.consumeLinkedList(z.child);
|
|
504
|
+
for (const node of nodes) {
|
|
505
|
+
this.mergeWithRoot(node);
|
|
506
|
+
node.parent = undefined;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
this.removeFromRoot(z);
|
|
511
|
+
|
|
512
|
+
if (z === z.right) {
|
|
513
|
+
this.min = undefined;
|
|
514
|
+
this.root = undefined;
|
|
515
|
+
} else {
|
|
516
|
+
this.min = z.right;
|
|
517
|
+
this.consolidate();
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
this.size--;
|
|
521
|
+
|
|
522
|
+
return z.element;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* O(log n) time operation.
|
|
527
|
+
* merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
|
|
528
|
+
* @param heapToMerge
|
|
529
|
+
*/
|
|
530
|
+
merge(heapToMerge: FibonacciHeap<E>): void {
|
|
531
|
+
if (heapToMerge.size === 0) {
|
|
532
|
+
return; // Nothing to merge
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Merge the root lists of the two heaps
|
|
536
|
+
if (this.root && heapToMerge.root) {
|
|
537
|
+
const thisRoot = this.root;
|
|
538
|
+
const otherRoot = heapToMerge.root;
|
|
539
|
+
|
|
540
|
+
const thisRootRight = thisRoot.right!;
|
|
541
|
+
const otherRootLeft = otherRoot.left!;
|
|
542
|
+
|
|
543
|
+
thisRoot.right = otherRoot;
|
|
544
|
+
otherRoot.left = thisRoot;
|
|
545
|
+
|
|
546
|
+
thisRootRight.left = otherRootLeft;
|
|
547
|
+
otherRootLeft.right = thisRootRight;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// Update the minimum node
|
|
551
|
+
if (!this.min || (heapToMerge.min && this.comparator(heapToMerge.min.element, this.min.element) < 0)) {
|
|
552
|
+
this.min = heapToMerge.min;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// Update the size
|
|
556
|
+
this.size += heapToMerge.size;
|
|
557
|
+
|
|
558
|
+
// Clear the heap that was merged
|
|
559
|
+
heapToMerge.clear();
|
|
560
|
+
}
|
|
561
|
+
}
|
|
@@ -130,12 +130,12 @@ export class SkipList<K, V> {
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
/**
|
|
133
|
-
* The `
|
|
133
|
+
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
134
134
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
135
|
-
* @returns The `
|
|
135
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
|
|
136
136
|
* skip list, and `false` if the key was not found in the skip list.
|
|
137
137
|
*/
|
|
138
|
-
|
|
138
|
+
delete(key: K): boolean {
|
|
139
139
|
const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
|
|
140
140
|
let current = this.head;
|
|
141
141
|
|
|
@@ -277,12 +277,12 @@ export class ArrayDeque<E> {
|
|
|
277
277
|
}
|
|
278
278
|
|
|
279
279
|
/**
|
|
280
|
-
* The
|
|
280
|
+
* The delete function removes an element from an array at a specified index.
|
|
281
281
|
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
282
282
|
* is a number that represents the index of the element to be removed.
|
|
283
283
|
* @returns The method is returning an array containing the removed element.
|
|
284
284
|
*/
|
|
285
|
-
|
|
285
|
+
delete(index: number) {
|
|
286
286
|
return this._nodes.splice(index, 1);
|
|
287
287
|
}
|
|
288
288
|
|
|
@@ -106,7 +106,7 @@ export class Queue<E = any> {
|
|
|
106
106
|
|
|
107
107
|
if (this.offset * 2 < this.nodes.length) return first;
|
|
108
108
|
|
|
109
|
-
// only
|
|
109
|
+
// only delete dequeued elements when reaching half size
|
|
110
110
|
// to decrease latency of shifting elements.
|
|
111
111
|
this.nodes = this.nodes.slice(this.offset);
|
|
112
112
|
this.offset = 0;
|
|
@@ -119,10 +119,10 @@ export class Trie {
|
|
|
119
119
|
|
|
120
120
|
/**
|
|
121
121
|
* Remove a word from the Trie structure.
|
|
122
|
-
* @param{string} word - The word to
|
|
122
|
+
* @param{string} word - The word to delete.
|
|
123
123
|
* @returns {boolean} True if the word was successfully removed.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
delete(word: string) {
|
|
126
126
|
word = this._caseProcess(word);
|
|
127
127
|
let isDeleted = false;
|
|
128
128
|
const dfs = (cur: TrieNode, i: number): boolean => {
|
|
@@ -6,5 +6,5 @@ export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
|
|
|
6
6
|
|
|
7
7
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
10
10
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './utils';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type AnyFunction = (...args: any[]) => any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './big-o';
|
|
@@ -41,56 +41,56 @@ describe('AVL Tree Test', () => {
|
|
|
41
41
|
expect(bfs[0].key).toBe(8);
|
|
42
42
|
expect(bfs[bfs.length - 1].key).toBe(16);
|
|
43
43
|
|
|
44
|
-
expect(tree.
|
|
44
|
+
expect(tree.delete(11)[0].deleted?.key).toBe(11);
|
|
45
45
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
46
46
|
expect(node15 && tree.getHeight(node15)).toBe(2);
|
|
47
47
|
|
|
48
|
-
expect(tree.
|
|
48
|
+
expect(tree.delete(1)[0].deleted?.key).toBe(1);
|
|
49
49
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
50
50
|
expect(tree.getHeight()).toBe(4);
|
|
51
51
|
|
|
52
|
-
expect(tree.
|
|
52
|
+
expect(tree.delete(4)[0].deleted?.key).toBe(4);
|
|
53
53
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
54
54
|
expect(tree.getHeight()).toBe(4);
|
|
55
55
|
|
|
56
|
-
expect(tree.
|
|
56
|
+
expect(tree.delete(10)[0].deleted?.key).toBe(10);
|
|
57
57
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
58
58
|
expect(tree.getHeight()).toBe(3);
|
|
59
59
|
|
|
60
|
-
expect(tree.
|
|
60
|
+
expect(tree.delete(15)[0].deleted?.key).toBe(15);
|
|
61
61
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
62
62
|
|
|
63
63
|
expect(tree.getHeight()).toBe(3);
|
|
64
64
|
|
|
65
|
-
expect(tree.
|
|
65
|
+
expect(tree.delete(5)[0].deleted?.key).toBe(5);
|
|
66
66
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
67
67
|
expect(tree.getHeight()).toBe(3);
|
|
68
68
|
|
|
69
|
-
expect(tree.
|
|
69
|
+
expect(tree.delete(13)[0].deleted?.key).toBe(13);
|
|
70
70
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
71
71
|
expect(tree.getHeight()).toBe(3);
|
|
72
72
|
|
|
73
|
-
expect(tree.
|
|
73
|
+
expect(tree.delete(3)[0].deleted?.key).toBe(3);
|
|
74
74
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
75
75
|
expect(tree.getHeight()).toBe(3);
|
|
76
76
|
|
|
77
|
-
expect(tree.
|
|
77
|
+
expect(tree.delete(8)[0].deleted?.key).toBe(8);
|
|
78
78
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
79
79
|
expect(tree.getHeight()).toBe(3);
|
|
80
80
|
|
|
81
|
-
expect(tree.
|
|
82
|
-
expect(tree.
|
|
81
|
+
expect(tree.delete(6)[0].deleted?.key).toBe(6);
|
|
82
|
+
expect(tree.delete(6).length).toBe(0);
|
|
83
83
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
84
84
|
expect(tree.getHeight()).toBe(2);
|
|
85
85
|
|
|
86
|
-
expect(tree.
|
|
86
|
+
expect(tree.delete(7)[0].deleted?.key).toBe(7);
|
|
87
87
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
88
88
|
expect(tree.getHeight()).toBe(2);
|
|
89
89
|
|
|
90
|
-
expect(tree.
|
|
90
|
+
expect(tree.delete(9)[0].deleted?.key).toBe(9);
|
|
91
91
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
92
92
|
expect(tree.getHeight()).toBe(2);
|
|
93
|
-
expect(tree.
|
|
93
|
+
expect(tree.delete(14)[0].deleted?.key).toBe(14);
|
|
94
94
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
95
95
|
expect(tree.getHeight()).toBe(1);
|
|
96
96
|
|
|
@@ -83,12 +83,12 @@ describe('BinaryTree', () => {
|
|
|
83
83
|
expect(binaryTree.size).toBe(1);
|
|
84
84
|
});
|
|
85
85
|
|
|
86
|
-
test('should
|
|
86
|
+
test('should delete a node', () => {
|
|
87
87
|
const node = binaryTree.add(1);
|
|
88
88
|
expect(binaryTree.size).toBe(1);
|
|
89
89
|
|
|
90
90
|
if (node) {
|
|
91
|
-
const result = binaryTree.
|
|
91
|
+
const result = binaryTree.delete(node);
|
|
92
92
|
expect(result).toHaveLength(1);
|
|
93
93
|
expect(binaryTree.size).toBe(0);
|
|
94
94
|
}
|