-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_tree.cpp
More file actions
358 lines (318 loc) · 6.89 KB
/
heap_tree.cpp
File metadata and controls
358 lines (318 loc) · 6.89 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include<iostream>
#include<string>
#include<cstdlib>
#include<queue>
#include<vector>
using namespace std;
// g++ -std=c++11 file.cpp
struct tree{
int data;
struct tree *parent = NULL;
struct tree *leftPtr = NULL;
struct tree *rightPtr = NULL;
};
typedef tree tree;
typedef tree *treePtr;
void swap(treePtr x,treePtr y){
treePtr temp = new tree;
temp->data = x->data;
x->data = y->data;
y->data = temp->data;
}
string getDirection(int n){
if(n==0)
return "-1";
string s = "";
while(n>0){
int x = n%2;
n = n/2;
string a = to_string(x) + s;
if(n!=0)
s = a;
}
return s;
}
void order_max(treePtr root,string s,int i){
if(root!=NULL && i<s.length()){
if(s[i]=='1'){i=i+1;
order_max(root->rightPtr,s,i);
if(root->rightPtr!=NULL && root->rightPtr->data > root->data){
swap(root,root->rightPtr);}
}
else if(s[i]=='0'){i=i+1;
order_max(root->leftPtr,s,i);
if(root->leftPtr!=NULL && root->leftPtr->data>root->data)
swap(root,root->leftPtr);
}
}
}
class MaxHeap{
treePtr root = new tree;
treePtr prevPtr;
string direction;
int heap_size;
public:
MaxHeap();
void MaxHeapify(treePtr ptr);
treePtr parent(treePtr ptr);
treePtr left(treePtr ptr){
return ptr->leftPtr;
}
treePtr right(treePtr ptr){
return ptr->rightPtr;
}
int extractMax();
void increaseKey(treePtr ptr,int new_value);
int getMax(){
return (root)->data;
}
treePtr getRootPtr(){
return root;
}
void deleteKey(treePtr ptr);
void insertKey(int k);
vector<int> heapSort();
void BuildMaxHeap(int *a);// creates a max-heap for given array
void BuildMaxHeap(vector<int> a);// creates a max-heap for given vector of integers
void build_heap_property(treePtr root);
void print(treePtr ptr);
};
/*constructor*/
MaxHeap::MaxHeap(){
heap_size = 0;
direction="-1";
}
void MaxHeap::BuildMaxHeap(int *a){
/* accessing every element once,so it should be O(n)*/
root = new tree;
root->data = a[0];
heap_size++;
int n = sizeof(a)/sizeof(*a);cout<<sizeof(a);
queue<treePtr> Queue;
Queue.push(root);
int i=1;
while(i<7){
treePtr temp = Queue.front();
Queue.pop();
treePtr Left = new tree;
Left->data = a[i];
Queue.push(Left);
temp->leftPtr = Left;
heap_size++;i++;
if(i<n){
treePtr Right = new tree;
Right->data = a[i];
Queue.push(Right);
heap_size++;i++;
temp->rightPtr = Right;
}
}
/* T(n) = 2T(n/2) + O(logn)*/
build_heap_property(root);
}
void MaxHeap::BuildMaxHeap(vector<int> a){
root = new tree;
vector<int>:: iterator i=a.begin();
root->data = *i;
heap_size++;
queue<treePtr> Queue;
Queue.push(root);
i++;
while(i<a.end()){
treePtr temp = Queue.front();
Queue.pop();
treePtr Left = new tree;
Left->data = *i;
Left->parent = temp;
Queue.push(Left);
temp->leftPtr = Left;
heap_size++;i++;
if(i<a.end()){
treePtr Right = new tree;
Right->data = *i;
Right->parent = temp;
Queue.push(Right);
heap_size++;i++;
temp->rightPtr = Right;
}
}
build_heap_property(root);
}
/*add an element*/
void MaxHeap::insertKey(int k){
int i=0;
treePtr temp = new tree;
treePtr Parent = new tree;
Parent = root;
temp = root;
for(i=0;i<=direction.length()-1 && direction!="-1";i++){
if(direction[i]=='0'){
if(temp->leftPtr==NULL){
Parent = temp;
temp->leftPtr = new tree;
}
temp = temp->leftPtr;
}
if(direction[i]=='1'){
if(temp->rightPtr==NULL){
Parent = temp;
temp->rightPtr = new tree;
}
temp = temp->rightPtr;
}
}
temp->data = k;
temp->parent = Parent;
treePtr ptr = new tree;
ptr = temp;
heap_size++;
while(ptr->parent!=root || ptr->parent->data < ptr->data){
swap(ptr->parent,ptr);
ptr = ptr->parent;
}
direction = getDirection(heap_size+1);
}
/*assumption: left and right satisfy property
and ith element is made to satisfy property*/
void MaxHeap::MaxHeapify(treePtr ptr){
if(ptr!=NULL){
treePtr temp = new tree;
temp = ptr;
if(ptr->leftPtr!=NULL && ptr->data < ptr->leftPtr->data)
temp = ptr->leftPtr;
if(ptr->rightPtr!=NULL && temp->data < ptr->rightPtr->data)
temp = ptr->rightPtr;
if(temp!=ptr){
swap(ptr,temp);
MaxHeapify(temp);}
}
}
void MaxHeap::build_heap_property(treePtr root){
if(root!=NULL){
build_heap_property(root->leftPtr);
build_heap_property(root->rightPtr);
MaxHeapify(root);
}
}
/*extract the largest element and arrange the elements to satisfy the property*/
int MaxHeap::extractMax(){
if(heap_size==0){
printf("Empty!!!");
return 0;
}
else if(heap_size==1){
heap_size--;
int var = root->data;
direction = "-1";
root = NULL;
return var;
}
else{
heap_size--;
direction = getDirection(heap_size+1);
int num = direction.length();
treePtr temp = new tree;
treePtr prev = new tree;
temp = root;
int i=0,t;
while(i!=num){
prev = temp;
t = 1;
if(direction[i]=='0'){
t = 0;
temp = temp->leftPtr;}
else
temp = temp->rightPtr;
i++;
}
swap(root,temp);
int var = temp->data;
if(t == 0)
prev->leftPtr = NULL;
else
prev->rightPtr = NULL;
MaxHeapify(root);
return var;
}
}
/*if a value is changed to higher value*/
void MaxHeap::increaseKey(treePtr ptr,int new_value){
if(new_value<=ptr->data)
cout<<"ERROR!"<<endl;
else{
ptr->data = new_value;
while(ptr->parent!=root || ptr->parent->data<ptr->data){
swap(ptr,ptr->parent);
ptr=ptr->parent;
}
}
}
/*delete an element*/
void MaxHeap::deleteKey(treePtr ptr){
if(heap_size==0 || ptr==NULL){
cout<<"EMPTY!"<<endl;
}
else{
increaseKey(ptr,getMax()+1);
extractMax();
direction = getDirection(heap_size+1);
}
}
void MaxHeap::print(treePtr ptr){
if(ptr!=NULL){
cout<<ptr->data<<" ";
print(ptr->leftPtr);
print(ptr->rightPtr);
}
}
/*sorting the heap*/
vector<int> MaxHeap::heapSort(){
if(heap_size==0){
cout<<"Nothing to print"<<endl;
}
else{
int n = heap_size;
vector<int> sorted;
while(heap_size>0){
sorted.push_back(extractMax());
}
/*heap does not exist after this*/
BuildMaxHeap(sorted);
return sorted;
}
}
int main(){
MaxHeap maxHeap;
maxHeap.insertKey(23);
maxHeap.insertKey(5);
maxHeap.insertKey(56);
maxHeap.insertKey(16);
maxHeap.insertKey(79);
maxHeap.insertKey(132);
maxHeap.insertKey(569);
cout<<"PreOrder traversal of tree: ";
maxHeap.print(maxHeap.getRootPtr());
cout<<endl;
cout<<"Maximum element deleted: "<<maxHeap.extractMax()<<endl;
cout<<"PreOrder traversal of tree: ";
maxHeap.print(maxHeap.getRootPtr());
cout<<endl;
maxHeap.insertKey(80);
cout<<"inserted element: 80"<<endl;
cout<<"PreOrder traversal of tree after inserting 80: ";
maxHeap.print(maxHeap.getRootPtr());
cout<<endl;
vector<int> temp;
temp = maxHeap.heapSort();
vector<int>::iterator i=temp.begin();
cout<<"Sorted heap: ";
while(i<temp.end()){
cout<<*i<<" ";
i++;
}
cout<<endl;
cout<<"Preorder traversal of heap: ";
maxHeap.print(maxHeap.getRootPtr());
cout<<endl;
return 0;
}