-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_array.cpp
More file actions
200 lines (173 loc) · 3.39 KB
/
heap_array.cpp
File metadata and controls
200 lines (173 loc) · 3.39 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
#include<iostream>
using namespace std;
// g++ -std=c++11 file.cpp
void swap(int *x,int *y){
int temp;
temp = *x;
*x = *y;
*y = temp;
}
class MaxHeap{
int *heap;
int capacity;
int heap_size;
public:
MaxHeap(int capacity);
void MaxHeapify(int );
int parent(int i){
return (i-1)/2;
}
int left(int i){
return 2*i+1;
}
int right(int i){
return 2*(i+1);
}
int extractMax();
void increaseKey(int i,int newValue);
int getMax(){
return heap[0];
}
void deleteKey(int i);
void insertKey(int k);
void heapSort();
void BuildMaxHeap();
void print();
};
/*constructor*/
MaxHeap::MaxHeap(int cap){
heap_size=0;
capacity = cap;
heap = new int[cap];
}
// creates a max-heap for the given array
void MaxHeap::BuildMaxHeap(){
int i;
for(i=heap_size/2-1;i>=0;i--){
MaxHeapify(i);
}
}
/*assumption: left and right satisfy property
and ith element is made to satisfy property*/
void MaxHeap::MaxHeapify(int i){
int l=left(i);
int largest;
if(l<heap_size && heap[l]>heap[i]){
largest = l;
}
else
largest = i;
int r = right(i);
if(r<heap_size && heap[r]>heap[largest])
largest = r;
if(largest!=i){
swap(&heap[i],&heap[largest]);
MaxHeapify(largest);
}
}
/*extract the largest element and arrange the elements to satisfy the property*/
int MaxHeap::extractMax(){
if (heap_size <= 0){
cout<<"ERROR"<<endl;
return 0;
}
if (heap_size == 1)
{
heap_size--;
return heap[0];
}
swap(&heap[0],&heap[heap_size-1]);
heap_size--;
MaxHeapify(0);
return heap[heap_size];
}
/*if a value is changed to higher value*/
void MaxHeap::increaseKey(int i,int new_value){
if(heap[i]>new_value){
cout<<"ERROR!"<<endl;
}
heap[i] = new_value;
while(i!=0 && heap[parent(i)]<heap[i]){
swap(&heap[i],&heap[parent(i)]);
i = parent(i);
}
}
/*delete an element*/
void MaxHeap::deleteKey(int i){
if(i>=heap_size || i<0){
cout<<"ERROR!"<<endl;
return ;
}
increaseKey(i,getMax()+1);
extractMax();
}
/*add an element*/
void MaxHeap::insertKey(int k){
if(heap_size == capacity){
cout<<"ERROR!\n";
return ;
}
heap_size++;
heap[heap_size-1] = k;
int i = heap_size-1;
while(i!=0 && heap[i]>heap[parent(i)]){
swap(&heap[parent(i)],&heap[i]);
i = parent(i);
}
}
/*sorting the heap*/
void MaxHeap::heapSort(){
//BuildMaxHeap();
int i = heap_size-1;
int count=heap_size;
while(i>0){
swap(&heap[0],&heap[i]);
heap_size--;
MaxHeapify(0);
i--;
}
heap_size = count;
// but the array does not satisfy property now
}
void MaxHeap::print(){
int i;
if(heap_size==0)
cout<<"Nothing to print"<<endl;
else{
for(i=0;i<heap_size;i++){
cout<<heap[i]<<" ";
}
cout<<endl;
}
}
int main(){
MaxHeap maxHeap(5);
maxHeap.insertKey(23);
maxHeap.insertKey(5);
maxHeap.insertKey(56);
maxHeap.insertKey(16);
maxHeap.insertKey(79);
cout<<"Sorting the heap: ";
maxHeap.heapSort();
maxHeap.print();
maxHeap.BuildMaxHeap();
cout<<"Maximum element deleted: "<<maxHeap.extractMax()<<endl;
cout<<"Sorting the heap: ";
maxHeap.heapSort();
maxHeap.print();
maxHeap.BuildMaxHeap();
maxHeap.insertKey(50);
cout<<"inserted element: 50"<<endl;
cout<<"Sorting: ";
maxHeap.heapSort();
maxHeap.print();
maxHeap.BuildMaxHeap();
maxHeap.print();
cout<<"Deleted index 2 after Building heap"<<endl;
maxHeap.deleteKey(2);
cout<<"Sorting the heap: ";
maxHeap.heapSort();
maxHeap.print();
maxHeap.BuildMaxHeap();
return 0;
}