-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree1.c
More file actions
100 lines (83 loc) · 1.46 KB
/
Copy pathtree1.c
File metadata and controls
100 lines (83 loc) · 1.46 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
#include <stdio.h>
int h[101];
int n;
void swap(int x, int y)
{
int t;
t = h[x];
h[x] = h[y];
h[y] = t;
return;
}
void siftdown(int i)
{
int t, flag=0;
// i*2 <= n 表示的含义是i节点至少有左儿子
// i*2+1 <= n 表示i节点有两个儿子
while(i*2 <= n && flag == 0)
{
if(h[i] > h[i*2])
t = i*2;
else
t = i;
if(i*2+1 <= n)
{
if(h[t] > h[i*2 + 1])
t = i*2 + 1;
}
if(t != i)
{
swap(t,i);
// 继续向下检索,直到超过堆元素位置
i = t;
}
else
// 防止已经取到合适的位置
flag=1;
}
return;
}
void siftup(int i)
{
int flag=0;
if(i == 1) return;
while(i != 1 && flag==0)
{
if(h[i]<h[i/2])
swap(i,i/2);
else
flag = 1;
i = i/2;
}
return;
}
void create()
{
int i;
for(i = n/2; i>=1; i--)
{
siftdown(i);
}
return;
}
int deletemax()
{
int t;
t = h[1];
h[1] = h[n];
n--;
siftdown(1);
return t;
}
int main()
{
int i, num;
scanf("%d", &num);
for(i=1; i<=num; i++)
scanf("%d", &h[i]);
n = num;
create();
for(i=1; i<=num; i++)
printf("%d ", deletemax());
return 0;
}