-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.cpp
90 lines (77 loc) · 1.71 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
#include <iostream>
#include <vector>
using namespace std;
void insertHeap(int* heap, int& n, int x);
int getTopHeap(int* heap, int& n);
void upHeap(int* heap, int index);
void downHeap(int* heap, int n, int index);
void printHeap(int* heap, int n);
int main() {
freopen("output.txt", "w", stdout);
int* heap;
int n = 0;
heap = new int[50];
vector<int> insertValues = {3, 2, 4, 5, 6, 1, 7};
for (int v : insertValues) {
insertHeap(heap, n, v);
printHeap(heap, n);
}
while (n > 0) {
cout << "Top Heap: " << getTopHeap(heap, n) << '\n';
printHeap(heap, n);
}
delete[] heap;
return 0;
}
void printHeap(int* heap, int n) {
for (int i = 0; i < n; ++i) {
cout << heap[i] << ' ';
if (((i + 2) & (i + 1)) == 0 || i == n - 1)
cout << '\n';
}
cout << '\n';
}
void upHeap(int* heap, int index) {
while (index > 0) {
int parent = index / 2;
if (heap[index] > heap[parent]) {
int temp = heap[index];
heap[index] = heap[parent];
heap[parent] = temp;
index = parent;
} else
break;
}
}
void insertHeap(int* heap, int& n, int x) {
++n;
heap[n - 1] = x;
upHeap(heap, n - 1);
}
void downHeap(int* heap, int n, int index) {
while (index < n) {
int leftChild = index * 2 + 1;
int rightChild = index * 2 + 2;
int largest = index;
if (leftChild < n && heap[largest] < heap[leftChild])
largest = leftChild;
if (rightChild < n && heap[largest] < heap[rightChild])
largest = rightChild;
if (largest != index) {
int temp = heap[index];
heap[index] = heap[largest];
heap[largest] = temp;
index = largest;
} else
break;
}
}
int getTopHeap(int* heap, int& n) {
if (n == 0)
return -1;
int ans = heap[0];
heap[0] = heap[n - 1];
--n;
downHeap(heap, n, 0);
return ans;
}