-
Notifications
You must be signed in to change notification settings - Fork 2
/
Heap.cpp
95 lines (87 loc) · 1.28 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
#include "Heap.h"
Heap::Heap(int _capacity) : size(0) , capacity(_capacity)
{
data = new int[_capacity];
}
Heap::~Heap()
{
delete[] data;
}
bool Heap::push(int element)
{
if (size + 1 >= capacity)
{
return false;
}
size++;
int father = size / 2;
int current = size;
while (father > 0 && element > data[father])
{
data[current] = data[father];
current = father;
father = current / 2;
}
data[current] = element;
return true;
}
bool Heap::pop()
{
if (size <= 0)
{
return false;
}
swap(data[1], data[size]);
size --;
update();
return false;
}
int Heap::top()
{
if (size > 0)
{
return data[size];
}
return 0;
}
void Heap::output()
{
for (int i = 1; i <= size; i++) {
cout << data[i] << " ";
}
cout << endl;
}
void Heap::update()
{
if (size == 2)
{
if (data[1] < data[2])
{
swap(data[1], data[2]);
}
return;
}
int current = 1;
int max_child = current * 2;
//没考虑到只有两个元素的情况
while (max_child <= size &&data[current] < data[max_child])
{
if (data[max_child + 1] > data[max_child])
{
max_child++;
}
swap(data[current], data[max_child]);
current = max_child;
max_child = current * 2;
}
}
void Heap::heap_sort()
{
int copy_size = size;
for (; size > 1; size--)
{
update();
swap(data[1], data[size]);
}
size = copy_size;
}