-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinHeap.cpp
More file actions
140 lines (119 loc) · 3.04 KB
/
MinHeap.cpp
File metadata and controls
140 lines (119 loc) · 3.04 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
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
//============================================================================
// Name : MinHeap.cpp
// Author : SidPro
// Version : 1.0
// Description :
/*
MinHeap creation with all operations.
*/
//============================================================================
#include <climits>
#include <iostream>
using namespace std;
void swap(int &x, int &y){
int temp = x;
x = y;
y = temp;
}
class MinHeap{
public:
int capacity; // maximum possible size of min heap
int heapsize; // Current number of elements in min heap
int * harr; // pointer to array of elements in heap
MinHeap(int size){
capacity = size;
heapsize = 0;
harr = new int[size];
}
void insertKey(int k){
if(heapsize == capacity){
cout<<"\nOverFlow: cloud not insert Key\n";
return;
}
++heapsize;
// first insert new key at the end
int i = heapsize - 1;
harr[i] = k;
while(i!=0 && harr[parent(i)]>harr[i]){
swap(harr[i],harr[parent(i)]);
i = parent(i);
}
}
void printArry(){
cout<<"PRINT Heap Array:\n";
for(int i=0;i<heapsize;++i)
cout<<harr[i]<<" ";
cout<<"\n";
}
int parent(int i){
return (i-1)/2;
}
int left(int i){
return i*2+1;
}
int right(int i){
return i*2+2;
}
int getMin() {
return harr[0];
}
void MinHeapify(int i){
int l = left(i);
int r = right(i);
int smallest = i;
if(l < heapsize && harr[l] < harr[i])smallest=l;
if(r < heapsize && harr[r] < harr[smallest])smallest = r;
if(smallest != i){
swap(harr[i],harr[smallest]);
MinHeapify(smallest);
}
}
// Method to remove minimum element (or root) from min heap
int extractMin(){
if(heapsize<=0)return INT_MAX;
if(heapsize==1){
--heapsize;
return harr[0];
}
// Store the minimum value, and remove it from heap
int root = harr[0];
harr[0] = harr[heapsize-1];
--heapsize;
MinHeapify(0);
return root;
}
void decreaseKey(int i,int new_val){
harr[i] = new_val;
while(i!=0 && harr[parent(i)] > harr[i]){
swap(harr[i],harr[parent(i)]);
i = parent(i);
}
}
// This function deletes key at index i. It first reduced value to minus
// infinite, then calls extractMin()
void deleteKey(int i){
decreaseKey(i,INT_MIN);
extractMin();
}
int linearSearch(int val) {
int i=0;
for (; i < heapsize; i++) {
if (harr[i] == val) {
break;
}
}
return i;
}
};
int main() {
MinHeap obj(6);
obj.insertKey(1);
obj.insertKey(2);
obj.printArry();
obj.insertKey(3);
obj.insertKey(9);
obj.insertKey(10);
obj.insertKey(12);
obj.printArry();
return 0;
}