data-structure-typed 1.42.8 → 1.42.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/CHANGELOG.md +1 -1
- package/README.md +12 -12
- package/benchmark/report.html +12 -12
- package/benchmark/report.json +106 -106
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +388 -201
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/cjs/src/data-structures/binary-tree/bst.js +121 -67
- package/dist/cjs/src/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/cjs/src/data-structures/graph/abstract-graph.js +147 -36
- package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/cjs/src/data-structures/graph/directed-graph.js +126 -0
- package/dist/cjs/src/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/cjs/src/data-structures/graph/undirected-graph.js +63 -0
- package/dist/cjs/src/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/src/data-structures/heap/heap.d.ts +175 -12
- package/dist/cjs/src/data-structures/heap/heap.js +175 -12
- package/dist/cjs/src/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/src/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/cjs/src/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/cjs/src/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/src/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/cjs/src/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/cjs/src/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/src/data-structures/queue/deque.d.ts +113 -3
- package/dist/cjs/src/data-structures/queue/deque.js +113 -3
- package/dist/cjs/src/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/src/data-structures/queue/queue.d.ts +87 -0
- package/dist/cjs/src/data-structures/queue/queue.js +87 -0
- package/dist/cjs/src/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/src/data-structures/stack/stack.d.ts +42 -0
- package/dist/cjs/src/data-structures/stack/stack.js +42 -0
- package/dist/cjs/src/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/src/data-structures/trie/trie.d.ts +76 -0
- package/dist/cjs/src/data-structures/trie/trie.js +76 -1
- package/dist/cjs/src/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +388 -201
- package/dist/mjs/src/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/mjs/src/data-structures/binary-tree/bst.js +121 -67
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/mjs/src/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/mjs/src/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/mjs/src/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/mjs/src/data-structures/graph/abstract-graph.js +147 -36
- package/dist/mjs/src/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/mjs/src/data-structures/graph/directed-graph.js +126 -0
- package/dist/mjs/src/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/mjs/src/data-structures/graph/undirected-graph.js +63 -0
- package/dist/mjs/src/data-structures/heap/heap.d.ts +175 -12
- package/dist/mjs/src/data-structures/heap/heap.js +175 -12
- package/dist/mjs/src/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/mjs/src/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/mjs/src/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/mjs/src/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/mjs/src/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/mjs/src/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/mjs/src/data-structures/queue/deque.d.ts +113 -3
- package/dist/mjs/src/data-structures/queue/deque.js +113 -3
- package/dist/mjs/src/data-structures/queue/queue.d.ts +87 -0
- package/dist/mjs/src/data-structures/queue/queue.js +87 -0
- package/dist/mjs/src/data-structures/stack/stack.d.ts +42 -0
- package/dist/mjs/src/data-structures/stack/stack.js +42 -0
- package/dist/mjs/src/data-structures/trie/trie.d.ts +76 -0
- package/dist/mjs/src/data-structures/trie/trie.js +76 -1
- package/dist/umd/data-structure-typed.min.js +1 -1
- 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 +97 -23
- package/src/data-structures/binary-tree/binary-tree.ts +419 -204
- package/src/data-structures/binary-tree/bst.ts +130 -68
- package/src/data-structures/binary-tree/rb-tree.ts +106 -19
- package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
- package/src/data-structures/graph/abstract-graph.ts +133 -7
- package/src/data-structures/graph/directed-graph.ts +145 -1
- package/src/data-structures/graph/undirected-graph.ts +72 -0
- package/src/data-structures/heap/heap.ts +201 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
- package/src/data-structures/queue/deque.ts +127 -3
- package/src/data-structures/queue/queue.ts +99 -0
- package/src/data-structures/stack/stack.ts +48 -0
- package/src/data-structures/trie/trie.ts +87 -4
- package/test/integration/index.html +24 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +19 -2
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -1
|
@@ -33,21 +33,49 @@ export declare class Heap<E = any> {
|
|
|
33
33
|
comparator: Comparator<E>;
|
|
34
34
|
}): Heap<E>;
|
|
35
35
|
/**
|
|
36
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
37
|
+
* Space Complexity: O(1)
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
41
|
+
* Space Complexity: O(1)
|
|
42
|
+
*
|
|
36
43
|
* Insert an element into the heap and maintain the heap properties.
|
|
37
44
|
* @param element - The element to be inserted.
|
|
38
45
|
*/
|
|
39
46
|
add(element: E): Heap<E>;
|
|
40
47
|
/**
|
|
48
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
49
|
+
* Space Complexity: O(1)
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
53
|
+
* Space Complexity: O(1)
|
|
54
|
+
*
|
|
41
55
|
* Insert an element into the heap and maintain the heap properties.
|
|
42
56
|
* @param element - The element to be inserted.
|
|
43
57
|
*/
|
|
44
58
|
push(element: E): Heap<E>;
|
|
45
59
|
/**
|
|
60
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
61
|
+
* Space Complexity: O(1)
|
|
62
|
+
*/
|
|
63
|
+
/**
|
|
64
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
65
|
+
* Space Complexity: O(1)
|
|
66
|
+
*
|
|
46
67
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
47
68
|
* @returns The top element or undefined if the heap is empty.
|
|
48
69
|
*/
|
|
49
70
|
poll(): E | undefined;
|
|
50
71
|
/**
|
|
72
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
73
|
+
* Space Complexity: O(1)
|
|
74
|
+
*/
|
|
75
|
+
/**
|
|
76
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
77
|
+
* Space Complexity: O(1)
|
|
78
|
+
*
|
|
51
79
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
52
80
|
* @returns The top element or undefined if the heap is empty.
|
|
53
81
|
*/
|
|
@@ -67,49 +95,116 @@ export declare class Heap<E = any> {
|
|
|
67
95
|
*/
|
|
68
96
|
clear(): void;
|
|
69
97
|
/**
|
|
98
|
+
* Time Complexity: O(n), where n is the number of elements in the nodes array.
|
|
99
|
+
* Space Complexity: O(n)
|
|
100
|
+
*/
|
|
101
|
+
/**
|
|
102
|
+
* Time Complexity: O(n), where n is the number of elements in the nodes array.
|
|
103
|
+
* Space Complexity: O(n)
|
|
104
|
+
*
|
|
70
105
|
* Clear and add nodes of the heap
|
|
71
106
|
* @param nodes
|
|
72
107
|
*/
|
|
73
108
|
refill(nodes: E[]): void;
|
|
74
109
|
/**
|
|
110
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
111
|
+
* Space Complexity: O(1)
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
115
|
+
* Space Complexity: O(1)
|
|
116
|
+
*
|
|
75
117
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
76
118
|
* @param element - the element to check.
|
|
77
119
|
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
78
120
|
*/
|
|
79
121
|
has(element: E): boolean;
|
|
80
122
|
/**
|
|
123
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
124
|
+
* Space Complexity: O(h), where h is the height of the heap.
|
|
125
|
+
*/
|
|
126
|
+
/**
|
|
127
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
128
|
+
* Space Complexity: O(h), where h is the height of the heap.
|
|
129
|
+
*
|
|
81
130
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
82
131
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
83
132
|
* @returns An array containing elements traversed in the specified order.
|
|
84
133
|
*/
|
|
85
134
|
dfs(order: DFSOrderPattern): E[];
|
|
86
135
|
/**
|
|
136
|
+
* Time Complexity: O(n)
|
|
137
|
+
* Space Complexity: O(n)
|
|
138
|
+
*/
|
|
139
|
+
/**
|
|
140
|
+
* Time Complexity: O(n)
|
|
141
|
+
* Space Complexity: O(n)
|
|
142
|
+
*
|
|
87
143
|
* Convert the heap to an array.
|
|
88
144
|
* @returns An array containing the elements of the heap.
|
|
89
145
|
*/
|
|
90
146
|
toArray(): E[];
|
|
147
|
+
/**
|
|
148
|
+
* Time Complexity: O(1)
|
|
149
|
+
* Space Complexity: O(1)
|
|
150
|
+
*/
|
|
91
151
|
getNodes(): E[];
|
|
92
152
|
/**
|
|
153
|
+
* Time Complexity: O(n)
|
|
154
|
+
* Space Complexity: O(n)
|
|
155
|
+
*/
|
|
156
|
+
/**
|
|
157
|
+
* Time Complexity: O(n)
|
|
158
|
+
* Space Complexity: O(n)
|
|
159
|
+
*
|
|
93
160
|
* Clone the heap, creating a new heap with the same elements.
|
|
94
161
|
* @returns A new Heap instance containing the same elements.
|
|
95
162
|
*/
|
|
96
163
|
clone(): Heap<E>;
|
|
97
164
|
/**
|
|
165
|
+
* Time Complexity: O(n log n)
|
|
166
|
+
* Space Complexity: O(n)
|
|
167
|
+
*/
|
|
168
|
+
/**
|
|
169
|
+
* Time Complexity: O(n log n)
|
|
170
|
+
* Space Complexity: O(n)
|
|
171
|
+
*
|
|
98
172
|
* Sort the elements in the heap and return them as an array.
|
|
99
173
|
* @returns An array containing the elements sorted in ascending order.
|
|
100
174
|
*/
|
|
101
175
|
sort(): E[];
|
|
102
176
|
/**
|
|
177
|
+
* Time Complexity: O(log n)
|
|
178
|
+
* Space Complexity: O(1)
|
|
179
|
+
*/
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(log n)
|
|
182
|
+
* Space Complexity: O(1)
|
|
183
|
+
*
|
|
103
184
|
* Float operation to maintain heap properties after adding an element.
|
|
104
185
|
* @param index - The index of the newly added element.
|
|
105
186
|
*/
|
|
106
187
|
protected bubbleUp(index: number): void;
|
|
107
188
|
/**
|
|
189
|
+
* Time Complexity: O(log n)
|
|
190
|
+
* Space Complexity: O(1)
|
|
191
|
+
*/
|
|
192
|
+
/**
|
|
193
|
+
* Time Complexity: O(log n)
|
|
194
|
+
* Space Complexity: O(1)
|
|
195
|
+
*
|
|
108
196
|
* Sinking operation to maintain heap properties after removing the top element.
|
|
109
197
|
* @param index - The index from which to start sinking.
|
|
110
198
|
*/
|
|
111
199
|
protected sinkDown(index: number): void;
|
|
112
200
|
/**
|
|
201
|
+
* Time Complexity: O(n)
|
|
202
|
+
* Space Complexity: O(1)
|
|
203
|
+
*/
|
|
204
|
+
/**
|
|
205
|
+
* Time Complexity: O(n)
|
|
206
|
+
* Space Complexity: O(1)
|
|
207
|
+
*
|
|
113
208
|
* Fix the entire heap to maintain heap properties.
|
|
114
209
|
*/
|
|
115
210
|
protected fix(): void;
|
|
@@ -140,28 +235,52 @@ export declare class FibonacciHeap<E> {
|
|
|
140
235
|
*/
|
|
141
236
|
clear(): void;
|
|
142
237
|
/**
|
|
143
|
-
* O(1)
|
|
238
|
+
* Time Complexity: O(1)
|
|
239
|
+
* Space Complexity: O(1)
|
|
240
|
+
*/
|
|
241
|
+
/**
|
|
242
|
+
* Time Complexity: O(1)
|
|
243
|
+
* Space Complexity: O(1)
|
|
244
|
+
*
|
|
144
245
|
* Insert an element into the heap and maintain the heap properties.
|
|
145
246
|
* @param element
|
|
146
247
|
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
147
248
|
*/
|
|
148
249
|
add(element: E): FibonacciHeap<E>;
|
|
149
250
|
/**
|
|
150
|
-
* O(1)
|
|
251
|
+
* Time Complexity: O(1)
|
|
252
|
+
* Space Complexity: O(1)
|
|
253
|
+
*/
|
|
254
|
+
/**
|
|
255
|
+
* Time Complexity: O(1)
|
|
256
|
+
* Space Complexity: O(1)
|
|
257
|
+
*
|
|
151
258
|
* Insert an element into the heap and maintain the heap properties.
|
|
152
259
|
* @param element
|
|
153
260
|
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
154
261
|
*/
|
|
155
262
|
push(element: E): FibonacciHeap<E>;
|
|
156
263
|
/**
|
|
157
|
-
* O(1)
|
|
264
|
+
* Time Complexity: O(1)
|
|
265
|
+
* Space Complexity: O(1)
|
|
266
|
+
*/
|
|
267
|
+
/**
|
|
268
|
+
* Time Complexity: O(1)
|
|
269
|
+
* Space Complexity: O(1)
|
|
270
|
+
*
|
|
158
271
|
* Peek at the top element of the heap without removing it.
|
|
159
272
|
* @returns The top element or undefined if the heap is empty.
|
|
160
273
|
* @protected
|
|
161
274
|
*/
|
|
162
275
|
peek(): E | undefined;
|
|
163
276
|
/**
|
|
164
|
-
* O(
|
|
277
|
+
* Time Complexity: O(n), where n is the number of nodes in the linked list.
|
|
278
|
+
* Space Complexity: O(1)
|
|
279
|
+
*/
|
|
280
|
+
/**
|
|
281
|
+
* Time Complexity: O(n), where n is the number of nodes in the linked list.
|
|
282
|
+
* Space Complexity: O(1)
|
|
283
|
+
*
|
|
165
284
|
* Get the size (number of elements) of the heap.
|
|
166
285
|
* @param {FibonacciHeapNode<E>} head - The head of the linked list.
|
|
167
286
|
* @protected
|
|
@@ -169,26 +288,45 @@ export declare class FibonacciHeap<E> {
|
|
|
169
288
|
*/
|
|
170
289
|
consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[];
|
|
171
290
|
/**
|
|
172
|
-
* O(
|
|
173
|
-
*
|
|
291
|
+
* Time Complexity: O(1)
|
|
292
|
+
* Space Complexity: O(1)
|
|
293
|
+
*
|
|
174
294
|
* @param parent
|
|
175
295
|
* @param node
|
|
176
296
|
*/
|
|
177
297
|
mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void;
|
|
178
298
|
/**
|
|
179
|
-
* O(log n)
|
|
299
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
300
|
+
* Space Complexity: O(1)
|
|
301
|
+
*/
|
|
302
|
+
/**
|
|
303
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
304
|
+
* Space Complexity: O(1)
|
|
305
|
+
*
|
|
180
306
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
181
307
|
* @returns The top element or undefined if the heap is empty.
|
|
182
308
|
*/
|
|
183
309
|
poll(): E | undefined;
|
|
184
310
|
/**
|
|
185
|
-
* O(log n)
|
|
311
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
312
|
+
* Space Complexity: O(1)
|
|
313
|
+
*/
|
|
314
|
+
/**
|
|
315
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
316
|
+
* Space Complexity: O(1)
|
|
317
|
+
*
|
|
186
318
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
187
319
|
* @returns The top element or undefined if the heap is empty.
|
|
188
320
|
*/
|
|
189
321
|
pop(): E | undefined;
|
|
190
322
|
/**
|
|
191
|
-
* O(
|
|
323
|
+
* Time Complexity: O(1)
|
|
324
|
+
* Space Complexity: O(1)
|
|
325
|
+
*/
|
|
326
|
+
/**
|
|
327
|
+
* Time Complexity: O(1)
|
|
328
|
+
* Space Complexity: O(1)
|
|
329
|
+
*
|
|
192
330
|
* merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
|
|
193
331
|
* @param heapToMerge
|
|
194
332
|
*/
|
|
@@ -207,19 +345,38 @@ export declare class FibonacciHeap<E> {
|
|
|
207
345
|
*/
|
|
208
346
|
protected createNode(element: E): FibonacciHeapNode<E>;
|
|
209
347
|
/**
|
|
348
|
+
* Time Complexity: O(1)
|
|
349
|
+
* Space Complexity: O(1)
|
|
350
|
+
*/
|
|
351
|
+
/**
|
|
352
|
+
* Time Complexity: O(1)
|
|
353
|
+
* Space Complexity: O(1)
|
|
354
|
+
*
|
|
210
355
|
* Merge the given node with the root list.
|
|
211
356
|
* @param node - The node to be merged.
|
|
212
357
|
*/
|
|
213
358
|
protected mergeWithRoot(node: FibonacciHeapNode<E>): void;
|
|
214
359
|
/**
|
|
215
|
-
* O(
|
|
360
|
+
* Time Complexity: O(1)
|
|
361
|
+
* Space Complexity: O(1)
|
|
362
|
+
*/
|
|
363
|
+
/**
|
|
364
|
+
* Time Complexity: O(1)
|
|
365
|
+
* Space Complexity: O(1)
|
|
366
|
+
*.
|
|
216
367
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
217
368
|
* @param node - The node to be removed.
|
|
218
369
|
* @protected
|
|
219
370
|
*/
|
|
220
371
|
protected removeFromRoot(node: FibonacciHeapNode<E>): void;
|
|
221
372
|
/**
|
|
222
|
-
* O(
|
|
373
|
+
* Time Complexity: O(1)
|
|
374
|
+
* Space Complexity: O(1)
|
|
375
|
+
*/
|
|
376
|
+
/**
|
|
377
|
+
* Time Complexity: O(1)
|
|
378
|
+
* Space Complexity: O(1)
|
|
379
|
+
*
|
|
223
380
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
224
381
|
* @param y
|
|
225
382
|
* @param x
|
|
@@ -227,7 +384,13 @@ export declare class FibonacciHeap<E> {
|
|
|
227
384
|
*/
|
|
228
385
|
protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
|
|
229
386
|
/**
|
|
230
|
-
* O(log n)
|
|
387
|
+
* Time Complexity: O(n log n), where n is the number of nodes in the heap.
|
|
388
|
+
* Space Complexity: O(n)
|
|
389
|
+
*/
|
|
390
|
+
/**
|
|
391
|
+
* Time Complexity: O(n log n), where n is the number of nodes in the heap.
|
|
392
|
+
* Space Complexity: O(n)
|
|
393
|
+
*
|
|
231
394
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
232
395
|
* @protected
|
|
233
396
|
*/
|
|
@@ -45,6 +45,13 @@ class Heap {
|
|
|
45
45
|
return new Heap(options);
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
49
|
+
* Space Complexity: O(1)
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
53
|
+
* Space Complexity: O(1)
|
|
54
|
+
*
|
|
48
55
|
* Insert an element into the heap and maintain the heap properties.
|
|
49
56
|
* @param element - The element to be inserted.
|
|
50
57
|
*/
|
|
@@ -52,6 +59,13 @@ class Heap {
|
|
|
52
59
|
return this.push(element);
|
|
53
60
|
}
|
|
54
61
|
/**
|
|
62
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
63
|
+
* Space Complexity: O(1)
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
67
|
+
* Space Complexity: O(1)
|
|
68
|
+
*
|
|
55
69
|
* Insert an element into the heap and maintain the heap properties.
|
|
56
70
|
* @param element - The element to be inserted.
|
|
57
71
|
*/
|
|
@@ -61,6 +75,13 @@ class Heap {
|
|
|
61
75
|
return this;
|
|
62
76
|
}
|
|
63
77
|
/**
|
|
78
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
79
|
+
* Space Complexity: O(1)
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
83
|
+
* Space Complexity: O(1)
|
|
84
|
+
*
|
|
64
85
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
65
86
|
* @returns The top element or undefined if the heap is empty.
|
|
66
87
|
*/
|
|
@@ -77,6 +98,13 @@ class Heap {
|
|
|
77
98
|
return topValue;
|
|
78
99
|
}
|
|
79
100
|
/**
|
|
101
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
102
|
+
* Space Complexity: O(1)
|
|
103
|
+
*/
|
|
104
|
+
/**
|
|
105
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
106
|
+
* Space Complexity: O(1)
|
|
107
|
+
*
|
|
80
108
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
81
109
|
* @returns The top element or undefined if the heap is empty.
|
|
82
110
|
*/
|
|
@@ -107,6 +135,13 @@ class Heap {
|
|
|
107
135
|
this._nodes = [];
|
|
108
136
|
}
|
|
109
137
|
/**
|
|
138
|
+
* Time Complexity: O(n), where n is the number of elements in the nodes array.
|
|
139
|
+
* Space Complexity: O(n)
|
|
140
|
+
*/
|
|
141
|
+
/**
|
|
142
|
+
* Time Complexity: O(n), where n is the number of elements in the nodes array.
|
|
143
|
+
* Space Complexity: O(n)
|
|
144
|
+
*
|
|
110
145
|
* Clear and add nodes of the heap
|
|
111
146
|
* @param nodes
|
|
112
147
|
*/
|
|
@@ -115,6 +150,13 @@ class Heap {
|
|
|
115
150
|
this.fix();
|
|
116
151
|
}
|
|
117
152
|
/**
|
|
153
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
154
|
+
* Space Complexity: O(1)
|
|
155
|
+
*/
|
|
156
|
+
/**
|
|
157
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
158
|
+
* Space Complexity: O(1)
|
|
159
|
+
*
|
|
118
160
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
119
161
|
* @param element - the element to check.
|
|
120
162
|
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
@@ -123,6 +165,13 @@ class Heap {
|
|
|
123
165
|
return this.nodes.includes(element);
|
|
124
166
|
}
|
|
125
167
|
/**
|
|
168
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
169
|
+
* Space Complexity: O(h), where h is the height of the heap.
|
|
170
|
+
*/
|
|
171
|
+
/**
|
|
172
|
+
* Time Complexity: O(n), where n is the number of nodes in the heap.
|
|
173
|
+
* Space Complexity: O(h), where h is the height of the heap.
|
|
174
|
+
*
|
|
126
175
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
127
176
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
128
177
|
* @returns An array containing elements traversed in the specified order.
|
|
@@ -153,16 +202,34 @@ class Heap {
|
|
|
153
202
|
return result;
|
|
154
203
|
}
|
|
155
204
|
/**
|
|
205
|
+
* Time Complexity: O(n)
|
|
206
|
+
* Space Complexity: O(n)
|
|
207
|
+
*/
|
|
208
|
+
/**
|
|
209
|
+
* Time Complexity: O(n)
|
|
210
|
+
* Space Complexity: O(n)
|
|
211
|
+
*
|
|
156
212
|
* Convert the heap to an array.
|
|
157
213
|
* @returns An array containing the elements of the heap.
|
|
158
214
|
*/
|
|
159
215
|
toArray() {
|
|
160
216
|
return [...this.nodes];
|
|
161
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Time Complexity: O(1)
|
|
220
|
+
* Space Complexity: O(1)
|
|
221
|
+
*/
|
|
162
222
|
getNodes() {
|
|
163
223
|
return this.nodes;
|
|
164
224
|
}
|
|
165
225
|
/**
|
|
226
|
+
* Time Complexity: O(n)
|
|
227
|
+
* Space Complexity: O(n)
|
|
228
|
+
*/
|
|
229
|
+
/**
|
|
230
|
+
* Time Complexity: O(n)
|
|
231
|
+
* Space Complexity: O(n)
|
|
232
|
+
*
|
|
166
233
|
* Clone the heap, creating a new heap with the same elements.
|
|
167
234
|
* @returns A new Heap instance containing the same elements.
|
|
168
235
|
*/
|
|
@@ -172,6 +239,13 @@ class Heap {
|
|
|
172
239
|
return clonedHeap;
|
|
173
240
|
}
|
|
174
241
|
/**
|
|
242
|
+
* Time Complexity: O(n log n)
|
|
243
|
+
* Space Complexity: O(n)
|
|
244
|
+
*/
|
|
245
|
+
/**
|
|
246
|
+
* Time Complexity: O(n log n)
|
|
247
|
+
* Space Complexity: O(n)
|
|
248
|
+
*
|
|
175
249
|
* Sort the elements in the heap and return them as an array.
|
|
176
250
|
* @returns An array containing the elements sorted in ascending order.
|
|
177
251
|
*/
|
|
@@ -186,6 +260,13 @@ class Heap {
|
|
|
186
260
|
return visitedNode;
|
|
187
261
|
}
|
|
188
262
|
/**
|
|
263
|
+
* Time Complexity: O(log n)
|
|
264
|
+
* Space Complexity: O(1)
|
|
265
|
+
*/
|
|
266
|
+
/**
|
|
267
|
+
* Time Complexity: O(log n)
|
|
268
|
+
* Space Complexity: O(1)
|
|
269
|
+
*
|
|
189
270
|
* Float operation to maintain heap properties after adding an element.
|
|
190
271
|
* @param index - The index of the newly added element.
|
|
191
272
|
*/
|
|
@@ -205,6 +286,13 @@ class Heap {
|
|
|
205
286
|
}
|
|
206
287
|
}
|
|
207
288
|
/**
|
|
289
|
+
* Time Complexity: O(log n)
|
|
290
|
+
* Space Complexity: O(1)
|
|
291
|
+
*/
|
|
292
|
+
/**
|
|
293
|
+
* Time Complexity: O(log n)
|
|
294
|
+
* Space Complexity: O(1)
|
|
295
|
+
*
|
|
208
296
|
* Sinking operation to maintain heap properties after removing the top element.
|
|
209
297
|
* @param index - The index from which to start sinking.
|
|
210
298
|
*/
|
|
@@ -227,6 +315,13 @@ class Heap {
|
|
|
227
315
|
}
|
|
228
316
|
}
|
|
229
317
|
/**
|
|
318
|
+
* Time Complexity: O(n)
|
|
319
|
+
* Space Complexity: O(1)
|
|
320
|
+
*/
|
|
321
|
+
/**
|
|
322
|
+
* Time Complexity: O(n)
|
|
323
|
+
* Space Complexity: O(1)
|
|
324
|
+
*
|
|
230
325
|
* Fix the entire heap to maintain heap properties.
|
|
231
326
|
*/
|
|
232
327
|
fix() {
|
|
@@ -284,7 +379,13 @@ class FibonacciHeap {
|
|
|
284
379
|
this._size = 0;
|
|
285
380
|
}
|
|
286
381
|
/**
|
|
287
|
-
* O(1)
|
|
382
|
+
* Time Complexity: O(1)
|
|
383
|
+
* Space Complexity: O(1)
|
|
384
|
+
*/
|
|
385
|
+
/**
|
|
386
|
+
* Time Complexity: O(1)
|
|
387
|
+
* Space Complexity: O(1)
|
|
388
|
+
*
|
|
288
389
|
* Insert an element into the heap and maintain the heap properties.
|
|
289
390
|
* @param element
|
|
290
391
|
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
@@ -293,7 +394,13 @@ class FibonacciHeap {
|
|
|
293
394
|
return this.push(element);
|
|
294
395
|
}
|
|
295
396
|
/**
|
|
296
|
-
* O(1)
|
|
397
|
+
* Time Complexity: O(1)
|
|
398
|
+
* Space Complexity: O(1)
|
|
399
|
+
*/
|
|
400
|
+
/**
|
|
401
|
+
* Time Complexity: O(1)
|
|
402
|
+
* Space Complexity: O(1)
|
|
403
|
+
*
|
|
297
404
|
* Insert an element into the heap and maintain the heap properties.
|
|
298
405
|
* @param element
|
|
299
406
|
* @returns {FibonacciHeap<E>} FibonacciHeap<E> - The heap itself.
|
|
@@ -310,7 +417,13 @@ class FibonacciHeap {
|
|
|
310
417
|
return this;
|
|
311
418
|
}
|
|
312
419
|
/**
|
|
313
|
-
* O(1)
|
|
420
|
+
* Time Complexity: O(1)
|
|
421
|
+
* Space Complexity: O(1)
|
|
422
|
+
*/
|
|
423
|
+
/**
|
|
424
|
+
* Time Complexity: O(1)
|
|
425
|
+
* Space Complexity: O(1)
|
|
426
|
+
*
|
|
314
427
|
* Peek at the top element of the heap without removing it.
|
|
315
428
|
* @returns The top element or undefined if the heap is empty.
|
|
316
429
|
* @protected
|
|
@@ -319,7 +432,13 @@ class FibonacciHeap {
|
|
|
319
432
|
return this.min ? this.min.element : undefined;
|
|
320
433
|
}
|
|
321
434
|
/**
|
|
322
|
-
* O(
|
|
435
|
+
* Time Complexity: O(n), where n is the number of nodes in the linked list.
|
|
436
|
+
* Space Complexity: O(1)
|
|
437
|
+
*/
|
|
438
|
+
/**
|
|
439
|
+
* Time Complexity: O(n), where n is the number of nodes in the linked list.
|
|
440
|
+
* Space Complexity: O(1)
|
|
441
|
+
*
|
|
323
442
|
* Get the size (number of elements) of the heap.
|
|
324
443
|
* @param {FibonacciHeapNode<E>} head - The head of the linked list.
|
|
325
444
|
* @protected
|
|
@@ -344,8 +463,9 @@ class FibonacciHeap {
|
|
|
344
463
|
return nodes;
|
|
345
464
|
}
|
|
346
465
|
/**
|
|
347
|
-
* O(
|
|
348
|
-
*
|
|
466
|
+
* Time Complexity: O(1)
|
|
467
|
+
* Space Complexity: O(1)
|
|
468
|
+
*
|
|
349
469
|
* @param parent
|
|
350
470
|
* @param node
|
|
351
471
|
*/
|
|
@@ -361,7 +481,13 @@ class FibonacciHeap {
|
|
|
361
481
|
}
|
|
362
482
|
}
|
|
363
483
|
/**
|
|
364
|
-
* O(log n)
|
|
484
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
485
|
+
* Space Complexity: O(1)
|
|
486
|
+
*/
|
|
487
|
+
/**
|
|
488
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
489
|
+
* Space Complexity: O(1)
|
|
490
|
+
*
|
|
365
491
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
366
492
|
* @returns The top element or undefined if the heap is empty.
|
|
367
493
|
*/
|
|
@@ -369,7 +495,13 @@ class FibonacciHeap {
|
|
|
369
495
|
return this.pop();
|
|
370
496
|
}
|
|
371
497
|
/**
|
|
372
|
-
* O(log n)
|
|
498
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
499
|
+
* Space Complexity: O(1)
|
|
500
|
+
*/
|
|
501
|
+
/**
|
|
502
|
+
* Time Complexity: O(log n), where n is the number of nodes in the heap.
|
|
503
|
+
* Space Complexity: O(1)
|
|
504
|
+
*
|
|
373
505
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
374
506
|
* @returns The top element or undefined if the heap is empty.
|
|
375
507
|
*/
|
|
@@ -397,7 +529,13 @@ class FibonacciHeap {
|
|
|
397
529
|
return z.element;
|
|
398
530
|
}
|
|
399
531
|
/**
|
|
400
|
-
* O(
|
|
532
|
+
* Time Complexity: O(1)
|
|
533
|
+
* Space Complexity: O(1)
|
|
534
|
+
*/
|
|
535
|
+
/**
|
|
536
|
+
* Time Complexity: O(1)
|
|
537
|
+
* Space Complexity: O(1)
|
|
538
|
+
*
|
|
401
539
|
* merge two heaps. The heap that is merged will be cleared. The heap that is merged into will remain.
|
|
402
540
|
* @param heapToMerge
|
|
403
541
|
*/
|
|
@@ -447,6 +585,13 @@ class FibonacciHeap {
|
|
|
447
585
|
return new FibonacciHeapNode(element);
|
|
448
586
|
}
|
|
449
587
|
/**
|
|
588
|
+
* Time Complexity: O(1)
|
|
589
|
+
* Space Complexity: O(1)
|
|
590
|
+
*/
|
|
591
|
+
/**
|
|
592
|
+
* Time Complexity: O(1)
|
|
593
|
+
* Space Complexity: O(1)
|
|
594
|
+
*
|
|
450
595
|
* Merge the given node with the root list.
|
|
451
596
|
* @param node - The node to be merged.
|
|
452
597
|
*/
|
|
@@ -462,7 +607,13 @@ class FibonacciHeap {
|
|
|
462
607
|
}
|
|
463
608
|
}
|
|
464
609
|
/**
|
|
465
|
-
* O(
|
|
610
|
+
* Time Complexity: O(1)
|
|
611
|
+
* Space Complexity: O(1)
|
|
612
|
+
*/
|
|
613
|
+
/**
|
|
614
|
+
* Time Complexity: O(1)
|
|
615
|
+
* Space Complexity: O(1)
|
|
616
|
+
*.
|
|
466
617
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
467
618
|
* @param node - The node to be removed.
|
|
468
619
|
* @protected
|
|
@@ -476,7 +627,13 @@ class FibonacciHeap {
|
|
|
476
627
|
node.right.left = node.left;
|
|
477
628
|
}
|
|
478
629
|
/**
|
|
479
|
-
* O(
|
|
630
|
+
* Time Complexity: O(1)
|
|
631
|
+
* Space Complexity: O(1)
|
|
632
|
+
*/
|
|
633
|
+
/**
|
|
634
|
+
* Time Complexity: O(1)
|
|
635
|
+
* Space Complexity: O(1)
|
|
636
|
+
*
|
|
480
637
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
481
638
|
* @param y
|
|
482
639
|
* @param x
|
|
@@ -491,7 +648,13 @@ class FibonacciHeap {
|
|
|
491
648
|
y.parent = x;
|
|
492
649
|
}
|
|
493
650
|
/**
|
|
494
|
-
* O(log n)
|
|
651
|
+
* Time Complexity: O(n log n), where n is the number of nodes in the heap.
|
|
652
|
+
* Space Complexity: O(n)
|
|
653
|
+
*/
|
|
654
|
+
/**
|
|
655
|
+
* Time Complexity: O(n log n), where n is the number of nodes in the heap.
|
|
656
|
+
* Space Complexity: O(n)
|
|
657
|
+
*
|
|
495
658
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
496
659
|
* @protected
|
|
497
660
|
*/
|