1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define HEAP_SIZE 100
5 #define HEAP_FULL_VALUE -100
6
7 #if 0
8 /*小顶堆存储结构*/
9 typedef struct small_heap
10 {
11 int data[HEAP_SIZE];
12 int num;
13 }SMALL_HEAP;
14 #endif
15
16
17 /*
18 * name: heap_Swap
19 *
20 * purpose:
21 * swap two value of heap
22 */
23 static void heap_Swap(int heap[],int index_src,int index_dst)
24 {
25 int tmp = heap[index_src];
26
27 heap[index_src] = heap[index_dst];
28 heap[index_dst] = tmp;
29 }
30
31 /*
32 * name: heap_Up
33 *
34 * purpose:
35 * move up value of the index position to adjust heap struct
36 */
37 static void heap_Up(int heap[],int index)
38 {
39 int parent = index / 2;
40
41 while(parent >= 1)
42 {
43 if(heap[index] < heap[parent])
44 {
45 heap_Swap(heap,index,parent);
46 index = parent;
47 }
48 else
49 {
50 break;
51 }
52 }
53 }
54
55 /*
56 * name: heap_Down
57 *
58 * purpose:
59 * move down value of the index position to adjust heap struct
60 */
61 static void heap_Down(int heap[],int index,int heap_data_num)
62 {
63 if(index * 2 > heap_data_num)
64 {//leaf node can not move down
65 return;
66 }
67
68 while(index * 2 <= heap_data_num)
69 {
70 int child = index * 2; // left child
71
72 if(child > heap_data_num)
73 {
74 return;
75 }
76
77 if(child * 2 < heap_data_num)
78 {//the node have two child
79 //use multiply 2 to judge not use divide 2 to judge to pretend error
80 if(heap[child + 1] < heap[child])
81 {
82 child += 1; //right child is smaller update
83 }
84
85 }
86
87 if(heap[child] < heap[index])
88 {//the child samller than index swap value
89 heap_Swap(heap,index,child);
90 index = child;
91 }
92 else
93 {
94 break;
95 }
96 }
97 }
98
99 /*
100 * name: heap_Insert
101 *
102 * purpose:
103 * insert a value into heap and ajust heap struct
104 */
105 void heap_Insert(int heap[],int *heap_data_num,int value)
106 {
107 if(*heap_data_num == 0)
108 {
109 heap[0] = HEAP_FULL_VALUE; //data 0 do not save in the heap
110 }
111
112 (*heap_data_num)++; //update heap size
113 heap[*heap_data_num] = value; //add value to heap
114
115 heap_Up(heap,*heap_data_num); //adjust heap struct
116 }
117
118 /*
119 * name: heap_Delete
120 *
121 * purpost:
122 * delete a value from heap
123 */
124 void heap_Delete(int heap[],int *heap_data_num,int value)
125 {
126 int index;
127
128 for(index = 1; index <= *heap_data_num; index++)
129 {
130 if(heap[index] == value)
131 {
132 break;
133 }
134 }
135
136 if(index > *heap_data_num)
137 {//the value is not exist
138 return;
139 }
140
141 heap[index] = heap[*heap_data_num]; //set the index value as final value
142
143 (*heap_data_num)--;//the final value is not as the heap
144
145 heap_Down(heap,index,*heap_data_num); //move down
146
147 int parent = index / 2;
148 if(parent > 0 && heap[index] < heap[parent])
149 {//ajust to the special situation
150 heap_Up(heap,index);
151 }
152
153 heap[*heap_data_num + 1] = HEAP_FULL_VALUE; //delete final data
154 }
155
156 void heap_Print(int heap[],int heap_data_num)
157 {
158 int i;
159 for(i = 1; i <= heap_data_num; i++)
160 {
161 printf("%d ",heap[i]);
162 }
163
164 printf("
");
165 }
166
167
168 int main()
169 {
170 int heap[HEAP_SIZE];
171 int i,heap_data_num = 0;
172
173 for(i = 0; i < heap_data_num; i++)
174 {
175 heap[i] = HEAP_FULL_VALUE;
176 }
177
178 #if 0
179 heap_Insert(heap,&heap_data_num,1);
180 heap_Insert(heap,&heap_data_num,3);
181 heap_Insert(heap,&heap_data_num,4);
182 heap_Insert(heap,&heap_data_num,5);
183 heap_Insert(heap,&heap_data_num,8);
184 heap_Insert(heap,&heap_data_num,2);
185 heap_Insert(heap,&heap_data_num,7);
186 heap_Insert(heap,&heap_data_num,6);
187
188 heap_Print(heap,heap_data_num);
189
190 heap_Delete(heap,&heap_data_num,2);
191 heap_Print(heap,heap_data_num);
192 heap_Delete(heap,&heap_data_num,1);
193 heap_Print(heap,heap_data_num);
194
195 #endif
196
197 #if 1
198 heap_Insert(heap,&heap_data_num,1);
199 heap_Insert(heap,&heap_data_num,3);
200 heap_Insert(heap,&heap_data_num,11);
201 heap_Insert(heap,&heap_data_num,5);
202 heap_Insert(heap,&heap_data_num,4);
203 heap_Insert(heap,&heap_data_num,8);
204 heap_Insert(heap,&heap_data_num,7);
205 heap_Insert(heap,&heap_data_num,6);
206
207 heap_Print(heap,heap_data_num);
208
209 heap_Delete(heap,&heap_data_num,8);
210
211 heap_Print(heap,heap_data_num);
212 #endif
213
214 }
注:需要注意一点就是在进行节点是否有两个孩子的判断时,要用*2去判断,不能用除2判断,因为除2自动取整会导致少1的错误。