-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.test.ts
766 lines (705 loc) · 25.3 KB
/
heap.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
import { beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { FibonacciHeap, HeapNode, numberComparator, Ordering, stringComparator } from '../src'
// A bunch of tests of this test file are taken from "ts-fibonacci-heap" which is under the MIT license
// Copyright (c) 2014 Daniel Imms, http://www.growingwiththeweb.com
// See https://github.com/gwtw/ts-fibonacci-heap/tree/master/src/test
describe('fibonacci heap', () => {
let heap: FibonacciHeap<number>
beforeAll(() => {
const comparator = (n1: number, n2: number) => {
if (n1 === n2) return Ordering.EQ
else if (n1 < n2) return Ordering.LT
return Ordering.GT
}
heap = new FibonacciHeap<number>(comparator)
})
beforeEach(() => heap.clear())
it('should insert strings into heap', () => {
const heap = new FibonacciHeap<string>(stringComparator)
const A1 = heap.insert('A')
const A2 = heap.insert('A')
const B = heap.insert('B')
const C = heap.insert('C')
expect(heap.size).toBe(4)
expect(heap.extractMin()).toEqual(A1)
expect(heap.extractMin()).toEqual(A2)
expect(heap.extractMin()).toEqual(B)
expect(heap.extractMin()).toEqual(C)
expect(heap.size).toBe(0)
})
it('should create an empty heap', () => {
expect(heap).not.toBeNull
})
it('should be possible to add "null" values to the heap', () => {
heap.insert(null!)
expect(heap.size).toBe(1)
expect(heap.minimum().value).toBeNull()
expect(heap.extractMin().value).toBeNull()
expect(heap.size).toBe(0)
heap.insert(null!)
heap.insert(null!)
expect(heap.size).toBe(2)
expect(heap.minimum().value).toBeNull()
expect(heap.extractMin().value).toBeNull()
expect(heap.extractMin().value).toBeNull()
expect(heap.size).toBe(0)
heap.insert(null!)
heap.insert(null!)
heap.insert(null!)
expect(heap.size).toBe(3)
expect(heap.minimum().value).toBeNull()
expect(heap.extractMin().value).toBeNull()
expect(heap.extractMin().value).toBeNull()
expect(heap.extractMin().value).toBeNull()
expect(heap.size).toBe(0)
})
it('should not be possible to add "undefined" values to the heap', () => {
const undef = heap.insert(undefined!)
expect(undef).toBeUndefined()
expect(heap.size).toBe(0)
})
describe('extract correct values', () => {
it('should return correct values (bit bigger heap)', () => {
heap.insert(-10)
heap.insert(0)
heap.insert(-20)
heap.insert(-30)
heap.insert(10)
heap.insert(20)
heap.insert(30)
heap.insert(-40)
heap.insert(40)
heap.insert(-50)
heap.insert(50)
expect(heap.size).toBe(11)
expect(heap.extractMin().value).toBe(-50)
expect(heap.extractMin().value).toBe(-40)
expect(heap.extractMin().value).toBe(-30)
expect(heap.extractMin().value).toBe(-20)
expect(heap.extractMin().value).toBe(-10)
expect(heap.extractMin().value).toBe(0)
expect(heap.extractMin().value).toBe(10)
expect(heap.extractMin().value).toBe(20)
expect(heap.extractMin().value).toBe(30)
expect(heap.extractMin().value).toBe(40)
expect(heap.extractMin().value).toBe(50)
expect(heap.size).toBe(0)
})
it('should return the correct values', () => {
heap.insert(0)
heap.insert(1)
heap.insert(2)
expect(heap.size).toBe(3)
expect(heap.extractMin().value).toBe(0)
expect(heap.extractMin().value).toBe(1)
expect(heap.extractMin().value).toBe(2)
expect(heap.size).toBe(0)
heap.insert(-1)
heap.insert(0)
heap.insert(-2)
expect(heap.size).toBe(3)
expect(heap.extractMin().value).toBe(-2)
expect(heap.extractMin().value).toBe(-1)
expect(heap.extractMin().value).toBe(0)
expect(heap.size).toBe(0)
heap.insert(-1) // {-1}
heap.insert(1) // {-1, 1}
expect(heap.extractMin().value).toBe(-1) // {1}
heap.insert(-1) // {-1, 1}
heap.insert(2) // {-1, 1, 2}
expect(heap.extractMin().value).toBe(-1) // {1, 2}
heap.insert(10) // {1, 2, 10}
expect(heap.extractMin().value).toBe(1) // {2, 10}
heap.insert(-10) // {-10, 2, 10}
expect(heap.extractMin().value).toBe(-10) // {2, 10}
expect(heap.extractMin().value).toBe(2) // {10}
expect(heap.extractMin().value).toBe(10) // {}
})
it('should consolidate 8 nodes into a well formed order 2 tree', () => {
const heap = new FibonacciHeap<number>(numberComparator)
const node0 = heap.insert(0)
const node1 = heap.insert(1)
const node2 = heap.insert(2)
const node3 = heap.insert(3)
const node4 = heap.insert(4)
// Extracting minimum should trigger consolidate.
//
// 1
// /|
// 0--1--2--3--4 -> 3 2
// |
// 4
//
expect(heap.extractMin().value).toBe(node0.value)
expect(heap.size).toBe(4)
expect(node1.parent).toBeFalsy()
expect(numberComparator(node2.parent!.value!, node1.value!)).toEqual(Ordering.EQ)
expect(numberComparator(node3.parent!.value!, node1.value!)).toEqual(Ordering.EQ)
expect(numberComparator(node4.parent!.value!, node3.value!)).toEqual(Ordering.EQ)
expect(numberComparator(node1.right!.value!, node1.value!)).toEqual(Ordering.EQ)
expect(numberComparator(node2.right!.value!, node3.value!)).toEqual(Ordering.EQ)
expect(numberComparator(node3.right!.value!, node2.value!)).toEqual(Ordering.EQ)
expect(numberComparator(node4.right!.value!, node4.value!)).toEqual(Ordering.EQ)
expect(numberComparator(node1.child!.value!, node2.value!)).toEqual(Ordering.EQ)
expect(node2.child).toBeFalsy()
expect(numberComparator(node3.child!.value!, node4.value!)).toEqual(Ordering.EQ)
expect(node4.child).toBeFalsy()
})
it('should consolidate after extract min is called on a tree with a single tree in the root node list', () => {
const heap = new FibonacciHeap<number>(numberComparator)
const node0 = heap.insert(0)
const node1 = heap.insert(1)
const node2 = heap.insert(2)
const node3 = heap.insert(3)
heap.insert(4)
heap.insert(5)
heap.insert(6)
heap.insert(7)
heap.insert(8)
// Extract minimum to trigger a consolidate nodes into a single Fibonacci tree.
//
// __1
// / /|
// 2 c d
// 1--2--3--4--5--6--7--8 -> /| |
// a b f
// |
// e
//
expect(heap.extractMin().value).toBe(node0.value)
// Delete the 2nd smallest node in the heap which is the child of the single node in the root
// list. After this operation is performed the minimum node (1) is no longer pointing the minimum
// child in the node list!
//
// __1
// / /| __1
// 2 c d / /| Note that a in this illustration could also be b, the main thing
// /| | -> a c d to take note of here is that a (or b) may not be the minimum child
// a b f /| | of 1 anymore, despite being the node it's linked to.
// | e b f
// e
//
heap.delete(node2)
// Extracting the minimum node at this point must trigger consolidate on the new list, otherwise
// the next minimum may not be the actual minimum.
//
//
// __1
// / /| a---c---d
// a c d -> /| | -> Consolidate now to ensure the heap's minimum is correct
// /| | e b f
// e b f
//
//
expect(heap.extractMin().value).toBe(node1.value)
expect(heap.minimum().value).toBe(node3.value)
})
})
it('should have the correct size', () => {
expect(() => heap.minimum()).toThrowError('no such element')
expect(() => heap.extractMin()).toThrowError('no such element')
expect(heap.size).toBe(0)
expect(heap.isEmpty()).toBeTruthy()
heap.insert(0)
expect(heap.size).toBe(1)
expect(heap.isEmpty()).toBeFalsy()
heap.insert(-1)
expect(heap.isEmpty()).toBeFalsy()
expect(heap.size).toBe(2)
heap.insert(-2)
expect(heap.isEmpty()).toBeFalsy()
expect(heap.size).toBe(3)
heap.insert(1)
expect(heap.isEmpty()).toBeFalsy()
expect(heap.size).toBe(4)
})
it('should clear the heap', () => {
heap.insert(1)
heap.insert(2)
heap.insert(3)
heap.insert(4)
expect(heap.minNode).not.toBeUndefined
expect(heap.rootList).not.toBeUndefined
expect(heap.size).toBe(4)
heap.clear()
expect(heap.size).toBe(0)
expect(heap.minNode).toBeUndefined()
expect(heap.rootList).toBeUndefined()
expect(typeof heap.comparator).toBe('function')
})
it('should correctly iterate through the heap', () => {
const results = [-100, -10, -1, 0, 1, 10, 100]
heap.insert(0)
heap.insert(1)
heap.insert(-1)
heap.insert(10)
heap.insert(-10)
heap.insert(100)
heap.insert(-100)
let index = 0
expect(heap.size).toBe(7)
for (const heapNode of heap) {
expect(heapNode).toBe(results[index++])
}
expect(heap.size).toBe(7)
index = 0
for (const heapElement of heap) {
expect(heapElement).toBe(results[index++])
}
expect(heap.size).toBe(7)
index = 0
for (const heapNode of heap.nodeIterator()) {
expect(heapNode.value).toBe(results[index++])
}
expect(heap.size).toBe(7)
heap.clear()
let iterationCount = 0
for (let {} of heap) {
iterationCount++
}
expect(iterationCount).toBe(0)
})
describe('union', () => {
it('should union the 2 heaps together given 2 heaps of size 5 with overlapping elements added in order together', () => {
const heap = new FibonacciHeap<number>(numberComparator)
heap.insert(0)
heap.insert(2)
heap.insert(4)
heap.insert(6)
heap.insert(8)
const other = new FibonacciHeap<number>(numberComparator)
other.insert(1)
other.insert(3)
other.insert(5)
other.insert(7)
other.insert(9)
expect(heap.size).toBe(5)
expect(other.size).toBe(5)
heap.union(other)
expect(heap.size).toBe(10)
for (let i = 0; i < 10; i++) {
expect(heap.extractMin().value).toBe(i)
}
expect(heap.isEmpty()).toBeTruthy()
})
it('should union the 2 heaps together given 2 heaps of size 5 with overlapping elements added in reverse order together', () => {
const heap = new FibonacciHeap<number>(numberComparator)
heap.insert(9)
heap.insert(7)
heap.insert(5)
heap.insert(3)
heap.insert(1)
const other = new FibonacciHeap<number>(numberComparator)
other.insert(8)
other.insert(6)
other.insert(4)
other.insert(2)
other.insert(0)
expect(heap.size).toBe(5)
expect(other.size).toBe(5)
heap.union(other)
expect(heap.size).toBe(10)
for (let i = 0; i < 10; i++) {
expect(heap.extractMin().value).toBe(i)
}
expect(heap.isEmpty()).toBeTruthy()
})
it('should union the 2 heaps together', () => {
const heaps = constructJumbledHeaps()
heaps[0].union(heaps[1])
expect(heaps[0].size).toBe(10)
for (let i = 0; i < 10; i++) {
expect(heaps[0].extractMin().value).toBe(i)
}
expect(heaps[0].isEmpty()).toBeTruthy()
})
it('should union the 2 heaps together after extracting the minimum from each', () => {
const heaps = constructJumbledHeaps()
expect(heaps[0].extractMin().value).toBe(1)
expect(heaps[1].extractMin().value).toBe(0)
heaps[0].union(heaps[1])
expect(heaps[0].size).toBe(8)
for (let i = 2; i < 10; i++) {
const min = heaps[0].extractMin()
if (min === null) {
expect(true).toBeFalsy(); return
}
expect(min.value).toBe(i)
}
expect(heaps[0].isEmpty()).toBeTruthy()
})
it('should union an empty heap with another non empty one', () => {
const emptyHeap = new FibonacciHeap(numberComparator)
const otherHeap = new FibonacciHeap(numberComparator)
const n1 = otherHeap.insert(1)
const n2 = otherHeap.insert(2)
expect(emptyHeap.size).toBe(0)
expect(otherHeap.size).toBe(2)
emptyHeap.union(otherHeap)
expect(emptyHeap.size).toBe(2)
expect(otherHeap.size).toBe(2)
expect(emptyHeap.extractMin()).toEqual(n1)
expect(emptyHeap.extractMin()).toEqual(n2)
})
})
describe('delete', () => {
it('should delete the min value of the heap', () => {
const heap = new FibonacciHeap(numberComparator)
const node1 = heap.insert(1)
const node2 = heap.insert(2)
expect(heap.delete(node1)).toEqual(node1)
expect(heap.extractMin()).toEqual(node2)
expect(heap.isEmpty()).toBeTruthy()
})
it('should delete nodes in a heap with multiple elements', () => {
const heap = new FibonacciHeap<number>(numberComparator)
const node3 = heap.insert(13)
const node4 = heap.insert(26)
const node2 = heap.insert(3)
const node1 = heap.insert(-6)
const node5 = heap.insert(27)
expect(heap.size).toBe(5)
expect(heap.delete(node1)).toEqual(node1)
expect(heap.size).toBe(4)
expect(heap.extractMin()).toEqual(node2)
expect(heap.delete(node3)).toEqual(node3)
expect(heap.extractMin()).toEqual(node4)
expect(heap.extractMin()).toEqual(node5)
expect(heap.isEmpty())
})
it('should delete nodes in a flat Fibonacci heap', () => {
const heap = new FibonacciHeap(numberComparator)
const node3 = heap.insert(13)
const node4 = heap.insert(26)
const node2 = heap.insert(3)
const node1 = heap.insert(-6)
const node5 = heap.insert(27)
expect(heap.size).toBe(5)
expect(heap.delete(node3)).toEqual(node3)
expect(heap.size).toBe(4)
expect(heap.extractMin()).toEqual(node1)
expect(heap.extractMin()).toEqual(node2)
expect(heap.extractMin()).toEqual(node4)
expect(heap.extractMin()).toEqual(node5)
expect(heap.isEmpty())
})
it('should cut the node from the tree if the node is not the minimum it does not have a grandparent', () => {
const heap = new FibonacciHeap(numberComparator)
const node1 = heap.insert(1)
const node2 = heap.insert(2)
const node3 = heap.insert(3)
const node4 = heap.insert(4)
// Extract the minimum, forcing the construction of an order 2 tree which
// is changed to an order 0 and order 1 tree after the minimum is extracted.
//
// 1
// /| 3--2
// 1--2--3--4 -> 3 2 -> |
// | 4
// 4
//
expect(heap.extractMin()).toEqual(node1)
// Deleting the node should trigger a cut and cascadingCut on the heap.
expect(heap.delete(node4)).toEqual(node4)
expect(heap.size).toBe(2)
expect(heap.extractMin()).toEqual(node2)
expect(heap.extractMin()).toEqual(node3)
expect(heap.isEmpty()).toBeTruthy()
})
it('should cut the node from the tree if the node is not the minimum and it has a grandparent', () => {
const heap = new FibonacciHeap(numberComparator)
const node0 = heap.insert(0)
const node1 = heap.insert(1)
const node2 = heap.insert(2)
const node3 = heap.insert(3)
const node4 = heap.insert(4)
const node5 = heap.insert(5)
const node6 = heap.insert(6)
const node7 = heap.insert(7)
const node8 = heap.insert(8)
// extractMinimum on 0 should trigger a cut and cascadingCut on the heap.
//
// __1
// / /|
// 5 3 2
// 0--1--2--3--4--5--6--7--8 -> /| |
// 7 6 4
// |
// 8
//
expect(heap.extractMin()).toEqual(node0)
// Delete node 8
//
// __1
// / /| __1
// 5 3 2 / /|
// /| | -> 5 3 2
// 7 6 4 /| |
// | 7 6 4
// 8
//
expect(heap.size).toBe(8)
expect(heap.delete(node8)).toEqual(node8)
expect(heap.minNode).not.toBeNull()
expect(heap.minNode).not.toBeUndefined()
expect(heap.size).toBe(7)
expect(heap.extractMin()).toBe(node1)
expect(heap.extractMin()).toBe(node2)
expect(heap.extractMin()).toBe(node3)
expect(heap.extractMin()).toBe(node4)
expect(heap.extractMin()).toBe(node5)
expect(heap.extractMin()).toBe(node6)
expect(heap.extractMin()).toBe(node7)
expect(heap.isEmpty())
})
it('should cut the node from the tree if the node is not the minimum, it has a grandparent and its parent is marked', () => {
const heap = new FibonacciHeap(numberComparator)
const node0 = heap.insert(0)
const node1 = heap.insert(1)
const node2 = heap.insert(2)
const node3 = heap.insert(3)
const node4 = heap.insert(4)
const node5 = heap.insert(5)
const node6 = heap.insert(6)
const node7 = heap.insert(7)
const node8 = heap.insert(8)
// Extracting minimum should trigger consolidate.
//
// __1
// / /|
// 5 3 2
// 0--1--2--3--4--5--6--7--8 -> /| |
// 7 6 4
// |
// 8
//
expect(heap.extractMin()).toEqual(node0)
// Delete node 6, marking 5
//
// __1 __1
// / /| / /|
// 5 3 2 >5 3 2
// /| | -> / |
// 7 6 4 7 4
// | |
// 8 8
//
expect(heap.delete(node6)).toEqual(node6)
expect(node5.marked)
// Delete node 7, cutting the sub-tree
//
// __1
// / /| 1--5
// >5 3 2 /| |
// / | -> 3 2 8
// 7 4 |
// | 4
// 8
//
expect(heap.delete(node7)).toEqual(node7)
expect(heap.size).toBe(6)
expect(heap.extractMin()).toEqual(node1)
expect(heap.extractMin()).toEqual(node2)
expect(heap.extractMin()).toEqual(node3)
expect(heap.extractMin()).toEqual(node4)
expect(heap.extractMin()).toEqual(node5)
expect(heap.extractMin()).toEqual(node8)
expect(heap.isEmpty())
})
it('should correctly assign an indirect child when a direct child is cut from the parent', () => {
const heap = new FibonacciHeap(numberComparator)
const node0 = heap.insert(0)
heap.insert(1)
heap.insert(2)
heap.insert(3)
heap.insert(4)
const node5 = heap.insert(5)
const node6 = heap.insert(6)
const node7 = heap.insert(7)
heap.insert(8)
// Extracting minimum should trigger consolidate.
//
// __1
// / /|
// 5 3 2
// 0--1--2--3--4--5--6--7--8 -> /| |
// 7 6 4
// |
// 8
//
expect(heap.extractMin()).toEqual(node0)
// Delete node 6, marking 5
//
// __1 __1
// / /| / /|
// 5 3 2 >5 3 2
// /| | -> / |
// 7 6 4 7 4
// | |
// 8 8
//
expect(heap.delete(node6)).toEqual(node6)
expect(node5.child).toEqual(node7)
expect(node5.child).toEqual(node7)
})
})
describe('decreaseKey', () => {
it('should throw an exception given a non-existent node', () => {
const heap = new FibonacciHeap(numberComparator)
expect(() => heap.decreaseKey(<any>undefined, 2)).toThrowError('node to decrease is null!')
expect(() => heap.decreaseKey(<any>null, 2)).toThrowError('node to decrease is null!')
})
it('should throw an exception given a new key larger than the old key', () => {
const heap = new FibonacciHeap(numberComparator)
expect(() => {
const node = heap.insert(1)
heap.decreaseKey(node, 2)
}).toThrowError('new value is greater then old one')
})
it('should decrease the minimum node', () => {
const heap = new FibonacciHeap(numberComparator)
const node1 = heap.insert(1)
heap.insert(2)
heap.decreaseKey(node1, -3)
const value = heap.minimum().value
expect(value).toBe(node1.value)
expect(value).toBe(-3)
})
it('should decrease and bubble up a non-minimum node', () => {
const heap = new FibonacciHeap(numberComparator)
heap.insert(1)
const node2 = heap.insert(2)
heap.decreaseKey(node2, -3)
const value = heap.minimum().value
expect(value).toBe(node2.value)
expect(value).toBe(-3)
})
it('should decrease and bubble up a non-minimum node in a large heap', () => {
const heap = new FibonacciHeap(numberComparator)
heap.insert(13)
heap.insert(26)
heap.insert(3)
heap.insert(-6)
const node5 = heap.insert(27)
heap.insert(88)
heap.insert(59)
heap.insert(-10)
heap.insert(16)
expect(heap.minimum().value).toBe(-10)
heap.decreaseKey(node5, -11)
expect(heap.minimum().value).toBe(node5.value)
})
it('should leave a valid tree on a flat Fibonacci heap', () => {
const heap = new FibonacciHeap(numberComparator)
heap.insert(13)
heap.insert(26)
heap.insert(3)
heap.insert(-6)
heap.insert(27)
const node6 = heap.insert(88)
heap.insert(59)
heap.insert(-10)
heap.insert(16)
heap.decreaseKey(node6, -8)
expect(heap.extractMin().value).toBe(-10)
expect(heap.extractMin().value).toBe(-8)
expect(heap.extractMin().value).toBe(-6)
expect(heap.extractMin().value).toBe(3)
expect(heap.extractMin().value).toBe(13)
expect(heap.extractMin().value).toBe(16)
expect(heap.extractMin().value).toBe(26)
expect(heap.extractMin().value).toBe(27)
expect(heap.extractMin().value).toBe(59)
})
it('should leave a valid tree on a consolidated Fibonacci heap', () => {
const heap = new FibonacciHeap(numberComparator)
const node0 = heap.insert(0)
const node1 = heap.insert(1)
const node2 = heap.insert(2)
const node3 = heap.insert(3)
const node4 = heap.insert(4)
const node5 = heap.insert(5)
const node6 = heap.insert(6)
const node7 = heap.insert(7)
const node8 = heap.insert(8)
// Extracting minimum should trigger consolidate.
//
// __1
// / /|
// 5 3 2
// 0--1--2--3--4--5--6--7--8 -> /| |
// 7 6 4
// |
// 8
//
expect(heap.extractMin()).toEqual(node0)
// Decrease node 8 to 0
//
// __1
// / /| __1--0
// 5 3 2 / /|
// /| | -> 5 3 2
// 7 6 4 /| |
// | 7 6 4
// 8
//
heap.decreaseKey(node8, 0)
expect(node1.right).toEqual(node8)
expect(heap.size).toBe(8)
expect(heap.extractMin()).toEqual(node8)
expect(heap.extractMin()).toEqual(node1)
expect(heap.extractMin()).toEqual(node2)
expect(heap.extractMin()).toEqual(node3)
expect(heap.extractMin()).toEqual(node4)
expect(heap.extractMin()).toEqual(node5)
expect(heap.extractMin()).toEqual(node6)
expect(heap.extractMin()).toEqual(node7)
expect(heap.isEmpty()).toBeTruthy()
})
it('should delete the node\'s parent reference after a cut', () => {
const heap = new FibonacciHeap(numberComparator)
const node1 = heap.insert(1)
heap.insert(2)
const node3 = heap.insert(3)
expect(heap.size).toBe(3)
// Trigger a consolidate
//
// 2
// 1--2--3 -> |
// 3
//
expect(heap.extractMin()).toEqual(node1)
// Decrease 3's key such that it's less than its parent
//
// 2 1
// | -> |
// 3 2
//
heap.decreaseKey(node3, 1)
// Ensure 1's parent is null (the link to 2 has been cut)
expect(node3.parent).toBeFalsy()
})
})
it('should ', () => {
expect(heap['removeFromRootList']).toBeTruthy()
heap['removeFromRootList']({ parent: {} } as HeapNode<number>)
heap['removeFromChildList']({ degree: 0 } as HeapNode<number>, {} as HeapNode<number>)
})
})
function constructJumbledHeaps(): FibonacciHeap<number>[] {
const first = new FibonacciHeap<number>(numberComparator)
first.insert(9)
first.insert(2)
first.insert(6)
first.insert(1)
first.insert(3)
expect(first.size).toBe(5)
const second = new FibonacciHeap<number>(numberComparator)
second.insert(4)
second.insert(8)
second.insert(5)
second.insert(7)
second.insert(0)
expect(second.size).toBe(5)
return [first, second]
}