堆實際上是一個數組對象,可以被視為一個完全二叉樹,有完全二叉樹的遍歷得到(算法導論第六章)
思想:
最大堆和最小堆:
本文以最大堆作為介紹,主要的函數 max_heapify 和 heap_sort 利用遞歸
max_heapify函數的主要作用是調整對的結構,是其滿足最大堆的性質(其中利用遞歸),
max_heapify(int i,int len)len參數是數組的個數,i參數是“備用根”的下標。
代碼:
別人的實現:
http://www.cnblogs.com/dolphin0520/archive/2011/10/06/2199741.html
http://student.zjzk.cn/course_ware/data_structure/web/paixu/paixu8.4.2.2.htm
http://www.cnblogs.com/lovemo1314/archive/2011/09/13/2175032.html
思想:
最大堆和最小堆:
本文以最大堆作為介紹,主要的函數 max_heapify 和 heap_sort 利用遞歸
max_heapify函數的主要作用是調整對的結構,是其滿足最大堆的性質(其中利用遞歸),
max_heapify(int i,int len)len參數是數組的個數,i參數是“備用根”的下標。
代碼:
1
#include<iostream>
2
#include<stdlib.h>
3
#include<time.h>
4
using namespace std;
5
6
class heap{
7
public:
8
heap()
9
{
10
a=NULL;
11
size=10;
12
heap(size);
13
}
14
15
heap(int size_t):size(size_t)
16
{
17
a=new int[size+1];
18
srand(time(NULL));
19
20
for(int i=1;i<=size;i++)
21
{
22
a[i]=rand()%1000;
23
}
24
}
25
26
/* heap(int *b)
27
{
28
int len=sizeof(b);
29
size=len;
30
a=new int[size+1];
31
32
for(int i=1;i<=size;i++)
33
{
34
a[i]=b[i-1];
35
}
36
}*/
37
38
~heap()
39
{
40
cout<<"Destroy
..\n";
41
delete []a;
42
a=NULL;
43
}
44
45
void max_heapify(int i,int len);
46
void build_heap();
47
void heap_sort();
48
49
50
void print();
51
52
int left(int i){return 2*i;}
53
int right(int i){return 2*i+1;}
54
int parent(int i ){return i/2;}
55
private:
56
int *a;
57
int size;
58
};
59
60
void heap::heap_sort()
61
{
62
int len=size;
63
int t;
64
65
for(int i=size;i>=2;i--)
66
{
67
t=a[1];
68
a[1]=a[i];
69
a[i]=t;
70
71
len--;
72
73
max_heapify(1,len);
74
}
75
}
76
77
78
void heap::max_heapify(int i,int len)
79
{
80
int lt,rt;
81
int max=0;
82
83
lt=left(i);
84
rt=right(i);
85
86
if(lt<=len&&a[lt]>a[i]){
87
max=lt;
88
}
89
else{
90
max=i;
91
}
92
93
if(rt<=len&&a[rt]>a[max]){
94
max=rt;
95
}
96
97
if(max!=i){
98
int t;
99
t=a[i];
100
a[i]=a[max];
101
a[max]=t;
102
103
max_heapify(max,len);
104
}
105
}
106
107
void heap::build_heap()
108
{
109
for(int i=size/2;i>=1;i--)
110
{
111
max_heapify(i,size);
112
}
113
}
114
115
void heap::print()
116
{
117
for(int i=1;i<=size;i++)
118
{
119
cout<<a[i]<<"\t";
120
}
121
cout<<endl;
122
}
123
124
125
int main()
126
{
127
heap test(10);
128
// test.print();
129
130
131
//cout<<endl;
132
test.build_heap();
133
test.print();
134
135
cout<<endl;
136
test.heap_sort();
137
test.print() ;
138
139
}

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

別人的實現:
http://www.cnblogs.com/dolphin0520/archive/2011/10/06/2199741.html
http://student.zjzk.cn/course_ware/data_structure/web/paixu/paixu8.4.2.2.htm
http://www.cnblogs.com/lovemo1314/archive/2011/09/13/2175032.html