二项堆原理与解析

概要

本章介绍二项堆,它和之前所讲的堆(二叉堆左倾堆斜堆)一样,也是用于实现优先队列的。和以往一样,本文会先对二项堆的理论知识进行简单介绍,然后给出C语言的实现。后续再分别给出C++和Java版本的实现;实现的语言虽不同,但是原理一样,选择其中之一进行了解即可。若文章有错误或不足的地方,请不吝指出!

目录
1. 二项树的介绍
2. 二项堆的介绍
3. 二项堆的基本操作
4. 二项堆的C实现(完整源码)
5. 二项堆的C测试程序

转载请注明出处:二项堆(一)之 图文解析 和 C语言的实现 - 如果天空不死 - 博客园


更多内容:数据结构与算法系列 目录

(01) 二项堆(一)之 图文解析 和 C语言的实现
(02) 二项堆(二)之 C++的实现
(03) 二项堆(二)之 Java的实现

二项树的介绍

二项树的定义

二项堆是二项树的集合。在了解二项堆之前,先对二项树进行介绍。

二项树是一种递归定义的有序树。它的递归定义如下:
(01) 二项树B0只有一个结点;
(02) 二项树Bk由两棵二项树B(k-1)组成的,其中一棵树是另一棵树根的最左孩子。
如下图所示:

上图的B0、B1、B2、B3、B4都是二项树。对比前面提到的二项树的定义:B0只有一个节点,B1由两个B0所组成,B2由两个B1所组成,B3由两个B2所组成,B4由两个B3所组成;而且,当两颗相同的二项树组成另一棵树时,其中一棵树是另一棵树的最左孩子。

二项树的性质

二项树有以下性质:
[性质一] Bk共有2k个节点。
[性质二] Bk的高度为k。
[性质三] Bk在深度i处恰好有C(k,i)个节点,其中i=0,1,2,...,k。
[性质四] 根的度数为k,它大于任何其它节点的度数。
注意:树的高度和深度是相同的。关于树的高度的概念,《算法导论》中只有一个节点的树的高度是0,而"维基百科"中只有一个节点的树的高度是1。本文使用了《算法导论中》"树的高度和深度"的概念。

下面对这几个性质进行简单说明:
[性质一] Bk共有2k个节点。
               如上图所示,B0有20=1节点,B1有21=2个节点,B2有22=4个节点,...
[性质二] Bk的高度为k。
               如上图所示,B0的高度为0,B1的高度为1,B2的高度为2,...
[性质三] Bk在深度i处恰好有C(k,i)个节点,其中i=0,1,2,...,k。
              C(k,i)是高中数学中阶乘元素,例如,C(10,3)=(10*9*8) / (3*2*1)=240
              B4中深度为0的节点C(4,0)=1
              B4中深度为1的节点C(4,1)= 4 / 1 = 4
              B4中深度为2的节点C(4,2)= (4*3) / (2*1) = 6
              B4中深度为3的节点C(4,3)= (4*3*2) / (3*2*1) = 4
              B4中深度为4的节点C(4,4)= (4*3*2*1) / (4*3*2*1) = 1
             合计得到B4的节点分布是(1,4,6,4,1)。
[性质四] 根的度数为k,它大于任何其它节点的度数。
              节点的度数是该结点拥有的子树的数目。

二项堆的介绍

二项堆通常被用来实现优先队列,它堆是指满足以下性质的二项树的集合:
(01) 每棵二项树都满足最小堆性质。即,父节点的关键字 <= 它的孩子的关键字。
(02) 不能有两棵或以上的二项树具有相同的度数(包括度数为0)。换句话说,具有度数k的二项树有0个或1个。

上图就是一棵二项堆,它由二项树B0、B2和B3组成。对比二项堆的定义:(01)二项树B0、B2、B3都是最小堆;(02)二项堆不包含相同度数的二项树。

二项堆的第(01)个性质保证了二项堆的最小节点是某一棵二项树的根节点,第(02)个性质则说明结点数为n的二项堆最多只有log{n} + 1棵二项树。实际上,将包含n个节点的二项堆,表示成若干个2的指数和(或者转换成二进制),则每一个2个指数都对应一棵二项树。例如,13(二进制是1101)的2个指数和为13=23 + 22 + 20, 因此具有13个节点的二项堆由度数为3, 2, 0的三棵二项树组成。

二项堆的基本操作

二项堆是可合并堆,它的合并操作的复杂度是O(log n)。

1. 基本定义

 1 #ifndef _BINOMIAL_HEAP_H_
 2 #define _BINOMIAL_HEAP_H_
 3 
 4 typedef int Type;
 5 
 6 typedef struct _BinomialNode{
 7     Type   key;                     // 关键字(键值)
 8     int degree;                     // 度数
 9     struct _BinomialNode *child;    // 左孩子
10     struct _BinomialNode *parent;   // 父节点
11     struct _BinomialNode *next;     // 兄弟
12 }BinomialNode, *BinomialHeap;
13 
14 // 新建key对应的节点,并将其插入到二项堆中。
15 BinomialNode* binomial_insert(BinomialHeap heap, Type key);
16 // 删除节点:删除键值为key的节点,并返回删除节点后的二项树
17 BinomialNode* binomial_delete(BinomialHeap heap, Type key);
18 // 将二项堆heap的键值oldkey更新为newkey
19 void binomial_update(BinomialHeap heap, Type oldkey, Type newkey);
20 
21 // 合并二项堆:将h1, h2合并成一个堆,并返回合并后的堆
22 BinomialNode* binomial_union(BinomialHeap h1, BinomialHeap h2) ;
23 
24 // 查找:在二项堆中查找键值为key的节点
25 BinomialNode* binomial_search(BinomialHeap heap, Type key);
26 // 获取二项堆中的最小节点
27 BinomialNode* binomial_minimum(BinomialHeap heap) ;
28 // 移除最小节点,并返回移除节点后的二项堆
29 BinomialNode* binomial_extract_minimum(BinomialHeap heap);
30 
31 // 打印"二项堆"
32 void binomial_print(BinomialHeap heap);
33 
34 #endif

BinomialNode是二项堆的节点。它包括了关键字(key),用于比较节点大小;度数(degree),用来表示当前节点的度数;左孩子(child)、父节点(parent)以及兄弟节点(next)。
下面是一棵二项堆的树形图和它对应的内存结构关系图。

2. 合并操作

合并操作是二项堆的重点,二项堆的添加操作也是基于合并操作来实现的。

合并两个二项堆,需要的步骤概括起来如下:
(01) 将两个二项堆的根链表合并成一个链表。合并后的新链表按照"节点的度数"单调递增排列。
(02) 将新链表中"根节点度数相同的二项树"连接起来,直到所有根节点度数都不相同。

下面,先看看合并操作的代码;然后再通过示意图对合并操作进行说明。
binomial_merge()代码(C语言)

 View Code

binomial_link()代码(C语言)

 View Code

合并操作代码(C语言)

 1 /*
 2  * 合并二项堆:将h1, h2合并成一个堆,并返回合并后的堆
 3  */
 4 BinomialNode* binomial_union(BinomialHeap h1, BinomialHeap h2) 
 5 {
 6     BinomialNode *heap;
 7     BinomialNode *prev_x, *x, *next_x;
 8 
 9     // 将h1, h2中的根表合并成一个按度数递增的链表heap
10     heap = binomial_merge(h1, h2);
11     if (heap == NULL)
12         return NULL;
13  
14     prev_x = NULL;
15     x      = heap;
16     next_x = x->next;
17  
18     while (next_x != NULL)
19     {
20         if (   (x->degree != next_x->degree) 
21             || ((next_x->next != NULL) && (next_x->degree == next_x->next->degree))) 
22         {
23             // Case 1: x->degree != next_x->degree
24             // Case 2: x->degree == next_x->degree == next_x->next->degree
25             prev_x = x;
26             x = next_x;
27         } 
28         else if (x->key <= next_x->key) 
29         {
30             // Case 3: x->degree == next_x->degree != next_x->next->degree
31             //      && x->key    <= next_x->key
32             x->next = next_x->next;
33             binomial_link(next_x, x);
34         } 
35         else 
36         {
37             // Case 4: x->degree == next_x->degree != next_x->next->degree
38             //      && x->key    >  next_x->key
39             if (prev_x == NULL) 
40             {
41                 heap = next_x;
42             } 
43             else 
44             {
45                 prev_x->next = next_x;
46             }
47             binomial_link(x, next_x);
48             x = next_x;
49         }
50         next_x = x->next;
51     }
52 
53     return heap;
54 }

合并函数binomial_union(h1, h2)的作用是将h1和h2合并,并返回合并后的二项堆。在binomial_union(h1, h2)中,涉及到了两个函数binomial_merge(h1, h2)和binomial_link(child, heap)。
binomial_merge(h1, h2)就是我们前面所说的"两个二项堆的根链表合并成一个链表,合并后的新链表按照'节点的度数'单调递增排序"。
binomial_link(child, heap)则是为了合并操作的辅助函数,它的作用是将"二项堆child的根节点"设为"二项堆heap的左孩子",从而将child整合到heap中去。

        在binomial_union(h1, h2)中对h1和h2进行合并时;首先通过 binomial_merge(h1, h2) 将h1和h2的根链表合并成一个"按节点的度数单调递增"的链表;然后进入while循环,对合并得到的新链表进行遍历,将新链表中"根节点度数相同的二项树"连接起来,直到所有根节点度数都不相同为止。在将新联表中"根节点度数相同的二项树"连接起来时,可以将被连接的情况概括为4种。
x是根链表的当前节点,next_x是x的下一个(兄弟)节点。
Case 1: x->degree != next_x->degree
             即,"当前节点的度数"与"下一个节点的度数"相等时。此时,不需要执行任何操作,继续查看后面的节点。
Case 2: x->degree == next_x->degree == next_x->next->degree
             即,"当前节点的度数"、"下一个节点的度数"和"下下一个节点的度数"都相等时。此时,暂时不执行任何操作,还是继续查看后面的节点。实际上,这里是将"下一个节点"和"下下一个节点"等到后面再进行整合连接。
Case 3: x->degree == next_x->degree != next_x->next->degree
        && x->key <= next_x->key
             即,"当前节点的度数"与"下一个节点的度数"相等,并且"当前节点的键值"<="下一个节点的度数"。此时,将"下一个节点(对应的二项树)"作为"当前节点(对应的二项树)的左孩子"。
Case 4: x->degree == next_x->degree != next_x->next->degree
        && x->key > next_x->key
             即,"当前节点的度数"与"下一个节点的度数"相等,并且"当前节点的键值">"下一个节点的度数"。此时,将"当前节点(对应的二项树)"作为"下一个节点(对应的二项树)的左孩子"。


下面通过示意图来对合并操作进行说明。

第1步:将两个二项堆的根链表合并成一个链表
          执行完第1步之后,得到的新链表中有许多度数相同的二项树。实际上,此时得到的是对应"Case 4"的情况,"树41"(根节点为41的二项树)和"树13"的度数相同,且"树41"的键值 > "树13"的键值。此时,将"树41"作为"树13"的左孩子。
第2步:合并"树41"和"树13"
         执行完第2步之后,得到的是对应"Case 3"的情况,"树13"和"树28"的度数相同,且"树13"的键值 < "树28"的键值。此时,将"树28"作为"树13"的左孩子。
第3步:合并"树13"和"树28"
         执行完第3步之后,得到的是对应"Case 2"的情况,"树13"、"树28"和"树7"这3棵树的度数都相同。此时,将x设为下一个节点。
第4步:将x和next_x往后移
         执行完第4步之后,得到的是对应"Case 3"的情况,"树7"和"树11"的度数相同,且"树7"的键值 < "树11"的键值。此时,将"树11"作为"树7"的左孩子。
第5步:合并"树7"和"树11"
         执行完第5步之后,得到的是对应"Case 4"的情况,"树7"和"树6"的度数相同,且"树7"的键值 > "树6"的键值。此时,将"树7"作为"树6"的左孩子。
第6步:合并"树7"和"树6"
         此时,合并操作完成!

PS. 合并操作的图文解析过程与"二项堆的测试程序(main.c)中的test_union()函数"是对应的!

3. 插入操作

理解了"合并"操作之后,插入操作就相当简单了。插入操作可以看作是将"要插入的节点"和当前已有的堆进行合并。

插入操作代码(C语言)

 1 /*
 2  * 新建key对应的节点,并将其插入到二项堆中。
 3  *
 4  * 参数说明:
 5  *     heap -- 原始的二项树。
 6  *     key -- 键值
 7  * 返回值:
 8  *     插入key之后的二项树
 9  */
10 BinomialNode* binomial_insert(BinomialHeap heap, Type key)
11 {
12     BinomialNode* node;
13 
14     if (binomial_search(heap, key) != NULL)
15     {
16         printf("insert failed: the key(%d) is existed already!\n", key);
17         return heap;
18     }
19 
20     node = make_binomial_node(key);
21     if (node==NULL)
22         return heap;
23 
24     return binomial_union(heap, node);
25 }

在插入时,首先通过binomial_search(heap, key)查找键值为key的节点。存在的话,则直接返回;不存在的话,则通过make_binomial_node(key)新建键值为key的节点node,然后将node和heap进行合并。

注意:我这里实现的二项堆是"进制插入相同节点的"!若你想允许插入相同键值的节点,则屏蔽掉插入操作中的binomial_search(heap, key)部分代码即可。

4. 删除操作

删除二项堆中的某个节点,需要的步骤概括起来如下:
(01) 将"该节点"交换到"它所在二项树"的根节点位置。方法是,从"该节点"不断向上(即向树根方向)"遍历,不断交换父节点和子节点的数据,直到被删除的键值到达树根位置。
(02) 将"该节点所在的二项树"从二项堆中移除;将该二项堆记为heap。
(03) 将"该节点所在的二项树"进行反转。反转的意思,就是将根的所有孩子独立出来,并将这些孩子整合成二项堆,将该二项堆记为child。
(04) 将child和heap进行合并操作。

下面,先看看删除操作的代码;再进行图文说明。
binomial_reverse()代码(C语言)

 View Code

删除操作代码(C语言)

 1 /* 
 2  * 删除节点:删除键值为key的节点,并返回删除节点后的二项树
 3  */
 4 BinomialNode* binomial_delete(BinomialHeap heap, Type key)
 5 {
 6     BinomialNode *node;
 7     BinomialNode *parent, *prev, *pos;
 8 
 9     if (heap==NULL)
10         return heap;
11 
12     // 查找键值为key的节点
13     if ((node = binomial_search(heap, key)) == NULL)
14         return heap;
15 
16     // 将被删除的节点的数据数据上移到它所在的二项树的根节点
17     parent = node->parent;
18     while (parent != NULL)
19     {
20         // 交换数据
21         swap(node->key, parent->key);
22         // 下一个父节点
23         node   = parent;
24         parent = node->parent;
25     }
26 
27     // 找到node的前一个根节点(prev)
28     prev = NULL;
29     pos  = heap;
30     while (pos != node) 
31     {
32         prev = pos;
33         pos  = pos->next;
34     }
35     // 移除node节点
36     if (prev)
37         prev->next = node->next;
38     else
39         heap = node->next;
40 
41     heap = binomial_union(heap, binomial_reverse(node->child)); 
42 
43     free(node);
44 
45     return heap;
46 }

binomial_delete(heap, key)的作用是删除二项堆heap中键值为key的节点,并返回删除节点后的二项堆。
binomial_reverse(heap)的作用是反转二项堆heap,并返回反转之后的根节点。


下面通过示意图来对删除操作进行说明(删除二项堆中的节点20)。

总的思想,就是将被"删除节点"从它所在的二项树中孤立出来,然后再对二项树进行相应的处理。

PS. 删除操作的图文解析过程与"二项堆的测试程序(main.c)中的test_delete()函数"是对应的!

5. 更新操作

更新二项堆中的某个节点,就是修改节点的值,它包括两部分分:"减少节点的值" 和 "增加节点的值" 。

更新操作代码(C语言)

 1 /* 
 2  * 更新二项堆heap的节点node的键值为key
 3  */
 4 static void binomial_update_key(BinomialHeap heap, BinomialNode* node, Type key)
 5 {
 6     if (node == NULL)
 7         return ;
 8 
 9     if(key < node->key)
10         binomial_decrease_key(heap, node, key);
11     else if(key > node->key)
12         binomial_increase_key(heap, node, key);
13     else
14         printf("No need to update!!!\n");
15 }

5.1 减少节点的值

减少节点值的操作很简单:该节点一定位于一棵二项树中,减小"二项树"中某个节点的值后要保证"该二项树仍然是一个最小堆";因此,就需要我们不断的将该节点上调。

减少操作代码(C语言)

 1 /* 
 2  * 减少关键字的值:将二项堆heap中的节点node的键值减小为key。
 3  */
 4 static void binomial_decrease_key(BinomialHeap heap, BinomialNode *node, Type key)
 5 {
 6     if ((key >= node->key) || (binomial_search(heap, key) != NULL))
 7     {
 8         printf("decrease failed: the new key(%d) is existed already, \
 9                 or is no smaller than current key(%d)\n", key, node->key);
10         return ;
11     }
12     node->key = key;
13  
14     BinomialNode *child, *parent;
15     child = node;
16     parent = node->parent;
17     while(parent != NULL && child->key < parent->key)
18     {
19         swap(parent->key, child->key);
20         child = parent;
21         parent = child->parent;
22     }
23 }

下面是减少操作的示意图(20->2)

减少操作的思想很简单,就是"保持被减节点所在二项树的最小堆性质"。

PS. 减少操作的图文解析过程与"二项堆的测试程序(main.c)中的test_decrease()函数"是对应的!

5.2 增加节点的值

增加节点值的操作也很简单。上面说过减少要将被减少的节点不断上调,从而保证"被减少节点所在的二项树"的最小堆性质;而增加操作则是将被增加节点不断的下调,从而保证"被增加节点所在的二项树"的最小堆性质。

增加操作代码(C语言)

 1 /* 
 2  * 增加关键字的值:将二项堆heap中的节点node的键值增加为key。
 3  */
 4 static void binomial_increase_key(BinomialHeap heap, BinomialNode *node, Type key)
 5 {
 6     if ((key <= node->key) || (binomial_search(heap, key) != NULL))
 7     {
 8         printf("increase failed: the new key(%d) is existed already, \
 9                 or is no greater than current key(%d)\n", key, node->key);
10         return ;
11     }
12     node->key = key;
13 
14     BinomialNode *cur, *child, *least;
15     cur = node;
16     child = cur->child;
17     while (child != NULL) 
18     {
19         if(cur->key > child->key)
20         {
21             // 如果"当前节点" < "它的左孩子",
22             // 则在"它的孩子中(左孩子 和 左孩子的兄弟)"中,找出最小的节点;
23             // 然后将"最小节点的值" 和 "当前节点的值"进行互换
24             least = child;
25             while(child->next != NULL)
26             {
27                 if (least->key > child->next->key)
28                 {
29                     least = child->next;
30                 }
31                 child = child->next;
32             }
33             // 交换最小节点和当前节点的值
34             swap(least->key, cur->key);
35 
36             // 交换数据之后,再对"原最小节点"进行调整,使它满足最小堆的性质:父节点 <= 子节点
37             cur = least;
38             child = cur->child;
39         }
40         else
41         {
42             child = child->next;
43         }
44     }
45 }

下面是增加操作的示意图(6->60)

增加操作的思想很简单,"保持被增加点所在二项树的最小堆性质"。

PS. 增加操作的图文解析过程与"二项堆的测试程序(main.c)中的test_increase()函数"是对应的!

注意:关于二项堆的"查找"、"打印"等其它接口就不再单独介绍了,后文的源码中有给出它们的实现代码。有兴趣的话,Please RTFSC(Read The Fucking Source Code)!

二项堆的C实现(完整源码)

二项堆的头文件(binomial_heap.h)

#ifndef _BINOMIAL_HEAP_H_
#define _BINOMIAL_HEAP_H_

typedef int Type;

typedef struct _BinomialNode{
    Type   key;                        // 关键字(键值)
    int degree;                        // 度数
    struct _BinomialNode *child;    // 左孩子
    struct _BinomialNode *parent;    // 父节点
    struct _BinomialNode *next;        // 兄弟
}BinomialNode, *BinomialHeap;

// 新建key对应的节点,并将其插入到二项堆中。
BinomialNode* binomial_insert(BinomialHeap heap, Type key);
// 删除节点:删除键值为key的节点,并返回删除节点后的二项树
BinomialNode* binomial_delete(BinomialHeap heap, Type key);
// 将二项堆heap的键值oldkey更新为newkey
void binomial_update(BinomialHeap heap, Type oldkey, Type newkey);

// 合并二项堆:将h1, h2合并成一个堆,并返回合并后的堆
BinomialNode* binomial_union(BinomialHeap h1, BinomialHeap h2) ;

// 查找:在二项堆中查找键值为key的节点
BinomialNode* binomial_search(BinomialHeap heap, Type key);
// 获取二项堆中的最小节点
BinomialNode* binomial_minimum(BinomialHeap heap) ;
// 移除最小节点,并返回移除节点后的二项堆
BinomialNode* binomial_extract_minimum(BinomialHeap heap);

// 打印"二项堆"
void binomial_print(BinomialHeap heap);

#endif

二项堆的实现文件(binomial_heap.c)

/**
 * C语言实现的二项堆
 *
 * @author skywang
 * @date 2014/04/01
 */

#include <stdio.h>
#include <stdlib.h>
#include "binomial_heap.h"

#define swap(a,b) (a^=b,b^=a,a^=b)

/*
 * 查找:在二项堆中查找键值为key的节点
 */
BinomialNode* binomial_search(BinomialHeap heap, Type key)
{
    BinomialNode *child;
    BinomialNode *parent = heap;

    parent = heap;
    while (parent != NULL)
    {
        if (parent->key == key)
            return parent;
        else
        {
            if((child = binomial_search(parent->child, key)) != NULL)
                return child;
            parent = parent->next;
        }
    }

    return NULL;
}

/*
 * 获取二项堆中的最小根节点(*y)
 *
 * 参数说明:
 *     heap    -- 二项堆
 *     prev_y  -- [输出参数]最小根节点y的前一个根节点
 *     y       -- [输出参数]最小根节点
 */
static void _binomial_minimum(BinomialHeap heap,
        BinomialNode **prev_y, BinomialNode **y)
{
    BinomialNode *x, *prev_x;    // x是用来遍历的当前节点

    if (heap==NULL)
        return ;

    prev_x  = heap;
    x       = heap->next;
    *prev_y = NULL;
    *y      = heap;
    // 找到最小节点
    while (x != NULL) {
        if (x->key < (*y)->key) {
            *y = x;
            *prev_y = prev_x;
        }
        prev_x = x;
        x = x->next;
    }
}

BinomialNode* binomial_minimum(BinomialHeap heap)
{
    BinomialNode *prev_y, *y;

    _binomial_minimum(heap, &prev_y, &y);

    return y;
}

/*
 * 合并两个二项堆:将child合并到heap中
 */
static void binomial_link(BinomialHeap child, BinomialHeap heap)
{
    child->parent = heap;
    child->next   = heap->child;
    heap->child = child;
    heap->degree++;
}

/*
 * 将h1, h2中的根表合并成一个按度数递增的链表,返回合并后的根节点
 */
static BinomialNode* binomial_merge(BinomialHeap h1, BinomialHeap h2)
{
    BinomialNode* head = NULL; //heap为指向新堆根结点
    BinomialNode** pos = &head;

    while (h1 && h2)
    {
        if (h1->degree < h2->degree)
        {
            *pos = h1;
            h1 = h1->next;
        }
        else
        {
            *pos = h2;
            h2 = h2->next;
        }
        pos = &(*pos)->next;
    }
    if (h1)
        *pos = h1;
    else
        *pos = h2;

    return head;
}

/*
 * 合并二项堆:将h1, h2合并成一个堆,并返回合并后的堆
 */
BinomialNode* binomial_union(BinomialHeap h1, BinomialHeap h2)
{
    BinomialNode *heap;
    BinomialNode *prev_x, *x, *next_x;

    // 将h1, h2中的根表合并成一个按度数递增的链表heap
    heap = binomial_merge(h1, h2);
    if (heap == NULL)
        return NULL;

    prev_x = NULL;
    x      = heap;
    next_x = x->next;

    while (next_x != NULL)
    {
        if (   (x->degree != next_x->degree)
            || ((next_x->next != NULL) && (next_x->degree == next_x->next->degree)))
        {
            // Case 1: x->degree != next_x->degree
            // Case 2: x->degree == next_x->degree == next_x->next->degree
            prev_x = x;
            x = next_x;
        }
        else if (x->key <= next_x->key)
        {
            // Case 3: x->degree == next_x->degree != next_x->next->degree
            //      && x->key    <= next_x->key
            x->next = next_x->next;
            binomial_link(next_x, x);
        }
        else
        {
            // Case 4: x->degree == next_x->degree != next_x->next->degree
            //      && x->key    >  next_x->key
            if (prev_x == NULL)
            {
                heap = next_x;
            }
            else
            {
                prev_x->next = next_x;
            }
            binomial_link(x, next_x);
            x = next_x;
        }
        next_x = x->next;
    }

    return heap;
}

/*
 * 新建二项堆节点
 */
static BinomialNode* make_binomial_node(Type key)
{
    BinomialNode* node;

    node = (BinomialNode*)malloc(sizeof(BinomialNode));
    if (node==NULL)
    {
        printf("malloc BinomialNode failed!\n");
        return NULL;
    }

    node->key = key;
    node->degree = 0;
    node->parent = NULL;
    node->child = NULL;
    node->next = NULL;

    return node;
}

/*
 * 新建key对应的节点,并将其插入到二项堆中。
 *
 * 参数说明:
 *     heap -- 原始的二项树。
 *     key -- 键值
 * 返回值:
 *     插入key之后的二项树
 */
BinomialNode* binomial_insert(BinomialHeap heap, Type key)
{
    BinomialNode* node;

    if (binomial_search(heap, key) != NULL)
    {
        printf("insert failed: the key(%d) is existed already!\n", key);
        return heap;
    }

    node = make_binomial_node(key);
    if (node==NULL)
        return heap;

    return binomial_union(heap, node);
}

/*
 * 反转二项堆heap
 */
static BinomialNode* binomial_reverse(BinomialNode* heap)
{
    BinomialNode* next;
    BinomialNode* tail = NULL;

    if (!heap)
        return heap;

    heap->parent = NULL;
    while (heap->next)
    {
        next          = heap->next;
        heap->next = tail;
        tail          = heap;
        heap          = next;
        heap->parent  = NULL;
    }
    heap->next = tail;

    return heap;
}

/*
 * 移除最小节点,并返回移除节点后的二项堆
 */
BinomialNode* binomial_extract_minimum(BinomialHeap heap)
{
    BinomialNode *y, *prev_y;    // y是最小节点

    if (heap==NULL)
        return heap;

    // 找到"最小节点根y"和"它的前一个根节点prev_y"
    _binomial_minimum(heap, &prev_y, &y);

    if (prev_y == NULL)    // heap的根节点就是最小根节点
        heap = heap->next;
    else                // heap的根节点不是最小根节点
        prev_y->next = y->next;

    // 反转最小节点的左孩子,得到最小堆child;
    // 这样,就使得最小节点所在二项树的孩子们都脱离出来成为一棵独立的二项树(不包括最小节点)
    BinomialNode* child = binomial_reverse(y->child);
    // 将"删除最小节点的二项堆child"和"heap"进行合并。
    heap = binomial_union(heap, child);

    // 删除最小节点
    free(y);

    return heap;
}

/*
 * 减少关键字的值:将二项堆heap中的节点node的键值减小为key。
 */
static void binomial_decrease_key(BinomialHeap heap, BinomialNode *node, Type key)
{
    if ((key >= node->key) || (binomial_search(heap, key) != NULL))
    {
        printf("decrease failed: the new key(%d) is existed already, \
                or is no smaller than current key(%d)\n", key, node->key);
        return ;
    }
    node->key = key;

    BinomialNode *child, *parent;
    child = node;
    parent = node->parent;
    while(parent != NULL && child->key < parent->key)
    {
        swap(parent->key, child->key);
        child = parent;
        parent = child->parent;
    }
}

/*
 * 增加关键字的值:将二项堆heap中的节点node的键值增加为key。
 */
static void binomial_increase_key(BinomialHeap heap, BinomialNode *node, Type key)
{
    if ((key <= node->key) || (binomial_search(heap, key) != NULL))
    {
        printf("increase failed: the new key(%d) is existed already, \
                or is no greater than current key(%d)\n", key, node->key);
        return ;
    }
    node->key = key;

    BinomialNode *cur, *child, *least;
    cur = node;
    child = cur->child;
    while (child != NULL)
    {
        if(cur->key > child->key)
        {
            // 如果"当前节点" < "它的左孩子",
            // 则在"它的孩子中(左孩子 和 左孩子的兄弟)"中,找出最小的节点;
            // 然后将"最小节点的值" 和 "当前节点的值"进行互换
            least = child;
            while(child->next != NULL)
            {
                if (least->key > child->next->key)
                {
                    least = child->next;
                }
                child = child->next;
            }
            // 交换最小节点和当前节点的值
            swap(least->key, cur->key);

            // 交换数据之后,再对"原最小节点"进行调整,使它满足最小堆的性质:父节点 <= 子节点
            cur = least;
            child = cur->child;
        }
        else
        {
            child = child->next;
        }
    }
}

/*
 * 更新二项堆heap的节点node的键值为key
 */
static void binomial_update_key(BinomialHeap heap, BinomialNode* node, Type key)
{
    if (node == NULL)
        return ;

    if(key < node->key)
        binomial_decrease_key(heap, node, key);
    else if(key > node->key)
        binomial_increase_key(heap, node, key);
    else
        printf("No need to update!!!\n");
}

/*
 * 将二项堆heap的键值oldkey更新为newkey
 */
void binomial_update(BinomialHeap heap, Type oldkey, Type newkey)
{
    BinomialNode *node;

    node = binomial_search(heap, oldkey);
    if (node != NULL)
        binomial_update_key(heap, node, newkey);
}

/*
 * 删除节点:删除键值为key的节点,并返回删除节点后的二项树
 */
BinomialNode* binomial_delete(BinomialHeap heap, Type key)
{
    BinomialNode *node;
    BinomialNode *parent, *prev, *pos;

    if (heap==NULL)
        return heap;

    // 查找键值为key的节点
    if ((node = binomial_search(heap, key)) == NULL)
        return heap;

    // 将被删除的节点的数据数据上移到它所在的二项树的根节点
    parent = node->parent;
    while (parent != NULL)
    {
        // 交换数据
        swap(node->key, parent->key);
        // 下一个父节点
        node   = parent;
        parent = node->parent;
    }

    // 找到node的前一个根节点(prev)
    prev = NULL;
    pos  = heap;
    while (pos != node)
    {
        prev = pos;
        pos  = pos->next;
    }
    // 移除node节点
    if (prev)
        prev->next = node->next;
    else
        heap = node->next;

    heap = binomial_union(heap, binomial_reverse(node->child));

    free(node);

    return heap;
}

/*
 * 打印"二项堆"
 *
 * 参数说明:
 *     node       -- 当前节点
 *     prev       -- 当前节点的前一个节点(父节点or兄弟节点)
 *     direction  --  1,表示当前节点是一个左孩子;
 *                    2,表示当前节点是一个兄弟节点。
 */
static void _binomial_print(BinomialNode *node, BinomialNode *prev, int direction)
{
    while(node != NULL)
    {
        //printf("%2d \n", node->key);
        if (direction == 1)
            printf("\t%2d(%d) is %2d's child\n", node->key, node->degree, prev->key);
        else
            printf("\t%2d(%d) is %2d's next\n", node->key, node->degree, prev->key);

        if (node->child != NULL)
            _binomial_print(node->child, node, 1);

        // 兄弟节点
        prev = node;
        node = node->next;
        direction = 2;
    }
}

void binomial_print(BinomialHeap heap)
{
    if (heap == NULL)
        return ;

    BinomialNode *p = heap;
    printf("== 二项堆( ");
    while (p != NULL)
    {
        printf("B%d ", p->degree);
        p = p->next;
    }
    printf(")的详细信息:\n");

    int i=0;
    while (heap != NULL)
    {
        i++;
        printf("%d. 二项树B%d: \n", i, heap->degree);
        printf("\t%2d(%d) is root\n", heap->key, heap->degree);

        _binomial_print(heap->child, heap, 1);
        heap = heap->next;
    }
    printf("\n");
}

二项堆的测试程序(main.c)

/**
 * C语言实现的二项堆
 *
 * @author skywang
 * @date 2014/04/01
 */

#include <stdio.h>
#include "binomial_heap.h"

#define DEBUG 1

#if DEBUG
#define log(x, ...)   printf(x, __VA_ARGS__)
#else
#define log(x, ...)
#endif

#define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) )

// 共7个 = 1+2+4
int a[] = {12,  7, 25, 15, 28,
           33, 41};
// 共13个 = 1+4+8
int b[] = {18, 35, 20, 42,  9,
           31, 23,  6, 48, 11,
           24, 52, 13 };
// 验证"二项堆的插入操作"
void test_insert()
{
    int i;
    int alen=LENGTH(a);
    BinomialHeap ha=NULL;

    // 二项堆ha
    printf("== 二项堆(ha)中依次添加: ");
    for(i=0; i<alen; i++)
    {
        printf("%d ", a[i]);
        ha = binomial_insert(ha, a[i]);
    }
    printf("\n");
    // 打印二项堆ha
    printf("== 二项堆(ha)的详细信息: \n");
    binomial_print(ha);
}

// 验证"二项堆的合并操作"
void test_union()
{
    int i;
    int alen=LENGTH(a);
    int blen=LENGTH(b);
    BinomialHeap ha,hb;

    ha=hb=NULL;

    // 二项堆ha
    printf("== 二项堆(ha)中依次添加: ");
    for(i=0; i<alen; i++)
    {
        printf("%d ", a[i]);
        ha = binomial_insert(ha, a[i]);
    }
    printf("\n");
    printf("== 二项堆(ha)的详细信息: \n");
    binomial_print(ha); // 打印二项堆ha

    // 二项堆hb
    printf("== 二项堆(hb)中依次添加: ");
    for(i=0; i<blen; i++)
    {
        printf("%d ", b[i]);
        hb = binomial_insert(hb, b[i]);
    }
    printf("\n");
    printf("== 二项堆(hb)的详细信息: \n");
    binomial_print(hb); // 打印二项堆hb

    // 将"二项堆hb"合并到"二项堆ha"中。
    ha = binomial_union(ha, hb);
    printf("== 合并ha和hb后的详细信息:\n");
    binomial_print(ha); // 打印二项堆ha的详细信息
}

// 验证"二项堆的删除操作"
void test_delete()
{
    int i;
    int blen=LENGTH(b);
    BinomialHeap hb=NULL;

    // 二项堆hb
    printf("== 二项堆(hb)中依次添加: ");
    for(i=0; i<blen; i++)
    {
        printf("%d ", b[i]);
        hb = binomial_insert(hb, b[i]);
    }
    printf("\n");
    printf("== 二项堆(hb)的详细信息: \n");
    binomial_print(hb); // 打印二项堆hb

    // 删除二项堆hb中的节点
    i = 20;
    hb = binomial_delete(hb, i);
    printf("== 删除节点%d后的详细信息: \n", i);
    binomial_print(hb); // 打印二项堆hb
}

// 验证"二项堆的更新(减少)操作"
void test_decrease()
{
    int i;
    int blen=LENGTH(b);
    BinomialHeap hb=NULL;

    // 二项堆hb
    printf("== 二项堆(hb)中依次添加: ");
    for(i=0; i<blen; i++)
    {
        printf("%d ", b[i]);
        hb = binomial_insert(hb, b[i]);
    }
    printf("\n");
    printf("== 二项堆(hb)的详细信息: \n");
    binomial_print(hb); // 打印二项堆hb

    // 将节点20更新为2
    binomial_update(hb, 20, 2);
    printf("== 更新节点20->2后的详细信息: \n");
    binomial_print(hb); // 打印二项堆hb
}

// 验证"二项堆的更新(增加)操作"
void test_increase()
{
    int i;
    int blen=LENGTH(b);
    BinomialHeap hb=NULL;

    // 二项堆hb
    printf("== 二项堆(hb)中依次添加: ");
    for(i=0; i<blen; i++)
    {
        printf("%d ", b[i]);
        hb = binomial_insert(hb, b[i]);
    }
    printf("\n");
    printf("== 二项堆(hb)的详细信息: \n");
    binomial_print(hb); // 打印二项堆hb

    // 将节点6更新为20
    binomial_update(hb, 6, 60);
    printf("== 更新节点6->60后的详细信息: \n");
    binomial_print(hb); // 打印二项堆hb
}


void main()
{
    // 1. 验证"二项堆的插入操作"
    test_insert();
    // 2. 验证"二项堆的合并操作"
    //test_union();
    // 3. 验证"二项堆的删除操作"
    //test_delete();
    // 4. 验证"二项堆的更新(减少)操作"
    //test_decrease();
    // 5. 验证"二项堆的更新(增加)操作"
    //test_increase();
}

二项堆的C测试程序

二项堆的测试程序包括了五部分,分别是"插入"、"删除"、"增加"、"减少"、"合并"这5种功能的测试代码。默认是运行的"插入"功能代码,你可以根据自己的需要来对相应的功能进行验证!

下面是插入功能运行结果:

== 二项堆(ha)中依次添加: 12 7 25 15 28 33 41 
== 二项堆(ha)的详细信息: 
== 二项堆( B0 B1 B2 )的详细信息:
1. 二项树B0: 
    41(0) is root
2. 二项树B1: 
    28(1) is root
    33(0) is 28's child
3. 二项树B2: 
     7(2) is root
    15(1) is  7's child
    25(0) is 15's child
    12(0) is 15's next

二项堆的C++实现(完整源码)

二项堆的实现文件(BinomialHeap.h)

/**
 * C++: 二项堆
 *
 * @author skywang
 * @date 2014/04/02
 */

#ifndef _BINOMIAL_TREE_HPP_
#define _BINOMIAL_TREE_HPP_

#include <iomanip>
#include <iostream>
using namespace std;

template <class T>
class BinomialNode {
    public:
        T key;                      // 关键字(键值)
        int degree;                 // 度数
        BinomialNode<T> *child;     // 左孩子
        BinomialNode<T> *parent;    // 父节点
        BinomialNode<T> *next;      // 兄弟节点

        BinomialNode(T value):key(value), degree(0),
            child(NULL),parent(NULL),next(NULL) {}
};

template <class T>
class BinomialHeap {
    private:
        BinomialNode<T> *mRoot;    // 根结点

    public:
        BinomialHeap();
        ~BinomialHeap();

        // 新建key对应的节点,并将其插入到二项堆中
        void insert(T key);
        // 将二项堆中键值oldkey更新为newkey
        void update(T oldkey, T newkey);
        // 删除键值为key的节点
        void remove(T key);
        // 移除二项堆中的最小节点
        void extractMinimum();

         // 将other的二项堆合并到当前二项堆中
        void combine(BinomialHeap<T>* other);

         // 获取二项堆中的最小节点的键值
        T minimum();
        // 二项堆中是否包含键值key
        bool contains(T key);
        // 打印二项堆
        void print();
    private:

        // 合并两个二项堆:将child合并到root中
        void link(BinomialNode<T>* child, BinomialNode<T>* root);
        // 将h1, h2中的根表合并成一个按度数递增的链表,返回合并后的根节点
        BinomialNode<T>* merge(BinomialNode<T>* h1, BinomialNode<T>* h2);
         // 合并二项堆:将h1, h2合并成一个堆,并返回合并后的堆
        BinomialNode<T>* combine(BinomialNode<T>* h1, BinomialNode<T>* h2);
        // 反转二项堆root,并返回反转后的根节点
        BinomialNode<T>* reverse(BinomialNode<T>* root);
        // 移除二项堆root中的最小节点,并返回删除节点后的二项树
        BinomialNode<T>* extractMinimum(BinomialNode<T>* root);
        // 删除节点:删除键值为key的节点,并返回删除节点后的二项树
        BinomialNode<T>* remove(BinomialNode<T> *root, T key);
        // 在二项树root中查找键值为key的节点
        BinomialNode<T>* search(BinomialNode<T>* root, T key);

        // 增加关键字的值:将二项堆中的节点node的键值增加为key。
        void increaseKey(BinomialNode<T>* node, T key);
        // 减少关键字的值:将二项堆中的节点node的键值减小为key
        void decreaseKey(BinomialNode<T>* node, T key);
        // 更新关键字的值:更新二项堆的节点node的键值为key
        void updateKey(BinomialNode<T>* node, T key);

        // 获取二项堆中的最小根节点
        void minimum(BinomialNode<T>* root, BinomialNode<T> *&prev_y, BinomialNode<T> *&y);
        // 打印二项堆
        void print(BinomialNode<T>* node, BinomialNode<T>* prev, int direction);
};

/*
 * 构造函数
 */
template <class T>
BinomialHeap<T>::BinomialHeap():mRoot(NULL)
{
}

/*
 * 析构函数
 */
template <class T>
BinomialHeap<T>::~BinomialHeap()
{
}

/*
 * 获取二项堆中的最小根节点
 *
 * 参数说明:
 *     root    -- 二项堆
 *     prev_y  -- [输出参数]最小根节点y的前一个根节点
 *     y       -- [输出参数]最小根节点
 */
template <class T>
void BinomialHeap<T>::minimum(BinomialNode<T>* root,
        BinomialNode<T> *&prev_y, BinomialNode<T> *&y)
{
    BinomialNode<T> *x, *prev_x;    // x是用来遍历的当前节点

    if (root==NULL)
        return ;

    prev_x  = root;
    x       = root->next;
    prev_y = NULL;
    y      = root;
    // 找到最小节点
    while (x != NULL) {
        if (x->key < y->key) {
            y = x;
            prev_y = prev_x;
        }
        prev_x = x;
        x = x->next;
    }
}

/*
 * 获取二项堆中的最小节点的键值
 */
template <class T>
T BinomialHeap<T>::minimum()
{
    BinomialNode<T> *prev_y, *y;

    minimum(mRoot, prev_y, y);

   return y->key;
}


/*
 * 合并两个二项堆:将child合并到root中
 */
template <class T>
void BinomialHeap<T>::link(BinomialNode<T>* child, BinomialNode<T>* root)
{
    child->parent = root;
    child->next   = root->child;
    root->child = child;
    root->degree++;
}

/*
 * 将h1, h2中的根表合并成一个按度数递增的链表,返回合并后的根节点
 */
template <class T>
BinomialNode<T>* BinomialHeap<T>::merge(BinomialNode<T>* h1, BinomialNode<T>* h2)
{
    BinomialNode<T>* root = NULL; //heap为指向新堆根结点
    BinomialNode<T>** pos = &root;

    while (h1 && h2)
    {
        if (h1->degree < h2->degree)
        {
            *pos = h1;
            h1 = h1->next;
        }
        else
        {
            *pos = h2;
            h2 = h2->next;
        }
        pos = &(*pos)->next;
    }
    if (h1)
        *pos = h1;
    else
        *pos = h2;

    return root;
}

/*
 * 合并二项堆:将h1, h2合并成一个堆,并返回合并后的堆
 */
template <class T>
BinomialNode<T>* BinomialHeap<T>::combine(BinomialNode<T>* h1, BinomialNode<T>* h2)
{
    BinomialNode<T> *root;
    BinomialNode<T> *prev_x, *x, *next_x;

    // 将h1, h2中的根表合并成一个按度数递增的链表root
    root = merge(h1, h2);
    if (root == NULL)
        return NULL;

    prev_x = NULL;
    x      = root;
    next_x = x->next;

    while (next_x != NULL)
    {
        if (   (x->degree != next_x->degree)
            || ((next_x->next != NULL) && (next_x->degree == next_x->next->degree)))
        {
            // Case 1: x->degree != next_x->degree
            // Case 2: x->degree == next_x->degree == next_x->next->degree
            prev_x = x;
            x = next_x;
        }
        else if (x->key <= next_x->key)
        {
            // Case 3: x->degree == next_x->degree != next_x->next->degree
            //      && x->key    <= next_x->key
            x->next = next_x->next;
            link(next_x, x);
        }
        else
        {
            // Case 4: x->degree == next_x->degree != next_x->next->degree
            //      && x->key    >  next_x->key
            if (prev_x == NULL)
            {
                root = next_x;
            }
            else
            {
                prev_x->next = next_x;
            }
            link(x, next_x);
            x = next_x;
        }
        next_x = x->next;
    }

    return root;
}

/*
 * 将二项堆other合并到当前堆中
 */
template <class T>
void BinomialHeap<T>::combine(BinomialHeap<T> *other)
{
    if (other!=NULL && other->mRoot!=NULL)
        mRoot = combine(mRoot, other->mRoot);
}

/*
 * 新建key对应的节点,并将其插入到二项堆中。
 */
template <class T>
void BinomialHeap<T>::insert(T key)
{
    BinomialNode<T>* node;

    // 禁止插入相同的键值
    if (contains(key))
    {
        cout << "Insert Error: the key (" << key << ") is existed already!" << endl;
        return ;
    }

    node = new BinomialNode<T>(key);
    if (node==NULL)
        return ;

    mRoot = combine(mRoot, node);
}

/*
 * 反转二项堆root,并返回反转后的根节点
 */
template <class T>
BinomialNode<T>* BinomialHeap<T>::reverse(BinomialNode<T>* root)
{
    BinomialNode<T>* next;
    BinomialNode<T>* tail = NULL;

    if (!root)
        return root;

    root->parent = NULL;
    while (root->next)
    {
        next          = root->next;
        root->next = tail;
        tail          = root;
        root          = next;
        root->parent  = NULL;
    }
    root->next = tail;

    return root;
}

/*
 * 移除二项堆root中的最小节点,并返回删除节点后的二项树
 */
template <class T>
BinomialNode<T>* BinomialHeap<T>::extractMinimum(BinomialNode<T>* root)
{
    BinomialNode<T> *y, *prev_y;    // y是最小节点

    if (root==NULL)
        return root;

    // 找到"最小节点根y"和"它的前一个根节点prev_y"
    minimum(root, prev_y, y);

    if (prev_y == NULL)    // root的根节点就是最小根节点
        root = root->next;
    else                // root的根节点不是最小根节点
        prev_y->next = y->next;

    // 反转最小节点的左孩子,得到最小堆child;
    // 这样,就使得最小节点所在二项树的孩子们都脱离出来成为一棵独立的二项树(不包括最小节点)
    BinomialNode<T>* child = reverse(y->child);
    // 将"删除最小节点的二项堆child"和"root"进行合并。
    root = combine(root, child);

    // 删除最小节点
    delete y;

    return root;
}

template <class T>
void BinomialHeap<T>::extractMinimum()
{
    mRoot = extractMinimum(mRoot);
}

/*
 * 减少关键字的值:将二项堆中的节点node的键值减小为key。
 */
template <class T>
void BinomialHeap<T>::decreaseKey(BinomialNode<T>* node, T key)
{
    if(key>=node->key || contains(key))
    {
        cout << "decrease failed: the new key(" << key <<") is existed already, "
             << "or is no smaller than current key(" << node->key <<")" << endl;
        return ;
    }
    node->key = key;

    BinomialNode<T> *child, *parent;
    child = node;
    parent = node->parent;
    while(parent != NULL && child->key < parent->key)
    {
        swap(parent->key, child->key);
        child = parent;
        parent = child->parent;
    }
}

/*
 * 增加关键字的值:将二项堆中的节点node的键值增加为key。
 */
template <class T>
void BinomialHeap<T>::increaseKey(BinomialNode<T>* node, T key)
{
    if(key<=node->key || contains(key))
    {
        cout << "decrease failed: the new key(" << key <<") is existed already, "
             << "or is no greater than current key(" << node->key <<")" << endl;
        return ;
    }

    node->key = key;

    BinomialNode<T> *cur, *child, *least;
    cur = node;
    child = cur->child;
    while (child != NULL)
    {
        if(cur->key > child->key)
        {
            // 如果"当前节点" < "它的左孩子",
            // 则在"它的孩子中(左孩子 和 左孩子的兄弟)"中,找出最小的节点;
            // 然后将"最小节点的值" 和 "当前节点的值"进行互换
            least = child;
            while(child->next != NULL)
            {
                if (least->key > child->next->key)
                {
                    least = child->next;
                }
                child = child->next;
            }
            // 交换最小节点和当前节点的值
            swap(least->key, cur->key);

            // 交换数据之后,再对"原最小节点"进行调整,使它满足最小堆的性质:父节点 <= 子节点
            cur = least;
            child = cur->child;
        }
        else
        {
            child = child->next;
        }
    }
}

/*
 * 更新二项堆的节点node的键值为key
 */
template <class T>
void BinomialHeap<T>::updateKey(BinomialNode<T>* node, T key)
{
    if (node == NULL)
        return ;

    if(key < node->key)
        decreaseKey(node, key);
    else if(key > node->key)
        increaseKey(node, key);
    else
        cout <<"No need to update!!!" <<endl;
}

/*
 * 将二项堆中键值oldkey更新为newkey
 */
template <class T>
void BinomialHeap<T>::update(T oldkey, T newkey)
{
    BinomialNode<T> *node;

    node = search(mRoot, oldkey);
    if (node != NULL)
        updateKey(node, newkey);
}

/*
 * 查找:在二项堆中查找键值为key的节点
 */
template <class T>
BinomialNode<T>* BinomialHeap<T>::search(BinomialNode<T>* root, T key)
{
    BinomialNode<T> *child;
    BinomialNode<T> *parent = root;

    parent = root;
    while (parent != NULL)
    {
        if (parent->key == key)
            return parent;
        else
        {
            if((child = search(parent->child, key)) != NULL)
                return child;
            parent = parent->next;
        }
    }

    return NULL;
}

/*
 * 二项堆中是否包含键值key
 */
template <class T>
bool BinomialHeap<T>::contains(T key)
{
    return search(mRoot, key)!=NULL ? true : false;
}

/*
 * 删除节点:删除键值为key的节点
 */
template <class T>
BinomialNode<T>* BinomialHeap<T>::remove(BinomialNode<T>* root, T key)
{
    BinomialNode<T> *node;
    BinomialNode<T> *parent, *prev, *pos;

    if (root==NULL)
        return root;

    // 查找键值为key的节点
    if ((node = search(root, key)) == NULL)
        return root;

    // 将被删除的节点的数据数据上移到它所在的二项树的根节点
    parent = node->parent;
    while (parent != NULL)
    {
        // 交换数据
        swap(node->key, parent->key);
        // 下一个父节点
        node   = parent;
        parent = node->parent;
    }

    // 找到node的前一个根节点(prev)
    prev = NULL;
    pos  = root;
    while (pos != node)
    {
        prev = pos;
        pos  = pos->next;
    }
    // 移除node节点
    if (prev)
        prev->next = node->next;
    else
        root = node->next;

    root = combine(root, reverse(node->child));

    delete node;

    return root;
}

template <class T>
void BinomialHeap<T>::remove(T key)
{
    mRoot = remove(mRoot, key);
}



/*
 * 打印"二项堆"
 *
 * 参数说明:
 *     node       -- 当前节点
 *     prev       -- 当前节点的前一个节点(父节点or兄弟节点)
 *     direction  --  1,表示当前节点是一个左孩子;
 *                    2,表示当前节点是一个兄弟节点。
 */
template <class T>
void BinomialHeap<T>::print(BinomialNode<T>* node, BinomialNode<T>* prev, int direction)
{
    while(node != NULL)
    {
        if(direction==1)    // node是根节点
            cout << "\t" << setw(2) << node->key << "(" << node->degree << ") is "<< setw(2) << prev->key << "'s child" << endl;
        else                // node是分支节点
            cout << "\t" << setw(2) << node->key << "(" << node->degree << ") is "<< setw(2) << prev->key << "'s next" << endl;

        if (node->child != NULL)
            print(node->child, node, 1);

        // 兄弟节点
        prev = node;
        node = node->next;
        direction = 2;
    }
}

template <class T>
void BinomialHeap<T>::print()
{
    BinomialNode<T> *p;
    if (mRoot == NULL)
        return ;

    cout << "== 二项堆( ";
    p = mRoot;
    while (p != NULL)
    {
        cout << "B" << p->degree << " ";
        p = p->next;
    }
    cout << ")的详细信息:" << endl;

    int i=0;
    p = mRoot;
    while (p != NULL)
    {
        i++;
        cout << i << ". 二项树B" << p->degree << ":" << endl;
        cout << "\t" << setw(2) << p->key << "(" << p->degree << ") is root" << endl;

        print(p->child, p, 1);
        p = p->next;
    }
    cout << endl;
}
#endif

二项堆的测试程序(Main.cpp)

/**
 * C 语言: 二项堆
 *
 * @author skywang
 * @date 2014/04/02
 */

#include <iostream>
#include "BinomialHeap.h"
using namespace std;

#define DEBUG 0

// 共7个 = 1+2+4
int a[] = {12,  7, 25, 15, 28,
           33, 41};
// 共13个 = 1+4+8
int b[] = {18, 35, 20, 42,  9,
           31, 23,  6, 48, 11,
           24, 52, 13 };

// 验证"二项堆的插入操作"
void testInsert()
{
    int i;
    int alen=sizeof(a)/sizeof(a[0]);
    BinomialHeap<int>* ha=new BinomialHeap<int>();

    cout << "== 二项堆(ha)中依次添加: ";
    for(i=0; i<alen; i++)
    {
        cout << a[i] <<" ";
        ha->insert(a[i]);
    }
    cout << endl;
    cout << "== 二项堆(ha)的详细信息: " << endl;
    ha->print();
}

// 验证"二项堆的合并操作"
void testUnion()
{
    int i;
    int alen=sizeof(a)/sizeof(a[0]);
    int blen=sizeof(b)/sizeof(b[0]);
    BinomialHeap<int>* ha=new BinomialHeap<int>();
    BinomialHeap<int>* hb=new BinomialHeap<int>();

    cout << "== 二项堆(ha)中依次添加: ";
    for(i=0; i<alen; i++)
    {
        cout << a[i] <<" ";
        ha->insert(a[i]);
    }
    cout << endl;
    cout << "== 二项堆(ha)的详细信息: " << endl;
    ha->print();


    cout << "== 二项堆(hb)中依次添加: ";
    for(i=0; i<blen; i++)
    {
        cout << b[i] <<" ";
        hb->insert(b[i]);
    }
    cout << endl;
    cout << "== 二项堆(hb)的详细信息: " << endl;
    hb->print();


    // 将"二项堆hb"合并到"二项堆ha"中。
    ha->combine(hb);
    cout << "== 合并ha和hb后的详细信息: " << endl;
    ha->print();
}

// 验证"二项堆的删除操作"
void testDelete()
{
    int i;
    int blen=sizeof(b)/sizeof(b[0]);
    BinomialHeap<int>* hb=new BinomialHeap<int>();

    cout << "== 二项堆(hb)中依次添加: ";
    for(i=0; i<blen; i++)
    {
        cout << b[i] <<" ";
        hb->insert(b[i]);
    }
    cout << endl;
    cout << "== 二项堆(hb)的详细信息: " << endl;
    hb->print();


    // 将"二项堆hb"合并到"二项堆ha"中。
    hb->remove(20);
    cout << "== 删除节点20后的详细信息: " << endl;
    hb->print();
}

// 验证"二项堆的更新(减少)操作"
void testDecrease()
{
    int i;
    int blen=sizeof(b)/sizeof(b[0]);
    BinomialHeap<int>* hb=new BinomialHeap<int>();

    cout << "== 二项堆(hb)中依次添加: ";
    for(i=0; i<blen; i++)
    {
        cout << b[i] <<" ";
        hb->insert(b[i]);
    }
    cout << endl;
    cout << "== 二项堆(hb)的详细信息: " << endl;
    hb->print();


    // 将节点20更新为2
    hb->update(20, 2);
    cout << "== 更新节点20->2后的详细信息: " << endl;
    hb->print();
}

// 验证"二项堆的更新(增加)操作"
void testIncrease()
{
    int i;
    int blen=sizeof(b)/sizeof(b[0]);
    BinomialHeap<int>* hb=new BinomialHeap<int>();

    cout << "== 二项堆(hb)中依次添加: ";
    for(i=0; i<blen; i++)
    {
        cout << b[i] <<" ";
        hb->insert(b[i]);
    }
    cout << endl;
    cout << "== 二项堆(hb)的详细信息: " << endl;
    hb->print();


    // 将节点6更新为60
    hb->update(6, 60);
    cout << "== 更新节点6->60后的详细信息: " << endl;
    hb->print();
}

int main()
{
    // 1. 验证"二项堆的插入操作"
    testInsert();
    // 2. 验证"二项堆的合并操作"
    //testUnion();
    // 3. 验证"二项堆的删除操作"
    //testDelete();
    // 4. 验证"二项堆的更新(减少)操作"
    //testDecrease();
    // 5. 验证"二项堆的更新(增加)操作"
    //testIncrease();

    return 0;
}

二项堆的C++测试程序

二项堆的测试程序包括了五部分,分别是"插入"、"删除"、"增加"、"减少"、"合并"这5种功能的测试代码。默认是运行的"插入"功能代码,你可以根据自己的需要来对相应的功能进行验证!

下面是插入功能运行结果:

== 二项堆(ha)中依次添加: 12 7 25 15 28 33 41 
== 二项堆(ha)的详细信息: 
== 二项堆( B0 B1 B2 )的详细信息:
1. 二项树B0: 
    41(0) is root
2. 二项树B1: 
    28(1) is root
    33(0) is 28's child
3. 二项树B2: 
     7(2) is root
    15(1) is  7's child
    25(0) is 15's child
    12(0) is 15's next

二项堆的Java实现(完整源码)

二项堆的实现文件(BinomialHeap.java)

/**
 * Java 语言: 二项堆
 *
 * @author skywang
 * @date 2014/04/03
 */

public class BinomialHeap<T extends Comparable<T>> {

    private BinomialNode<T> mRoot;    // 根结点

    private class BinomialNode<T extends Comparable<T>> {
        T key;                // 关键字(键值)
        int degree;            // 度数
        BinomialNode<T> child;    // 左孩子
        BinomialNode<T> parent;    // 父节点
        BinomialNode<T> next;    // 兄弟节点

        public BinomialNode(T key) {
            this.key = key;
            this.degree = 0;
            this.child = null;
            this.parent = null;
            this.next = null;
        }

        public String toString() {
            return "key:"+key;
        }
    }

    public BinomialHeap() {
        mRoot = null;
    }

    /*
     * 获取二项堆中的最小节点的键值
     */
    public T minimum() {
        if (mRoot==null)
            return null;

        BinomialNode<T> x, prev_x;    // x是用来遍历的当前节点
        BinomialNode<T> y, prev_y;    // y是最小节点

        prev_x  = mRoot;
        x       = mRoot.next;
        prev_y  = null;
        y       = mRoot;
        // 找到最小节点
        while (x != null) {
            if (x.key.compareTo(y.key) < 0) {
                y = x;
                prev_y = prev_x;
            }
            prev_x = x;
            x = x.next;
        }

        return y.key;
    }


    /*
     * 合并两个二项堆:将child合并到root中
     */
    private void link(BinomialNode<T> child, BinomialNode<T> root) {
        child.parent = root;
        child.next   = root.child;
        root.child = child;
        root.degree++;
    }

    /*
     * 将h1, h2中的根表合并成一个按度数递增的链表,返回合并后的根节点
     */
    private BinomialNode<T> merge(BinomialNode<T> h1, BinomialNode<T> h2) {
        if (h1 == null) return h2;
        if (h2 == null) return h1;

        // root是新堆的根,h3用来遍历h1和h3的。
        BinomialNode<T> pre_h3, h3, root=null;

        pre_h3 = null;
        //整个while,h1, h2, pre_h3, h3都在往后顺移
        while ((h1!=null) && (h2!=null)) {

            if (h1.degree <= h2.degree) {
                h3 = h1;
                h1 = h1.next;
            } else {
                h3 = h2;
                h2 = h2.next;
            }

            if (pre_h3 == null) {
                pre_h3 = h3;
                root = h3;
            } else {
                pre_h3.next = h3;
                pre_h3 = h3;
            }

            if (h1 != null) {
                h3.next = h1;
            } else {
                h3.next = h2;
            }
        }
        return root;
    }

    /*
     * 合并二项堆:将h1, h2合并成一个堆,并返回合并后的堆
     */
    private BinomialNode<T> union(BinomialNode<T> h1, BinomialNode<T> h2) {
        BinomialNode<T> root;

        // 将h1, h2中的根表合并成一个按度数递增的链表root
        root = merge(h1, h2);
        if (root == null)
            return null;

        BinomialNode<T> prev_x = null;
        BinomialNode<T> x      = root;
        BinomialNode<T> next_x = x.next;
        while (next_x != null) {

            if (   (x.degree != next_x.degree)
                || ((next_x.next != null) && (next_x.degree == next_x.next.degree))) {
                // Case 1: x.degree != next_x.degree
                // Case 2: x.degree == next_x.degree == next_x.next.degree
                prev_x = x;
                x = next_x;
            } else if (x.key.compareTo(next_x.key) <= 0) {
                // Case 3: x.degree == next_x.degree != next_x.next.degree
                //      && x.key    <= next_x.key
                x.next = next_x.next;
                link(next_x, x);
            } else {
                // Case 4: x.degree == next_x.degree != next_x.next.degree
                //      && x.key    >  next_x.key
                if (prev_x == null) {
                    root = next_x;
                } else {
                    prev_x.next = next_x;
                }
                link(x, next_x);
                x = next_x;
            }
            next_x = x.next;
        }

        return root;
    }

    /*
     * 将二项堆other合并到当前堆中
     */
    public void union(BinomialHeap<T> other) {
        if (other!=null && other.mRoot!=null)
            mRoot = union(mRoot, other.mRoot);
    }

    /*
     * 新建key对应的节点,并将其插入到二项堆中。
     */
    public void insert(T key) {
        BinomialNode<T> node;

        // 禁止插入相同的键值
        if (contains(key)==true) {
            System.out.printf("insert failed: the key(%s) is existed already!\n", key);
            return ;
        }

        node = new BinomialNode<T>(key);
        if (node==null)
            return ;

        mRoot = union(mRoot, node);
    }

    /*
     * 反转二项堆root,并返回反转后的根节点
     */
    private BinomialNode<T> reverse(BinomialNode<T> root) {
        BinomialNode<T> next;
        BinomialNode<T> tail = null;

        if (root==null)
            return root;

        root.parent = null;
        while (root.next!=null) {
            next         = root.next;
            root.next    = tail;
            tail         = root;
            root         = next;
            root.parent  = null;
        }
        root.next = tail;

        return root;
    }

    /*
     * 移除二项堆root中的最小节点,并返回删除节点后的二项树
     */
    private BinomialNode<T> extractMinimum(BinomialNode<T> root) {
        if (root==null)
            return root;

        BinomialNode<T> x, prev_x;    // x是用来遍历的当前节点
        BinomialNode<T> y, prev_y;    // y是最小节点

        prev_x  = root;
        x       = root.next;
        prev_y = null;
        y      = root;
        // 找到最小节点
        while (x != null) {
            if (x.key.compareTo(y.key) < 0) {
                y = x;
                prev_y = prev_x;
            }
            prev_x = x;
            x = x.next;
        }

        if (prev_y == null)    // root的根节点就是最小根节点
            root = root.next;
        else                // root的根节点不是最小根节点
            prev_y.next = y.next;

        // 反转最小节点的左孩子,得到最小堆child;
        // 这样,就使得最小节点所在二项树的孩子们都脱离出来成为一棵独立的二项树(不包括最小节点)
        BinomialNode<T> child = reverse(y.child);
        // 将"删除最小节点的二项堆child"和"root"进行合并。
        root = union(root, child);

        // help GC
        y = null;

        return root;
    }

    public void extractMinimum() {
        mRoot = extractMinimum(mRoot);
    }

    /*
     * 减少关键字的值:将二项堆中的节点node的键值减小为key。
     */
    private void decreaseKey(BinomialNode<T> node, T key) {
        if(key.compareTo(node.key)>=0 || contains(key)==true) {
            System.out.println("decrease failed: the new key("+key+") is existed already, or is no smaller than current key("+node.key+")");
            return ;
        }
        node.key = key;

        BinomialNode<T> child, parent;
        child = node;
        parent = node.parent;
        while(parent != null && child.key.compareTo(parent.key)<0) {
            // 交换parent和child的数据
            T tmp = parent.key;
            parent.key = child.key;
            child.key = tmp;

            child = parent;
            parent = child.parent;
        }
    }

    /*
     * 增加关键字的值:将二项堆中的节点node的键值增加为key。
     */
    private void increaseKey(BinomialNode<T> node, T key) {
        if(key.compareTo(node.key)<=0 || contains(key)==true) {
            System.out.println("increase failed: the new key("+key+") is existed already, or is no greater than current key("+node.key+")");
            return ;
        }
        node.key = key;

        BinomialNode<T> cur = node;
        BinomialNode<T> child = cur.child;
        while (child != null) {

            if(cur.key.compareTo(child.key) > 0) {
                // 如果"当前节点" < "它的左孩子",
                // 则在"它的孩子中(左孩子 和 左孩子的兄弟)"中,找出最小的节点;
                // 然后将"最小节点的值" 和 "当前节点的值"进行互换
                BinomialNode<T> least = child;    // least是child和它的兄弟中的最小节点
                while(child.next != null) {
                    if (least.key.compareTo(child.next.key) > 0)
                        least = child.next;
                    child = child.next;
                }
                // 交换最小节点和当前节点的值
                T tmp = least.key;
                least.key = cur.key;
                cur.key = tmp;

                // 交换数据之后,再对"原最小节点"进行调整,使它满足最小堆的性质:父节点 <= 子节点
                cur = least;
                child = cur.child;
            } else {
                child = child.next;
            }
        }
    }

    /*
     * 更新二项堆的节点node的键值为key
     */
    private void updateKey(BinomialNode<T> node, T key) {
        if (node == null)
            return ;

        int cmp = key.compareTo(node.key);
        if(cmp < 0)                            // key < node.key
            decreaseKey(node, key);
        else if(cmp > 0)                    // key > node.key
            increaseKey(node, key);
        else
            System.out.println("No need to update!!!");
    }

    /*
     * 将二项堆中键值oldkey更新为newkey
     */
    public void update(T oldkey, T newkey) {
        BinomialNode<T> node;

        node = search(mRoot, oldkey);
        if (node != null)
            updateKey(node, newkey);
    }

    /*
     * 查找:在二项堆中查找键值为key的节点
     */
    private BinomialNode<T> search(BinomialNode<T> root, T key) {
        BinomialNode<T> child;
        BinomialNode<T> parent = root;

        parent = root;
        while (parent != null) {
            if (parent.key.compareTo(key) == 0)
                return parent;
            else {
                if((child = search(parent.child, key)) != null)
                    return child;
                parent = parent.next;
            }
        }

        return null;
    }

    /*
     * 二项堆中是否包含键值key
     */
    public boolean contains(T key) {
        return search(mRoot, key)!=null ? true : false;
    }

    /*
     * 删除节点:删除键值为key的节点
     */
    private BinomialNode<T> remove(BinomialNode<T> root, T key) {
        if (root==null)
            return root;

        BinomialNode<T> node;

        // 查找键值为key的节点
        if ((node = search(root, key)) == null)
            return root;

        // 将被删除的节点的数据数据上移到它所在的二项树的根节点
        BinomialNode<T> parent = node.parent;
        while (parent != null) {
            // 交换数据
            T tmp = node.key;
            node.key = parent.key;
            parent.key = tmp;

            // 下一个父节点
            node   = parent;
            parent = node.parent;
        }

        // 找到node的前一个根节点(prev)
        BinomialNode<T> prev = null;
        BinomialNode<T> pos  = root;
        while (pos != node) {
            prev = pos;
            pos  = pos.next;
        }
        // 移除node节点
        if (prev!=null)
            prev.next = node.next;
        else
            root = node.next;

        root = union(root, reverse(node.child));

        // help GC
        node = null;

        return root;
    }

    public void remove(T key) {
        mRoot = remove(mRoot, key);
    }

    /*
     * 打印"二项堆"
     *
     * 参数说明:
     *     node       -- 当前节点
     *     prev       -- 当前节点的前一个节点(父节点or兄弟节点)
     *     direction  --  1,表示当前节点是一个左孩子;
     *                    2,表示当前节点是一个兄弟节点。
     */
    private void print(BinomialNode<T> node, BinomialNode<T> prev, int direction) {
        while(node != null)
        {
            if(direction==1)    // node是根节点
                System.out.printf("\t%2d(%d) is %2d's child\n", node.key, node.degree, prev.key);
            else                // node是分支节点
                System.out.printf("\t%2d(%d) is %2d's next\n", node.key, node.degree, prev.key);

            if (node.child != null)
                print(node.child, node, 1);

            // 兄弟节点
            prev = node;
            node = node.next;
            direction = 2;
        }
    }

    public void print() {
        if (mRoot == null)
            return ;

        BinomialNode<T> p = mRoot;
        System.out.printf("== 二项堆( ");
        while (p != null) {
            System.out.printf("B%d ", p.degree);
            p = p.next;
        }
        System.out.printf(")的详细信息:\n");

        int i=0;
        p = mRoot;
        while (p != null) {
            i++;
            System.out.printf("%d. 二项树B%d: \n", i, p.degree);
            System.out.printf("\t%2d(%d) is root\n", p.key, p.degree);

            print(p.child, p, 1);
            p = p.next;
        }
        System.out.printf("\n");
    }
}

二项堆的测试程序(Main.java)

/**
 * Java 语言: 二项堆
 *
 * @author skywang
 * @date 2014/03/31
 */

public class Main {

    private static final boolean DEBUG = false;

    // 共7个 = 1+2+4
    private static int a[] = {12,  7, 25, 15, 28, 33, 41};
    // 共13个 = 1+4+8
    private static int b[] = {18, 35, 20, 42,  9,
               31, 23,  6, 48, 11,
               24, 52, 13 };


    // 验证"二项堆的插入操作"
    public static void testInsert() {
        BinomialHeap<Integer> ha=new BinomialHeap<Integer>();

        // 二项堆ha
        System.out.printf("== 二项堆(ha)中依次添加: ");
        for(int i=0; i<a.length; i++) {
            System.out.printf("%d ", a[i]);
            ha.insert(a[i]);
        }
        System.out.printf("\n");
        System.out.printf("== 二项堆(ha)的详细信息: \n");
        ha.print();
    }

    // 验证"二项堆的合并操作"
    public static void testUnion() {
        BinomialHeap<Integer> ha=new BinomialHeap<Integer>();
        BinomialHeap<Integer> hb=new BinomialHeap<Integer>();

        // 二项堆ha
        System.out.printf("== 二项堆(ha)中依次添加: ");
        for(int i=0; i<a.length; i++) {
            System.out.printf("%d ", a[i]);
            ha.insert(a[i]);
        }
        System.out.printf("\n");
        System.out.printf("== 二项堆(ha)的详细信息: \n");
        ha.print();

        // 二项堆hb
        System.out.printf("== 二项堆(hb)中依次添加: ");
        for(int i=0; i<b.length; i++) {
            System.out.printf("%d ", b[i]);
            hb.insert(b[i]);
        }
        System.out.printf("\n");
        // 打印二项堆hb
        System.out.printf("== 二项堆(hb)的详细信息: \n");
        hb.print();

        // 将"二项堆hb"合并到"二项堆ha"中。
        ha.union(hb);
        // 打印二项堆ha的详细信息
        System.out.printf("== 合并ha和hb后的详细信息:\n");
        ha.print();
    }

    // 验证"二项堆的删除操作"
    public static void testDelete() {
        BinomialHeap<Integer> hb=new BinomialHeap<Integer>();

        // 二项堆hb
        System.out.printf("== 二项堆(hb)中依次添加: ");
        for(int i=0; i<b.length; i++) {
            System.out.printf("%d ", b[i]);
            hb.insert(b[i]);
        }
        System.out.printf("\n");
        // 打印二项堆hb
        System.out.printf("== 二项堆(hb)的详细信息: \n");
        hb.print();

        // 将"二项堆hb"合并到"二项堆ha"中。
        hb.remove(20);
        System.out.printf("== 删除节点20后的详细信息: \n");
        hb.print();
    }

    // 验证"二项堆的更新(减少)操作"
    public static void testDecrease() {
        BinomialHeap<Integer> hb=new BinomialHeap<Integer>();

        // 二项堆hb
        System.out.printf("== 二项堆(hb)中依次添加: ");
        for(int i=0; i<b.length; i++) {
            System.out.printf("%d ", b[i]);
            hb.insert(b[i]);
        }
        System.out.printf("\n");
        // 打印二项堆hb
        System.out.printf("== 二项堆(hb)的详细信息: \n");
        hb.print();

        // 将节点20更新为2
        hb.update(20, 2);
        System.out.printf("== 更新节点20->2后的详细信息: \n");
        hb.print();
    }

    // 验证"二项堆的更新(减少)操作"
    public static void testIncrease() {
        BinomialHeap<Integer> hb=new BinomialHeap<Integer>();

        // 二项堆hb
        System.out.printf("== 二项堆(hb)中依次添加: ");
        for(int i=0; i<b.length; i++) {
            System.out.printf("%d ", b[i]);
            hb.insert(b[i]);
        }
        System.out.printf("\n");
        // 打印二项堆hb
        System.out.printf("== 二项堆(hb)的详细信息: \n");
        hb.print();

        // 将节点6更新为60
        hb.update(6, 60);
        System.out.printf("== 更新节点6->60后的详细信息: \n");
        hb.print();
    }

    public static void main(String[] args) {
        // 1. 验证"二项堆的插入操作"
        testInsert();
        // 2. 验证"二项堆的合并操作"
        //testUnion();
        // 3. 验证"二项堆的删除操作"
        //testDelete();
        // 4. 验证"二项堆的更新(减少)操作"
        //testDecrease();
        // 5. 验证"二项堆的更新(增加)操作"
        //testIncrease();
    }
}

二项堆的Java测试程序

二项堆的测试程序包括了五部分,分别是"插入"、"删除"、"增加"、"减少"、"合并"这5种功能的测试代码。默认是运行的"插入"功能代码,你可以根据自己的需要来对相应的功能进行验证!

下面是插入功能运行结果:

== 二项堆(ha)中依次添加: 12 7 25 15 28 33 41 
== 二项堆(ha)的详细信息: 
== 二项堆( B0 B1 B2 )的详细信息:
1. 二项树B0: 
    41(0) is root
2. 二项树B1: 
    28(1) is root
    33(0) is 28's child
3. 二项树B2: 
     7(2) is root
    15(1) is  7's child
    25(0) is 15's child
    12(0) is 15's next

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值