-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_heap.cpp
106 lines (88 loc) · 2.32 KB
/
min_heap.cpp
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
/*
C++ code for building a min heap and performing actions on it
*/
#include <bits/stdc++.h>
void delete_root(int heap[], int heap_size){
// here is how to delete the root
heap[0] = heap[heap_size-1];
heap_size--;
int left = 1, right = 2, par = 0;
while(heap[par] > heap[left] || heap[par] > heap[right]){
if(heap[left] < heap[right]){
heap[par] = heap[left]+heap[par];
heap[left] = heap[par]-heap[left];
heap[par] = heap[par]-heap[left];
par = left;
}
else{
heap[par] = heap[right]+heap[par];
heap[right] = heap[par]-heap[right];
heap[par] = heap[par]-heap[right];
par = right;
}
left = 2*par+1;
right = left+1;
}
std::cout << "\nafter root delete:\n";
for(int i = 0; i < heap_size; i++){
std::cout << heap[i] << " ";
}
}
int main(){
int heap[100] , heap_size = 0;
for(int i = 0; i <15 ; i++){
heap[i] = (int)std::rand()%50;
std::cout << heap[i] << " ";
heap_size++;
int parent = (i-1)/2, child = i;
while(parent >= 0 && heap[parent] > heap[child]){
// swap them
heap[parent] = heap[child]+heap[parent];
heap[child] = heap[parent]-heap[child];
heap[parent] = heap[parent]-heap[child];
child = parent;
parent = (child-1)/2;
}
}
std::cout << "\nresult:\n";
for(int i = 0; i < heap_size; i++){
std::cout << heap[i] << " ";
}
/*
Pseudo code for insertion on a heap:
// num is the number to be inserted and "heap_end" is the end point of the heap
Insert(num, heap_end){
heap[heap_end] = num
child = heap_end
parent = (child-1)/2 //is heap index starting from 0
while(parent >=0 && heap[parent] > heap[child]){
swap(heap[parent], heap[child])
child = parent
parent = (child-1)/2
}
heap_end++;
This takes heapify as well
}
*/
delete_root(heap, heap_size);
std::cout << "\n\nafter general element delete:";
//delete element 8
heap[8] = -1; //give it the least possible value
heap_size--;
// float it up
int parent = (8-1)/2, child = 8;
while(parent >= 0 && heap[parent] > heap[child]){
// swap them
heap[parent] = heap[child]+heap[parent];
heap[child] = heap[parent]-heap[child];
heap[parent] = heap[parent]-heap[child];
child = parent;
parent = (child-1)/2;
}
std::cout << "\nsmall value floated up:\n";
for(int i = 0; i < heap_size; i++){
std::cout << heap[i] << " ";
}
delete_root(heap, heap_size);
return 0;
}