-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxHeap.cpp
More file actions
214 lines (128 loc) · 3.15 KB
/
MaxHeap.cpp
File metadata and controls
214 lines (128 loc) · 3.15 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
#include <iostream>
#include <fstream>
#include "MaxHeap.h"
using namespace std;
MaxHeap * initMaxHeap() {
MaxHeap * heap;
heap = new MaxHeap;
heap->size = 0;
return heap;
}
MaxHeap * initMaxHeapFromFile (char filename[100]) {
ifstream inputFile;
MaxHeap * heap;
int index;
int num;
inputFile.open (filename);
if (!inputFile.is_open()) {
cout << "File could not be opened to create heap: " << filename << endl;
exit (1);
}
heap = new MaxHeap;
index = 0;
inputFile >> num;
while (!inputFile.eof()) {
index++;
heap->A[index] = num;
inputFile >> num;
}
inputFile.close();
heap->size = index;
return heap;
}
MaxHeap * initMaxHeapFromArray (int A[], int numElements) {
MaxHeap * heap;
heap = new MaxHeap;
for (int i=0; i<numElements; i++) {
heap->A[i+1] = A[i];
}
heap->size = numElements;
return heap;
}
void displayMaxHeap (MaxHeap * heap) {
int i;
for (i=1; i<=heap->size; i = i + 1)
cout << heap->A[i] << " ";
cout << endl;
}
int sizeMaxHeap (MaxHeap * heap) {
return heap->size;
}
bool isEmptyMaxHeap (MaxHeap * heap) {
return (heap->size == 0);
}
bool maxHeapPropertyHolds (MaxHeap * heap, int i) {
int parent;
if (i < 1 || i > heap->size)
return false;
if (i == 1)
return true;
parent = i / 2;
if (heap->A[parent] >= heap->A[i])
return true;
}
bool isMaxHeap (MaxHeap * heap) {
int heapSize = heap->size;
for (int i=2; i<=heapSize; i++) {
if (!maxHeapPropertyHolds(heap, i))
return false;
}
return true;
}
void maxHeapify (MaxHeap * heap, int i) {
int left, right, largest=i;
left = i * 2;
right = i * 2 + 1;
if (left <= heap->size && heap->A[left] > heap->A[largest])
largest = left;
if (right <= heap->size && heap->A[right] > heap->A[largest])
largest = right;
if (largest != i) {
int temp = heap->A[i];
heap->A[i] = heap->A[largest];
heap->A[largest] = temp;
maxHeapify(heap, largest);
}
}
int maximum (MaxHeap * heap) {
return heap->A[1];
}
int deleteMaxHeap (MaxHeap * heap, int i) {
int toDelete = heap->A[i];
heap->A[i] = heap->A[heap->size];
heap->size = heap->size - 1;
maxHeapify (heap, i);
return toDelete;
}
void insertMaxHeap (MaxHeap * heap, int data) {
heap->size = heap->size + 1;
heap->A[heap->size] = data;
int i = heap->size;
while (i > 1 && heap->A[i/2] < heap->A[i]) {
int temp = heap->A[i/2];
heap->A[i/2] = heap->A[i];
heap->A[i] = temp;
i = i / 2;
}
}
void buildMaxHeap(MaxHeap * heap) {
for (int i = (heap->size/2); i >= 1; i = i - 1){
maxHeapify(heap, i);
displayMaxHeap(heap);
cout<<endl;
}
}
void deleteAllMaxHeap (MaxHeap * heap) {
int size = heap->size;
for (int i=size; i>=1; i--) {
heap->A[i] = deleteMaxHeap (heap, 1);
}
}
void heapSort (int A[], int numElements) {
MaxHeap * heap = initMaxHeapFromArray (A, numElements);
buildMaxHeap (heap);
deleteAllMaxHeap (heap);
for (int i=1; i<=numElements; i++) {
A[i-1] = heap->A[i];
}
}