min-heap-typed 1.50.1 → 1.50.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -21,6 +21,16 @@ import { IterableElementBase } from '../base';
|
|
|
21
21
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
22
22
|
*/
|
|
23
23
|
export class Heap<E = any> extends IterableElementBase<E> {
|
|
24
|
+
/**
|
|
25
|
+
* The constructor initializes a heap data structure with optional elements and options.
|
|
26
|
+
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
27
|
+
* elements to be added to the heap. It is an optional parameter and if not provided, the heap will
|
|
28
|
+
* be initialized as empty.
|
|
29
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
30
|
+
* configuration options for the heap. In this case, it is used to specify a custom comparator
|
|
31
|
+
* function for comparing elements in the heap. The comparator function is used to determine the
|
|
32
|
+
* order of elements in the heap.
|
|
33
|
+
*/
|
|
24
34
|
constructor(elements: Iterable<E> = [], options?: HeapOptions<E>) {
|
|
25
35
|
super();
|
|
26
36
|
|
|
@@ -33,7 +43,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
33
43
|
for (const el of elements) {
|
|
34
44
|
this.add(el);
|
|
35
45
|
}
|
|
36
|
-
// this.fix();
|
|
37
46
|
}
|
|
38
47
|
}
|
|
39
48
|
|
|
@@ -45,12 +54,20 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
45
54
|
}
|
|
46
55
|
};
|
|
47
56
|
|
|
57
|
+
/**
|
|
58
|
+
* The function returns the value of the _comparator property.
|
|
59
|
+
* @returns The `_comparator` property is being returned.
|
|
60
|
+
*/
|
|
48
61
|
get comparator() {
|
|
49
62
|
return this._comparator;
|
|
50
63
|
}
|
|
51
64
|
|
|
52
65
|
protected _elements: E[] = [];
|
|
53
66
|
|
|
67
|
+
/**
|
|
68
|
+
* The function returns an array of elements.
|
|
69
|
+
* @returns The elements array is being returned.
|
|
70
|
+
*/
|
|
54
71
|
get elements(): E[] {
|
|
55
72
|
return this._elements;
|
|
56
73
|
}
|
|
@@ -81,12 +98,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
81
98
|
}
|
|
82
99
|
|
|
83
100
|
/**
|
|
84
|
-
* Time Complexity: O(log n)
|
|
101
|
+
* Time Complexity: O(log n)
|
|
85
102
|
* Space Complexity: O(1)
|
|
103
|
+
* where n is the number of elements in the heap.
|
|
86
104
|
*/
|
|
87
105
|
|
|
88
106
|
/**
|
|
89
|
-
* Time Complexity: O(log n)
|
|
107
|
+
* Time Complexity: O(log n)
|
|
90
108
|
* Space Complexity: O(1)
|
|
91
109
|
*
|
|
92
110
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -98,12 +116,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
98
116
|
}
|
|
99
117
|
|
|
100
118
|
/**
|
|
101
|
-
* Time Complexity: O(log n)
|
|
119
|
+
* Time Complexity: O(log n)
|
|
102
120
|
* Space Complexity: O(1)
|
|
121
|
+
* where n is the number of elements in the heap.
|
|
103
122
|
*/
|
|
104
123
|
|
|
105
124
|
/**
|
|
106
|
-
* Time Complexity: O(log n)
|
|
125
|
+
* Time Complexity: O(log n)
|
|
107
126
|
* Space Complexity: O(1)
|
|
108
127
|
*
|
|
109
128
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -121,6 +140,14 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
121
140
|
}
|
|
122
141
|
|
|
123
142
|
/**
|
|
143
|
+
* Time Complexity: O(1)
|
|
144
|
+
* Space Complexity: O(1)
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Time Complexity: O(1)
|
|
149
|
+
* Space Complexity: O(1)
|
|
150
|
+
*
|
|
124
151
|
* Peek at the top element of the heap without removing it.
|
|
125
152
|
* @returns The top element or undefined if the heap is empty.
|
|
126
153
|
*/
|
|
@@ -144,12 +171,12 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
144
171
|
}
|
|
145
172
|
|
|
146
173
|
/**
|
|
147
|
-
* Time Complexity: O(n)
|
|
174
|
+
* Time Complexity: O(n)
|
|
148
175
|
* Space Complexity: O(n)
|
|
149
176
|
*/
|
|
150
177
|
|
|
151
178
|
/**
|
|
152
|
-
* Time Complexity: O(n)
|
|
179
|
+
* Time Complexity: O(n)
|
|
153
180
|
* Space Complexity: O(n)
|
|
154
181
|
*
|
|
155
182
|
* Clear and add elements of the heap
|
|
@@ -161,12 +188,12 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
161
188
|
}
|
|
162
189
|
|
|
163
190
|
/**
|
|
164
|
-
* Time Complexity: O(n)
|
|
191
|
+
* Time Complexity: O(n)
|
|
165
192
|
* Space Complexity: O(1)
|
|
166
193
|
*/
|
|
167
194
|
|
|
168
195
|
/**
|
|
169
|
-
* Time Complexity: O(n)
|
|
196
|
+
* Time Complexity: O(n)
|
|
170
197
|
* Space Complexity: O(1)
|
|
171
198
|
*
|
|
172
199
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
@@ -178,12 +205,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
178
205
|
}
|
|
179
206
|
|
|
180
207
|
/**
|
|
181
|
-
* Time Complexity: O(n)
|
|
208
|
+
* Time Complexity: O(n)
|
|
182
209
|
* Space Complexity: O(1)
|
|
210
|
+
* The worst-case O(n) This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
|
|
183
211
|
*/
|
|
184
212
|
|
|
185
213
|
/**
|
|
186
|
-
* Time Complexity: O(n)
|
|
214
|
+
* Time Complexity: O(n)
|
|
187
215
|
* Space Complexity: O(1)
|
|
188
216
|
*
|
|
189
217
|
* The `delete` function removes an element from an array-like data structure, maintaining the order
|
|
@@ -209,13 +237,14 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
209
237
|
}
|
|
210
238
|
|
|
211
239
|
/**
|
|
212
|
-
* Time Complexity: O(n)
|
|
213
|
-
* Space Complexity: O(
|
|
240
|
+
* Time Complexity: O(n)
|
|
241
|
+
* Space Complexity: O(log n)
|
|
242
|
+
* where log n is the height of the heap.
|
|
214
243
|
*/
|
|
215
244
|
|
|
216
245
|
/**
|
|
217
|
-
* Time Complexity: O(n)
|
|
218
|
-
* Space Complexity: O(
|
|
246
|
+
* Time Complexity: O(n)
|
|
247
|
+
* Space Complexity: O(log n)
|
|
219
248
|
*
|
|
220
249
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
221
250
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
@@ -307,13 +336,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
307
336
|
}
|
|
308
337
|
|
|
309
338
|
/**
|
|
310
|
-
* Time Complexity: O(log n)
|
|
311
|
-
* Space Complexity: O(
|
|
339
|
+
* Time Complexity: O(n log n)
|
|
340
|
+
* Space Complexity: O(n)
|
|
312
341
|
*/
|
|
313
342
|
|
|
314
343
|
/**
|
|
315
|
-
* Time Complexity: O(n)
|
|
316
|
-
* Space Complexity: O(
|
|
344
|
+
* Time Complexity: O(n log n)
|
|
345
|
+
* Space Complexity: O(n)
|
|
317
346
|
*
|
|
318
347
|
* Fix the entire heap to maintain heap properties.
|
|
319
348
|
*/
|
|
@@ -391,6 +420,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
391
420
|
return mappedHeap;
|
|
392
421
|
}
|
|
393
422
|
|
|
423
|
+
/**
|
|
424
|
+
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
|
425
|
+
*/
|
|
394
426
|
protected* _getIterator(): IterableIterator<E> {
|
|
395
427
|
for (const element of this.elements) {
|
|
396
428
|
yield element;
|
|
@@ -398,7 +430,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
398
430
|
}
|
|
399
431
|
|
|
400
432
|
/**
|
|
401
|
-
* Time Complexity: O(n)
|
|
433
|
+
* Time Complexity: O(log n)
|
|
402
434
|
* Space Complexity: O(1)
|
|
403
435
|
*/
|
|
404
436
|
|
|
@@ -422,6 +454,11 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
422
454
|
return true;
|
|
423
455
|
}
|
|
424
456
|
|
|
457
|
+
/**
|
|
458
|
+
* Time Complexity: O(log n)
|
|
459
|
+
* Space Complexity: O(1)
|
|
460
|
+
*/
|
|
461
|
+
|
|
425
462
|
/**
|
|
426
463
|
* Time Complexity: O(log n)
|
|
427
464
|
* Space Complexity: O(1)
|
|
@@ -458,6 +495,16 @@ export class FibonacciHeapNode<E> {
|
|
|
458
495
|
parent?: FibonacciHeapNode<E>;
|
|
459
496
|
marked: boolean;
|
|
460
497
|
|
|
498
|
+
/**
|
|
499
|
+
* The constructor function initializes an object with an element and a degree, and sets the marked
|
|
500
|
+
* property to false.
|
|
501
|
+
* @param {E} element - The "element" parameter represents the value or data that will be stored in
|
|
502
|
+
* the node of a data structure. It can be any type of data, such as a number, string, object, or
|
|
503
|
+
* even another data structure.
|
|
504
|
+
* @param [degree=0] - The degree parameter represents the degree of the element in a data structure
|
|
505
|
+
* called a Fibonacci heap. The degree of a node is the number of children it has. By default, the
|
|
506
|
+
* degree is set to 0 when a new node is created.
|
|
507
|
+
*/
|
|
461
508
|
constructor(element: E, degree = 0) {
|
|
462
509
|
this.element = element;
|
|
463
510
|
this.degree = degree;
|
|
@@ -466,6 +513,13 @@ export class FibonacciHeapNode<E> {
|
|
|
466
513
|
}
|
|
467
514
|
|
|
468
515
|
export class FibonacciHeap<E> {
|
|
516
|
+
/**
|
|
517
|
+
* The constructor function initializes a FibonacciHeap object with an optional comparator function.
|
|
518
|
+
* @param [comparator] - The `comparator` parameter is an optional argument that represents a
|
|
519
|
+
* function used to compare elements in the FibonacciHeap. If a comparator function is provided, it
|
|
520
|
+
* will be used to determine the order of elements in the heap. If no comparator function is
|
|
521
|
+
* provided, a default comparator function will be used.
|
|
522
|
+
*/
|
|
469
523
|
constructor(comparator?: Comparator<E>) {
|
|
470
524
|
this.clear();
|
|
471
525
|
this._comparator = comparator || this._defaultComparator;
|
|
@@ -477,24 +531,41 @@ export class FibonacciHeap<E> {
|
|
|
477
531
|
|
|
478
532
|
protected _root?: FibonacciHeapNode<E>;
|
|
479
533
|
|
|
534
|
+
/**
|
|
535
|
+
* The function returns the root node of a Fibonacci heap.
|
|
536
|
+
* @returns The method is returning either a FibonacciHeapNode object or undefined.
|
|
537
|
+
*/
|
|
480
538
|
get root(): FibonacciHeapNode<E> | undefined {
|
|
481
539
|
return this._root;
|
|
482
540
|
}
|
|
483
541
|
|
|
484
542
|
protected _size = 0;
|
|
485
543
|
|
|
544
|
+
/**
|
|
545
|
+
* The function returns the size of an object.
|
|
546
|
+
* @returns The size of the object, which is a number.
|
|
547
|
+
*/
|
|
486
548
|
get size(): number {
|
|
487
549
|
return this._size;
|
|
488
550
|
}
|
|
489
551
|
|
|
490
552
|
protected _min?: FibonacciHeapNode<E>;
|
|
491
553
|
|
|
554
|
+
/**
|
|
555
|
+
* The function returns the minimum node in a Fibonacci heap.
|
|
556
|
+
* @returns The method is returning the minimum node of the Fibonacci heap, which is of type
|
|
557
|
+
* `FibonacciHeapNode<E>`. If there is no minimum node, it will return `undefined`.
|
|
558
|
+
*/
|
|
492
559
|
get min(): FibonacciHeapNode<E> | undefined {
|
|
493
560
|
return this._min;
|
|
494
561
|
}
|
|
495
562
|
|
|
496
563
|
protected _comparator: Comparator<E>;
|
|
497
564
|
|
|
565
|
+
/**
|
|
566
|
+
* The function returns the comparator used for comparing elements.
|
|
567
|
+
* @returns The `_comparator` property of the object.
|
|
568
|
+
*/
|
|
498
569
|
get comparator(): Comparator<E> {
|
|
499
570
|
return this._comparator;
|
|
500
571
|
}
|
|
@@ -623,12 +694,12 @@ export class FibonacciHeap<E> {
|
|
|
623
694
|
}
|
|
624
695
|
|
|
625
696
|
/**
|
|
626
|
-
* Time Complexity: O(log n)
|
|
697
|
+
* Time Complexity: O(log n)
|
|
627
698
|
* Space Complexity: O(1)
|
|
628
699
|
*/
|
|
629
700
|
|
|
630
701
|
/**
|
|
631
|
-
* Time Complexity: O(log n)
|
|
702
|
+
* Time Complexity: O(log n)
|
|
632
703
|
* Space Complexity: O(1)
|
|
633
704
|
*
|
|
634
705
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -639,12 +710,12 @@ export class FibonacciHeap<E> {
|
|
|
639
710
|
}
|
|
640
711
|
|
|
641
712
|
/**
|
|
642
|
-
* Time Complexity: O(log n)
|
|
713
|
+
* Time Complexity: O(log n)
|
|
643
714
|
* Space Complexity: O(1)
|
|
644
715
|
*/
|
|
645
716
|
|
|
646
717
|
/**
|
|
647
|
-
* Time Complexity: O(log n)
|
|
718
|
+
* Time Complexity: O(log n)
|
|
648
719
|
* Space Complexity: O(1)
|
|
649
720
|
*
|
|
650
721
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -808,12 +879,12 @@ export class FibonacciHeap<E> {
|
|
|
808
879
|
}
|
|
809
880
|
|
|
810
881
|
/**
|
|
811
|
-
* Time Complexity: O(n log n)
|
|
882
|
+
* Time Complexity: O(n log n)
|
|
812
883
|
* Space Complexity: O(n)
|
|
813
884
|
*/
|
|
814
885
|
|
|
815
886
|
/**
|
|
816
|
-
* Time Complexity: O(n log n)
|
|
887
|
+
* Time Complexity: O(n log n)
|
|
817
888
|
* Space Complexity: O(n)
|
|
818
889
|
*
|
|
819
890
|
* Remove and return the top element (smallest or largest element) from the heap.
|