-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_by_array.cpp
85 lines (76 loc) · 2.04 KB
/
heap_by_array.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
#include <iostream>
using namespace std;
#define MAX (1 << 9);
class Heap{
private:
int cursize;
int Maxsize;
int *heap;
public:
Heap(int size){
cursize = 0;
Maxsize = size;
heap = new int[Maxsize + 1];
heap[0] = -MAX;
}
void add(int data){
if(Maxsize <= cursize) cout << "heap is full\n";
else{
int curidx = cursize + 1;
heap[curidx] = data;
while(heap[curidx / 2] > heap[curidx]){
heap[curidx] = heap[curidx / 2];
curidx /= 2;
}
heap[curidx] = data;
cursize++;
}
}
int pop(){
if (cursize <= 0) cout << "heap is Empty\n";
else{
int for_return = heap[1];
int now = heap[1] = heap[cursize--];
int curidx = 1;
while(curidx * 2 + 1 <= cursize){
int nextidx = curidx * 2;
nextidx += (heap[nextidx] > heap[nextidx + 1]);
int smaller = heap[nextidx];
if(smaller < now){
heap[curidx] = smaller;
curidx = nextidx;
}
else{
heap[curidx] = now;
break;
}
}
if (curidx * 2 <= cursize && heap[curidx * 2] < now){
heap[curidx] = heap[curidx * 2];
heap[curidx * 2] = now;
}
else heap[curidx] = now;
return for_return;
}
}
void Print(){
cout << "cursize: " << cursize << "\n";
for (int i = 1; i < Maxsize + 1; i++) cout << heap[i] << " ";
cout << "\n\n";
}
};
int main(){
Heap h(5);
h.add(4);
h.add(3);
h.add(2);
h.add(10);
h.add(123);
h.add(4444);
cout << "pop:" << h.pop() << "\n";
cout << "pop:" << h.pop() << "\n";
cout << "pop:" << h.pop() << "\n";
cout << "pop:" << h.pop() << "\n";
cout << "pop:" << h.pop() << "\n";
cout << "pop:" << h.pop() << "\n";
}