-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_heap.cpp
114 lines (113 loc) · 2.3 KB
/
max_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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class max_heap
{
private:
vector<int> node;
public:
max_heap();
void adjust_to_botton(int current_index, int last_index);
void adjust_to_top(int current_index);
void add(int d);
void free(int d);
int search(int d);
void show();
};
max_heap::max_heap()
{
node.push_back(NULL);
}
int max_heap::search(int d)
{
for (int i = 1; i < node.size() - 1; i++)
{
if (d == node[i])
{
return i;
}
}
}
void max_heap::adjust_to_top(int current_index)
{
while (current_index > 1)
{
if (node[current_index] <= node[current_index / 2])
{
break;
}
else
{
swap(node[current_index], node[current_index / 2]);
}
current_index /= 2;
}
}
void max_heap::adjust_to_botton(int current_index, int last_index)
{
int child_index = current_index * 2;
int tmp = node[current_index];
while (child_index <= last_index)
{
if ((node[child_index + 1] > node[child_index]) && (child_index + 1 <= node.size() - 1))
{
child_index++;
}
if (node[current_index] > node[child_index])
{
break;
}
else
{
node[current_index] = node[child_index];
child_index *= 2;
}
node[child_index / 2] = tmp;
}
}
void max_heap::add(int d)
{
node.push_back(d);
adjust_to_top(node.size() - 1);
}
void max_heap::free(int d)
{
int target = search(d);
swap(node[node.size() - 1], node[target]);
node.pop_back();
if (node.size() - 1 > 2)
{
if (node[target] > node[target / 2] && target > 2)
{
max_heap::adjust_to_top(target);
}
else
{
max_heap::adjust_to_botton(target, node.size() - 1);
}
}
}
void max_heap::show()
{
for (int i = 1; i <= node.size() - 1; i++)
{
cout << node[i] << "\n";
}
cout << "\n";
}
int main()
{
max_heap heap_tree;
heap_tree.add(15);
heap_tree.add(23);
heap_tree.add(10);
heap_tree.add(40);
heap_tree.add(8);
heap_tree.show();
heap_tree.free(23);
heap_tree.show();
heap_tree.free(40);
heap_tree.show();
return 0;
}