-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap - Max.js
168 lines (88 loc) · 2.61 KB
/
Heap - Max.js
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
class MaxHeap {
constructor() {
this.heap = [];
}
// function to get parent index
getParentIndex(index) {
return Math.floor((index - 1) / 2);
}
// function to get left child index
getLeftChildIndex(index) {
return 2 * index + 1;
}
// function to get right child index
getRightChildIndex(index) {
return 2 * index + 2;
}
// function to swap two elements in the heap
swap(index1, index2) {
const temp = this.heap[index1];
this.heap[index1] = this.heap[index2];
this.heap[index2] = temp;
}
// function to heapify up
heapifyUp(index) {
const parentIndex = this.getParentIndex(index);
if (parentIndex >= 0 && this.heap[parentIndex] < this.heap[index]) {
this.swap(index, parentIndex);
this.heapifyUp(parentIndex);
}
}
// function to heapify down
heapifyDown(index) {
const leftChildIndex = this.getLeftChildIndex(index);
const rightChildIndex = this.getRightChildIndex(index);
let maxIndex = index;
if (leftChildIndex < this.heap.length && this.heap[leftChildIndex] > this.heap[maxIndex]) {
maxIndex = leftChildIndex;
}
if (rightChildIndex < this.heap.length && this.heap[rightChildIndex] > this.heap[maxIndex]) {
maxIndex = rightChildIndex;
}
if (maxIndex !== index) {
this.swap(index, maxIndex);
this.heapifyDown(maxIndex);
}
}
// function to insert an element in the heap
insert(value) {
this.heap.push(value);
this.heapifyUp(this.heap.length - 1);
}
// function to remove the maximum element from the heap
remove() {
if (this.heap.length === 0) {
return null;
}
if (this.heap.length === 1) {
return this.heap.pop();
}
const max = this.heap[0];
this.heap[0] = this.heap.pop();
this.heapifyDown(0);
return max;
}
// function to perform heap sort
heapSort() {
const sortedArray = [];
while (this.heap.length > 0) {
sortedArray.push(this.remove());
}
return sortedArray;
}
// function to build heap from an array
buildHeapFromArray(array) {
this.heap = array;
const firstNonLeafIndex = Math.floor((array.length - 2) / 2);
for (let i = firstNonLeafIndex; i >= 0; i--) {
this.heapifyDown(i);
}
}
}
// ==================TEST CASES==================
const maxHeap = new MaxHeap();
maxHeap.buildHeapFromArray([3, 7, 2, 1, 9, 8]);
console.log(maxHeap.heap); // [9, 7, 8, 1, 3, 2]
console.log(maxHeap.remove()); // 9
console.log(maxHeap.heap); // [8, 7, 2, 1, 3]
console.log(maxHeap.heapSort()); // [1, 2, 3, 7, 8]