-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.cpp
More file actions
126 lines (101 loc) · 2.64 KB
/
Copy pathheap.cpp
File metadata and controls
126 lines (101 loc) · 2.64 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
#include<iostream>
using namespace std;
class Heap{
public:
int size=0;
int capacity=10;
int *items = new int[capacity];
Heap(){
cout<<"Heap: Hey, I am heap. what you got?"<<endl;
}
~Heap(){
cout<<"Heap: Ouch !!, What happened ?!, Where are you going ?."<<endl;
}
int get_left_child_index(int parent_index){ return 2*parent_index + 1; }
int get_right_child_index(int parent_index){ return 2*parent_index + 2; }
int get_parent_index(int child_index){ return (child_index-1)/2; }
bool has_left_child(int index){ return get_left_child_index(index) < size; }
bool has_right_child(int index){ return get_right_child_index(index) < size; }
bool has_parent(int index){ return get_parent_index(index) >= 0 ;}
int left_child(int index){ return items[get_left_child_index(index)]; }
int right_child(int index){ return items[get_right_child_index(index)]; }
int parent(int index){ return items[get_parent_index(index)]; }
void swap(int idx1, int idx2){
int temp=items[idx1];
items[idx1]=items[idx2];
items[idx2]=temp;
}
void ensure_extra_capacity(){
if(size==capacity){
int *temp = new int[2*capacity];
//copy(begin(items), end(items), begin(temp));
for(int i=0;i<size;++i){
temp[i]=items[i];
}
delete[] items;
items=temp;
capacity=2*capacity;
}
}
};
class MinHeap: public Heap{
public:
MinHeap(){ cout<<"MinHeap: Hi, My name is MinHeap and I am in your service."<<endl<<endl; }
~MinHeap(){ cout<<endl<<"MinHeap: Hey, It was good to work with you. :)"<<endl; }
int top(){
if(size==0){
cout<<"MinHeap: Panic: MinHeap Empty !!"<<endl<<endl;
exit(0);
}
return items[0];
}
int pop(){
if(size==0){
cout<<"MinHeap: Panic: MinHeap Empty !!"<<endl<<endl;
exit(0);
}
int item=items[0];
items[0]=items[size-1];
size--;
heapify_down();
return item;
}
int push(int item){
ensure_extra_capacity();
items[size]=item;
size++;
heapify_up();
}
void heapify_up(){
int index=size-1;
while(has_parent(index) && items[get_parent_index(index)] > items[index]){
swap(get_parent_index(index), index);
index=get_parent_index(index);
}
}
void heapify_down(){
int index=0;
while(has_left_child(index)){
int smaller_child_index=get_left_child_index(index);
if(has_right_child(index) && items[get_right_child_index(index)] < items[smaller_child_index]){
smaller_child_index=get_right_child_index(index);
}
if(items[index] < items[smaller_child_index]){
break;
}
else{
swap(index, smaller_child_index);
}
index=smaller_child_index;
}
}
};
int main(){
MinHeap m;
m.push(10);
m.push(15);
m.push(20);
m.push(17);
m.push(25);
cout<<m.top()<<endl;
}