@@ -23,61 +23,89 @@ const isRootNode = index => {
23
23
return index === 0 ;
24
24
} ;
25
25
26
-
27
26
const isEmpty = ( element ) => {
28
27
return element === undefined || element === null ;
29
28
}
30
29
31
-
32
30
const heapifyUp = ( array , index ) => {
31
+ index = index || array . length - 1 ;
33
32
const parentIndex = getParentIndex ( index ) ;
34
33
if ( parentIndex >= 0 && array [ index ] > array [ parentIndex ] ) {
35
34
swap ( array , index , parentIndex ) ;
36
35
heapifyUp ( array , parentIndex ) ;
37
36
}
38
37
}
39
38
40
-
41
39
const heapifyDown = ( array , index ) => {
42
40
const element = array [ index ] ;
43
41
const leftChildIndex = getLeftChildIndex ( index ) ;
44
42
const rightChildIndex = getRightChildIndex ( index ) ;
45
43
const leftChildElement = array [ leftChildIndex ] ;
46
44
const rightChildElement = array [ rightChildIndex ] ;
45
+ let maxIndex = null ;
46
+
47
47
if ( isEmpty ( leftChildElement ) && isEmpty ( rightChildElement ) ) {
48
48
return ;
49
49
}
50
+
51
+ if ( isEmpty ( leftChildElement ) ) {
52
+ maxIndex = rightChildIndex ;
53
+ }
54
+ else if ( isEmpty ( rightChildElement ) ) {
55
+ maxIndex = leftChildIndex ;
56
+ }
50
57
else {
51
- if ( element < leftChildElement || element < rightChildElement ) {
52
- const maxIndex = getMaxIndex ( array , leftChildIndex , rightChildIndex ) ;
53
- swap ( array , index , maxIndex ) ;
54
- heapifyDown ( array , maxIndex ) ;
55
- }
58
+ maxIndex = getMaxIndex ( array , leftChildIndex , rightChildIndex ) ;
56
59
}
57
- }
58
60
61
+ if ( element <= leftChildElement || element < rightChildElement ) {
62
+ swap ( array , index , maxIndex ) ;
63
+ heapifyDown ( array , maxIndex ) ;
64
+ }
65
+
66
+ }
59
67
60
- // delete the root node
61
68
const deleteNode = ( array ) => {
62
69
swap ( array , 0 , array . length - 1 ) ;
63
70
array . length = array . length - 1 ;
64
- heapifyDown ( array , 0 )
71
+ heapifyDown ( array , 0 ) ;
65
72
}
66
73
67
74
const insertNode = ( array , value ) => {
68
75
array . push ( value ) ;
69
- heapifyUp ( array , array . length - 1 )
76
+ heapifyUp ( array )
70
77
}
71
78
72
79
const buildHeap = ( array , element ) => {
73
- for ( let index = array . length - 1 ; index > = array . length / 2 ; index -- ) {
80
+ for ( let index = Math . floor ( array . length / 2 ) ; index < = array . length - 1 ; index ++ ) {
74
81
heapifyUp ( array , index ) ;
75
82
}
76
83
84
+ // for (let index = Math.floor(array.length / 2); index >= 0; index--) {
85
+ // heapifyDown(array, index);
86
+ // }
77
87
}
78
88
79
- // const heap = [0, 1, 2, 3, 4, 5, 6];
80
- // buildHeap(heap);
89
+
90
+ const heapSort = ( array ) => {
91
+ buildHeap ( array ) ;
92
+ const sortedArray = [ ] ;
93
+ while ( array . length > 0 ) {
94
+ swap ( array , 0 , array . length - 1 ) ;
95
+ sortedArray [ array . length - 1 ] = array [ array . length - 1 ] ;
96
+ array . length = array . length - 1 ;
97
+ heapifyDown ( array , 0 ) ;
98
+ }
99
+
100
+ array . push ( ...sortedArray ) ;
101
+ sortedArray . length = 0 ;
102
+ }
103
+
104
+ // Arr[ N/2+1 ] to Arr[ N ] are the leaf nodes
105
+
106
+ const array = [ 4 , 1 , 5 , 3 , 0 , 2 , 6 ] ;
107
+ // heapSort(array);
108
+ // buildHeap(array);
81
109
// const heap = [6, 4, 5, 3, 1, 0, 2];
82
110
// deleteNode(array);
83
111
// const heap = [];
@@ -89,12 +117,12 @@ const buildHeap = (array, element) => {
89
117
// insertNode(heap, 2, heap.length);
90
118
// insertNode(heap, 3, heap.length);
91
119
// console.log(heap);
92
- // deleteNode(heap );
93
- // deleteNode(heap );
94
- // deleteNode(heap );
95
- // console.log(heap );
96
- // insertNode(heap , 2);
97
- // insertNode(heap , 3);
98
- // console.log(heap );
120
+ // deleteNode(array );
121
+ // deleteNode(array );
122
+ // deleteNode(array );
123
+ // console.log(array );
124
+ // insertNode(array , 2);
125
+ // insertNode(array , 3);
126
+ console . log ( array ) ;
99
127
100
128
0 commit comments