forked from BigWheel92/Min-Heap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinHeap.h
182 lines (134 loc) · 2.88 KB
/
MinHeap.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
171
172
173
174
175
176
177
178
179
180
181
182
#pragma once
#include <assert.h>
#include <algorithm>
template <typename k, typename v>
class MinHeap
{
//each item in heap will be HeapItem (a <key, value> pair). Both key and value can be of the same datatype or different data type.
// Key and value can be of a user-defined class as well. If key is of non-primitive datatype, then the > and < operators must be overloaded.
struct HeapItem
{
k key;
v value;
};
private:
HeapItem *arr;
int capacity;
int totalItems;
void doubleCapacity()
{
if (this->arr==nullptr)
{
this->arr = new HeapItem[1];
this->capacity = 1;
return;
}
int newCapacity = capacity*2;
HeapItem *newArr = new HeapItem[newCapacity];
for (int i = 0; i < this->totalItems; i++)
{
newArr[i] = this->arr[i];
}
if (this->arr!=nullptr)
delete this->arr;
this->capacity = newCapacity;
this->arr = newArr;
}
void shiftUp(int index)
{
if (index < 1)
return;
int parent = (index-1) / 2;
if (this->arr[index].key < this->arr[parent].key)
{
swap(this->arr[index], this->arr[parent]);
shiftUp(parent);
}
return;
}
void shiftDown(int index)
{
int minIndex = -1;
int lChildIndex = index * 2+1;
int rChildIndex = (index * 2) + 2;
if (lChildIndex < totalItems)
{
if (arr[index].key > arr[lChildIndex].key)
{
minIndex = lChildIndex;
}
}
if (rChildIndex < totalItems)
{
if (arr[(minIndex == -1 ? index : minIndex)].key > arr[rChildIndex].key)
{
minIndex = rChildIndex;
}
}
if (minIndex == -1)
return;
swap(arr[index], arr[minIndex]);
shiftDown(minIndex);
}
public:
MinHeap()
{
this->arr = nullptr;
this->capacity = 0;
this->totalItems = 0;
}
MinHeap(int _capacity)
{
assert(_capacity >= 1);
this->arr = new HeapItem[_capacity];
this->capacity = _capacity;
this->totalItems = 0;
}
void insert(k const key, v const value)
{
if (this->totalItems == this->capacity)
{
doubleCapacity();
}
this->arr[totalItems].key = key;
this->arr[totalItems].value = value;
shiftUp(totalItems);
this->totalItems++;
//iterative shift up
//for (int i =this->totalItems; i >= 2 && (this->arr[i].key < this->arr[i / 2].key); i=i/ 2)
//{
// swap(this->arr[i], this->arr[i / 2]);
//}
}
void getMin(v & value)
{
assert(totalItems != 0);
value = this->arr[0].value;
}
void deleteMin()
{
assert(totalItems != 0);
swap(arr[0], arr[this->totalItems-1]);
totalItems--;
//shift down
shiftDown(0);
}
bool isEmpty() const
{
return (totalItems == 0);
}
void deleteAll()
{
if (this->arr != nullptr)
{
delete[]arr;
arr = nullptr;
this->capacity = 0;
this->totalItems = 0;
}
}
~MinHeap()
{
deleteAll();
}
};