data-structure-typed 1.38.0 → 1.38.1
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/binary-tree/avl-tree.d.ts +9 -9
- package/dist/data-structures/binary-tree/avl-tree.js +22 -22
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -31
- package/dist/data-structures/binary-tree/binary-tree.js +32 -32
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -9
- package/dist/data-structures/binary-tree/tree-multiset.js +23 -23
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +25 -25
- package/dist/data-structures/hash/hash-map.js +59 -59
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +34 -34
- package/dist/data-structures/hash/hash-table.js +99 -99
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/heap/heap.d.ts +66 -66
- package/dist/data-structures/heap/heap.js +167 -167
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +3 -3
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +17 -17
- package/dist/data-structures/linked-list/skip-linked-list.js +34 -34
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +7 -7
- package/dist/data-structures/matrix/matrix2d.js +9 -9
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +6 -6
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/helpers.js.map +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +9 -9
- package/lib/data-structures/binary-tree/avl-tree.js +22 -22
- package/lib/data-structures/binary-tree/binary-tree.d.ts +31 -31
- package/lib/data-structures/binary-tree/binary-tree.js +32 -32
- package/lib/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +9 -9
- package/lib/data-structures/binary-tree/tree-multiset.js +23 -23
- package/lib/data-structures/hash/hash-map.d.ts +25 -25
- package/lib/data-structures/hash/hash-map.js +59 -59
- package/lib/data-structures/hash/hash-table.d.ts +34 -34
- package/lib/data-structures/hash/hash-table.js +99 -99
- package/lib/data-structures/heap/heap.d.ts +66 -66
- package/lib/data-structures/heap/heap.js +167 -167
- package/lib/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
- package/lib/data-structures/linked-list/doubly-linked-list.js +3 -3
- package/lib/data-structures/linked-list/skip-linked-list.d.ts +17 -17
- package/lib/data-structures/linked-list/skip-linked-list.js +34 -34
- package/lib/data-structures/matrix/matrix2d.d.ts +7 -7
- package/lib/data-structures/matrix/matrix2d.js +9 -9
- package/lib/data-structures/trie/trie.d.ts +2 -2
- package/lib/data-structures/trie/trie.js +6 -6
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +27 -27
- package/src/data-structures/binary-tree/binary-tree.ts +55 -55
- package/src/data-structures/binary-tree/bst.ts +4 -0
- package/src/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multiset.ts +29 -29
- package/src/data-structures/hash/hash-map.ts +81 -75
- package/src/data-structures/hash/hash-table.ts +112 -109
- package/src/data-structures/heap/heap.ts +182 -181
- package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
- package/src/data-structures/linked-list/skip-linked-list.ts +45 -38
- package/src/data-structures/matrix/matrix2d.ts +10 -10
- package/src/data-structures/trie/trie.ts +9 -9
- package/src/types/helpers.ts +5 -1
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +1 -1
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash-table.js","sourceRoot":"","sources":["../../../src/data-structures/hash/hash-table.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,MAAa,aAAa;IAKxB,YAAY,GAAM,EAAE,GAAM;QACxB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAVD,sCAUC;AAID,MAAa,SAAS;
|
|
1
|
+
{"version":3,"file":"hash-table.js","sourceRoot":"","sources":["../../../src/data-structures/hash/hash-table.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,MAAa,aAAa;IAKxB,YAAY,GAAM,EAAE,GAAM;QACxB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAVD,sCAUC;AAID,MAAa,SAAS;IAIpB,YAAY,WAAmB,SAAS,CAAC,gBAAgB,EAAE,MAAwB;QACjF,IAAI,CAAC,OAAO,GAAG,MAAM,IAAI,IAAI,CAAC,cAAc,CAAC;QAC7C,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,SAAS,CAAC,gBAAgB,CAAC,CAAC;QAChE,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;QACf,IAAI,CAAC,QAAQ,GAAG,IAAI,KAAK,CAA6B,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnF,CAAC;IAID,IAAI,QAAQ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AC,CAAC;gBAC7C,MAAM,OAAO,GAAG,IAAI,aAAa,CAAO,WAAW,CAAC,GAAG,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;gBAE1E,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;oBACzB,UAAU,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC;iBAChC;qBAAM;oBACL,IAAI,cAAc,GAAG,UAAU,CAAC,QAAQ,CAAE,CAAC;oBAC3C,OAAO,cAAc,CAAC,IAAI,EAAE;wBAC1B,cAAc,GAAG,cAAc,CAAC,IAAI,CAAC;qBACtC;oBACD,cAAc,CAAC,IAAI,GAAG,OAAO,CAAC;iBAC/B;gBACD,WAAW,GAAG,WAAW,CAAC,IAAI,CAAC;aAChC;SACF;QAED,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,WAAW,CAAC;IAC/B,CAAC;;AAhQH,8BAiQC;AAhQyB,0BAAgB,GAAG,EAAE,CAAC;AACtB,qBAAW,GAAG,IAAI,CAAC"}
|
|
@@ -9,6 +9,22 @@ export declare class Heap<E> {
|
|
|
9
9
|
protected nodes: E[];
|
|
10
10
|
protected readonly comparator: Comparator<E>;
|
|
11
11
|
constructor(comparator: Comparator<E>);
|
|
12
|
+
/**
|
|
13
|
+
* Get the size (number of elements) of the heap.
|
|
14
|
+
*/
|
|
15
|
+
get size(): number;
|
|
16
|
+
/**
|
|
17
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
18
|
+
* @returns The last element or undefined if the heap is empty.
|
|
19
|
+
*/
|
|
20
|
+
get leaf(): E | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
23
|
+
* @param nodes
|
|
24
|
+
* @param comparator - Comparison function.
|
|
25
|
+
* @returns A new Heap instance.
|
|
26
|
+
*/
|
|
27
|
+
static heapify<E>(nodes: E[], comparator: Comparator<E>): Heap<E>;
|
|
12
28
|
/**
|
|
13
29
|
* Insert an element into the heap and maintain the heap properties.
|
|
14
30
|
* @param element - The element to be inserted.
|
|
@@ -29,34 +45,11 @@ export declare class Heap<E> {
|
|
|
29
45
|
* @returns The top element or undefined if the heap is empty.
|
|
30
46
|
*/
|
|
31
47
|
pop(): E | undefined;
|
|
32
|
-
/**
|
|
33
|
-
* Float operation to maintain heap properties after adding an element.
|
|
34
|
-
* @param index - The index of the newly added element.
|
|
35
|
-
*/
|
|
36
|
-
protected bubbleUp(index: number): void;
|
|
37
|
-
/**
|
|
38
|
-
* Sinking operation to maintain heap properties after removing the top element.
|
|
39
|
-
* @param index - The index from which to start sinking.
|
|
40
|
-
*/
|
|
41
|
-
protected sinkDown(index: number): void;
|
|
42
|
-
/**
|
|
43
|
-
* Fix the entire heap to maintain heap properties.
|
|
44
|
-
*/
|
|
45
|
-
protected fix(): void;
|
|
46
48
|
/**
|
|
47
49
|
* Peek at the top element of the heap without removing it.
|
|
48
50
|
* @returns The top element or undefined if the heap is empty.
|
|
49
51
|
*/
|
|
50
52
|
peek(): E | undefined;
|
|
51
|
-
/**
|
|
52
|
-
* Get the size (number of elements) of the heap.
|
|
53
|
-
*/
|
|
54
|
-
get size(): number;
|
|
55
|
-
/**
|
|
56
|
-
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
57
|
-
* @returns The last element or undefined if the heap is empty.
|
|
58
|
-
*/
|
|
59
|
-
get leaf(): E | undefined;
|
|
60
53
|
/**
|
|
61
54
|
* Check if the heap is empty.
|
|
62
55
|
* @returns True if the heap is empty, otherwise false.
|
|
@@ -100,12 +93,19 @@ export declare class Heap<E> {
|
|
|
100
93
|
*/
|
|
101
94
|
sort(): E[];
|
|
102
95
|
/**
|
|
103
|
-
*
|
|
104
|
-
* @param
|
|
105
|
-
* @param comparator - Comparison function.
|
|
106
|
-
* @returns A new Heap instance.
|
|
96
|
+
* Float operation to maintain heap properties after adding an element.
|
|
97
|
+
* @param index - The index of the newly added element.
|
|
107
98
|
*/
|
|
108
|
-
|
|
99
|
+
protected bubbleUp(index: number): void;
|
|
100
|
+
/**
|
|
101
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
102
|
+
* @param index - The index from which to start sinking.
|
|
103
|
+
*/
|
|
104
|
+
protected sinkDown(index: number): void;
|
|
105
|
+
/**
|
|
106
|
+
* Fix the entire heap to maintain heap properties.
|
|
107
|
+
*/
|
|
108
|
+
protected fix(): void;
|
|
109
109
|
}
|
|
110
110
|
export declare class FibonacciHeapNode<E> {
|
|
111
111
|
element: E;
|
|
@@ -119,33 +119,15 @@ export declare class FibonacciHeapNode<E> {
|
|
|
119
119
|
}
|
|
120
120
|
export declare class FibonacciHeap<E> {
|
|
121
121
|
root?: FibonacciHeapNode<E>;
|
|
122
|
-
protected min?: FibonacciHeapNode<E>;
|
|
123
122
|
size: number;
|
|
123
|
+
protected min?: FibonacciHeapNode<E>;
|
|
124
124
|
protected readonly comparator: Comparator<E>;
|
|
125
125
|
constructor(comparator?: Comparator<E>);
|
|
126
|
-
/**
|
|
127
|
-
* Default comparator function used by the heap.
|
|
128
|
-
* @param {E} a
|
|
129
|
-
* @param {E} b
|
|
130
|
-
* @protected
|
|
131
|
-
*/
|
|
132
|
-
protected defaultComparator(a: E, b: E): number;
|
|
133
126
|
/**
|
|
134
127
|
* Get the size (number of elements) of the heap.
|
|
135
128
|
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
136
129
|
*/
|
|
137
130
|
clear(): void;
|
|
138
|
-
/**
|
|
139
|
-
* Create a new node.
|
|
140
|
-
* @param element
|
|
141
|
-
* @protected
|
|
142
|
-
*/
|
|
143
|
-
protected createNode(element: E): FibonacciHeapNode<E>;
|
|
144
|
-
/**
|
|
145
|
-
* Merge the given node with the root list.
|
|
146
|
-
* @param node - The node to be merged.
|
|
147
|
-
*/
|
|
148
|
-
protected mergeWithRoot(node: FibonacciHeapNode<E>): void;
|
|
149
131
|
/**
|
|
150
132
|
* O(1) time operation.
|
|
151
133
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -175,13 +157,6 @@ export declare class FibonacciHeap<E> {
|
|
|
175
157
|
* @returns FibonacciHeapNode<E>[] - An array containing the nodes of the linked list.
|
|
176
158
|
*/
|
|
177
159
|
consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[];
|
|
178
|
-
/**
|
|
179
|
-
* O(log n) time operation.
|
|
180
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
181
|
-
* @param node - The node to be removed.
|
|
182
|
-
* @protected
|
|
183
|
-
*/
|
|
184
|
-
protected removeFromRoot(node: FibonacciHeapNode<E>): void;
|
|
185
160
|
/**
|
|
186
161
|
* O(log n) time operation.
|
|
187
162
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -192,33 +167,58 @@ export declare class FibonacciHeap<E> {
|
|
|
192
167
|
/**
|
|
193
168
|
* O(log n) time operation.
|
|
194
169
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
195
|
-
* @
|
|
196
|
-
* @param x
|
|
197
|
-
* @protected
|
|
170
|
+
* @returns The top element or undefined if the heap is empty.
|
|
198
171
|
*/
|
|
199
|
-
|
|
172
|
+
poll(): E | undefined;
|
|
200
173
|
/**
|
|
201
174
|
* O(log n) time operation.
|
|
202
175
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
176
|
+
* @returns The top element or undefined if the heap is empty.
|
|
177
|
+
*/
|
|
178
|
+
pop(): E | undefined;
|
|
179
|
+
/**
|
|
180
|
+
* O(log n) time operation.
|
|
181
|
+
* merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
|
|
182
|
+
* @param heapToMerge
|
|
183
|
+
*/
|
|
184
|
+
merge(heapToMerge: FibonacciHeap<E>): void;
|
|
185
|
+
/**
|
|
186
|
+
* Default comparator function used by the heap.
|
|
187
|
+
* @param {E} a
|
|
188
|
+
* @param {E} b
|
|
203
189
|
* @protected
|
|
204
190
|
*/
|
|
205
|
-
protected
|
|
191
|
+
protected defaultComparator(a: E, b: E): number;
|
|
192
|
+
/**
|
|
193
|
+
* Create a new node.
|
|
194
|
+
* @param element
|
|
195
|
+
* @protected
|
|
196
|
+
*/
|
|
197
|
+
protected createNode(element: E): FibonacciHeapNode<E>;
|
|
198
|
+
/**
|
|
199
|
+
* Merge the given node with the root list.
|
|
200
|
+
* @param node - The node to be merged.
|
|
201
|
+
*/
|
|
202
|
+
protected mergeWithRoot(node: FibonacciHeapNode<E>): void;
|
|
206
203
|
/**
|
|
207
204
|
* O(log n) time operation.
|
|
208
205
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
209
|
-
* @
|
|
206
|
+
* @param node - The node to be removed.
|
|
207
|
+
* @protected
|
|
210
208
|
*/
|
|
211
|
-
|
|
209
|
+
protected removeFromRoot(node: FibonacciHeapNode<E>): void;
|
|
212
210
|
/**
|
|
213
211
|
* O(log n) time operation.
|
|
214
212
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
215
|
-
* @
|
|
213
|
+
* @param y
|
|
214
|
+
* @param x
|
|
215
|
+
* @protected
|
|
216
216
|
*/
|
|
217
|
-
|
|
217
|
+
protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
|
|
218
218
|
/**
|
|
219
219
|
* O(log n) time operation.
|
|
220
|
-
*
|
|
221
|
-
* @
|
|
220
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
221
|
+
* @protected
|
|
222
222
|
*/
|
|
223
|
-
|
|
223
|
+
protected consolidate(): void;
|
|
224
224
|
}
|
|
@@ -12,6 +12,32 @@ class Heap {
|
|
|
12
12
|
this.nodes = [];
|
|
13
13
|
this.comparator = comparator;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Get the size (number of elements) of the heap.
|
|
17
|
+
*/
|
|
18
|
+
get size() {
|
|
19
|
+
return this.nodes.length;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
23
|
+
* @returns The last element or undefined if the heap is empty.
|
|
24
|
+
*/
|
|
25
|
+
get leaf() {
|
|
26
|
+
var _a;
|
|
27
|
+
return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : undefined;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
31
|
+
* @param nodes
|
|
32
|
+
* @param comparator - Comparison function.
|
|
33
|
+
* @returns A new Heap instance.
|
|
34
|
+
*/
|
|
35
|
+
static heapify(nodes, comparator) {
|
|
36
|
+
const binaryHeap = new Heap(comparator);
|
|
37
|
+
binaryHeap.nodes = [...nodes];
|
|
38
|
+
binaryHeap.fix(); // Fix heap properties
|
|
39
|
+
return binaryHeap;
|
|
40
|
+
}
|
|
15
41
|
/**
|
|
16
42
|
* Insert an element into the heap and maintain the heap properties.
|
|
17
43
|
* @param element - The element to be inserted.
|
|
@@ -51,54 +77,6 @@ class Heap {
|
|
|
51
77
|
pop() {
|
|
52
78
|
return this.poll();
|
|
53
79
|
}
|
|
54
|
-
/**
|
|
55
|
-
* Float operation to maintain heap properties after adding an element.
|
|
56
|
-
* @param index - The index of the newly added element.
|
|
57
|
-
*/
|
|
58
|
-
bubbleUp(index) {
|
|
59
|
-
const element = this.nodes[index];
|
|
60
|
-
while (index > 0) {
|
|
61
|
-
const parentIndex = Math.floor((index - 1) / 2);
|
|
62
|
-
const parent = this.nodes[parentIndex];
|
|
63
|
-
if (this.comparator(element, parent) < 0) {
|
|
64
|
-
this.nodes[index] = parent;
|
|
65
|
-
this.nodes[parentIndex] = element;
|
|
66
|
-
index = parentIndex;
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Sinking operation to maintain heap properties after removing the top element.
|
|
75
|
-
* @param index - The index from which to start sinking.
|
|
76
|
-
*/
|
|
77
|
-
sinkDown(index) {
|
|
78
|
-
const leftChildIndex = 2 * index + 1;
|
|
79
|
-
const rightChildIndex = 2 * index + 2;
|
|
80
|
-
const length = this.nodes.length;
|
|
81
|
-
let targetIndex = index;
|
|
82
|
-
if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
|
|
83
|
-
targetIndex = leftChildIndex;
|
|
84
|
-
}
|
|
85
|
-
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
86
|
-
targetIndex = rightChildIndex;
|
|
87
|
-
}
|
|
88
|
-
if (targetIndex !== index) {
|
|
89
|
-
const temp = this.nodes[index];
|
|
90
|
-
this.nodes[index] = this.nodes[targetIndex];
|
|
91
|
-
this.nodes[targetIndex] = temp;
|
|
92
|
-
this.sinkDown(targetIndex);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Fix the entire heap to maintain heap properties.
|
|
97
|
-
*/
|
|
98
|
-
fix() {
|
|
99
|
-
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
100
|
-
this.sinkDown(i);
|
|
101
|
-
}
|
|
102
80
|
/**
|
|
103
81
|
* Peek at the top element of the heap without removing it.
|
|
104
82
|
* @returns The top element or undefined if the heap is empty.
|
|
@@ -109,20 +87,6 @@ class Heap {
|
|
|
109
87
|
}
|
|
110
88
|
return this.nodes[0];
|
|
111
89
|
}
|
|
112
|
-
/**
|
|
113
|
-
* Get the size (number of elements) of the heap.
|
|
114
|
-
*/
|
|
115
|
-
get size() {
|
|
116
|
-
return this.nodes.length;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
120
|
-
* @returns The last element or undefined if the heap is empty.
|
|
121
|
-
*/
|
|
122
|
-
get leaf() {
|
|
123
|
-
var _a;
|
|
124
|
-
return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : undefined;
|
|
125
|
-
}
|
|
126
90
|
/**
|
|
127
91
|
* Check if the heap is empty.
|
|
128
92
|
* @returns True if the heap is empty, otherwise false.
|
|
@@ -216,16 +180,52 @@ class Heap {
|
|
|
216
180
|
return visitedNode;
|
|
217
181
|
}
|
|
218
182
|
/**
|
|
219
|
-
*
|
|
220
|
-
* @param
|
|
221
|
-
* @param comparator - Comparison function.
|
|
222
|
-
* @returns A new Heap instance.
|
|
183
|
+
* Float operation to maintain heap properties after adding an element.
|
|
184
|
+
* @param index - The index of the newly added element.
|
|
223
185
|
*/
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
186
|
+
bubbleUp(index) {
|
|
187
|
+
const element = this.nodes[index];
|
|
188
|
+
while (index > 0) {
|
|
189
|
+
const parentIndex = Math.floor((index - 1) / 2);
|
|
190
|
+
const parent = this.nodes[parentIndex];
|
|
191
|
+
if (this.comparator(element, parent) < 0) {
|
|
192
|
+
this.nodes[index] = parent;
|
|
193
|
+
this.nodes[parentIndex] = element;
|
|
194
|
+
index = parentIndex;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
203
|
+
* @param index - The index from which to start sinking.
|
|
204
|
+
*/
|
|
205
|
+
sinkDown(index) {
|
|
206
|
+
const leftChildIndex = 2 * index + 1;
|
|
207
|
+
const rightChildIndex = 2 * index + 2;
|
|
208
|
+
const length = this.nodes.length;
|
|
209
|
+
let targetIndex = index;
|
|
210
|
+
if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
|
|
211
|
+
targetIndex = leftChildIndex;
|
|
212
|
+
}
|
|
213
|
+
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
214
|
+
targetIndex = rightChildIndex;
|
|
215
|
+
}
|
|
216
|
+
if (targetIndex !== index) {
|
|
217
|
+
const temp = this.nodes[index];
|
|
218
|
+
this.nodes[index] = this.nodes[targetIndex];
|
|
219
|
+
this.nodes[targetIndex] = temp;
|
|
220
|
+
this.sinkDown(targetIndex);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Fix the entire heap to maintain heap properties.
|
|
225
|
+
*/
|
|
226
|
+
fix() {
|
|
227
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
228
|
+
this.sinkDown(i);
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
231
|
exports.Heap = Heap;
|
|
@@ -246,19 +246,6 @@ class FibonacciHeap {
|
|
|
246
246
|
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
247
247
|
}
|
|
248
248
|
}
|
|
249
|
-
/**
|
|
250
|
-
* Default comparator function used by the heap.
|
|
251
|
-
* @param {E} a
|
|
252
|
-
* @param {E} b
|
|
253
|
-
* @protected
|
|
254
|
-
*/
|
|
255
|
-
defaultComparator(a, b) {
|
|
256
|
-
if (a < b)
|
|
257
|
-
return -1;
|
|
258
|
-
if (a > b)
|
|
259
|
-
return 1;
|
|
260
|
-
return 0;
|
|
261
|
-
}
|
|
262
249
|
/**
|
|
263
250
|
* Get the size (number of elements) of the heap.
|
|
264
251
|
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
@@ -268,29 +255,6 @@ class FibonacciHeap {
|
|
|
268
255
|
this.min = undefined;
|
|
269
256
|
this.size = 0;
|
|
270
257
|
}
|
|
271
|
-
/**
|
|
272
|
-
* Create a new node.
|
|
273
|
-
* @param element
|
|
274
|
-
* @protected
|
|
275
|
-
*/
|
|
276
|
-
createNode(element) {
|
|
277
|
-
return new FibonacciHeapNode(element);
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Merge the given node with the root list.
|
|
281
|
-
* @param node - The node to be merged.
|
|
282
|
-
*/
|
|
283
|
-
mergeWithRoot(node) {
|
|
284
|
-
if (!this.root) {
|
|
285
|
-
this.root = node;
|
|
286
|
-
}
|
|
287
|
-
else {
|
|
288
|
-
node.right = this.root.right;
|
|
289
|
-
node.left = this.root;
|
|
290
|
-
this.root.right.left = node;
|
|
291
|
-
this.root.right = node;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
258
|
/**
|
|
295
259
|
* O(1) time operation.
|
|
296
260
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -351,20 +315,6 @@ class FibonacciHeap {
|
|
|
351
315
|
}
|
|
352
316
|
return nodes;
|
|
353
317
|
}
|
|
354
|
-
/**
|
|
355
|
-
* O(log n) time operation.
|
|
356
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
357
|
-
* @param node - The node to be removed.
|
|
358
|
-
* @protected
|
|
359
|
-
*/
|
|
360
|
-
removeFromRoot(node) {
|
|
361
|
-
if (this.root === node)
|
|
362
|
-
this.root = node.right;
|
|
363
|
-
if (node.left)
|
|
364
|
-
node.left.right = node.right;
|
|
365
|
-
if (node.right)
|
|
366
|
-
node.right.left = node.left;
|
|
367
|
-
}
|
|
368
318
|
/**
|
|
369
319
|
* O(log n) time operation.
|
|
370
320
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -382,52 +332,6 @@ class FibonacciHeap {
|
|
|
382
332
|
parent.child.right = node;
|
|
383
333
|
}
|
|
384
334
|
}
|
|
385
|
-
/**
|
|
386
|
-
* O(log n) time operation.
|
|
387
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
388
|
-
* @param y
|
|
389
|
-
* @param x
|
|
390
|
-
* @protected
|
|
391
|
-
*/
|
|
392
|
-
link(y, x) {
|
|
393
|
-
this.removeFromRoot(y);
|
|
394
|
-
y.left = y;
|
|
395
|
-
y.right = y;
|
|
396
|
-
this.mergeWithChild(x, y);
|
|
397
|
-
x.degree++;
|
|
398
|
-
y.parent = x;
|
|
399
|
-
}
|
|
400
|
-
/**
|
|
401
|
-
* O(log n) time operation.
|
|
402
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
403
|
-
* @protected
|
|
404
|
-
*/
|
|
405
|
-
consolidate() {
|
|
406
|
-
const A = new Array(this.size);
|
|
407
|
-
const nodes = this.consumeLinkedList(this.root);
|
|
408
|
-
let x, y, d, t;
|
|
409
|
-
for (const node of nodes) {
|
|
410
|
-
x = node;
|
|
411
|
-
d = x.degree;
|
|
412
|
-
while (A[d]) {
|
|
413
|
-
y = A[d];
|
|
414
|
-
if (this.comparator(x.element, y.element) > 0) {
|
|
415
|
-
t = x;
|
|
416
|
-
x = y;
|
|
417
|
-
y = t;
|
|
418
|
-
}
|
|
419
|
-
this.link(y, x);
|
|
420
|
-
A[d] = undefined;
|
|
421
|
-
d++;
|
|
422
|
-
}
|
|
423
|
-
A[d] = x;
|
|
424
|
-
}
|
|
425
|
-
for (let i = 0; i < this.size; i++) {
|
|
426
|
-
if (A[i] && this.comparator(A[i].element, this.min.element) <= 0) {
|
|
427
|
-
this.min = A[i];
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
335
|
/**
|
|
432
336
|
* O(log n) time operation.
|
|
433
337
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -493,6 +397,102 @@ class FibonacciHeap {
|
|
|
493
397
|
// Clear the heap that was merged
|
|
494
398
|
heapToMerge.clear();
|
|
495
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* Default comparator function used by the heap.
|
|
402
|
+
* @param {E} a
|
|
403
|
+
* @param {E} b
|
|
404
|
+
* @protected
|
|
405
|
+
*/
|
|
406
|
+
defaultComparator(a, b) {
|
|
407
|
+
if (a < b)
|
|
408
|
+
return -1;
|
|
409
|
+
if (a > b)
|
|
410
|
+
return 1;
|
|
411
|
+
return 0;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Create a new node.
|
|
415
|
+
* @param element
|
|
416
|
+
* @protected
|
|
417
|
+
*/
|
|
418
|
+
createNode(element) {
|
|
419
|
+
return new FibonacciHeapNode(element);
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Merge the given node with the root list.
|
|
423
|
+
* @param node - The node to be merged.
|
|
424
|
+
*/
|
|
425
|
+
mergeWithRoot(node) {
|
|
426
|
+
if (!this.root) {
|
|
427
|
+
this.root = node;
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
node.right = this.root.right;
|
|
431
|
+
node.left = this.root;
|
|
432
|
+
this.root.right.left = node;
|
|
433
|
+
this.root.right = node;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* O(log n) time operation.
|
|
438
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
439
|
+
* @param node - The node to be removed.
|
|
440
|
+
* @protected
|
|
441
|
+
*/
|
|
442
|
+
removeFromRoot(node) {
|
|
443
|
+
if (this.root === node)
|
|
444
|
+
this.root = node.right;
|
|
445
|
+
if (node.left)
|
|
446
|
+
node.left.right = node.right;
|
|
447
|
+
if (node.right)
|
|
448
|
+
node.right.left = node.left;
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* O(log n) time operation.
|
|
452
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
453
|
+
* @param y
|
|
454
|
+
* @param x
|
|
455
|
+
* @protected
|
|
456
|
+
*/
|
|
457
|
+
link(y, x) {
|
|
458
|
+
this.removeFromRoot(y);
|
|
459
|
+
y.left = y;
|
|
460
|
+
y.right = y;
|
|
461
|
+
this.mergeWithChild(x, y);
|
|
462
|
+
x.degree++;
|
|
463
|
+
y.parent = x;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* O(log n) time operation.
|
|
467
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
468
|
+
* @protected
|
|
469
|
+
*/
|
|
470
|
+
consolidate() {
|
|
471
|
+
const A = new Array(this.size);
|
|
472
|
+
const nodes = this.consumeLinkedList(this.root);
|
|
473
|
+
let x, y, d, t;
|
|
474
|
+
for (const node of nodes) {
|
|
475
|
+
x = node;
|
|
476
|
+
d = x.degree;
|
|
477
|
+
while (A[d]) {
|
|
478
|
+
y = A[d];
|
|
479
|
+
if (this.comparator(x.element, y.element) > 0) {
|
|
480
|
+
t = x;
|
|
481
|
+
x = y;
|
|
482
|
+
y = t;
|
|
483
|
+
}
|
|
484
|
+
this.link(y, x);
|
|
485
|
+
A[d] = undefined;
|
|
486
|
+
d++;
|
|
487
|
+
}
|
|
488
|
+
A[d] = x;
|
|
489
|
+
}
|
|
490
|
+
for (let i = 0; i < this.size; i++) {
|
|
491
|
+
if (A[i] && this.comparator(A[i].element, this.min.element) <= 0) {
|
|
492
|
+
this.min = A[i];
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
496
|
}
|
|
497
497
|
exports.FibonacciHeap = FibonacciHeap;
|
|
498
498
|
//# sourceMappingURL=heap.js.map
|