-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminheap.cpp
184 lines (171 loc) · 4.14 KB
/
minheap.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Do NOT add/remove any includes statements from this header file */
/* unless EXPLICTLY clarified on Piazza. */
// #include<iostream>
// using namespace std;
#include "minheap.h"
//Write your code below this line
int log2(int n) {
int result = 0;
while (n >>= 1) {
result++;
}
return result;
}
MinHeap::MinHeap(){
size = 0;
root = NULL;
}
int isone(int n,int i){
n = n>>i;
if((n & 1) == 1){return 1;}
return 0;
}
void MinHeap::push_heap(int num){
HeapNode* node = new HeapNode(num);
if(size == 0){
root = node;
size = 1;
// this->traverse(root);
return;
}
int n = size+1;
HeapNode* current = root;
for(int i = log2(n)-1;i>0;i--){
if (isone(n,i)){
current = current->right;
}
else {
current = current->left;
}
}
if(isone(n,0)){
current->right = node;current->right->par = current;current = current->right;
}
else{
current->left = node;current->left->par = current;current = current->left;
}
while(current->par!= NULL){
if(current->val < current->par->val){
int temp = current->val;
current->val = current->par->val;
current->par->val = temp;
current = current->par;
}
else break;
}
size++;
// this->traverse(root);
}
int MinHeap::get_min(){
if(root!= NULL){
return root->val;
}
else return -2;
}
void swapper(HeapNode* a,HeapNode* b){
int temp = a->val;
a->val = b->val;
b->val = temp;
}
void MinHeap::pop(){
if(size<1){return;}
if(size == 1){
delete root;
root = NULL;
size = 0;
// this->traverse(root);
return;
}
int n = size;
HeapNode* current = root;
for(int i = log2(n)-1;i>=0;i--){//Traversal
if (isone(n,i)){current = current->right;}
else {current = current->left;}
}
int temp = 0;
temp = current->val;
current->val = root->val;
root->val = temp;
if(current == current->par->left){current->par->left = NULL;}
else if(current == current->par->right){current->par->right = NULL;}
delete current;
current = root;
while(current != NULL){
HeapNode* smallest = current;
HeapNode* leftChild = current->left;
HeapNode* rightChild = current->right;
HeapNode* future;
if (leftChild != NULL && leftChild->val < smallest->val) {
smallest = leftChild;future = leftChild;
}
if (rightChild != NULL && rightChild->val < smallest->val) {
smallest = rightChild;future = rightChild;
}
if (smallest != current) {
int temp = current->val;
current->val = smallest->val;
smallest->val = temp;
current = future;
}
else{break;}
}
size--;
// this->traverse(root);
}
MinHeap::~MinHeap(){
if(root!=NULL) delete root;
}
// int main(){
// MinHeap* gojo = new MinHeap;
// // gojo->push_heap(3);
// // cout<<gojo->get_min()<<endl;
// // gojo->pop();
// // cout<<gojo->get_min()<<endl;
// gojo->push_heap(4);
// gojo->push_heap(3);
// gojo->push_heap(7);
// gojo->push_heap(2);
// gojo->push_heap(9);
// gojo->push_heap(11);
// gojo->push_heap(1);
// cout<<gojo->get_min()<<endl;
// gojo->pop();
// cout<<gojo->get_min()<<endl;
// gojo->pop();
// cout<<gojo->get_min()<<endl;
// gojo->pop();
// cout<<gojo->get_min()<<endl;
// gojo->pop();
// cout<<gojo->get_min()<<endl;
// gojo->pop();
// cout<<gojo->get_min()<<endl;
// gojo->pop();
// cout<<gojo->get_min()<<endl;
// gojo->pop();
// cout<<gojo->get_min()<<endl;
// gojo->pop();
// }
//Logic for insertion in node ;
/* Insertion at end : //I need location to put next element i.e.
Insert at 4 : 100 left -> left;
n
n n
n
Insert at 5 : 101 left ->right;
n
n n
n n
Insert at 6 : 110 right ->left;
n
n n
n n n
Insert at 7 : 111 right ->right;
n
n n
n n n n
Insert at 8 : 1000 left->left->left;
n
n n
n n n n
n
*/