Leftist Heap and Skew Heap

本文介绍了左偏堆的基本概念及性质,包括其定义、定理,并详细解释了合并操作的过程。此外,还探讨了斜堆作为左偏堆简化版的特点,强调了其在连续操作中的时间复杂度优势。

Leftist Heap

【Definition】The leftist heap property is that for every node X in the heap, the null path length of the left child is at least as large as that of the right child.(Npl(NULL)==-1)

【Theorem】A leftist tree with r nodes on the right path must have at least 2^r  – 1 nodes.So the length of right path is about logN 

Merging(O(logN)):sort the node in right path of two leftist heaps,and swap the left subtree and right subtree if necessary.

As for build a leftist tree:store the leftist tree in a queue(originally N one-node trees).Get the first two leftist trees, and merge them.Repeat the operation untill there is one leftist tree left.O(N) for doing it.(the worst case is O(N),when N=2^k)

struct TreeNode 
{ 
	ElementType Element;
	PriorityQueue Left;
	PriorityQueue Right;
	int Npl;
} ;
PriorityQueue  Merge ( PriorityQueue H1, PriorityQueue H2 )
{ 
	if ( H1 == NULL )   return H2;	
	if ( H2 == NULL )   return H1;	
	if ( H1->Element < H2->Element )  return Merge1( H1, H2 );
	else return Merge1( H2, H1 );
}
static PriorityQueue Merge1( PriorityQueue H1, PriorityQueue H2 )
{ 
	if ( H1->Left == NULL ) 	/* single node */
		H1->Left = H2;	/* H1->Right is already NULL 
				    and H1->Npl is already 0 */
	else {
		H1->Right = Merge( H1->Right, H2 );     /* Step 1 & 2 */
		if ( H1->Left->Npl < H1->Right->Npl )
			SwapChildren( H1 );	/* Step 3 */
		H1->Npl = H1->Right->Npl + 1;
	} /* end else */
	return H1;
}

Skew Heap

——a simple version of the leftist heaps

Any M consecutive operations take at most O(M log N) time.

ALWAYS SWAP CHILDREN

Di =  the resulting tree

ф( Di ) =number of heavy nodes

Definition】A node p is heavy if the number of descendants of p’s right subtree is at least half of the number of descendants of p, and light otherwise.  Note that the number of descendants of a node includes the node itself.

Hi : li + hi  ( i = 1, 2 )(along the right path)

T=l1+l2+h1+h2

ф( Di -1)=h+h1+h2    ф( Di ) =h+l1+l2(consider the worst case that every light node on the right path becomes heavy node)

T amortize=T+ф( Di ) -ф( Di -1)=2(l1+l2)  which means O(logN) 

### 介绍 左偏堆(Leftist Heap)是一种特殊的堆结构,是可合并堆的一种实现方式,它支持高效的合并操作,可用于需要频繁合并堆的场景,是一种自调整的堆结构 [^2]。 ### 原理 左偏堆每个节点除了包含关键字和指向左右子节点的指针外,还包含一个零路径长度(Null Path Length,NPL)的信息。零路径长度指从该节点到一个没有两个孩子的节点的最短路径长度。左偏堆满足左偏性质,即每个节点的左子节点的零路径长度不小于右子节点的零路径长度。这一性质保证了堆的不平衡性,但能在合并操作时提供较好的性能。 ### 应用 左偏堆的常见应用场景与需要频繁合并堆的任务相关,在实现优先队列的并行算法中表现出较高的效率。同时,它也可以用于处理需要按优先级处理的任务,是优先队列的一种高级实现方式 [^2][^4]。 ### 实现 以下是左偏堆在C语言中的实现示例,涵盖了左偏堆节点的定义以及一些基本操作的声明: ```c #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 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值