stack-typed 1.47.4 → 1.47.6
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 +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/data-structures/binary-tree/bst.js +56 -40
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/data-structures/hash/hash-map.js +10 -4
- package/dist/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/data-structures/hash/hash-table.js +50 -5
- package/dist/data-structures/heap/heap.d.ts +25 -22
- package/dist/data-structures/heap/heap.js +101 -41
- package/dist/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/data-structures/heap/max-heap.js +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/data-structures/heap/min-heap.js +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/queue/deque.d.ts +50 -49
- package/dist/data-structures/queue/deque.js +81 -71
- package/dist/data-structures/queue/queue.d.ts +46 -0
- package/dist/data-structures/queue/queue.js +80 -0
- package/dist/data-structures/stack/stack.d.ts +20 -6
- package/dist/data-structures/stack/stack.js +65 -8
- package/dist/data-structures/trie/trie.d.ts +5 -0
- package/dist/data-structures/trie/trie.js +47 -0
- package/dist/interfaces/binary-tree.d.ts +3 -1
- package/dist/types/common.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +115 -46
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
- package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +86 -75
- package/src/data-structures/queue/queue.ts +88 -0
- package/src/data-structures/stack/stack.ts +75 -10
- package/src/data-structures/trie/trie.ts +53 -0
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
|
@@ -9,18 +9,18 @@
|
|
|
9
9
|
export class HashTableNode<K, V> {
|
|
10
10
|
key: K;
|
|
11
11
|
value: V;
|
|
12
|
-
next: HashTableNode<K, V> |
|
|
12
|
+
next: HashTableNode<K, V> | undefined;
|
|
13
13
|
|
|
14
14
|
constructor(key: K, value: V) {
|
|
15
15
|
this.key = key;
|
|
16
16
|
this.value = value;
|
|
17
|
-
this.next =
|
|
17
|
+
this.next = undefined;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
import { HashFunction } from '../../types';
|
|
22
22
|
|
|
23
|
-
export class HashTable<K, V> {
|
|
23
|
+
export class HashTable<K = any, V = any> {
|
|
24
24
|
protected static readonly DEFAULT_CAPACITY = 16;
|
|
25
25
|
protected static readonly LOAD_FACTOR = 0.75;
|
|
26
26
|
|
|
@@ -28,7 +28,7 @@ export class HashTable<K, V> {
|
|
|
28
28
|
this._hashFn = hashFn || this._defaultHashFn;
|
|
29
29
|
this._capacity = Math.max(capacity, HashTable.DEFAULT_CAPACITY);
|
|
30
30
|
this._size = 0;
|
|
31
|
-
this._buckets = new Array<HashTableNode<K, V> |
|
|
31
|
+
this._buckets = new Array<HashTableNode<K, V> | undefined>(this._capacity).fill(undefined);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
protected _capacity: number;
|
|
@@ -43,9 +43,9 @@ export class HashTable<K, V> {
|
|
|
43
43
|
return this._size;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
protected _buckets: Array<HashTableNode<K, V> |
|
|
46
|
+
protected _buckets: Array<HashTableNode<K, V> | undefined>;
|
|
47
47
|
|
|
48
|
-
get buckets(): Array<HashTableNode<K, V> |
|
|
48
|
+
get buckets(): Array<HashTableNode<K, V> | undefined> {
|
|
49
49
|
return this._buckets;
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -125,7 +125,7 @@ export class HashTable<K, V> {
|
|
|
125
125
|
delete(key: K): void {
|
|
126
126
|
const index = this._hash(key);
|
|
127
127
|
let currentNode = this._buckets[index];
|
|
128
|
-
let prevNode: HashTableNode<K, V> |
|
|
128
|
+
let prevNode: HashTableNode<K, V> | undefined = undefined;
|
|
129
129
|
|
|
130
130
|
while (currentNode) {
|
|
131
131
|
if (currentNode.key === key) {
|
|
@@ -135,7 +135,7 @@ export class HashTable<K, V> {
|
|
|
135
135
|
this._buckets[index] = currentNode.next;
|
|
136
136
|
}
|
|
137
137
|
this._size--;
|
|
138
|
-
currentNode.next =
|
|
138
|
+
currentNode.next = undefined; // Release memory
|
|
139
139
|
return;
|
|
140
140
|
}
|
|
141
141
|
prevNode = currentNode;
|
|
@@ -143,6 +143,56 @@ export class HashTable<K, V> {
|
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
* [Symbol.iterator](): Generator<[K, V], void, undefined> {
|
|
147
|
+
for (const bucket of this._buckets) {
|
|
148
|
+
let currentNode = bucket;
|
|
149
|
+
while (currentNode) {
|
|
150
|
+
yield [currentNode.key, currentNode.value];
|
|
151
|
+
currentNode = currentNode.next;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
forEach(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => void): void {
|
|
157
|
+
let index = 0;
|
|
158
|
+
for (const entry of this) {
|
|
159
|
+
callback(entry, index, this);
|
|
160
|
+
index++;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
filter(predicate: (entry: [K, V], index: number, table: HashTable<K, V>) => boolean): HashTable<K, V> {
|
|
165
|
+
const newTable = new HashTable<K, V>();
|
|
166
|
+
let index = 0;
|
|
167
|
+
for (const [key, value] of this) {
|
|
168
|
+
if (predicate([key, value], index, this)) {
|
|
169
|
+
newTable.set(key, value);
|
|
170
|
+
}
|
|
171
|
+
index++;
|
|
172
|
+
}
|
|
173
|
+
return newTable;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
map<T>(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => T): HashTable<K, T> {
|
|
177
|
+
const newTable = new HashTable<K, T>();
|
|
178
|
+
let index = 0;
|
|
179
|
+
for (const [key, value] of this) {
|
|
180
|
+
newTable.set(key, callback([key, value], index, this));
|
|
181
|
+
index++;
|
|
182
|
+
}
|
|
183
|
+
return newTable;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
reduce<T>(callback: (accumulator: T, entry: [K, V], index: number, table: HashTable<K, V>) => T, initialValue: T): T {
|
|
187
|
+
let accumulator = initialValue;
|
|
188
|
+
let index = 0;
|
|
189
|
+
for (const entry of this) {
|
|
190
|
+
accumulator = callback(accumulator, entry, index, this);
|
|
191
|
+
index++;
|
|
192
|
+
}
|
|
193
|
+
return accumulator;
|
|
194
|
+
}
|
|
195
|
+
|
|
146
196
|
/**
|
|
147
197
|
* The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
|
|
148
198
|
* capacity of the data structure.
|
|
@@ -241,7 +291,7 @@ export class HashTable<K, V> {
|
|
|
241
291
|
*/
|
|
242
292
|
protected _expand(): void {
|
|
243
293
|
const newCapacity = this._capacity * 2;
|
|
244
|
-
const newBuckets = new Array<HashTableNode<K, V> |
|
|
294
|
+
const newBuckets = new Array<HashTableNode<K, V> | undefined>(newCapacity).fill(undefined);
|
|
245
295
|
|
|
246
296
|
for (const bucket of this._buckets) {
|
|
247
297
|
let currentNode = bucket;
|
|
@@ -6,13 +6,32 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
9
|
+
import { HeapOptions } from "../../types";
|
|
9
10
|
|
|
10
11
|
export class Heap<E = any> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
options: HeapOptions<E>;
|
|
13
|
+
|
|
14
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>) {
|
|
15
|
+
const defaultComparator = (a: E, b: E) => {
|
|
16
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
|
+
throw new Error('The a, b params of compare function must be number');
|
|
18
|
+
} else {
|
|
19
|
+
return a - b;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (options) {
|
|
23
|
+
this.options = options
|
|
24
|
+
} else {
|
|
25
|
+
this.options = {
|
|
26
|
+
comparator: defaultComparator
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (elements) {
|
|
31
|
+
for (const el of elements) {
|
|
32
|
+
this.push(el);
|
|
33
|
+
}
|
|
34
|
+
// this.fix();
|
|
16
35
|
}
|
|
17
36
|
}
|
|
18
37
|
|
|
@@ -22,12 +41,6 @@ export class Heap<E = any> {
|
|
|
22
41
|
return this._elements;
|
|
23
42
|
}
|
|
24
43
|
|
|
25
|
-
protected _comparator: Comparator<E>;
|
|
26
|
-
|
|
27
|
-
get comparator(): Comparator<E> {
|
|
28
|
-
return this._comparator;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
44
|
/**
|
|
32
45
|
* Get the size (number of elements) of the heap.
|
|
33
46
|
*/
|
|
@@ -46,10 +59,11 @@ export class Heap<E = any> {
|
|
|
46
59
|
/**
|
|
47
60
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
48
61
|
* @returns A new Heap instance.
|
|
62
|
+
* @param elements
|
|
49
63
|
* @param options
|
|
50
64
|
*/
|
|
51
|
-
static heapify<E>(
|
|
52
|
-
return new Heap<E>(options);
|
|
65
|
+
static heapify<E>(elements: Iterable<E>, options: { comparator: Comparator<E> }): Heap<E> {
|
|
66
|
+
return new Heap<E>(elements, options);
|
|
53
67
|
}
|
|
54
68
|
|
|
55
69
|
/**
|
|
@@ -226,29 +240,30 @@ export class Heap<E = any> {
|
|
|
226
240
|
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
227
241
|
* @returns An array containing elements traversed in the specified order.
|
|
228
242
|
*/
|
|
229
|
-
dfs(order: DFSOrderPattern): E[] {
|
|
243
|
+
dfs(order: DFSOrderPattern = 'pre'): E[] {
|
|
230
244
|
const result: E[] = [];
|
|
231
245
|
|
|
232
246
|
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
233
|
-
const
|
|
247
|
+
const _dfs = (index: number) => {
|
|
248
|
+
const left = 2 * index + 1, right = left + 1;
|
|
234
249
|
if (index < this.size) {
|
|
235
250
|
if (order === 'in') {
|
|
236
|
-
|
|
251
|
+
_dfs(left);
|
|
237
252
|
result.push(this.elements[index]);
|
|
238
|
-
|
|
253
|
+
_dfs(right);
|
|
239
254
|
} else if (order === 'pre') {
|
|
240
255
|
result.push(this.elements[index]);
|
|
241
|
-
|
|
242
|
-
|
|
256
|
+
_dfs(left);
|
|
257
|
+
_dfs(right);
|
|
243
258
|
} else if (order === 'post') {
|
|
244
|
-
|
|
245
|
-
|
|
259
|
+
_dfs(left);
|
|
260
|
+
_dfs(right);
|
|
246
261
|
result.push(this.elements[index]);
|
|
247
262
|
}
|
|
248
263
|
}
|
|
249
264
|
};
|
|
250
265
|
|
|
251
|
-
|
|
266
|
+
_dfs(0); // Traverse starting from the root node
|
|
252
267
|
|
|
253
268
|
return result;
|
|
254
269
|
}
|
|
@@ -282,7 +297,7 @@ export class Heap<E = any> {
|
|
|
282
297
|
* @returns A new Heap instance containing the same elements.
|
|
283
298
|
*/
|
|
284
299
|
clone(): Heap<E> {
|
|
285
|
-
const clonedHeap = new Heap<E>(
|
|
300
|
+
const clonedHeap = new Heap<E>([], this.options);
|
|
286
301
|
clonedHeap._elements = [...this.elements];
|
|
287
302
|
return clonedHeap;
|
|
288
303
|
}
|
|
@@ -324,11 +339,70 @@ export class Heap<E = any> {
|
|
|
324
339
|
for (let i = Math.floor(this.size / 2); i >= 0; i--) this._sinkDown(i, this.elements.length >> 1);
|
|
325
340
|
}
|
|
326
341
|
|
|
342
|
+
* [Symbol.iterator]() {
|
|
343
|
+
for (const element of this.elements) {
|
|
344
|
+
yield element;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
forEach(callback: (element: E, index: number, heap: this) => void): void {
|
|
349
|
+
let index = 0;
|
|
350
|
+
for (const el of this) {
|
|
351
|
+
callback(el, index, this);
|
|
352
|
+
index++;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
filter(predicate: (element: E, index: number, heap: Heap<E>) => boolean): Heap<E> {
|
|
357
|
+
const filteredHeap: Heap<E> = new Heap<E>([], this.options);
|
|
358
|
+
let index = 0;
|
|
359
|
+
for (const el of this) {
|
|
360
|
+
if (predicate(el, index, this)) {
|
|
361
|
+
filteredHeap.push(el);
|
|
362
|
+
}
|
|
363
|
+
index++;
|
|
364
|
+
}
|
|
365
|
+
return filteredHeap;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
map<T>(callback: (element: E, index: number, heap: Heap<E>) => T, comparator: Comparator<T>): Heap<T> {
|
|
369
|
+
|
|
370
|
+
const mappedHeap: Heap<T> = new Heap<T>([], { comparator: comparator });
|
|
371
|
+
let index = 0;
|
|
372
|
+
for (const el of this) {
|
|
373
|
+
mappedHeap.add(callback(el, index, this));
|
|
374
|
+
index++;
|
|
375
|
+
}
|
|
376
|
+
return mappedHeap;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
reduce<T>(
|
|
380
|
+
callback: (accumulator: T, currentValue: E, currentIndex: number, heap: Heap<E>) => T,
|
|
381
|
+
initialValue: T
|
|
382
|
+
): T {
|
|
383
|
+
let accumulator: T = initialValue;
|
|
384
|
+
let index = 0;
|
|
385
|
+
for (const el of this) {
|
|
386
|
+
accumulator = callback(accumulator, el, index, this);
|
|
387
|
+
index++;
|
|
388
|
+
}
|
|
389
|
+
return accumulator;
|
|
390
|
+
}
|
|
391
|
+
|
|
327
392
|
/**
|
|
328
393
|
* Time Complexity: O(log n)
|
|
329
394
|
* Space Complexity: O(1)
|
|
330
395
|
*/
|
|
331
396
|
|
|
397
|
+
print(): void {
|
|
398
|
+
console.log([...this]);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Time Complexity: O(n)
|
|
403
|
+
* Space Complexity: O(1)
|
|
404
|
+
*/
|
|
405
|
+
|
|
332
406
|
/**
|
|
333
407
|
* Time Complexity: O(log n)
|
|
334
408
|
* Space Complexity: O(1)
|
|
@@ -341,18 +415,13 @@ export class Heap<E = any> {
|
|
|
341
415
|
while (index > 0) {
|
|
342
416
|
const parent = (index - 1) >> 1;
|
|
343
417
|
const parentItem = this.elements[parent];
|
|
344
|
-
if (this.
|
|
418
|
+
if (this.options.comparator(parentItem, element) <= 0) break;
|
|
345
419
|
this.elements[index] = parentItem;
|
|
346
420
|
index = parent;
|
|
347
421
|
}
|
|
348
422
|
this.elements[index] = element;
|
|
349
423
|
}
|
|
350
424
|
|
|
351
|
-
/**
|
|
352
|
-
* Time Complexity: O(n)
|
|
353
|
-
* Space Complexity: O(1)
|
|
354
|
-
*/
|
|
355
|
-
|
|
356
425
|
/**
|
|
357
426
|
* Time Complexity: O(log n)
|
|
358
427
|
* Space Complexity: O(1)
|
|
@@ -369,12 +438,12 @@ export class Heap<E = any> {
|
|
|
369
438
|
let minItem = this.elements[left];
|
|
370
439
|
if (
|
|
371
440
|
right < this.elements.length &&
|
|
372
|
-
this.
|
|
441
|
+
this.options.comparator(minItem, this.elements[right]) > 0
|
|
373
442
|
) {
|
|
374
443
|
left = right;
|
|
375
444
|
minItem = this.elements[right];
|
|
376
445
|
}
|
|
377
|
-
if (this.
|
|
446
|
+
if (this.options.comparator(minItem, element) >= 0) break;
|
|
378
447
|
this.elements[index] = minItem;
|
|
379
448
|
index = left;
|
|
380
449
|
}
|
|
@@ -401,7 +470,7 @@ export class FibonacciHeapNode<E> {
|
|
|
401
470
|
export class FibonacciHeap<E> {
|
|
402
471
|
constructor(comparator?: Comparator<E>) {
|
|
403
472
|
this.clear();
|
|
404
|
-
this._comparator = comparator || this.
|
|
473
|
+
this._comparator = comparator || this._defaultComparator;
|
|
405
474
|
|
|
406
475
|
if (typeof this.comparator !== 'function') {
|
|
407
476
|
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
@@ -602,7 +671,7 @@ export class FibonacciHeap<E> {
|
|
|
602
671
|
this._root = undefined;
|
|
603
672
|
} else {
|
|
604
673
|
this._min = z.right;
|
|
605
|
-
this.
|
|
674
|
+
this._consolidate();
|
|
606
675
|
}
|
|
607
676
|
|
|
608
677
|
this._size--;
|
|
@@ -654,27 +723,27 @@ export class FibonacciHeap<E> {
|
|
|
654
723
|
heapToMerge.clear();
|
|
655
724
|
}
|
|
656
725
|
|
|
726
|
+
/**
|
|
727
|
+
* Create a new node.
|
|
728
|
+
* @param element
|
|
729
|
+
* @protected
|
|
730
|
+
*/
|
|
731
|
+
createNode(element: E): FibonacciHeapNode<E> {
|
|
732
|
+
return new FibonacciHeapNode<E>(element);
|
|
733
|
+
}
|
|
734
|
+
|
|
657
735
|
/**
|
|
658
736
|
* Default comparator function used by the heap.
|
|
659
737
|
* @param {E} a
|
|
660
738
|
* @param {E} b
|
|
661
739
|
* @protected
|
|
662
740
|
*/
|
|
663
|
-
protected
|
|
741
|
+
protected _defaultComparator(a: E, b: E): number {
|
|
664
742
|
if (a < b) return -1;
|
|
665
743
|
if (a > b) return 1;
|
|
666
744
|
return 0;
|
|
667
745
|
}
|
|
668
746
|
|
|
669
|
-
/**
|
|
670
|
-
* Create a new node.
|
|
671
|
-
* @param element
|
|
672
|
-
* @protected
|
|
673
|
-
*/
|
|
674
|
-
protected createNode(element: E): FibonacciHeapNode<E> {
|
|
675
|
-
return new FibonacciHeapNode<E>(element);
|
|
676
|
-
}
|
|
677
|
-
|
|
678
747
|
/**
|
|
679
748
|
* Time Complexity: O(1)
|
|
680
749
|
* Space Complexity: O(1)
|
|
@@ -731,7 +800,7 @@ export class FibonacciHeap<E> {
|
|
|
731
800
|
* @param x
|
|
732
801
|
* @protected
|
|
733
802
|
*/
|
|
734
|
-
protected
|
|
803
|
+
protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void {
|
|
735
804
|
this.removeFromRoot(y);
|
|
736
805
|
y.left = y;
|
|
737
806
|
y.right = y;
|
|
@@ -752,7 +821,7 @@ export class FibonacciHeap<E> {
|
|
|
752
821
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
753
822
|
* @protected
|
|
754
823
|
*/
|
|
755
|
-
protected
|
|
824
|
+
protected _consolidate(): void {
|
|
756
825
|
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.size);
|
|
757
826
|
const elements = this.consumeLinkedList(this.root);
|
|
758
827
|
let x: FibonacciHeapNode<E> | undefined,
|
|
@@ -773,7 +842,7 @@ export class FibonacciHeap<E> {
|
|
|
773
842
|
y = t;
|
|
774
843
|
}
|
|
775
844
|
|
|
776
|
-
this.
|
|
845
|
+
this._link(y, x);
|
|
777
846
|
A[d] = undefined;
|
|
778
847
|
d++;
|
|
779
848
|
}
|
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { Heap } from './heap';
|
|
10
|
-
import type {
|
|
10
|
+
import type { HeapOptions } from '../../types';
|
|
11
11
|
|
|
12
12
|
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
|
|
14
|
+
elements?: Iterable<E>,
|
|
15
|
+
options: HeapOptions<E> = {
|
|
15
16
|
comparator: (a: E, b: E) => {
|
|
16
17
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
18
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -19,8 +20,7 @@ export class MaxHeap<E = any> extends Heap<E> {
|
|
|
19
20
|
return b - a;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
super(options);
|
|
23
|
+
}) {
|
|
24
|
+
super(elements, options);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { Heap } from './heap';
|
|
10
|
-
import type {
|
|
10
|
+
import type { HeapOptions } from '../../types';
|
|
11
11
|
|
|
12
12
|
export class MinHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
|
|
14
|
+
elements?: Iterable<E>,
|
|
15
|
+
options: HeapOptions<E> = {
|
|
15
16
|
comparator: (a: E, b: E) => {
|
|
16
17
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
18
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -19,8 +20,7 @@ export class MinHeap<E = any> extends Heap<E> {
|
|
|
19
20
|
return a - b;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
super(options);
|
|
23
|
+
}) {
|
|
24
|
+
super(elements, options);
|
|
25
25
|
}
|
|
26
26
|
}
|