-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheapT.cpp
More file actions
161 lines (161 loc) · 2.61 KB
/
Copy pathheapT.cpp
File metadata and controls
161 lines (161 loc) · 2.61 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//heapT.cpp
//堆操作—模板函数版
#pragma once
//交换两个任意的类的对象
template <class Type>
void swapT(Type &a, Type &b)
{
Type t = a;
a = b;
b = t;
}
//结点上移max
template <class Type>
void siftUpMaxT(Type H[],int i)
{
bool done = false;
if (i!=0) {
while (!done && i!=0) {
if (H[i] > H[(i-1)/2])
swapT(H[i],H[(i-1)/2]);
else done = true;
i=(i-1)/2;
}
}
}
//结点下上移min
template <class Type>
void siftUpMinT(Type H[],int i)
{
bool done = false;
if (i!=0) {
while (!done && i!=0) {
if (H[i] < H[(i-1)/2])
swapT(H[i],H[(i-1)/2]);
else done = true;
i=(i-1)/2;
}
}
}
//结点下移Max
template <class Type>
void siftDownMaxT(Type H[],int n,int i)
{
bool done = false;
while (!done && ((i=2*i+1)<n)) {
if ((i+1<n)&&(H[i+1]>H[i]))
i = i + 1;
if (H[(i-1)/2] < H[i])
swapT(H[(i-1)/2],H[i]);
else done = true;
}
}
//结点下移Min
template <class Type>
void siftDownMinT(Type H[],int n,int i)
{
bool done = false;
while (!done && ((i=2*i+1)<n)) {
if ((i+1<n)&&(H[i+1]<H[i]))
i = i + 1;
if (H[(i-1)/2] > H[i])
swap(H[(i-1)/2],H[i]);
else done = true;
}
}
//插入元素Max, 堆数组必需有额外的空间
template <class Type>
void insertMaxT(Type H[],int &n, Type x)
{
H[n++] = x;
siftUpMaxT(H,n-1);
}
//插入元素Min, 堆数组必需有额外的空间
template <class Type>
void insertMinT(Type H[],int &n, Type x)
{
H[n++] = x;
siftUpMinT(H,n-1);
}
//删除元素max
template <class Type>
Type deleteIMaxT(Type H[],int &n,int i)
{
Type x,y;
x = H[i]; y = H[n-1];
n = n - 1;
if (i<n) {
H[i] = y;
if (y>x)
siftUpMaxT(H,i);
else
siftDownMaxT(H,n,i);
}
return x;
}
//删除元素min
template <class Type>
Type deleteIMinT(Type H[],int &n,int i)
{
Type x,y;
x = H[i]; y = H[n-1];
n = n - 1;
if (i<n) {
H[i] = y;
if (y<x)
siftUpMinT(H,i);
else
siftDownMinT(H,n,i);
}
return x;
}
//删除首元素Max
template <class Type>
Type deleteTopMaxT(Type H[],int &n)
{
Type x = deleteIMaxT(H,n,0);
return x;
}
//删除首元素Min
template <class Type>
Type deleteTopMinT(Type H[],int &n)
{
Type x = deleteIMinT(H,n,0);
return x;
}
//堆法建堆max
template <class Type>
void makeHeapMaxT(Type A[],int n)
{
for (int i=n/2-1; i>=0; i--) {
siftDownMaxT(A,n,i);
}
}
//堆法建堆min
template <class Type>
void makeHeapMinT(Type A[],int n)
{
for (int i=n/2-1; i>=0; i--) {
siftDownMinT(A,n,i);
}
}
//堆排序max
template <class Type>
void heapSortMaxT(Type A[],int n)
{
makeHeapMaxT(A,n);
for (int i=n-1;i>0;i--) {
swapT(A[0],A[i] );
siftDownMaxT(A,i,0);
}
}
//堆排序min
template <class Type>
void heapSortMinT(Type A[],int n)
{
makeHeapMinT(A,n);
for (int i=n-1;i>0;i--) {
swapT(A[0],A[i] );
siftDownMinT(A,i,0);
}
}