当前位置:网站首页>斜堆、、、
斜堆、、、
2022-08-01 23:21:00 【Wanderer001】
斜堆的介绍
斜堆(Skew heap)也叫自适应堆(self-adjusting heap),它是左倾堆的一个变种。和左倾堆一样,它通常也用于实现优先队列。它的合并操作的时间复杂度也是O(log n)。
相比于左倾堆,斜堆的节点没有"零距离"这个属性。除此之外,它们斜堆的合并操作也不同。斜堆的合并操作算法如下:
(01) 如果一个空斜堆与一个非空斜堆合并,返回非空斜堆。
(02) 如果两个斜堆都非空,那么比较两个根节点,取较小堆的根节点为新的根节点。将"较小堆的根节点的右孩子"和"较大堆"进行合并。
(03) 合并后,交换新堆根节点的左孩子和右孩子。
第(03)步是斜堆和左倾堆的合并操作差别的关键所在,如果是左倾堆,则合并后要比较左右孩子的零距离大小,若右孩子的零距离 > 左孩子的零距离,则交换左右孩子;最后,在设置根的零距离。
很多都是和左倾堆很相似的!
#include<stdio.h>
#include<stdlib.h>
typedef int Type;
typedef struct _SkewNode{
Type key; // 关键字(键值)
struct _SkewNode *left; // 左孩子
struct _SkewNode *right; // 右孩子
}SkewNode, *SkewHeap;
/*
* 合并"斜堆x"和"斜堆y"
*
* 返回值:
* 合并得到的树的根节点
*/
SkewNode* merge_skewheap(SkewHeap x, SkewHeap y)
{
if(x == NULL)
return y;
if(y == NULL)
return x;
// 合并x和y时,将x作为合并后的树的根;
// 这里的操作是保证: x的key < y的key
if(x->key > y->key)
swap_skewheap_node(x, y);
// 将x的右孩子和y合并,
// 合并后直接交换x的左右孩子,而不需要像左倾堆一样考虑它们的npl。
SkewNode *tmp = merge_skewheap(x->right, y);
x->right = x->left;
x->left = tmp;
return x;
}
/*
* 合并"斜堆x"和"斜堆y"
*
* 返回值:
* 合并得到的树的根节点
*/
SkewNode* merge_skewheap(SkewHeap x, SkewHeap y)
{
if(x == NULL)
return y;
if(y == NULL)
return x;
// 合并x和y时,将x作为合并后的树的根;
// 这里的操作是保证: x的key < y的key
if(x->key > y->key)
swap_skewheap_node(x, y);
// 将x的右孩子和y合并,
// 合并后直接交换x的左右孩子,而不需要像左倾堆一样考虑它们的npl。
SkewNode *tmp = merge_skewheap(x->right, y);
x->right = x->left;
x->left = tmp;
return x;
}
/*
* 取出根节点
*
* 返回值:
* 取出根节点后的新树的根节点
*/
SkewNode* delete_skewheap(SkewHeap heap)
{
SkewNode *l = heap->left;
SkewNode *r = heap->right;
// 删除根节点
free(heap);
return merge_skewheap(l, r); // 返回左右子树合并后的新树
}
边栏推荐
- Interpretation of the paper (GSAT) "Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism"
- sys_kill系统调用
- From 0 to 100: Notes on the Development of Enrollment Registration Mini Programs
- 6134. Find the closest node to the given two nodes - force double hundred code
- IDEA common plugins
- Oracle 数据库设置为只读及读写
- 浅析多服务在分布式系统下多事务通信处理机制方案
- 数据机构---第五章树与二叉树---二叉树的概念---应用题
- 【好书推荐】第一本无人驾驶技术书
- Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
猜你喜欢
UML diagram of soft skills
[Camp Experience Post] 2022 Cybersecurity Summer Camp
Making a Simple 3D Renderer
【数据分析03】
6134. Find the closest node to the given two nodes - force double hundred code
论文理解【RL - Exp Replay】—— Experience Replay with Likelihood-free Importance Weights
研发团队数字化转型实践
请问什么是 CICD
D - Linear Probing- 并查集
C language - branch statement and loop statement
随机推荐
IDEA常用插件
ping no reply
B. Difference Array--Codeforces Round #808 (Div. 1)
ROS2初级知识(8):Launching启动多节点
Oracle database is set to read-only and read-write
计算两点之间的距离
如何使用pywinauto和pyautogui将动漫小姐姐链接请回家
论文理解【RL - Exp Replay】—— Experience Replay with Likelihood-free Importance Weights
Oracle 数据库设置为只读及读写
测试岗月薪5-9k,如何实现涨薪到25k?
Chapter 12 End-User Task As Shell Scripts
chrome复制一张图片的base64数据
云原生DevOps环境搭建
Calculate the distance between two points
C语言——分支语句和循环语句
E - Integer Sequence Fair
JS 数组去重(含简单数组去重、对象数组去重)
中职网络安全竞赛B7比赛部署流程
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练