-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxHeap.cpp
More file actions
117 lines (99 loc) · 2.4 KB
/
MaxHeap.cpp
File metadata and controls
117 lines (99 loc) · 2.4 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
#include "MaxHeap.h"
MaxHeap::MaxHeap(int max) {
data = new Item*[max];
maxSize = max;
heapSize = 0;
allocated = 1;
}
MaxHeap::~MaxHeap() {
if (allocated) {
allocated = 0;
for (int i = 0; i < heapSize; i++) {
delete data[i];
}
delete[]data;
data = NULL;
}
}
int MaxHeap::Parent(int node) {
return (node - 1) / 2;
}
int MaxHeap::Left(int node) {
return (2 * node + 1);
}
int MaxHeap::Right(int node) {
return (2 * node + 2);
}
void MaxHeap::swap(Item*& a, Item*& b) {
Item* temp = a;
a = b;
b = temp;
int tempIndex = a->getIndex();
a->setIndexInHeap(b->getIndex());
b->setIndexInHeap(tempIndex);
}
void MaxHeap::siftUp(int index) {
if (index == 0) {
return;
}
int parentIndex = Parent(index);
if (data[index]->getPriority() > data[parentIndex]->getPriority()) {
swap(data[index], data[parentIndex]);
siftUp(parentIndex);
}
}
void MaxHeap::siftDown(int index) {
int leftChildIndex = Left(index);
int rightChildIndex = Right(index);
int largest = index;
if (leftChildIndex < heapSize && data[leftChildIndex]->getPriority() > data[largest]->getPriority()) {
largest = leftChildIndex;
}
if (rightChildIndex < heapSize && data[rightChildIndex]->getPriority() > data[largest]->getPriority()) {
largest = rightChildIndex;
}
if (largest != index) {
swap(data[index], data[largest]);
siftDown(largest);
}
}
void MaxHeap::Insert(Item *item) {
data[heapSize] = item;
item->setIndexInHeap(heapSize);
heapSize++;
siftUp(heapSize - 1);
}
void MaxHeap::Max() {
if (heapSize == 0) {
std::cout << "wrong input" << std::endl;
exit(1);
}
data[0]->printStr();
}
Item* MaxHeap::DeleteMax(bool print) {
if (heapSize == 0) {
cout << "wrong input" << endl;
exit(1);
}
heapSize--;
Item* temp = data[0];
data[0] = data[heapSize]; // this is the last one
siftDown(0);
if(print){
temp->printStr();
}
return temp;
}
void MaxHeap::Delete(int index){
heapSize--;
data[index] = data[heapSize];
int parentIndex = Parent(index);
if (data[index]->getPriority() > data[parentIndex]->getPriority())
siftUp(index);
else{
siftDown(index);
}
}
int MaxHeap::getMaxPriority() const{
return data[0]->getPriority();
}