-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapInsertionAndCreation.js
66 lines (52 loc) · 1.57 KB
/
HeapInsertionAndCreation.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
//Assuming we are given a max heap already, and we are given a task to insert a new element
let heapArray = [9,8,7,3,2,6];
// 9
// / \
// 8 7
// / \ /
// 3 2 6
//
// Given heap , and suppose element to be inserted is 10.
function insertInHeap(heapArray,element){
// insert at the new element at end of the heap
heapArray.push(element);
let len = heapArray.length;
let i= len - 1;
// compare the inserted element to its parent and move the parent down if its less.
while(i > 0 && element > heapArray[parent(i)]){
heapArray[i] = heapArray[parent(i)];
i = parent(i); // move i to its parent for every iteration
}
// finally copy the new element at the iTH index.
heapArray[i] = element;
return heapArray;
}
let array = [7,3,2,6,9,5,4];
// Given an array, turn it to a max heap
// e.g. an array [7,3,2,6,9,5,4]
// 7 9
// / \ / \
// 3 2 =====> 7 5
// / \ / \ / \ / \
// 6 9 5 4 3 6 2 4
//
//
function createMaxHeap(array){
let len = array.length;
for(let i=1; i < len; i++){
let element = array[i];
let j = i;
while(j > 0 && element > array[parent(j)] ){
array[j] = array[parent(j)];
j = parent(j);
}
array[j] = element;
}
}
function parent(i){
return Math.floor((i-1)/2);
}
insertInHeap(heapArray,10);
console.log(heapArray);
createMaxHeap(array);
console.log(array);