-
Notifications
You must be signed in to change notification settings - Fork 0
/
binomial-heap-algorithm-in-c.html
executable file
·1294 lines (1177 loc) · 117 KB
/
binomial-heap-algorithm-in-c.html
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>清风明月的笔记</title>
<meta name="description" content="">
<meta name="author" content="Ming">
<link rel="stylesheet" href="/theme/css/foundation.css" />
<link rel="stylesheet" href="/theme/css/normalize.css" />
<link rel="stylesheet" href="/theme/css/pygment/solarized-dark.css" />
<link rel="stylesheet" href="/theme/css/custom.css" />
<!-- Feeds -->
<!-- mathjax config similar to math.stackexchange -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
jax: ["input/TeX", "output/HTML-CSS"],
tex2jax: {
inlineMath: [ ['$', '$'] ],
displayMath: [ ['$$', '$$']],
processEscapes: true,
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
},
messageStyle: "none",
"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"] }
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
</head>
<body>
<div class="off-canvas-wrap" data-offcanvas>
<div class="inner-wrap">
<!-- mobile top bar to activate nav -->
<nav class="tab-bar show-for-small">
<section class="left-small">
<a class="left-off-canvas-toggle menu-icon" ><span></span></a>
</section>
<section class="middle tab-bar-section">
<h1 class="title">清风明月的笔记</h1>
</section>
</nav>
<!-- mobile side bar nav -->
<aside class="left-off-canvas-menu">
<ul class="off-canvas-list">
<li><a href="">Home</a></li>
<li ><a href="/pages/contact.html">Contact</a></li>
<li ><a href="/pages/about.html">About</a></li>
<li><label>Categories</label></li>
<li ><a href="/category/ji-he-suan-fa.html">几何算法</a></li>
<li class="active"><a href="/category/suan-fa.html">算法</a></li>
<!--
<li><label>Links</label></li>
<li><a href="http://coolshell.cn">酷 壳</a></li>
<li><a href="http://blog.codingnow.com">云风的 BLOG</a></li>
<li><a href="http://www.ruanyifeng.com/blog">阮一峰的网络日志</a></li>
-->
<!--
<li><label>Social</label></li>
<li><a href="#"></a></li>
-->
</ul>
</aside>
<!-- top bar nav -->
<nav class="top-bar hide-for-small-only" data-topbar>
<ul class="title-area">
<li class="name">
<h1><a href="/">清风明月的笔记</a></h1>
</li>
</ul>
<section class="top-bar-section">
<ul class="left">
<li><a href="/">Home</a></li>
</ul>
<ul class="right">
····
<li><a href="/pages/contact.html">Contact</a></li>
····
<li><a href="/pages/about.html">About</a></li>
</ul>
</section>
</nav>
<!-- Main Page Content and Sidebar -->
<section class="main-section">
<div class="row main-content">
<ul class="breadcrumbs">
<li class="unavailable"><a href="#">清风明月的笔记</a></li>
<li><a href="/">Home</a></li>
<li><a href="/category/suan-fa.html">算法</a></li>
</ul>
<article>
<h2>二项堆的C语言实现</h2>
<div class="toc">
<ul>
<li><a href="#_1">二项树</a></li>
<li><a href="#_2">二项堆</a></li>
<li><a href="#adt">二项堆的数据结构及ADT</a></li>
<li><a href="#_3">合并操作</a></li>
<li><a href="#_4">删除操作</a></li>
<li><a href="#_5">删除顶端元素</a></li>
<li><a href="#_6">更新操作</a></li>
<li><a href="#_7">完整源码</a></li>
<li><a href="#_8">测试程序的运行结果</a></li>
</ul>
</div>
<p>二项堆是一种比较复杂的堆结构,这种数据结构在"算法-c语言实现(第3版)"中有讲解,可惜这本书实在把二项堆说得云山雾罩。</p>
<p>二项堆是二项树的集合。下面先说明二项树再说明二项堆及其各种操作。</p>
<h3 id="_1"><strong> 二项树 </strong></h3>
<p>二项树是一种递归定义的树,它的定义如下:</p>
<ol>
<li>二项树B0只有一个节点。</li>
<li>二项树Bk由两棵二项树B(K-1)组成,其中一棵树是另一棵树的最左孩子。</li>
</ol>
<p>如下图所示:</p>
<p><img alt="图一" src="/images/binomial_heap/tree.png"/></p>
<p>上图的B0,B1,B2,B3,B4都是二项树,B0有一节点,B1由两B0组成,B2由两B1组成,B3由两B2组成,B4由两B3组成;而且,当两颗相同的二项树组成一棵树时,其中一棵树是另一棵树的最左孩子。</p>
<p>二项树有如下性质:</p>
<ol>
<li>BK共有2^K个节点</li>
<li>BK的高度是K</li>
<li>BK在深度i处有C(K,i)个节点,其中i=0,1,2,....,K</li>
<li>根的度为K,它大于任何其它节点的度数</li>
</ol>
<h3 id="_2"><strong> 二项堆 </strong></h3>
<p>二项堆一般用来实现优先队列,是由满足以下性质的二项树构成:</p>
<ol>
<li>每棵二项树都满足堆的性质</li>
<li>不能有两棵以上的二项树具有相同的度数。即在二项堆中,度数为K的二项树有0个或1个。</li>
</ol>
<p><img alt="图一" src="/images/binomial_heap/binomial.png"/></p>
<p>上图就是一棵二项堆。它由3棵二项树组成。
可以将包含n个节点的二项堆,表示成若干个2的指数和(或者转换成二进制),则每一个2个指数都对应一棵二项树。例如,13(二进制是1101)的2个指数和为13=23 + 22 + 20, 因此具有13个节点的二项堆由度数为3, 2, 0的三棵二项树组成。</p>
<h3 id="adt"><strong> 二项堆的数据结构及ADT </strong></h3>
<div class="highlight"><pre><span></span><span class="k">typedef</span> <span class="k">struct</span> <span class="n">binomial_node_s</span> <span class="o">*</span><span class="n">binomial_node_pt</span><span class="p">;</span>
<span class="k">typedef</span> <span class="k">struct</span> <span class="n">binomial_node_s</span> <span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">parent</span><span class="p">;</span> <span class="c1">//父节点</span>
<span class="n">binomial_node_pt</span> <span class="n">leftchild</span><span class="p">;</span> <span class="c1">//左孩子</span>
<span class="n">binomial_node_pt</span> <span class="n">sibling</span><span class="p">;</span> <span class="c1">//兄弟节点</span>
<span class="kt">int</span> <span class="n">degree</span><span class="p">;</span> <span class="c1">//度数</span>
<span class="n">Item</span> <span class="n">data</span><span class="p">;</span> <span class="c1">//数据</span>
<span class="p">}</span> <span class="n">binomial_node_t</span><span class="p">;</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_union</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">h1</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">h2</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_push</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">binomial_heap_update</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">oldelement</span><span class="p">,</span> <span class="n">Item</span> <span class="n">newelement</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_find</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_top</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_pop</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_delete</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">binomial_heap_destroy</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">binomial_heap_print</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">binomial_heap_export_dot</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">filename</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">label</span><span class="p">);</span>
</pre></div>
<h3 id="_3"><strong> 合并操作 </strong></h3>
<p>合并操作是二项堆的重点,二项堆的添加操作也是基于合并操作来实现的。</p>
<p>合并两个二项堆,需要的步骤如下:</p>
<ol>
<li>将两个二项堆的根链表合并成一个链表。合并后的新链表按照"节点的度数"单调递增排列。</li>
<li>将新链表中"根节点度数相同的二项树"连接起来,直到所有根节点度数都不相同。</li>
</ol>
<p>合并操作示意图</p>
<p><img alt="图一" src="/images/binomial_heap/union1.png"/></p>
<p><img alt="图二" src="/images/binomial_heap/union2.png"/></p>
<p><img alt="图三" src="/images/binomial_heap/union3.png"/></p>
<p><img alt="图四" src="/images/binomial_heap/union4.png"/></p>
<p><img alt="图五" src="/images/binomial_heap/union5.png"/></p>
<p><img alt="图六" src="/images/binomial_heap/union6.png"/></p>
<p>以下为实现代码</p>
<div class="highlight"><pre><span></span><span class="k">static</span> <span class="n">binomial_node_pt</span>
<span class="nf">binomial_tree_link</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">t1</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">t2</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">item_cmp</span><span class="p">(</span><span class="n">t1</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">t2</span><span class="o">-></span><span class="n">data</span><span class="p">))</span> <span class="p">{</span>
<span class="n">t1</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">t2</span><span class="o">-></span><span class="n">leftchild</span><span class="p">;</span>
<span class="n">t1</span><span class="o">-></span><span class="n">parent</span> <span class="o">=</span> <span class="n">t2</span><span class="p">;</span>
<span class="n">t2</span><span class="o">-></span><span class="n">leftchild</span> <span class="o">=</span> <span class="n">t1</span><span class="p">;</span>
<span class="n">t2</span><span class="o">-></span><span class="n">degree</span><span class="o">++</span><span class="p">;</span>
<span class="k">return</span> <span class="n">t2</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">t2</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">t1</span><span class="o">-></span><span class="n">leftchild</span><span class="p">;</span>
<span class="n">t2</span><span class="o">-></span><span class="n">parent</span> <span class="o">=</span> <span class="n">t1</span><span class="p">;</span>
<span class="n">t1</span><span class="o">-></span><span class="n">leftchild</span> <span class="o">=</span> <span class="n">t2</span><span class="p">;</span>
<span class="n">t1</span><span class="o">-></span><span class="n">degree</span><span class="o">++</span><span class="p">;</span>
<span class="k">return</span> <span class="n">t1</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">static</span> <span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_merge</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">h1</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">h2</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">head</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">binomial_node_pt</span> <span class="o">*</span><span class="n">pos</span> <span class="o">=</span> <span class="o">&</span><span class="n">head</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">h1</span> <span class="o">&&</span> <span class="n">h2</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">h1</span><span class="o">-></span><span class="n">degree</span> <span class="o"><</span> <span class="n">h2</span><span class="o">-></span><span class="n">degree</span><span class="p">)</span> <span class="p">{</span>
<span class="o">*</span><span class="n">pos</span> <span class="o">=</span> <span class="n">h1</span><span class="p">;</span>
<span class="n">h1</span> <span class="o">=</span> <span class="n">h1</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="o">*</span><span class="n">pos</span> <span class="o">=</span> <span class="n">h2</span><span class="p">;</span>
<span class="n">h2</span> <span class="o">=</span> <span class="n">h2</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">pos</span> <span class="o">=</span> <span class="o">&</span><span class="p">(</span><span class="o">*</span><span class="n">pos</span><span class="p">)</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">h1</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="o">*</span><span class="n">pos</span> <span class="o">=</span> <span class="n">h2</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="o">*</span><span class="n">pos</span> <span class="o">=</span> <span class="n">h1</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">head</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_union</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">h1</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">h2</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">pos</span><span class="p">,</span> <span class="o">*</span><span class="n">pos_pt</span><span class="p">,</span> <span class="n">next</span><span class="p">;</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">binomial_heap_merge</span><span class="p">(</span><span class="n">h1</span><span class="p">,</span> <span class="n">h2</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">heap</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">pos_pt</span> <span class="o">=</span> <span class="o">&</span><span class="n">heap</span><span class="p">;</span>
<span class="n">pos</span> <span class="o">=</span> <span class="o">*</span><span class="n">pos_pt</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">pos</span> <span class="o">!=</span> <span class="nb">NULL</span> <span class="o">&&</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">((</span><span class="n">pos</span><span class="o">-></span><span class="n">degree</span> <span class="o">==</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">degree</span><span class="p">)</span> <span class="o">&&</span>
<span class="p">(</span><span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">sibling</span><span class="o">==</span><span class="nb">NULL</span> <span class="o">||</span> <span class="n">pos</span><span class="o">-></span><span class="n">degree</span> <span class="o">!=</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">degree</span><span class="p">))</span> <span class="p">{</span>
<span class="n">next</span> <span class="o">=</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="o">*</span><span class="n">pos_pt</span> <span class="o">=</span> <span class="n">binomial_tree_link</span><span class="p">(</span><span class="n">pos</span><span class="p">,</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="p">);</span>
<span class="p">(</span><span class="o">*</span><span class="n">pos_pt</span><span class="p">)</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">next</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">pos_pt</span> <span class="o">=</span> <span class="o">&</span><span class="p">(</span><span class="o">*</span><span class="n">pos_pt</span><span class="p">)</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">pos</span> <span class="o">=</span> <span class="o">*</span><span class="n">pos_pt</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>binomial_heap_merge的作用就是"两个二项堆的根链表合并成一个链表,合并后的新链表按照'节点的度数'单调递增排序"。</p>
<p>binomial_tree_link则是为了合并操作的辅助函数,它的作用是将两个根节点度数相同的二项树连接起来。</p>
<h3 id="_4"><strong> 删除操作 </strong></h3>
<p>删除二项堆中的某个节点,需要的步骤如下:</p>
<ol>
<li>将"该节点"交换到"它所在二项树"的根节点位置。方法是,从"该节点"不断向上(即向树根方向)"遍历,不断交换父节点和子节点的数据,直到被删除的键值到达树根位置</li>
<li>将"该节点所在的二项树"从二项堆中移除</li>
<li>将"该节点所在的二项树"进行反转。反转的意思,就是将根的所有孩子独立出来,并将这些孩子整合成二项堆,将该二项堆记为child。</li>
<li>将child和heap进行合并操作。</li>
</ol>
<p>删除操作示意图</p>
<p><img alt="图一" src="/images/binomial_heap/delete1.png"/></p>
<p><img alt="图二" src="/images/binomial_heap/delete1.5.png"/></p>
<p><img alt="图三" src="/images/binomial_heap/delete2.png"/></p>
<p><img alt="图四" src="/images/binomial_heap/delete3.png"/></p>
<p><img alt="图五" src="/images/binomial_heap/delete4.png"/></p>
<p><img alt="图六" src="/images/binomial_heap/delete5.png"/></p>
<p>以下为实现代码</p>
<div class="highlight"><pre><span></span><span class="k">static</span> <span class="n">binomial_node_pt</span>
<span class="nf">binomial_reverse</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="n">temp</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">node</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">temp</span> <span class="o">=</span> <span class="n">node</span><span class="p">;</span>
<span class="n">node</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="n">temp</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">heap</span><span class="p">;</span>
<span class="n">temp</span><span class="o">-></span><span class="n">parent</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_delete</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">pre_head</span><span class="p">,</span> <span class="n">pos</span><span class="p">,</span> <span class="n">child</span><span class="p">;</span>
<span class="n">node</span> <span class="o">=</span> <span class="n">binomial_heap_find</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="n">element</span><span class="p">);</span>
<span class="k">while</span> <span class="p">(</span><span class="n">node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">while</span> <span class="p">(</span><span class="n">node</span><span class="o">-></span><span class="n">parent</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">swap</span><span class="p">(</span><span class="o">&</span><span class="n">node</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="o">&</span><span class="n">node</span><span class="o">-></span><span class="n">parent</span><span class="o">-></span><span class="n">data</span><span class="p">);</span>
<span class="n">node</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">parent</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">pre_head</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">heap</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">pos</span> <span class="o">!=</span> <span class="n">node</span> <span class="o">&&</span> <span class="n">pos</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">pre_head</span> <span class="o">=</span> <span class="n">pos</span><span class="p">;</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">pre_head</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">pre_head</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">child</span> <span class="o">=</span> <span class="n">binomial_reverse</span><span class="p">(</span><span class="n">node</span><span class="o">-></span><span class="n">leftchild</span><span class="p">);</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">binomial_heap_union</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="n">child</span><span class="p">);</span>
<span class="n">free</span><span class="p">(</span><span class="n">node</span><span class="p">);</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>binomial_reverse的作用是反转二项堆,并返回反转之后的根节点。</p>
<h3 id="_5"><strong> 删除顶端元素 </strong></h3>
<p>这个操作与删除操作相似,差别只是删除节点已经在树的根节点上,所以不再需要向上遍历。</p>
<p><img alt="图一" src="/images/binomial_heap/pop1.png"/></p>
<p><img alt="图二" src="/images/binomial_heap/pop2.png"/></p>
<p><img alt="图三" src="/images/binomial_heap/pop3.png"/></p>
<p><img alt="图四" src="/images/binomial_heap/pop4.png"/></p>
<p>以下为实现代码</p>
<div class="highlight"><pre><span></span><span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_pop</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">pre_node</span><span class="p">,</span> <span class="n">child</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">heap</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">_binomial_heap_top</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="o">&</span><span class="n">pre_node</span><span class="p">,</span> <span class="o">&</span><span class="n">node</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">pre_node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">pre_node</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">child</span> <span class="o">=</span> <span class="n">binomial_reverse</span><span class="p">(</span><span class="n">node</span><span class="o">-></span><span class="n">leftchild</span><span class="p">);</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">binomial_heap_union</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="n">child</span><span class="p">);</span>
<span class="n">free</span><span class="p">(</span><span class="n">node</span><span class="p">);</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<h3 id="_6"><strong> 更新操作 </strong></h3>
<p>更新二项堆中的某个节点,就是修改节点的值,它包括两部分分:"向上调整" 和 "向下调整"</p>
<p>更新操作实现代码:</p>
<div class="highlight"><pre><span></span><span class="kt">void</span>
<span class="nf">binomial_heap_update</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">oldelement</span><span class="p">,</span> <span class="n">Item</span> <span class="n">newelement</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">;</span>
<span class="n">node</span> <span class="o">=</span> <span class="n">binomial_heap_find</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="n">oldelement</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">item_cmp</span><span class="p">(</span><span class="n">oldelement</span><span class="p">,</span> <span class="n">newelement</span><span class="p">))</span> <span class="p">{</span>
<span class="n">binomial_tree_update_up</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">newelement</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">binomial_tree_update_down</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">newelement</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>向上调整很简单:需要更新的节点位于一棵二项树中;不断的将该节点上调,以至树符合二项树的规则。</p>
<p>更新操作向上调整示意图</p>
<p><img alt="图一" src="/images/binomial_heap/update_up1.png"/></p>
<p><img alt="图二" src="/images/binomial_heap/update_up2.png"/></p>
<p><img alt="图三" src="/images/binomial_heap/update_up3.png"/></p>
<p><img alt="图四" src="/images/binomial_heap/update_up4.png"/></p>
<p>以下为实现代码</p>
<div class="highlight"><pre><span></span><span class="cm">/* </span>
<span class="cm"> * Name: binomial_tree_update_up</span>
<span class="cm"> * Description: 更新高优先级数据后,向上调整二项树</span>
<span class="cm"> * </span>
<span class="cm"> */</span>
<span class="k">static</span> <span class="kt">void</span>
<span class="nf">binomial_tree_update_up</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">pos</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">node</span><span class="o">-></span><span class="n">data</span> <span class="o">=</span> <span class="n">element</span><span class="p">;</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">node</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">pos</span><span class="o">-></span><span class="n">parent</span> <span class="o">!=</span> <span class="nb">NULL</span> <span class="o">&&</span> <span class="n">item_cmp</span><span class="p">(</span><span class="n">pos</span><span class="o">-></span><span class="n">parent</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">pos</span><span class="o">-></span><span class="n">data</span><span class="p">))</span> <span class="p">{</span>
<span class="n">swap</span><span class="p">(</span><span class="o">&</span><span class="n">pos</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="o">&</span><span class="n">pos</span><span class="o">-></span><span class="n">parent</span><span class="o">-></span><span class="n">data</span><span class="p">);</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">pos</span><span class="o">-></span><span class="n">parent</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>向下调整也类似,需要更新的节点位于一棵二项树中;不断的将该节点下调,以至树符合二项树的规则。主要的区别是要从节点的所有子树中选择一个节点作为下调目标节点。</p>
<p><img alt="图一" src="/images/binomial_heap/update_down1.png"/></p>
<p><img alt="图二" src="/images/binomial_heap/update_down2.png"/></p>
<p><img alt="图三" src="/images/binomial_heap/update_down3.png"/></p>
<p><img alt="图四" src="/images/binomial_heap/update_down4.png"/></p>
<p>以下为实现代码</p>
<div class="highlight"><pre><span></span><span class="cm">/* </span>
<span class="cm"> * Name: binomial_tree_update_down</span>
<span class="cm"> * Description: 更新低优先级数据后,向下调整二项树</span>
<span class="cm"> * </span>
<span class="cm"> */</span>
<span class="k">static</span> <span class="kt">void</span>
<span class="nf">binomial_tree_update_down</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">pos</span><span class="p">,</span> <span class="n">child</span><span class="p">,</span> <span class="n">best_child</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">node</span><span class="o">-></span><span class="n">data</span> <span class="o">=</span> <span class="n">element</span><span class="p">;</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">node</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">pos</span><span class="o">-></span><span class="n">leftchild</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">child</span> <span class="o">=</span> <span class="n">pos</span><span class="o">-></span><span class="n">leftchild</span><span class="p">;</span>
<span class="n">best_child</span> <span class="o">=</span> <span class="n">child</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">child</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">item_cmp</span><span class="p">(</span><span class="n">best_child</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">child</span><span class="o">-></span><span class="n">data</span><span class="p">))</span> <span class="p">{</span>
<span class="n">best_child</span> <span class="o">=</span> <span class="n">child</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">child</span> <span class="o">=</span> <span class="n">child</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">best_child</span> <span class="o">==</span> <span class="nb">NULL</span> <span class="o">||</span> <span class="n">item_cmp</span><span class="p">(</span><span class="n">best_child</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">pos</span><span class="o">-></span><span class="n">data</span><span class="p">))</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">swap</span><span class="p">(</span><span class="o">&</span><span class="n">pos</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="o">&</span><span class="n">best_child</span><span class="o">-></span><span class="n">data</span><span class="p">);</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">best_child</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<h3 id="_7"><strong> 完整源码 </strong></h3>
<div class="highlight"><pre><span></span><span class="cm">/* binomial_heap.h */</span>
<span class="cp">#ifndef BINOMIAL_HEAP_H</span>
<span class="cp">#define BINOMIAL_HEAP_H</span>
<span class="k">typedef</span> <span class="kt">int</span> <span class="n">Item</span><span class="p">;</span>
<span class="k">typedef</span> <span class="k">struct</span> <span class="n">binomial_node_s</span> <span class="o">*</span><span class="n">binomial_node_pt</span><span class="p">;</span>
<span class="k">typedef</span> <span class="k">struct</span> <span class="n">binomial_node_s</span> <span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">parent</span><span class="p">;</span>
<span class="n">binomial_node_pt</span> <span class="n">leftchild</span><span class="p">;</span>
<span class="n">binomial_node_pt</span> <span class="n">sibling</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">degree</span><span class="p">;</span>
<span class="n">Item</span> <span class="n">data</span><span class="p">;</span>
<span class="p">}</span> <span class="n">binomial_node_t</span><span class="p">;</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_union</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">h1</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">h2</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_push</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">binomial_heap_update</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">oldelement</span><span class="p">,</span> <span class="n">Item</span> <span class="n">newelement</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_find</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_top</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_pop</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_delete</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">binomial_heap_destroy</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">binomial_heap_print</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">);</span>
<span class="kt">void</span> <span class="nf">binomial_heap_export_dot</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">filename</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">label</span><span class="p">);</span>
<span class="cp">#endif</span>
</pre></div>
<p>binomial_heap.h</p>
<div class="highlight"><pre><span></span><span class="cm">/*</span>
<span class="cm"> * =====================================================================================</span>
<span class="cm"> *</span>
<span class="cm"> * Filename: binomial_heap.c</span>
<span class="cm"> *</span>
<span class="cm"> * Description: 二项堆实现</span>
<span class="cm"> *</span>
<span class="cm"> * Version: 1.0</span>
<span class="cm"> * Created: 2016-09-26 20:13</span>
<span class="cm"> * Revision: none</span>
<span class="cm"> * Compiler: gcc</span>
<span class="cm"> *</span>
<span class="cm"> * Author: simon</span>
<span class="cm"> *</span>
<span class="cm"> * =====================================================================================</span>
<span class="cm"> */</span>
<span class="cp">#include</span> <span class="cpf"><stdio.h></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><stdlib.h></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><string.h></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">"binomial_heap.h"</span><span class="cp"></span>
<span class="cm">/* </span>
<span class="cm"> * 最小堆 ((a) > (b))</span>
<span class="cm"> * 最大堆 ({a} < (b))</span>
<span class="cm"> * </span>
<span class="cm"> */</span>
<span class="cp">#define item_cmp(a, b) ((a) > (b)) </span><span class="cm">/* 最小堆 */</span><span class="cp"></span>
<span class="cp">#define item_equal(a, b) ((a) == (b)) </span><span class="cm">/* 检查是否相等 */</span><span class="cp"></span>
<span class="k">static</span> <span class="kt">void</span> <span class="nf">swap</span><span class="p">(</span><span class="n">Item</span> <span class="o">*</span><span class="n">p1</span><span class="p">,</span> <span class="n">Item</span> <span class="o">*</span><span class="n">p2</span><span class="p">);</span>
<span class="k">static</span> <span class="n">binomial_node_pt</span> <span class="nf">binomial_node_new</span><span class="p">(</span><span class="n">Item</span> <span class="n">data</span><span class="p">);</span>
<span class="k">static</span> <span class="n">binomial_node_pt</span> <span class="nf">binomial_tree_link</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">t1</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">t2</span><span class="p">);</span>
<span class="k">static</span> <span class="n">binomial_node_pt</span> <span class="nf">binomial_heap_merge</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">h1</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">h2</span><span class="p">);</span>
<span class="k">static</span> <span class="kt">void</span> <span class="nf">binomial_tree_update_up</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">);</span>
<span class="k">static</span> <span class="kt">void</span> <span class="nf">binomial_tree_update_down</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">);</span>
<span class="k">static</span> <span class="kt">void</span>
<span class="nf">swap</span><span class="p">(</span><span class="n">Item</span> <span class="o">*</span><span class="n">p1</span><span class="p">,</span> <span class="n">Item</span> <span class="o">*</span><span class="n">p2</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">Item</span> <span class="n">temp</span><span class="p">;</span>
<span class="n">temp</span> <span class="o">=</span> <span class="o">*</span><span class="n">p1</span><span class="p">;</span>
<span class="o">*</span><span class="n">p1</span> <span class="o">=</span> <span class="o">*</span><span class="n">p2</span><span class="p">;</span>
<span class="o">*</span><span class="n">p2</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">static</span> <span class="n">binomial_node_pt</span>
<span class="nf">binomial_node_new</span><span class="p">(</span><span class="n">Item</span> <span class="n">data</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">;</span>
<span class="n">node</span> <span class="o">=</span> <span class="p">(</span><span class="n">binomial_node_pt</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">binomial_node_t</span><span class="p">));</span>
<span class="k">if</span> <span class="p">(</span><span class="n">node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"malloc binomial node failed!</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">node</span><span class="o">-></span><span class="n">parent</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">node</span><span class="o">-></span><span class="n">leftchild</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">node</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">node</span><span class="o">-></span><span class="n">degree</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">node</span><span class="o">-></span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="p">;</span>
<span class="k">return</span> <span class="n">node</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">static</span> <span class="n">binomial_node_pt</span>
<span class="nf">binomial_tree_link</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">t1</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">t2</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">item_cmp</span><span class="p">(</span><span class="n">t1</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">t2</span><span class="o">-></span><span class="n">data</span><span class="p">))</span> <span class="p">{</span>
<span class="n">t1</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">t2</span><span class="o">-></span><span class="n">leftchild</span><span class="p">;</span>
<span class="n">t1</span><span class="o">-></span><span class="n">parent</span> <span class="o">=</span> <span class="n">t2</span><span class="p">;</span>
<span class="n">t2</span><span class="o">-></span><span class="n">leftchild</span> <span class="o">=</span> <span class="n">t1</span><span class="p">;</span>
<span class="n">t2</span><span class="o">-></span><span class="n">degree</span><span class="o">++</span><span class="p">;</span>
<span class="k">return</span> <span class="n">t2</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">t2</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">t1</span><span class="o">-></span><span class="n">leftchild</span><span class="p">;</span>
<span class="n">t2</span><span class="o">-></span><span class="n">parent</span> <span class="o">=</span> <span class="n">t1</span><span class="p">;</span>
<span class="n">t1</span><span class="o">-></span><span class="n">leftchild</span> <span class="o">=</span> <span class="n">t2</span><span class="p">;</span>
<span class="n">t1</span><span class="o">-></span><span class="n">degree</span><span class="o">++</span><span class="p">;</span>
<span class="k">return</span> <span class="n">t1</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">static</span> <span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_merge</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">h1</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">h2</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">head</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">binomial_node_pt</span> <span class="o">*</span><span class="n">pos</span> <span class="o">=</span> <span class="o">&</span><span class="n">head</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">h1</span> <span class="o">&&</span> <span class="n">h2</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">h1</span><span class="o">-></span><span class="n">degree</span> <span class="o"><</span> <span class="n">h2</span><span class="o">-></span><span class="n">degree</span><span class="p">)</span> <span class="p">{</span>
<span class="o">*</span><span class="n">pos</span> <span class="o">=</span> <span class="n">h1</span><span class="p">;</span>
<span class="n">h1</span> <span class="o">=</span> <span class="n">h1</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="o">*</span><span class="n">pos</span> <span class="o">=</span> <span class="n">h2</span><span class="p">;</span>
<span class="n">h2</span> <span class="o">=</span> <span class="n">h2</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">pos</span> <span class="o">=</span> <span class="o">&</span><span class="p">(</span><span class="o">*</span><span class="n">pos</span><span class="p">)</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">h1</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="o">*</span><span class="n">pos</span> <span class="o">=</span> <span class="n">h2</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="o">*</span><span class="n">pos</span> <span class="o">=</span> <span class="n">h1</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">head</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_union</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">h1</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">h2</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">pos</span><span class="p">,</span> <span class="o">*</span><span class="n">pos_pt</span><span class="p">,</span> <span class="n">next</span><span class="p">;</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">binomial_heap_merge</span><span class="p">(</span><span class="n">h1</span><span class="p">,</span> <span class="n">h2</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">heap</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">pos_pt</span> <span class="o">=</span> <span class="o">&</span><span class="n">heap</span><span class="p">;</span>
<span class="n">pos</span> <span class="o">=</span> <span class="o">*</span><span class="n">pos_pt</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">pos</span> <span class="o">!=</span> <span class="nb">NULL</span> <span class="o">&&</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">((</span><span class="n">pos</span><span class="o">-></span><span class="n">degree</span> <span class="o">==</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">degree</span><span class="p">)</span> <span class="o">&&</span>
<span class="p">(</span><span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">sibling</span><span class="o">==</span><span class="nb">NULL</span> <span class="o">||</span> <span class="n">pos</span><span class="o">-></span><span class="n">degree</span> <span class="o">!=</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">degree</span><span class="p">))</span> <span class="p">{</span>
<span class="n">next</span> <span class="o">=</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="o">*</span><span class="n">pos_pt</span> <span class="o">=</span> <span class="n">binomial_tree_link</span><span class="p">(</span><span class="n">pos</span><span class="p">,</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="p">);</span>
<span class="p">(</span><span class="o">*</span><span class="n">pos_pt</span><span class="p">)</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">next</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">pos_pt</span> <span class="o">=</span> <span class="o">&</span><span class="p">(</span><span class="o">*</span><span class="n">pos_pt</span><span class="p">)</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">pos</span> <span class="o">=</span> <span class="o">*</span><span class="n">pos_pt</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_find</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">temp</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">heap</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">item_equal</span><span class="p">(</span><span class="n">heap</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">element</span><span class="p">))</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">((</span><span class="n">temp</span> <span class="o">=</span> <span class="n">binomial_heap_find</span><span class="p">(</span><span class="n">heap</span><span class="o">-></span><span class="n">sibling</span><span class="p">,</span> <span class="n">element</span><span class="p">))</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">temp</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">((</span><span class="n">temp</span> <span class="o">=</span> <span class="n">binomial_heap_find</span><span class="p">(</span><span class="n">heap</span><span class="o">-></span><span class="n">leftchild</span><span class="p">,</span> <span class="n">element</span><span class="p">))</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">temp</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_push</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">;</span>
<span class="n">node</span> <span class="o">=</span> <span class="n">binomial_node_new</span><span class="p">(</span><span class="n">element</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">binomial_heap_union</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="n">node</span><span class="p">);</span>
<span class="p">}</span>
<span class="cm">/* </span>
<span class="cm"> * Name: binomial_tree_update_up</span>
<span class="cm"> * Description: 更新高优先级数据后,向上调整二项树</span>
<span class="cm"> * </span>
<span class="cm"> */</span>
<span class="k">static</span> <span class="kt">void</span>
<span class="nf">binomial_tree_update_up</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">pos</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">node</span><span class="o">-></span><span class="n">data</span> <span class="o">=</span> <span class="n">element</span><span class="p">;</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">node</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">pos</span><span class="o">-></span><span class="n">parent</span> <span class="o">!=</span> <span class="nb">NULL</span> <span class="o">&&</span> <span class="n">item_cmp</span><span class="p">(</span><span class="n">pos</span><span class="o">-></span><span class="n">parent</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">pos</span><span class="o">-></span><span class="n">data</span><span class="p">))</span> <span class="p">{</span>
<span class="n">swap</span><span class="p">(</span><span class="o">&</span><span class="n">pos</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="o">&</span><span class="n">pos</span><span class="o">-></span><span class="n">parent</span><span class="o">-></span><span class="n">data</span><span class="p">);</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">pos</span><span class="o">-></span><span class="n">parent</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="cm">/* </span>
<span class="cm"> * Name: binomial_tree_update_down</span>
<span class="cm"> * Description: 更新低优先级数据后,向下调整二项树</span>
<span class="cm"> * </span>
<span class="cm"> */</span>
<span class="k">static</span> <span class="kt">void</span>
<span class="nf">binomial_tree_update_down</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">pos</span><span class="p">,</span> <span class="n">child</span><span class="p">,</span> <span class="n">best_child</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">node</span><span class="o">-></span><span class="n">data</span> <span class="o">=</span> <span class="n">element</span><span class="p">;</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">node</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">pos</span><span class="o">-></span><span class="n">leftchild</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">child</span> <span class="o">=</span> <span class="n">pos</span><span class="o">-></span><span class="n">leftchild</span><span class="p">;</span>
<span class="n">best_child</span> <span class="o">=</span> <span class="n">child</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">child</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">item_cmp</span><span class="p">(</span><span class="n">best_child</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">child</span><span class="o">-></span><span class="n">data</span><span class="p">))</span> <span class="p">{</span>
<span class="n">best_child</span> <span class="o">=</span> <span class="n">child</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">child</span> <span class="o">=</span> <span class="n">child</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">best_child</span> <span class="o">==</span> <span class="nb">NULL</span> <span class="o">||</span> <span class="n">item_cmp</span><span class="p">(</span><span class="n">best_child</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">pos</span><span class="o">-></span><span class="n">data</span><span class="p">))</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">swap</span><span class="p">(</span><span class="o">&</span><span class="n">pos</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="o">&</span><span class="n">best_child</span><span class="o">-></span><span class="n">data</span><span class="p">);</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">best_child</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kt">void</span>
<span class="nf">binomial_heap_update</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">oldelement</span><span class="p">,</span> <span class="n">Item</span> <span class="n">newelement</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">;</span>
<span class="n">node</span> <span class="o">=</span> <span class="n">binomial_heap_find</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="n">oldelement</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">item_cmp</span><span class="p">(</span><span class="n">oldelement</span><span class="p">,</span> <span class="n">newelement</span><span class="p">))</span> <span class="p">{</span>
<span class="n">binomial_tree_update_up</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">newelement</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">binomial_tree_update_down</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">newelement</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">static</span> <span class="kt">void</span>
<span class="nf">_binomial_heap_top</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="o">*</span><span class="n">pre_tree</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="o">*</span><span class="n">tree</span><span class="p">)</span>
<span class="p">{</span>
<span class="o">*</span><span class="n">pre_tree</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="o">*</span><span class="n">tree</span> <span class="o">=</span> <span class="n">heap</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">heap</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">while</span> <span class="p">(</span><span class="n">heap</span><span class="o">-></span><span class="n">sibling</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">item_cmp</span><span class="p">((</span><span class="o">*</span><span class="n">tree</span><span class="p">)</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">heap</span><span class="o">-></span><span class="n">sibling</span><span class="o">-></span><span class="n">data</span><span class="p">))</span> <span class="p">{</span>
<span class="o">*</span><span class="n">pre_tree</span> <span class="o">=</span> <span class="n">heap</span><span class="p">;</span>
<span class="o">*</span><span class="n">tree</span> <span class="o">=</span> <span class="n">heap</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">heap</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_top</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">pre_node</span><span class="p">;</span>
<span class="n">_binomial_heap_top</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="o">&</span><span class="n">pre_node</span><span class="p">,</span> <span class="o">&</span><span class="n">node</span><span class="p">);</span>
<span class="k">return</span> <span class="n">node</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">static</span> <span class="n">binomial_node_pt</span>
<span class="nf">binomial_reverse</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="n">temp</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">node</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">temp</span> <span class="o">=</span> <span class="n">node</span><span class="p">;</span>
<span class="n">node</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="n">temp</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">heap</span><span class="p">;</span>
<span class="n">temp</span><span class="o">-></span><span class="n">parent</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">temp</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_pop</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">pre_node</span><span class="p">,</span> <span class="n">child</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">heap</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">_binomial_heap_top</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="o">&</span><span class="n">pre_node</span><span class="p">,</span> <span class="o">&</span><span class="n">node</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">pre_node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">pre_node</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">child</span> <span class="o">=</span> <span class="n">binomial_reverse</span><span class="p">(</span><span class="n">node</span><span class="o">-></span><span class="n">leftchild</span><span class="p">);</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">binomial_heap_union</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="n">child</span><span class="p">);</span>
<span class="n">free</span><span class="p">(</span><span class="n">node</span><span class="p">);</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">binomial_node_pt</span>
<span class="nf">binomial_heap_delete</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="n">Item</span> <span class="n">element</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="n">pre_head</span><span class="p">,</span> <span class="n">pos</span><span class="p">,</span> <span class="n">child</span><span class="p">;</span>
<span class="n">node</span> <span class="o">=</span> <span class="n">binomial_heap_find</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="n">element</span><span class="p">);</span>
<span class="k">while</span> <span class="p">(</span><span class="n">node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">while</span> <span class="p">(</span><span class="n">node</span><span class="o">-></span><span class="n">parent</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">swap</span><span class="p">(</span><span class="o">&</span><span class="n">node</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="o">&</span><span class="n">node</span><span class="o">-></span><span class="n">parent</span><span class="o">-></span><span class="n">data</span><span class="p">);</span>
<span class="n">node</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">parent</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">pre_head</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">heap</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">pos</span> <span class="o">!=</span> <span class="n">node</span> <span class="o">&&</span> <span class="n">pos</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">pre_head</span> <span class="o">=</span> <span class="n">pos</span><span class="p">;</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">pos</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">pre_head</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">pre_head</span><span class="o">-></span><span class="n">sibling</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">node</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">child</span> <span class="o">=</span> <span class="n">binomial_reverse</span><span class="p">(</span><span class="n">node</span><span class="o">-></span><span class="n">leftchild</span><span class="p">);</span>
<span class="n">heap</span> <span class="o">=</span> <span class="n">binomial_heap_union</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="n">child</span><span class="p">);</span>
<span class="n">free</span><span class="p">(</span><span class="n">node</span><span class="p">);</span>
<span class="k">return</span> <span class="n">heap</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">void</span>
<span class="nf">binomial_heap_destroy</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">heap</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">binomial_heap_destroy</span><span class="p">(</span><span class="n">heap</span><span class="o">-></span><span class="n">leftchild</span><span class="p">);</span>
<span class="n">binomial_heap_destroy</span><span class="p">(</span><span class="n">heap</span><span class="o">-></span><span class="n">sibling</span><span class="p">);</span>
<span class="n">free</span><span class="p">(</span><span class="n">heap</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">static</span> <span class="kt">void</span>
<span class="nf">binomial_tree_printnode</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">node</span><span class="p">,</span> <span class="kt">int</span> <span class="n">h</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">h</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="n">printf</span><span class="p">(</span><span class="s">" "</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">node</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">node</span><span class="o">-></span><span class="n">data</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">static</span> <span class="kt">void</span>
<span class="nf">binomial_tree_show</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">tree</span><span class="p">,</span> <span class="kt">int</span> <span class="n">h</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">h</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"---------------------------</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">tree</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">binomial_tree_printnode</span><span class="p">(</span><span class="nb">NULL</span><span class="p">,</span> <span class="n">h</span><span class="p">);</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">binomial_tree_show</span><span class="p">(</span><span class="n">tree</span><span class="o">-></span><span class="n">leftchild</span><span class="p">,</span> <span class="n">h</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
<span class="n">binomial_tree_printnode</span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="n">h</span><span class="p">);</span>
<span class="n">binomial_tree_show</span><span class="p">(</span><span class="n">tree</span><span class="o">-></span><span class="n">sibling</span><span class="p">,</span> <span class="n">h</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span>
<span class="nf">binomial_heap_print</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">binomial_tree_show</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="cm">/* 打印节点计数变量 */</span>
<span class="k">static</span> <span class="kt">int</span> <span class="n">node_num</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">static</span> <span class="kt">void</span>
<span class="nf">export_dot</span><span class="p">(</span><span class="kt">FILE</span> <span class="o">*</span><span class="n">fp</span><span class="p">,</span> <span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">last_label</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">tree_first_label</span><span class="p">)</span>
<span class="p">{</span>
<span class="cm">/* 打印节点 */</span>
<span class="kt">char</span> <span class="n">node_name</span><span class="p">[</span><span class="mi">10</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
<span class="k">if</span> <span class="p">(</span><span class="n">heap</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">sprintf</span><span class="p">(</span><span class="n">node_name</span><span class="p">,</span> <span class="s">"n%d"</span><span class="p">,</span> <span class="n">node_num</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">last_label</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">strcpy</span><span class="p">(</span><span class="n">tree_first_label</span><span class="p">,</span> <span class="n">node_name</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s">"%s[label=%d, xlabel=%d];</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">node_name</span><span class="p">,</span> <span class="n">heap</span><span class="o">-></span><span class="n">data</span><span class="p">,</span> <span class="n">heap</span><span class="o">-></span><span class="n">degree</span><span class="p">);</span>
<span class="n">node_num</span><span class="o">++</span><span class="p">;</span>
<span class="cm">/* 打印边 */</span>
<span class="k">if</span> <span class="p">(</span><span class="n">last_label</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">heap</span><span class="o">-></span><span class="n">sibling</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s">"%s->%s[weight=100];</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">last_label</span><span class="p">,</span> <span class="n">node_name</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s">"%s->%s;</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">last_label</span><span class="p">,</span> <span class="n">node_name</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="cm">/* 打印子节点 */</span>
<span class="k">if</span> <span class="p">(</span><span class="n">heap</span><span class="o">-></span><span class="n">leftchild</span> <span class="o">==</span> <span class="nb">NULL</span> <span class="o">&&</span> <span class="n">heap</span><span class="o">-></span><span class="n">sibling</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="cm">/* 打印兄弟节点 */</span>
<span class="k">if</span> <span class="p">(</span><span class="n">last_label</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">export_dot</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">heap</span><span class="o">-></span><span class="n">sibling</span><span class="p">,</span> <span class="n">last_label</span><span class="p">,</span> <span class="n">tree_first_label</span><span class="p">);</span>
<span class="p">}</span>
<span class="cm">/* 打印子节点 */</span>
<span class="n">export_dot</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">heap</span><span class="o">-></span><span class="n">leftchild</span><span class="p">,</span> <span class="n">node_name</span><span class="p">,</span> <span class="n">tree_first_label</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span>
<span class="nf">binomial_heap_export_dot</span><span class="p">(</span><span class="n">binomial_node_pt</span> <span class="n">heap</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">filename</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">label</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">FILE</span> <span class="o">*</span><span class="n">fp</span><span class="p">;</span>
<span class="n">binomial_node_pt</span> <span class="n">tree</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">cluster_num</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kt">char</span> <span class="n">tree_name</span><span class="p">[</span><span class="mi">10</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
<span class="kt">char</span> <span class="n">last_tree_name</span><span class="p">[</span><span class="mi">10</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
<span class="n">fp</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">"w"</span><span class="p">);</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s">"digraph g{</span><span class="se">\n</span><span class="s">node[shape=circle];</span><span class="se">\n</span><span class="s">label=</span><span class="se">\"</span><span class="s">%s</span><span class="se">\"</span><span class="s">;</span><span class="se">\n</span><span class="s">labeljust=l;</span><span class="se">\n</span><span class="s">labelloc=t;</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">label</span><span class="p">);</span>
<span class="n">tree</span> <span class="o">=</span> <span class="n">heap</span><span class="p">;</span>
<span class="k">while</span> <span class="p">(</span><span class="n">tree</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span> <span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s">"subgraph cluster_%d {</span><span class="se">\n</span><span class="s">pencolor=green;label=</span><span class="se">\"</span><span class="s">二项树%d</span><span class="se">\"</span><span class="s">;</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">cluster_num</span><span class="p">,</span> <span class="n">cluster_num</span><span class="p">);</span>
<span class="n">strcpy</span><span class="p">(</span><span class="n">last_tree_name</span><span class="p">,</span> <span class="n">tree_name</span><span class="p">);</span>
<span class="n">memset</span><span class="p">(</span><span class="n">tree_name</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">tree_name</span><span class="p">)</span><span class="o">/</span><span class="k">sizeof</span><span class="p">(</span><span class="kt">char</span><span class="p">));</span>
<span class="n">export_dot</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">tree</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="n">tree_name</span><span class="p">);</span>
<span class="n">cluster_num</span><span class="o">++</span><span class="p">;</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s">"}</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">strlen</span><span class="p">(</span><span class="n">tree_name</span><span class="p">)</span> <span class="o">></span> <span class="mi">0</span> <span class="o">&&</span> <span class="n">strlen</span><span class="p">(</span><span class="n">last_tree_name</span><span class="p">)</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s">"%s->%s[constraint=false];</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">last_tree_name</span><span class="p">,</span> <span class="n">tree_name</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">tree</span> <span class="o">=</span> <span class="n">tree</span><span class="o">-></span><span class="n">sibling</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s">"}</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>binomial_heap.c</p>
<div class="highlight"><pre><span></span><span class="cm">/* binomial_heap_test.c */</span>
<span class="cp">#include</span> <span class="cpf"><stdlib.h></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><stdio.h></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">"binomial_heap.h"</span><span class="cp"></span>
<span class="cp">#define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) )</span>
<span class="kt">int</span>
<span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">a</span><span class="p">[]</span><span class="o">=</span> <span class="p">{</span><span class="mi">42</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">35</span><span class="p">,</span><span class="mi">18</span><span class="p">,</span><span class="mi">31</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">23</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">52</span><span class="p">,</span><span class="mi">24</span><span class="p">,</span><span class="mi">48</span><span class="p">,</span><span class="mi">11</span><span class="p">,</span><span class="mi">13</span><span class="p">};</span>
<span class="kt">int</span> <span class="n">b</span><span class="p">[]</span><span class="o">=</span> <span class="p">{</span><span class="mi">25</span><span class="p">,</span><span class="mi">15</span><span class="p">,</span><span class="mi">12</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">33</span><span class="p">,</span><span class="mi">28</span><span class="p">,</span><span class="mi">41</span><span class="p">};</span>
<span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">alen</span><span class="o">=</span><span class="n">LENGTH</span><span class="p">(</span><span class="n">a</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">blen</span><span class="o">=</span><span class="n">LENGTH</span><span class="p">(</span><span class="n">b</span><span class="p">);</span>
<span class="n">binomial_node_pt</span> <span class="n">ha</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="n">hb</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="n">hc</span><span class="o">=</span><span class="nb">NULL</span><span class="p">,</span> <span class="n">temp</span><span class="o">=</span><span class="nb">NULL</span><span class="p">;</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"== 二项堆(ha)中依次添加: "</span><span class="p">);</span>
<span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="n">alen</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"%d "</span><span class="p">,</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="n">ha</span> <span class="o">=</span> <span class="n">binomial_heap_push</span><span class="p">(</span><span class="n">ha</span><span class="p">,</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="p">}</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">== 二项堆(ha)的详细信息: </span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">binomial_heap_print</span><span class="p">(</span><span class="n">ha</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">== 二项堆(hb)中依次添加: "</span><span class="p">);</span>
<span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="n">blen</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"%d "</span><span class="p">,</span> <span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="n">hb</span> <span class="o">=</span> <span class="n">binomial_heap_push</span><span class="p">(</span><span class="n">hb</span><span class="p">,</span> <span class="n">b</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="p">}</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">== 二项堆(hb)的详细信息: </span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">binomial_heap_print</span><span class="p">(</span><span class="n">hb</span><span class="p">);</span>
<span class="n">hc</span> <span class="o">=</span> <span class="n">binomial_heap_union</span><span class="p">(</span><span class="n">ha</span><span class="p">,</span> <span class="n">hb</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">== 合并ha和hb后的详细信息: </span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">binomial_heap_print</span><span class="p">(</span><span class="n">hc</span><span class="p">);</span>
<span class="n">binomial_heap_export_dot</span><span class="p">(</span><span class="n">hc</span><span class="p">,</span> <span class="s">"union.dot"</span><span class="p">,</span> <span class="s">"合并结果"</span><span class="p">);</span>
<span class="n">binomial_heap_destroy</span><span class="p">(</span><span class="n">hc</span><span class="p">);</span>
<span class="n">ha</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="n">alen</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">ha</span> <span class="o">=</span> <span class="n">binomial_heap_push</span><span class="p">(</span><span class="n">ha</span><span class="p">,</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="p">}</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"== 二项堆(ha)中节点值20减少为2: "</span><span class="p">);</span>
<span class="n">binomial_heap_update</span><span class="p">(</span><span class="n">ha</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">== 二项堆ha中节点20减少为2后详细信息: </span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">binomial_heap_print</span><span class="p">(</span><span class="n">ha</span><span class="p">);</span>
<span class="n">binomial_heap_export_dot</span><span class="p">(</span><span class="n">ha</span><span class="p">,</span> <span class="s">"update_up.dot"</span><span class="p">,</span> <span class="s">"节点值减少"</span><span class="p">);</span>
<span class="n">binomial_heap_destroy</span><span class="p">(</span><span class="n">ha</span><span class="p">);</span>
<span class="n">ha</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="n">alen</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">ha</span> <span class="o">=</span> <span class="n">binomial_heap_push</span><span class="p">(</span><span class="n">ha</span><span class="p">,</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="p">}</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"== 二项堆(ha)中节点值6增加为60: "</span><span class="p">);</span>
<span class="n">binomial_heap_update</span><span class="p">(</span><span class="n">ha</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">60</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">== 二项堆ha中节点6增加为60后详细信息: </span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">binomial_heap_print</span><span class="p">(</span><span class="n">ha</span><span class="p">);</span>
<span class="n">binomial_heap_export_dot</span><span class="p">(</span><span class="n">ha</span><span class="p">,</span> <span class="s">"update_down.dot"</span><span class="p">,</span> <span class="s">"节点值增加"</span><span class="p">);</span>
<span class="n">binomial_heap_destroy</span><span class="p">(</span><span class="n">ha</span><span class="p">);</span>
<span class="n">ha</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="k">for</span><span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o"><</span><span class="n">alen</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>