-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap.h
222 lines (196 loc) · 4.95 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifndef DSW2D_HEAP_H
#define DSW2D_HEAP_H
#include <iostream>
#include "Clan.h"
template<class T>
class Heap{
struct HNode{
int Key;
T* element;
};
int size;
HNode* data;
int capacity;
void swapClanIndexes(Clan* c1,Clan* c2)
{
int id1=c1->getIndexInHeap();
int id2=c2->getIndexInHeap();
c1->setIndexInHeap(id2);
c2->setIndexInHeap(id1);
}
void updateClanIndex(Clan* c1,int i)
{
c1->setIndexInHeap(i);
}
public:
Heap():size(0),data(NULL),capacity(0){};
Heap(int capacity);
int getSize(){ return size;};
int getCapacity(){ return capacity;};
HNode& getData(int i){
//if(i<0 || i>size) return NULL;
return data[i];
};
void make(Clan** clans,int* Ids, int n)
{
for(int i=0; i<n; i++)
{
data[i].Key=Ids[i];
data[i].element=clans[i];
size++;
updateClanIndex(clans[i],i);
}
for(int i=n/2; i>=0; i--)
{
SiftDown(i);
}
// printHeap();
}
int getParent(int i){ return (i-1)/2;};
int getLeftSon(int i){ return 2*i+1;};
int getRightSon(int i){ return 2*i+2;};
bool Insert(T* Data,int key);
bool Insert(const HNode& val);
void deleteMin();
void delKey(const int& i);
void delElementByData(T* val){
Clan* c1=(Clan*)val;
int index=c1->getIndexInHeap();
if(index<0 || index>size){
return;
}
delKey(index);
c1->setIndexInHeap(-1);
// printHeap();
}
HNode& getMin(){
return data[0];
};
void decKey(const int& i,int x);
void SiftUp(int i);
void SiftDown(int i);
void printHeap();
void destroyHeap(){
// std::cout<<"\nHEEEEUHEPPEE";
if(size<=0)
return;
// for(int i=0;i<size;i++){
// // if(data[i].element)
// free(&(data[i]));
// }
delete[] data;
}
~Heap(){
}
};
template<class T>
Heap<T>::Heap(int capacity){
(*this).capacity=capacity*2;
data=new HNode[capacity*2];
size=0;
}
template<class T>
void swap(T *x, T *y)
{
T temp = *x;
*x = *y;
*y = temp;
}
template<class T>
bool Heap<T>::Insert(const HNode& val){
if(size==capacity){
return false;
}
if(size>=capacity/2){
HNode* newArr=new HNode[capacity*2];
for(int i=0;i<size;i++){
newArr[i]=data[i];
}
HNode* arrToDel=data;
data=newArr;
capacity=2*capacity;
delete[] arrToDel;
}
data[size]=val;
updateClanIndex((val.element),size);
int i=size;
size++;
//Now lets make sure that the heap qualities are still valid
SiftUp(i);
return true;
}
template<class T>
bool Heap<T>::Insert(T* Data,int key){
HNode node;
node.element=Data;
node.Key=key;
return Insert(node);
}
template<class T>
void Heap<T>::decKey(const int& i,int x){
if(i>size || i<0)
return;
if(getData(i).Key<x)
return;
data[i].Key=x;
SiftUp(i);
}
template<class T>
void Heap<T>::SiftUp(int i){
while(i>0 && (getData(getParent(i)).Key > getData(i).Key) )
{
swap(&data[i],&data[getParent(i)]);
//swapper(data[i],data[getParent(i)]);
swapClanIndexes((data[i].element),(data[getParent(i)].element));
i=getParent(i);
}
}
template<class T>
void Heap<T>::deleteMin(){
if(size<=0)
return;
size--;
// int a = data[0].Key;
// int b = data[1].Key;
// int c = data[2].Key;
// int d = data[3].Key;
updateClanIndex(data[0].element,-1);
data[0]=data[size]; //Replacing the minimum with the last leaf
updateClanIndex(data[0].element,0);
SiftDown(0);
}
template<class T>
void Heap<T>::SiftDown(int i){
int lson=getLeftSon(i);
int rson=getRightSon(i);
int minimum=i;
if(rson<size && data[rson].Key<data[i].Key){
if(data[rson].Key<data[minimum].Key) //the minimum must be the parent
minimum=rson;
}
if(lson<size && data[lson].Key<data[i].Key){
if(data[lson].Key<data[minimum].Key) //the minimum must be the parent
minimum=lson;
}
if(minimum==i) { // That means we are done;
return;
}
swap(&data[i],&data[minimum]);
swapClanIndexes((data[i].element),(data[minimum].element));
SiftDown(minimum);
}
template<class T>
void Heap<T>::delKey(const int& i){
decKey(i,-10000); //Now this key turns to be minimum
deleteMin(); // Now we delete it;:)
}
template<class T>
void Heap<T>::printHeap(){
int i=0;
std::cout<<"Heap:";
for(i=0;i<size;i++){
std::cout<<" "<<getData(i).Key;
}
std::cout<<"\n";
}
#endif //DSW2D_HEAP_H