# 左式堆合并的实现

这是第一篇博客，相信是个不错的开始！

//define the leftist heap struct
typedef struct Leftist pLeftist;
struct Leftist{

int element;
pLeftist left, right;
int npl;
};

//build the merged leftist heap and return it
pLeftist BuildHeap(LeftistQueue q){

Leftist h1, h2;

while(!IsEmpty(q)){
h1 = Dequeue(q);
if(IsEmpty(q))
return h1;		//the leftist heap has been built
h2 = Dequeue(q);

Enqueue(q, Merge(h1, h2));	//enqueue the new merged leftist heap
}

return NULL;	//there is no leftist heap in queue
}

pLeftist Merge(Leftist h1, Leftist h2){

if(!h1)
return h2;
if(!h2)
return h1;

if(h1->element<h2->element)		//compare the element and choose the root
return MergeHeap(h1, h2);
else
return MergeHeap(h2, h1);
}

pLeftist MergeHeap(Leftist h1, Leftist h2){

if(!h1->left){
h1->left = h2;		//single node
}else{
h1->right = Merge(h1->right, h2);
if(h1->left->npl<h1->right->npl)	//make left child's npl >= right's
SwapChl(h1);
h1->npl = h1->right->npl+1;		//make the npl equalling its right child+1
}

return h1;	//return the root
}

1.合并速度快，O(n)

2.链表比数组带来更多的开销，并且多一个域(npl)

3.代码相对复杂，其实也不复杂

#### 优先队列中左式堆的合并算法

2016-08-08 21:51:30

#### 左式堆 斜堆

2012-12-12 21:15:20

#### 优先队列基础知识（三）---左式堆

2015-05-08 17:17:40

#### 左式堆的实现与详解

2015-01-15 12:22:04

#### 左式堆

2016-05-23 19:07:57

#### 递归结合左式堆的一个具体说明！

2017-11-12 18:00:29

#### 左式堆的合并

2017-03-10 11:33:54

#### 左式堆及相关操作

2018-06-20 20:20:49

#### 可并堆——左偏树 Leftist Heap

2014-03-24 11:23:14

#### 二叉堆，d堆，左式堆

2014-01-21 09:32:53