-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.cpp
291 lines (289 loc) · 6.83 KB
/
heap.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include<iostream>
using namespace std;
template<class T>
class heap
{
T *cable;
T* heap_arr;
int size,current,dev;
public:
int get_dev()
{
return dev;
}
void set_dev(int dev)
{
this->dev=dev;
}
heap(int size = 50)
{
cable=new T[size]();
heap_arr=new T[size]();
this->size=size;
current=-1;
dev=-1;
}
void set_cable(T* cable)
{
this->cable=cable;
}
T* get_cable()
{
return cable;
}
void set_current(int current)
{
this->current=current;
}
int get_total()
{
return current;
}
void swap(T &a, T &b)
{
T c=a;
a=b;
b=c;
}
int prior(string from)
{
int month=0,year=0,date=0,count=0;
for(int a=0;from[a]!='-';a++)
{
if(from[a]>='0'&&from[a]<='9')
{
count++;
if(count<=2)
{
if(count==1)
{
date=date+((from[a]-48)*30);
}
else
date=date+(from[a]-48);
}
else if(count>=3&&count<=4)
{
if(count==3)
{
month+=((from[a]-48)*30);
}
else if(count==4)
{
month=month+(from[a]-48);
date=date+(month*12);
}
}
else if(count>=5&&count<=8)
{
if(count==8)
{
year+=((from[a]-48)*30);
date=date+(year*year*year);
}
else
year+=(from[a]-48);
}
}
}
return date;
}
void insert(T data)
{
if(current<size)
{
cable[++current]=data;
++dev;
int mover=current;
while((current>0)&&(prior(cable[mover])<prior(cable[(mover-1)/2])))
{
swap(cable[mover],cable[(mover-1)/2]);
mover=(mover-1)/2;
}
}
else
{
cout<<"the size exceeds from the cable "<<endl;
}
}
void after_insert(T data)
{
if(current<size)
{
cable[++dev]=data;
++current;
int mover=dev;
while((dev>0)&&(prior(cable[mover])>prior(cable[(mover-1)/2])))
{
swap(cable[mover],cable[(mover-1)/2]);
mover=(mover-1)/2;
}
}
else
{
cout<<"the size exceeds from the cable "<<endl;
}
}
int left(int mover)
{
if(((mover*2)+1)<=current)
return (mover*2)+1;
return -1;
}
int right(int mover)
{
if((mover*2)+2<=current)
return (mover*2)+2;
return -1;
}
int max(int a,int b)
{
if(a!=-1 &&b !=-1)
{
if(prior(cable[a])>prior(cable[b]))
return a;
return b;
}
else if(a!=-1&&b==-1)
{
return a;
}
else
{
return -1;
}
}
int min(int a,int b)
{
if(a!=-1 &&b !=-1)
{
if(prior(cable[a])<prior(cable[b]))
return a;
return b;
}
else if(a!=-1&&b==-1)
{
return a;
}
else
{
return -1;
}
}
T remove()
{
if(cable==NULL)
{
cout<<endl<<" the cable is empty "<<endl;
return NULL;
}
else
{
T dam=cable[0];
cable[0]=cable[current--];
int mover=0;
while(mover<current && min(left(mover),right(mover))!=-1)
{
if(prior(cable[mover])>prior(cable[min(left(mover),right(mover))]))
{
// cout<<prior(cable[mover])<<" & "<<prior(cable[min(left(mover),right(mover))])<<" then ";
swap(cable[mover],cable[min(left(mover),right(mover))]);
// cout<< prior(cable[mover])<<" & "<<prior(cable[min(left(mover),right(mover))])<<endl;
mover=max(left(mover),right(mover));
}
else
{
break;
}
}
return dam;
}
}
void erase()
{
if(cable==NULL)
{
cout<<endl<<" the cable is empty "<<endl;
// return NULL;
}
else
{
// T dam=cable[0];
cable[0]=cable[current--];
dev--;
int mover=0;
while(mover<current && min(left(mover),right(mover))!=-1)
{
if(prior(cable[mover])>prior(cable[min(left(mover),right(mover))]))
{
// cout<< swap(cable[mover]<<" & "<<cable[min(left(mover),right(mover))])<<" then ";
swap(cable[mover],cable[min(left(mover),right(mover))]);
// mover=min(left(mover),right(mover));
}
else
{
break;
}
}
// return dam;
}
}
void traverse(int a=0)
{
if(a>dev)
return;
traverse(a*2+1);
cout<<cable[a]<<endl;
traverse(a*2+2);
}
string upcoming_event()
{
// cout<<prior(cable[0]);
return cable[dev];
}
void remove_event()
{
// set_dev(get_dev()-1);
// set_current(get_total()-1);
--dev;
// --current;
}
void heapsort()
{
for (int a=current;a>-1;a--)
{
cable[a]=remove();
}
}
void traverse_heap()
{
// // string *heap= new string[current]{};
// for (int a=current;a>=0;a--)
// {
// heap_arr[a]=remove();
// // cout<<cable[a]<<" ";
// }
for(int a=dev;a>=0;a--)
{
cout<<" "<<cable[a]<<","<<prior(cable[a])<<endl;
}
}
void write_heap(ofstream& fout)
{
// string *heap= new string[current]{};
// for (int a=current;a>=0;a--)
// {
// heap_arr[a]=remove();
// }
for(int a=dev+1;a>=0;a--)
{
fout<<cable[a]<<",";
}
}
void operator = (heap<T>& obj)
{
set_cable(obj.get_cable());
set_current(obj.get_total());
set_dev(obj.get_dev());
}
};