Skip to content

Commit ebdf47c

Browse files
authored
Create main.c
1 parent 9337471 commit ebdf47c

File tree

1 file changed

+256
-0
lines changed

1 file changed

+256
-0
lines changed

斐波那契堆/main.c

+256
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
#include <stdio.h>
2+
#include "fibonacci_heap.h"
3+
4+
#define DEBUG 0
5+
6+
#if DEBUG
7+
#define log(x, ...) printf(x, __VA_ARGS__)
8+
#else
9+
#define log(x, ...)
10+
#endif
11+
12+
#define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) )
13+
14+
// 共8个
15+
int a[] = {12, 7, 25, 15, 28,
16+
33, 41, 1};
17+
// 共14个
18+
int b[] = {18, 35, 20, 42, 9,
19+
31, 23, 6, 48, 11,
20+
24, 52, 13, 2};
21+
22+
// 验证"基本信息(斐波那契堆的结构)"
23+
void test_basic()
24+
{
25+
int i;
26+
int blen=LENGTH(b);
27+
FibHeap *hb = fib_heap_make();
28+
29+
// 斐波那契堆hb
30+
printf("== 斐波那契堆(hb)中依次添加: ");
31+
for(i=0; i<blen; i++)
32+
{
33+
printf("%d ", b[i]);
34+
fib_heap_insert_key(hb, b[i]);
35+
}
36+
printf("\n");
37+
printf("== 斐波那契堆(hb)删除最小节点\n");
38+
fib_heap_extract_min(hb);
39+
fib_print(hb);
40+
41+
fib_heap_destroy(hb);
42+
}
43+
44+
// 验证"插入操作"
45+
void test_insert()
46+
{
47+
int i;
48+
int alen=LENGTH(a);
49+
FibHeap *ha = fib_heap_make();
50+
51+
// 斐波那契堆ha
52+
printf("== 斐波那契堆(ha)中依次添加: ");
53+
54+
for(i=0; i<alen; i++)
55+
{
56+
printf("%d ", a[i]);
57+
fib_heap_insert_key(ha, a[i]);
58+
}
59+
printf("\n");
60+
printf("== 斐波那契堆(ha)删除最小节点\n");
61+
fib_heap_extract_min(ha);
62+
fib_print(ha);
63+
64+
// 插入50
65+
printf("== 插入50\n");
66+
fib_heap_insert_key(ha, 50);
67+
fib_print(ha);
68+
69+
fib_heap_destroy(ha);
70+
}
71+
72+
// 验证"合并操作"
73+
void test_union()
74+
{
75+
int i;
76+
int alen=LENGTH(a);
77+
int blen=LENGTH(b);
78+
FibHeap *ha = fib_heap_make();
79+
FibHeap *hb = fib_heap_make();
80+
81+
// 斐波那契堆ha
82+
printf("== 斐波那契堆(ha)中依次添加: ");
83+
84+
for(i=0; i<alen; i++)
85+
{
86+
printf("%d ", a[i]);
87+
fib_heap_insert_key(ha, a[i]);
88+
}
89+
printf("\n");
90+
printf("== 斐波那契堆(ha)删除最小节点\n");
91+
fib_heap_extract_min(ha);
92+
fib_print(ha);
93+
94+
// 斐波那契堆hb
95+
printf("== 斐波那契堆(hb)中依次添加: ");
96+
for(i=0; i<blen; i++)
97+
{
98+
printf("%d ", b[i]);
99+
fib_heap_insert_key(hb, b[i]);
100+
}
101+
printf("\n");
102+
printf("== 斐波那契堆(hb)删除最小节点\n");
103+
fib_heap_extract_min(hb);
104+
fib_print(hb);
105+
106+
// 将"斐波那契堆hb"合并到"斐波那契堆ha"中。
107+
printf("== 合并ha和hb\n");
108+
ha = fib_heap_union(ha, hb);
109+
fib_print(ha);
110+
111+
// 销毁堆
112+
fib_heap_destroy(ha);
113+
}
114+
115+
// 验证"删除最小节点"
116+
void test_remove_min()
117+
{
118+
int i;
119+
int alen=LENGTH(a);
120+
int blen=LENGTH(b);
121+
FibHeap *ha = fib_heap_make();
122+
FibHeap *hb = fib_heap_make();
123+
124+
// 斐波那契堆ha
125+
printf("== 斐波那契堆(ha)中依次添加: ");
126+
127+
for(i=0; i<alen; i++)
128+
{
129+
printf("%d ", a[i]);
130+
fib_heap_insert_key(ha, a[i]);
131+
}
132+
printf("\n");
133+
printf("== 斐波那契堆(ha)删除最小节点\n");
134+
fib_heap_extract_min(ha);
135+
//fib_print(ha);
136+
137+
// 斐波那契堆hb
138+
printf("== 斐波那契堆(hb)中依次添加: ");
139+
for(i=0; i<blen; i++)
140+
{
141+
printf("%d ", b[i]);
142+
fib_heap_insert_key(hb, b[i]);
143+
}
144+
printf("\n");
145+
printf("== 斐波那契堆(hb)删除最小节点\n");
146+
fib_heap_extract_min(hb);
147+
//fib_print(hb);
148+
149+
// 将"斐波那契堆hb"合并到"斐波那契堆ha"中。
150+
printf("== 合并ha和hb\n");
151+
ha = fib_heap_union(ha, hb);
152+
fib_print(ha);
153+
154+
printf("== 删除最小节点\n");
155+
fib_heap_extract_min(ha);
156+
fib_print(ha);
157+
158+
// 销毁堆
159+
fib_heap_destroy(ha);
160+
}
161+
162+
// 验证"减小节点"
163+
void test_decrease()
164+
{
165+
int i;
166+
int blen=LENGTH(b);
167+
FibHeap *hb = fib_heap_make();
168+
169+
// 斐波那契堆hb
170+
printf("== 斐波那契堆(hb)中依次添加: ");
171+
for(i=0; i<blen; i++)
172+
{
173+
printf("%d ", b[i]);
174+
fib_heap_insert_key(hb, b[i]);
175+
}
176+
printf("\n");
177+
printf("== 斐波那契堆(hb)删除最小节点\n");
178+
fib_heap_extract_min(hb);
179+
fib_print(hb);
180+
181+
printf("== 将20减小为2\n");
182+
fib_heap_update(hb, 20, 2);
183+
fib_print(hb);
184+
185+
fib_heap_destroy(hb);
186+
}
187+
188+
// 验证"增大节点"
189+
void test_increase()
190+
{
191+
int i;
192+
int blen=LENGTH(b);
193+
FibHeap *hb = fib_heap_make();
194+
195+
// 斐波那契堆hb
196+
printf("== 斐波那契堆(hb)中依次添加: ");
197+
for(i=0; i<blen; i++)
198+
{
199+
printf("%d ", b[i]);
200+
fib_heap_insert_key(hb, b[i]);
201+
}
202+
printf("\n");
203+
printf("== 斐波那契堆(hb)删除最小节点\n");
204+
fib_heap_extract_min(hb);
205+
fib_print(hb);
206+
207+
fib_heap_update(hb, 20, 60);
208+
printf("== 将20增加为60\n");
209+
fib_print(hb);
210+
211+
fib_heap_destroy(hb);
212+
}
213+
214+
// 验证"删除节点"
215+
void test_delete()
216+
{
217+
int i;
218+
int blen=LENGTH(b);
219+
FibHeap *hb = fib_heap_make();
220+
221+
// 斐波那契堆hb
222+
printf("== 斐波那契堆(hb)中依次添加: ");
223+
for(i=0; i<blen; i++)
224+
{
225+
printf("%d ", b[i]);
226+
fib_heap_insert_key(hb, b[i]);
227+
}
228+
printf("\n");
229+
printf("== 斐波那契堆(hb)删除最小节点\n");
230+
fib_heap_extract_min(hb);
231+
fib_print(hb);
232+
233+
fib_heap_delete(hb, 20);
234+
printf("== 删除节点20\n");
235+
fib_print(hb);
236+
237+
fib_heap_destroy(hb);
238+
}
239+
240+
void main()
241+
{
242+
// 验证"基本信息(斐波那契堆的结构)"
243+
test_basic();
244+
// 验证"插入操作"
245+
//test_insert();
246+
// 验证"合并操作"
247+
//test_union();
248+
// 验证"删除最小节点"
249+
//test_remove_min();
250+
// 验证"减小节点"
251+
//test_decrease();
252+
// 验证"增大节点"
253+
//test_increase();
254+
// 验证"删除节点"
255+
//test_delete();
256+
}

0 commit comments

Comments
 (0)