-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheap.cc
More file actions
160 lines (131 loc) · 3.13 KB
/
heap.cc
File metadata and controls
160 lines (131 loc) · 3.13 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
#include <vector>
#include <iostream>
#include <stdexcept>
class Heap
{
public:
explicit Heap(int capacity = 100): array(capacity), currentSize(0) {}
explicit Heap(const std::vector<int> & item): array(item.size() + 10), currentSize(item.size())
{
for (int i = 0; i < item.size(); ++i)
array[i + 1] = item[i];
buildHeap();
}
~Heap() = default;
friend std::ostream &operator<<(std::ostream &, const Heap &);
friend std::vector<int> heapSort(Heap &);
bool isEmpty() const;
const int & findTop() const;
void insert(const int &x);
void deleteTop();
int popTop();
// void deleteTop( int & minItem);
// void makeEmpty();
private:
int currentSize;
std::vector<int> array;
void buildHeap();
void percolateDown(int hole);
};
std::vector<int> heapSort(Heap &h)
{
int originalSize = h.currentSize;
std::vector<int> res(originalSize);
for (int i = 0; i < originalSize; ++i)
{
res[i] = h.popTop();
}
return res;
}
std::ostream &operator<<(std::ostream &os, const Heap &h)
{
for (int i = 1; i <= h.currentSize; ++i)
{
os << h.array[i] << ",";
}
os << "\b ";
return os;
}
bool Heap::isEmpty() const
{
if (currentSize == 0)
return true;
return false;
}
const int & Heap::findTop() const
{
if (isEmpty())
throw std::underflow_error("oooh!");
return array[1];
}
void Heap::insert(const int &x)
{
++currentSize;
if (currentSize > array.size() - 1)
array.resize(array.size() * 2);
int pos = currentSize;
for (; pos > 1 && array[pos / 2] < x; pos /= 2)
{
array[pos] = array[pos / 2];
}
array[pos] = x;
}
void Heap::deleteTop()
{
if (isEmpty())
throw std::underflow_error("oooh!");
array[1] = array[currentSize--];
percolateDown(1);
}
int Heap::popTop()
{
if (isEmpty())
throw std::underflow_error("oooh!");
int res = array[1];
array[1] = array[currentSize--];
percolateDown(1);
return res;
}
void Heap::buildHeap()
{
for (int i = currentSize / 2; i > 0; --i)
percolateDown(i);
}
void Heap::percolateDown(int hole)
{
int child;
auto e = array[hole];
for (; (hole << 1) <= currentSize && e < array[hole << 1]; hole=child)
{
child = hole << 1;
if (child + 1 <= currentSize && array[child] < array[child + 1])
++child;
array[hole] = array[child];
}
array[hole] = e;
}
int main()
{
std::vector<int> v1 = {5, 2, 6, 9, 10, 1, 3};
for (auto e : v1)
std::cout << e << std::endl;
Heap h = Heap(5);
std::cout << h.isEmpty() << std::endl;
for (int i = 0; i < 10; ++i)
h.insert(i);
std::cout << h << std::endl;
h.deleteTop();
std::cout << h << std::endl;
Heap h2 = Heap(v1);
std::cout << h2 << std::endl;
std::cout << h2.isEmpty() << std::endl;
std::cout << h2.findTop() << std::endl;
for (int i = 0; i < v1.size(); ++i)
{
h2.deleteTop();
std::cout << h2 << std::endl;
}
auto res = heapSort(h2);
for (auto e : res)
std::cout << e << ", ";
}