-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.h
175 lines (141 loc) · 3.19 KB
/
heap.h
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
/*
* Heap.h
*
* Created on: Apr 27, 2018
* Author: Yuun
*/
#ifndef SRC_HEAP_H_
#define SRC_HEAP_H_
#include "Node.h"
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
template<typename T, typename Compare=std::less<T> >
class Heap {
private:
int size_;
vector<T> storage_;
Compare compare;
void swap(int idx1, int idx2);
void HeapSort(vector<T>& array);
void print();
public:
Heap();
virtual ~Heap();
void Push(T item);
T Pop();
bool is_empty() const;
int size();
void verify();
};
template<typename T, typename Compare>
Heap<T, Compare>::Heap() {
storage_.push_back(T());
size_ = 0;
}
template<typename T, typename Compare>
Heap<T, Compare>::~Heap() {}
template<typename T, typename Compare>
int Heap<T, Compare>::size() {
return size_;
}
template<typename T, typename Compare>
void Heap<T, Compare>::Push(T item) {
storage_.push_back(item);
size_++;
int index = size_;
while (index > 1) {
int parent_idx = index / 2;
T child = storage_[index];
T parent = storage_[parent_idx];
if (compare(child, parent)) {
swap(index, parent_idx);
index = index / 2;
} else {
index = -1;
}
}
}
// It returns the address of the Node pointer.
template<typename T, typename Compare>
T Heap<T, Compare>::Pop() {
// check if size = 0 or not?
T min = storage_[1];
storage_[1] = storage_[size_];
storage_.pop_back();
size_--;
int index = 1;
while (index < size_ / 2 && index > 0) {
int l_child = index * 2;
int r_child = index * 2 + 1;
T current = storage_[index];
T left = storage_[l_child];
T right = storage_[r_child];
if (compare(left, right)) {
if (compare(left, current)) {
swap(l_child, index);
index = l_child;
} else {
index = -1;
}
} else {
if (compare(right, current)) {
swap(r_child, index);
index = r_child;
} else {
index = -1;
}
}
}
return min;
}
template<typename T, typename Compare>
bool Heap<T, Compare>::is_empty() const {
return size_ == 0;
}
template<typename T, typename Compare>
void Heap<T, Compare>::print() {
for (int i = 1; i <= size_; i++) {
cerr << storage_[i] << endl;
}
}
template<typename T, typename Compare>
void Heap<T, Compare>::swap(int idx1, int idx2) {
T temp = storage_[idx1];
storage_[idx1] = storage_[idx2];
storage_[idx2] = temp;
}
template<typename T, typename Compare>
void Heap<T, Compare>::HeapSort(vector<T>& array) {
for (int i = 0; i < array.size(); i++) {
this->Push(array[i]);
}
for (int i = 0; i < array.size(); i++) {
array[i] = this->Pop();
}
}
// function to verify the heap property
template<typename T, typename Compare>
void Heap<T, Compare>::verify() {
cerr << "Heap property check" << endl;
for (int i = 1; i < size_/2; i++) {
T parent = storage_[i];
T left = storage_[i*2];
T right = storage_[i*2 + 1];
if (compare(left, parent) || compare(right, parent)) {
cerr << "Heap Property is violated here." << endl;
return;
}
}
if (size_ % 2 == 1) {
T parent = storage_[size_ / 2];
T last_child = storage_[size_ - 1];
if (compare(last_child, parent)) {
cerr<< "Heap property is violated here" << endl;
return;
}
}
cerr << "Heap property is not violated" << endl;
}
#endif /* SRC_HEAP_H_ */