-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxHeap.js
More file actions
71 lines (68 loc) · 2 KB
/
Copy pathmaxHeap.js
File metadata and controls
71 lines (68 loc) · 2 KB
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
// 0
// / \
// 1 2
// /\ /\
// 3 4 5 6
class MaxHeap {
constructor(){
this.arr = [];
}
left(index){
return (2 * index + 1);
}
right(index){
return (2 * index + 2);
}
parent(index) {
return Math.floor((index-1)/2);
}
swap(index1, index2){
let tmp = this.arr[index1];
this.arr[index1] = this.arr[index2];
this.arr[index2] = tmp;
}
insert(number) {
this.arr.push(number);
let currentIndex = this.arr.length-1;
while(currentIndex > 0 && this.arr[currentIndex] > this.arr[this.parent(currentIndex)] ){
this.swap(currentIndex, this.parent(currentIndex));
currentIndex = this.parent(currentIndex);
}
}
heapify(currentIndex){
let left = this.left(currentIndex);
let right = this.right(currentIndex);
let largeNumIndex = currentIndex;
if(left < this.arr.length && this.arr[left] > this.arr[currentIndex] ){
largeNumIndex = left;
}
if(right < this.arr.length && this.arr[right] > this.arr[largeNumIndex] ){
largeNumIndex = right;
}
if(largeNumIndex != currentIndex) {
this.swap(largeNumIndex, currentIndex);
this.heapify(largeNumIndex);
}
}
extractElement(){
// underflow
if(this.arr.length == 0 ) return "No Element"
let topElement = this.arr[0];
this.swap(0, this.arr.length-1);
this.arr.pop();
this.heapify(0);
return topElement;
}
getTopElement() {
return this.arr[0];
}
}
let maxHeapObj = new MaxHeap();
maxHeapObj.insert(5); console.log(maxHeapObj.arr);
maxHeapObj.insert(1); console.log(maxHeapObj.arr);
maxHeapObj.insert(9); console.log(maxHeapObj.arr);
maxHeapObj.insert(2); console.log(maxHeapObj.arr);
maxHeapObj.insert(7); console.log(maxHeapObj.arr);
console.log(maxHeapObj.getTopElement())
maxHeapObj.extractElement();
console.log(maxHeapObj.arr);