-
Notifications
You must be signed in to change notification settings - Fork 0
/
heaps.js
94 lines (71 loc) · 2.34 KB
/
heaps.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
class BinaryHeap {
constructor() {
this.list = [];
}
shiftUp() {
let index = this.list.length - 1;
// const parentIndex = Math.floor(index / 2);
while(index > 0) {
const parentIndex = Math.floor((index-1)/ 2);
if(this.list[parentIndex] >= this.list[index]) {
[this.list[index], this.list[parentIndex]] = [this.list[parentIndex], this.list[index]];
// Same like code below
// let temp = this.list[index];
// this.list[index] = this.list[parentIndex];
// this.list[parentIndex] = temp;
index = parentIndex;
} else {
break;
}
}
}
shiftDown(index) {
const leftChild = 2 * index + 1;
const rightChild = 2 * index + 2;
let smallest = index;
const totalData = this.list.length;
if(index <= totalData && this.list[index] > this.list[leftChild]) {
smallest = leftChild;
}
if(index <= totalData && this.list[smallest] > this.list[rightChild]) {
smallest = rightChild;
}
if(smallest !== index) {
// Swap the data between this.list[index] and this.list[smallest] with ES6
[this.list[index], this.list[smallest]] = [this.list[smallest], this.list[index]];
// Same like code below
// let temp = this.list[index];
// this.list[index] = this.list[smallest];
// this.list[smallest] = temp;
// Recursive
this.shiftDown(smallest);
}
}
insertData(value) {
// Add data to the array in the last index
this.list.push(value);
// Do the reorganize (shift up) the data
this.shiftUp();
}
deleteData() {
// Get the root value
let root = this.list[0];
// Root filled with the last data
root = this.list.pop();
// Do the reorganize ( shiftDown )
this.shiftDown(0);
return root;
}
}
const heap = new BinaryHeap();
for(let i = 8; i >= 0; i--) {
heap.insertData(i);
}
// heap.insertData(5);
// heap.insertData(7);
// heap.insertData(8);
// heap.insertData(9);
// heap.insertData(12);
// heap.insertData(18);
// heap.insertData(25);
console.log(heap.list);