-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminHeap.cpp
More file actions
107 lines (97 loc) · 2.22 KB
/
minHeap.cpp
File metadata and controls
107 lines (97 loc) · 2.22 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
#include<iostream>
#include<vector>
using namespace std;
class Heap{
private:
vector<int> heap;
int currentPos = -1;
public:
void insert(int data){
heap.push_back(data);
currentPos+=1;
heapifyUp(currentPos);
}
void heapifyUp(int index){
int parent = (index-1)/2;
while(parent >=0 && (heap[parent] > heap[index])){
int temp = heap[parent];
heap[parent] = heap[index];
heap[index] = temp;
index = parent;
parent = (index-1)/2;
}
}
void poll(){
heap[0] = heap.back();
heap.pop_back();
currentPos -= 1;
heapifyDown(0);
}
void remove(int data){
int index = findIndex(data);
if(index==-1){
cout<<"Element not present"<<endl;
return;
}
heap[index] = heap.back();
heap.pop_back();
currentPos -= 1;
heapifyDown(index);
heapifyUp(index);
}
int findIndex(int key){
for(int i=0; i<heap.size(); i++){
if(heap[i]==key){
return i;
}
}
return -1;
}
void heapifyDown(int start){
int left = 2*start + 1;
int right = 2*start + 2;
int smallest = start;
if(left < heap.size() && heap[left] < heap[smallest]){
smallest = left;
}
if(right < heap.size() && heap[right] < heap[smallest]){
smallest = right;
}
if(smallest!=start){
int temp = heap[smallest];
heap[smallest] = heap[start];
heap[start] = temp;
heapifyDown(smallest);
}
}
void traverse(){
int i = 0;
for(i=0; i<heap.size(); i++){
cout<<heap[i]<<" ";
}
}
int peek(){
if(currentPos==-1){
return -1;
}
return heap[0];
}
int size(){
return heap.size();
}
};
int main(){
//btw you can use priority_queue<int, vector<int>, greater<int>> heap;
//.top(), .pop(), push(), .size()
Heap *h = new Heap();
h->insert(1);
h->insert(3);
h->insert(2);
h->insert(5);
h->insert(6);
h->insert(4);
h->insert(7);
h->poll();
h->traverse();
return 0;
}