forked from gzc/CLRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p_queue.cpp
118 lines (101 loc) · 2 KB
/
p_queue.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
/*************************************************************************
> File Name: p_queue.cpp
> Author: Louis1992
> Mail: zhenchaogan@gmail.com
> Blog: http://gzc.github.io
> Created Time: Sun Jan 4 19:28:46 2015
************************************************************************/
#include "p_queue.h"
#include <limits>
p_queue::p_queue(int *A, int n)
{
for(int i = 0;i < n;i++)
{
a[i] = A[i];
}
size = n;
buildMaxHeap(a, size);
}
void p_queue::insert(int x)
{
size++;
a[size-1] = x;
int i = size-1;
while(i > 0 && a[parent(i)] < a[i])
{
std::swap(a[i], a[parent(i)]);
i = parent(i);
}
}
int p_queue::maximum()
{
if(size <= 0)
{
std::cerr << "heap overflow" << std::endl;
return -1;
}
return a[0];
}
int p_queue::extract_max()
{
if(size <= 0)
{
std::cerr << "heap overflow" << std::endl;
return -1;
}
int max = a[0];
a[0] = a[size-1];
size--;
maxHeapify(a, size, 0);
return max;
}
void p_queue::increase_key(int i, int x) {
a[i] = std::max(a[i], x);
while (i > 0 && a[parent(i)] < a[i]) {
std::swap(a[parent(i)], a[i]);
i = parent(i);
}
}
int p_queue::parent(int i)
{
return (i-1)/2;
}
int p_queue::left(int i)
{
return 2*i+1;
}
int p_queue::right(int i)
{
return 2*i+2;
}
void p_queue::maxHeapify(int A[], int n, int i)
{
int l = left(i);
int r = right(i);
int largest(0);
if (l <= (n-1) && A[l] > A[i])
largest = l;
else
largest = i;
if (r <= (n-1) && A[r] > A[largest])
largest = r;
if(largest != i)
{
std::swap(A[i], A[largest]);
maxHeapify(A, n, largest);
}
}
void p_queue::buildMaxHeap(int A[], int n)
{
for(int i = n/2-1;i >= 0;i--)
maxHeapify(A, n, i);
}
void p_queue::heapsort(int A[], int n)
{
buildMaxHeap(A, n);
for(int i = n-1;i > 0;i--)
{
std::swap(A[0],A[i]);
maxHeapify(A, --n, 0);
}
}