@@ -26,7 +26,7 @@ export class MinHeap {
26
26
clear ( ) {
27
27
this . heap = [ ] ;
28
28
}
29
- find ( ) {
29
+ findMinimum ( ) {
30
30
return this . isEmpty ( ) ? undefined : this . heap [ 0 ] ;
31
31
}
32
32
insert ( value ) {
@@ -43,12 +43,15 @@ export class MinHeap {
43
43
const left = this . getLeftIndex ( index ) ;
44
44
const right = this . getRightIndex ( index ) ;
45
45
const size = this . size ( ) ;
46
- if ( left < size && this . compareFn ( this . heap [ element ] , this . heap [ left ] ) > Compare . BIGGER_THAN ) {
46
+ if (
47
+ left < size &&
48
+ this . compareFn ( this . heap [ element ] , this . heap [ left ] ) === Compare . BIGGER_THAN
49
+ ) {
47
50
element = left ;
48
51
}
49
52
if (
50
53
right < size &&
51
- this . compareFn ( this . heap [ element ] , this . heap [ right ] ) > Compare . BIGGER_THAN
54
+ this . compareFn ( this . heap [ element ] , this . heap [ right ] ) === Compare . BIGGER_THAN
52
55
) {
53
56
element = right ;
54
57
}
@@ -59,7 +62,10 @@ export class MinHeap {
59
62
}
60
63
siftUp ( index ) {
61
64
let parent = this . getParentIndex ( index ) ;
62
- while ( index > 0 && this . compareFn ( this . heap [ parent ] , this . heap [ index ] ) > Compare . BIGGER_THAN ) {
65
+ while (
66
+ index > 0 &&
67
+ this . compareFn ( this . heap [ parent ] , this . heap [ index ] ) === Compare . BIGGER_THAN
68
+ ) {
63
69
swap ( this . heap , parent , index ) ;
64
70
index = parent ;
65
71
parent = this . getParentIndex ( index ) ;
@@ -79,11 +85,15 @@ export class MinHeap {
79
85
heapify ( array ) {
80
86
if ( array ) {
81
87
this . heap = array ;
82
- this . heap . unshift ( null ) ; // remove all null elements
83
88
}
84
- for ( let i = this . size ( ) - 1 ; i > 0 ; i -- ) {
89
+ const maxIndex = Math . floor ( this . size ( ) / 2 ) - 1 ;
90
+ for ( let i = 0 ; i <= maxIndex ; i ++ ) {
85
91
this . siftDown ( i ) ;
86
92
}
93
+ return this . heap ;
94
+ }
95
+ getAsArray ( ) {
96
+ return this . heap ;
87
97
}
88
98
}
89
99
export class MaxHeap extends MinHeap {
0 commit comments