Repository navigation
Expand file tree
/
Copy pathBinaryHeap.h
More file actions
154 lines (134 loc) · 2.92 KB
/
Copy pathBinaryHeap.h
File metadata and controls
154 lines (134 loc) · 2.92 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
#pragma once
#include "Array.h"
NS_BF_BEGIN;
template <typename T>
class BinaryMaxHeap : public Array<T>
{
private:
void HeapifyUp(int32 childIdx)
{
if (childIdx > 0)
{
int32 parentIdx = (childIdx - 1) / 2;
if (this->mVals[childIdx] > this->mVals[parentIdx])
{
// swap parent and child
T t = this->mVals[parentIdx];
this->mVals[parentIdx] = this->mVals[childIdx];
this->mVals[childIdx] = t;
HeapifyUp(parentIdx);
}
}
}
void HeapifyDown(int32 parentIdx)
{
int32 leftChildIdx = 2 * parentIdx + 1;
int32 rightChildIdx = leftChildIdx + 1;
int32 largestChildIdx = parentIdx;
if (leftChildIdx < this->mSize && this->mVals[leftChildIdx] > this->Vals[largestChildIdx])
{
largestChildIdx = leftChildIdx;
}
if (rightChildIdx < this->mSize && this->mVals[rightChildIdx] > this->mVals[largestChildIdx])
{
largestChildIdx = rightChildIdx;
}
if (largestChildIdx != parentIdx)
{
T t = this->mVals[parentIdx];
this->mVals[parentIdx] = this->mVals[largestChildIdx];
this->mVals[largestChildIdx] = t;
HeapifyDown(largestChildIdx);
}
}
public:
BinaryMaxHeap() : Array<T>()
{
}
/// Add an item to the heap
void Add(T item)
{
Array<T>::Add(item);
HeapifyUp(this->mSize - 1);
}
/// Get the item of the root
T Peek()
{
return this->mVals[0];
}
/// Extract the item of the root
T Pop()
{
T item = this->mVals[0];
this->mSize--;
this->mVals[0] = this->mVals[this->mSize];
HeapifyDown(0);
return item;
}
};
template <typename T>
class BinaryMinHeap : public Array<T>
{
private:
void HeapifyUp(int32 childIdx)
{
if (childIdx > 0)
{
int32 parentIdx = (childIdx - 1) / 2;
if (this->mVals[childIdx] < this->mVals[parentIdx])
{
// swap parent and child
T t = this->mVals[parentIdx];
this->mVals[parentIdx] = this->mVals[childIdx];
this->mVals[childIdx] = t;
HeapifyUp(parentIdx);
}
}
}
void HeapifyDown(int32 parentIdx)
{
int32 leftChildIdx = 2 * parentIdx + 1;
int32 rightChildIdx = leftChildIdx + 1;
int32 largestChildIdx = parentIdx;
if (leftChildIdx < this->mSize && this->mVals[leftChildIdx] < this->mVals[largestChildIdx])
{
largestChildIdx = leftChildIdx;
}
if (rightChildIdx < this->mSize && this->mVals[rightChildIdx] < this->mVals[largestChildIdx])
{
largestChildIdx = rightChildIdx;
}
if (largestChildIdx != parentIdx)
{
T t = this->mVals[parentIdx];
this->mVals[parentIdx] = this->mVals[largestChildIdx];
this->mVals[largestChildIdx] = t;
HeapifyDown(largestChildIdx);
}
}
public:
BinaryMinHeap() : Array<T>()
{
}
/// Add an item to the heap
void Add(T item)
{
Array<T>::Add(item);
HeapifyUp(this->mSize - 1);
}
/// Get the item of the root
T Peek()
{
return this->mVals[0];
}
/// Extract the item of the root
T Pop()
{
T item = this->mVals[0];
this->mSize--;
this->mVals[0] = this->mVals[this->mSize];
HeapifyDown(0);
return item;
}
};
NS_BF_END