skywang12345

导航

 

 

概要

本章介绍左倾堆,它和二叉堆一样,都是堆结构中的一员。和以往一样,本文会先对左倾堆的理论知识进行简单介绍,然后给出C语言的实现。后续再分别给出C++和Java版本的实现;实现的语言虽不同,但是原理如出一辙,选择其中之一进行了解即可。若文章有错误或不足的地方,请不吝指出!

目录
1. 左倾堆的介绍
2. 左倾堆的图文解析
3. 左倾堆的C实现(完整源码)
4. 左倾堆的C测试程序

转载请注明出处:http://www.cnblogs.com/skywang12345/p/3638327.html


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

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

 

左倾堆的介绍

左倾堆(leftist tree 或 leftist heap),又被成为左偏树、左偏堆,最左堆等。
它和二叉堆一样,都是优先队列实现方式。当优先队列中涉及到"对两个优先队列进行合并"的问题时,二叉堆的效率就无法令人满意了,而本文介绍的左倾堆,则可以很好地解决这类问题。

 

左倾堆的定义

左倾堆是一棵二叉树,它的节点除了和二叉树的节点一样具有左右子树指针外,还有两个属性:键值零距离
(01) 键值的作用是来比较节点的大小,从而对节点进行排序。
(02) 零距离(英文名NPL,即Null Path Length)则是从一个节点到一个"最近的不满节点"的路径长度。不满节点是指该该节点的左右孩子至少有有一个为NULL。叶节点的NPL为0,NULL节点的NPL为-1。

上图是一颗左倾堆,它满足左倾堆的基本性质:
[性质1] 节点的键值小于或等于它的左右子节点的键值。
[性质2] 节点的左孩子的NPL >= 右孩子的NPL。
[性质3] 节点的NPL = 它的右孩子的NPL + 1。

左倾堆,顾名思义,是有点向左倾斜的意思了。它在统计问题、最值问题、模拟问题和贪心问题等问题中有着广泛的应用。此外,斜堆是比左倾堆更为一般的数据结构。当然,今天讨论的是左倾堆,关于斜堆,以后再撰文来表。
前面说过,它能和好的解决"两个优先队列合并"的问题。实际上,左倾堆的合并操作的平摊时间复杂度为O(lg n),而完全二叉堆为O(n)。合并就是左倾树的重点,插入和删除操作都是以合并操作为基础的。插入操作,可以看作两颗左倾树合并;删除操作(移除优先队列中队首元素),则是移除根节点之后再合并剩余的两个左倾树。闲话说到这里,下面开始介绍左倾树的基本方法。

 

左倾堆的图文解析

合并操作是左倾堆的重点。合并两个左倾堆的基本思想如下:
(01) 如果一个空左倾堆与一个非空左倾堆合并,返回非空左倾堆。
(02) 如果两个左倾堆都非空,那么比较两个根节点,取较小堆的根节点为新的根节点。将"较小堆的根节点的右孩子"和"较大堆"进行合并。
(03) 如果新堆的右孩子的NPL > 左孩子的NPL,则交换左右孩子。
(04) 设置新堆的根节点的NPL = 右子堆NPL + 1

 

下面通过图文演示合并以下两个堆的过程。


提示:这两个堆的合并过程和测试程序相对应!

第1步:将"较小堆(根为10)的右孩子"和"较大堆(根为11)"进行合并。
合并的结果,相当于将"较大堆"设置"较小堆"的右孩子,如下图所示:

 

第2步:将上一步得到的"根11的右子树"和"根为12的树"进行合并,得到的结果如下:

 

第3步:将上一步得到的"根12的右子树"和"根为13的树"进行合并,得到的结果如下:

 

第4步:将上一步得到的"根13的右子树"和"根为16的树"进行合并,得到的结果如下:

 

第5步:将上一步得到的"根16的右子树"和"根为23的树"进行合并,得到的结果如下:

至此,已经成功的将两棵树合并成为一棵树了。接下来,对新生成的树进行调节。

 

第6步:上一步得到的"树16的右孩子的NPL > 左孩子的NPL",因此交换左右孩子。得到的结果如下:

 

第7步:上一步得到的"树12的右孩子的NPL > 左孩子的NPL",因此交换左右孩子。得到的结果如下:

 

第8步:上一步得到的"树10的右孩子的NPL > 左孩子的NPL",因此交换左右孩子。得到的结果如下:

至此,合并完毕。上面就是合并得到的左倾堆!


下面看看左倾堆的基本操作的代码

1. 头文件

#ifndef _LEFTIST_TREE_H_
#define _LEFTIST_TREE_H_

typedef int Type;

typedef struct _LeftistNode{
    Type   key;                    // 关键字(键值)
    int npl;                    // 零路经长度(Null Path Length)
    struct _LeftistNode *left;    // 左孩子
    struct _LeftistNode *right;    // 右孩子
}LeftistNode, *LeftistHeap;

// 前序遍历"左倾堆"
void preorder_leftist(LeftistHeap heap);
// 中序遍历"左倾堆"
void inorder_leftist(LeftistHeap heap);
// 后序遍历"左倾堆"
void postorder_leftist(LeftistHeap heap);

// 获取最小值(保存到pval中),成功返回0,失败返回-1。
int leftist_minimum(LeftistHeap heap, int *pval);
// 合并"左倾堆x"和"左倾堆y",并返回合并后的新树
LeftistNode* merge_leftist(LeftistHeap x, LeftistHeap y);
// 将结点插入到左倾堆中,并返回根节点
LeftistNode* insert_leftist(LeftistHeap heap, Type key);
// 删除结点(key为节点的值),并返回根节点
LeftistNode* delete_leftist(LeftistHeap heap);

// 销毁左倾堆
void destroy_leftist(LeftistHeap heap);

// 打印左倾堆
void print_leftist(LeftistHeap heap);

#endif

LeftistNode是左倾堆对应的节点类。

 

2. 合并

/* 
 * 合并"左倾堆x"和"左倾堆y"
 *
 * 返回值:
 *     合并得到的树的根节点
 */
LeftistNode* merge_leftist(LeftistHeap x, LeftistHeap y)
{
    if(x == NULL)
        return y;
    if(y == NULL)
        return x;

    // 合并x和y时,将x作为合并后的树的根;
    // 这里的操作是保证: x的key < y的key
    if(x->key > y->key)
        swap_leftist_node(x, y);

    // 将x的右孩子和y合并,"合并后的树的根"是x的右孩子。
    x->right = merge_leftist(x->right, y);

    // 如果"x的左孩子为空" 或者 "x的左孩子的npl<右孩子的npl"
    // 则,交换x和y
    if(x->left == NULL || x->left->npl < x->right->npl)
    {
        LeftistNode *tmp = x->left;
        x->left = x->right;
        x->right = tmp;
    }
    // 设置合并后的新树(x)的npl
    if (x->right == NULL || x->left == NULL)
        x->npl = 0;
    else
        x->npl = (x->left->npl > x->right->npl) ? (x->right->npl + 1) : (x->left->npl + 1);

    return x;
}

merge_leftist(x, y)的作用是合并x和y这两个左倾堆,并返回得到的新堆。merge_leftist(x, y)是递归实现的。


3. 添加

/* 
 * 新建结点(key),并将其插入到左倾堆中
 *
 * 参数说明:
 *     heap 左倾堆的根结点
 *     key 插入结点的键值
 * 返回值:
 *     根节点
 */
LeftistNode* insert_leftist(LeftistHeap heap, Type key)
{
    LeftistNode *node;    // 新建结点

    // 如果新建结点失败,则返回。
    if ((node = (LeftistNode *)malloc(sizeof(LeftistNode))) == NULL)
        return heap;
    node->key = key;
    node->npl = 0;
    node->left = node->right = NULL;

    return merge_leftist(heap, node);
}

insert_leftist(heap, key)的作用是新建键值为key的结点,并将其插入到左倾堆中,并返回堆的根节点。

 

4. 删除

/* 
 * 取出根节点
 *
 * 返回值:
 *     取出根节点后的新树的根节点
 */
LeftistNode* delete_leftist(LeftistHeap heap)
{
    if (heap == NULL)
        return NULL;

    LeftistNode *l = heap->left;
    LeftistNode *r = heap->right;

    // 删除根节点
    free(heap);

    return merge_leftist(l, r); // 返回左右子树合并后的新树
}

delete_leftist(heap)的作用是删除左倾堆的最小节点,并返回删除节点后的左倾堆根节点。

 

注意关于左倾堆的"前序遍历"、"中序遍历"、"后序遍历"、"打印"、"销毁"等接口就不再单独介绍了。后文的源码中有给出它们的实现代码,Please RTFSC(Read The Fucking Source Code)!

 

左倾堆的C实现(完整源码)

左倾堆的头文件(leftist.h)

 1 #ifndef _LEFTIST_TREE_H_
 2 #define _LEFTIST_TREE_H_
 3 
 4 typedef int Type;
 5 
 6 typedef struct _LeftistNode{
 7     Type   key;                    // 关键字(键值)
 8     int npl;                    // 零路经长度(Null Path Length)
 9     struct _LeftistNode *left;    // 左孩子
10     struct _LeftistNode *right;    // 右孩子
11 }LeftistNode, *LeftistHeap;
12 
13 // 前序遍历"左倾堆"
14 void preorder_leftist(LeftistHeap heap);
15 // 中序遍历"左倾堆"
16 void inorder_leftist(LeftistHeap heap);
17 // 后序遍历"左倾堆"
18 void postorder_leftist(LeftistHeap heap);
19 
20 // 获取最小值(保存到pval中),成功返回0,失败返回-1。
21 int leftist_minimum(LeftistHeap heap, int *pval);
22 // 合并"左倾堆x"和"左倾堆y",并返回合并后的新树
23 LeftistNode* merge_leftist(LeftistHeap x, LeftistHeap y);
24 // 将结点插入到左倾堆中,并返回根节点
25 LeftistNode* insert_leftist(LeftistHeap heap, Type key);
26 // 删除结点(key为节点的值),并返回根节点
27 LeftistNode* delete_leftist(LeftistHeap heap);
28 
29 // 销毁左倾堆
30 void destroy_leftist(LeftistHeap heap);
31 
32 // 打印左倾堆
33 void print_leftist(LeftistHeap heap);
34 
35 #endif
View Code

左倾堆的实现文件(leftist.c)

  1 /**
  2  * C语言实现的左倾堆
  3  *
  4  * @author skywang
  5  * @date 2014/03/31
  6  */
  7 
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include "leftist.h"
 11 
 12 /*
 13  * 前序遍历"左倾堆"
 14  */
 15 void preorder_leftist(LeftistHeap heap)
 16 {
 17     if(heap != NULL)
 18     {
 19         printf("%d ", heap->key);
 20         preorder_leftist(heap->left);
 21         preorder_leftist(heap->right);
 22     }
 23 }
 24 
 25 /*
 26  * 中序遍历"左倾堆"
 27  */
 28 void inorder_leftist(LeftistHeap heap)
 29 {
 30     if(heap != NULL)
 31     {
 32         inorder_leftist(heap->left);
 33         printf("%d ", heap->key);
 34         inorder_leftist(heap->right);
 35     }
 36 }
 37 
 38 /*
 39  * 后序遍历"左倾堆"
 40  */
 41 void postorder_leftist(LeftistHeap heap)
 42 {
 43     if(heap != NULL)
 44     {
 45         postorder_leftist(heap->left);
 46         postorder_leftist(heap->right);
 47         printf("%d ", heap->key);
 48     }
 49 }
 50 
 51 /*
 52  * 交换两个节点的内容
 53  */
 54 static void swap_leftist_node(LeftistNode *x, LeftistNode *y)
 55 {
 56     LeftistNode tmp = *x;
 57     *x = *y;
 58     *y = tmp;
 59 }
 60 
 61 /* 
 62  * 获取最小值
 63  *
 64  * 返回值:
 65  *    成功返回0,失败返回-1
 66  */
 67 int leftist_minimum(LeftistHeap heap, int *pval)
 68 {
 69     if (heap == NULL)
 70         return -1;
 71 
 72     *pval = heap->key;
 73 
 74     return 0;
 75 }
 76  
 77 /* 
 78  * 合并"左倾堆x"和"左倾堆y"
 79  *
 80  * 返回值:
 81  *     合并得到的树的根节点
 82  */
 83 LeftistNode* merge_leftist(LeftistHeap x, LeftistHeap y)
 84 {
 85     if(x == NULL)
 86         return y;
 87     if(y == NULL)
 88         return x;
 89 
 90     // 合并x和y时,将x作为合并后的树的根;
 91     // 这里的操作是保证: x的key < y的key
 92     if(x->key > y->key)
 93         swap_leftist_node(x, y);
 94 
 95     // 将x的右孩子和y合并,"合并后的树的根"是x的右孩子。
 96     x->right = merge_leftist(x->right, y);
 97 
 98     // 如果"x的左孩子为空" 或者 "x的左孩子的npl<右孩子的npl"
 99     // 则,交换x和y
100     if(x->left == NULL || x->left->npl < x->right->npl)
101     {
102         LeftistNode *tmp = x->left;
103         x->left = x->right;
104         x->right = tmp;
105     }
106     // 设置合并后的新树(x)的npl
107     if (x->right == NULL || x->left == NULL)
108         x->npl = 0;
109     else
110         x->npl = (x->left->npl > x->right->npl) ? (x->right->npl + 1) : (x->left->npl + 1);
111 
112     return x;
113 }
114 
115 /* 
116  * 新建结点(key),并将其插入到左倾堆中
117  *
118  * 参数说明:
119  *     heap 左倾堆的根结点
120  *     key 插入结点的键值
121  * 返回值:
122  *     根节点
123  */
124 LeftistNode* insert_leftist(LeftistHeap heap, Type key)
125 {
126     LeftistNode *node;    // 新建结点
127 
128     // 如果新建结点失败,则返回。
129     if ((node = (LeftistNode *)malloc(sizeof(LeftistNode))) == NULL)
130         return heap;
131     node->key = key;
132     node->npl = 0;
133     node->left = node->right = NULL;
134 
135     return merge_leftist(heap, node);
136 }
137 
138 /* 
139  * 取出根节点
140  *
141  * 返回值:
142  *     取出根节点后的新树的根节点
143  */
144 LeftistNode* delete_leftist(LeftistHeap heap)
145 {
146     if (heap == NULL)
147         return NULL;
148 
149     LeftistNode *l = heap->left;
150     LeftistNode *r = heap->right;
151 
152     // 删除根节点
153     free(heap);
154 
155     return merge_leftist(l, r); // 返回左右子树合并后的新树
156 }
157 
158 /*
159  * 销毁左倾堆
160  */
161 void destroy_leftist(LeftistHeap heap)
162 {
163     if (heap==NULL)
164         return ;
165 
166     if (heap->left != NULL)
167         destroy_leftist(heap->left);
168     if (heap->right != NULL)
169         destroy_leftist(heap->right);
170 
171     free(heap);
172 }
173 
174 /*
175  * 打印"左倾堆"
176  *
177  * heap       -- 左倾堆的节点
178  * key        -- 节点的键值 
179  * direction  --  0,表示该节点是根节点;
180  *               -1,表示该节点是它的父结点的左孩子;
181  *                1,表示该节点是它的父结点的右孩子。
182  */
183 static void leftist_print(LeftistHeap heap, Type key, int direction)
184 {
185     if(heap != NULL)
186     {
187         if(direction==0)    // heap是根节点
188             printf("%2d(%d) is root\n", heap->key, heap->npl);
189         else                // heap是分支节点
190             printf("%2d(%d) is %2d's %6s child\n", heap->key, heap->npl, key, direction==1?"right" : "left");
191 
192         leftist_print(heap->left, heap->key, -1);
193         leftist_print(heap->right,heap->key,  1);
194     }
195 }
196 
197 void print_leftist(LeftistHeap heap)
198 {
199     if (heap != NULL)
200         leftist_print(heap, heap->key,  0);
201 }
View Code

左倾堆的测试程序(leftist_test.c)

 1 /**
 2  * C语言实现的左倾堆
 3  *
 4  * @author skywang
 5  * @date 2014/03/31
 6  */
 7 
 8 #include <stdio.h>
 9 #include "leftist.h"
10 
11 #define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) )
12 
13 void main()
14 {
15     int i;
16     int a[]= {10,40,24,30,36,20,12,16};
17     int b[]= {17,13,11,15,19,21,23};
18     int alen=LENGTH(a);
19     int blen=LENGTH(b);
20     LeftistHeap ha,hb;
21 
22     ha=hb=NULL;
23 
24     printf("== 左倾堆(ha)中依次添加: ");
25     for(i=0; i<alen; i++)
26     {
27         printf("%d ", a[i]);
28         ha = insert_leftist(ha, a[i]);
29     }
30     printf("\n== 左倾堆(ha)的详细信息: \n");
31     print_leftist(ha);
32 
33 
34     printf("\n== 左倾堆(hb)中依次添加: ");
35     for(i=0; i<blen; i++)
36     {
37         printf("%d ", b[i]);
38         hb = insert_leftist(hb, b[i]);
39     }
40     printf("\n== 左倾堆(hb)的详细信息: \n");
41     print_leftist(hb);
42 
43     // 将"左倾堆hb"合并到"左倾堆ha"中。
44     ha = merge_leftist(ha, hb);
45     printf("\n== 合并ha和hb后的详细信息: \n");
46     print_leftist(ha);
47 
48 
49     // 销毁左倾堆
50     destroy_leftist(ha);
51 }
View Code

 

左倾堆的C测试程序

左倾堆的测试程序已经包含在它的实现文件(leftist_test.c)中了,这里仅给出它的运行结果:

== 左倾堆(ha)中依次添加: 10 40 24 30 36 20 12 16 
== 左倾堆(ha)的详细信息: 
10(2) is root
24(1) is 10's   left child
30(0) is 24's   left child
36(0) is 24's  right child
12(1) is 10's  right child
20(0) is 12's   left child
40(0) is 20's   left child
16(0) is 12's  right child

== 左倾堆(hb)中依次添加: 17 13 11 15 19 21 23 
== 左倾堆(hb)的详细信息: 
11(2) is root
15(1) is 11's   left child
19(0) is 15's   left child
21(0) is 15's  right child
13(1) is 11's  right child
17(0) is 13's   left child
23(0) is 13's  right child

== 合并ha和hb后的详细信息: 
10(2) is root
11(2) is 10's   left child
15(1) is 11's   left child
19(0) is 15's   left child
21(0) is 15's  right child
12(1) is 11's  right child
13(1) is 12's   left child
17(0) is 13's   left child
16(0) is 13's  right child
23(0) is 16's   left child
20(0) is 12's  right child
40(0) is 20's   left child
24(1) is 10's  right child
30(0) is 24's   left child
36(0) is 24's  right child

 

posted on 2014-04-08 08:40  如果天空不死  阅读(8288)  评论(2编辑  收藏  举报