当前位置:网站首页>斜堆、、、
斜堆、、、
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); // 返回左右子树合并后的新树
}边栏推荐
猜你喜欢

文件查询匹配神器 【glob.js】 实用教程

云原生DevOps环境搭建

华为无线设备配置双链路冷备份(AP指定配置方式)

研发团队数字化转型实践

The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)

How do programmers solve online problems gracefully?

软技能之UML图

请问什么是 CICD

隔离和降级

y84. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (15)
随机推荐
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解
qt-faststart 安装使用
问题解决方式了
excel change cell size
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
excel remove all carriage return from a cell
Access the selected node in the console
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
Calculate the distance between two points
drf生成序列化类代码
visual studio code multiple editing
[C language advanced] file operation (2)
IDEA入门看这一篇就够了
CF1703G Good Key, Bad Key
数据机构---第五章树与二叉树---二叉树的概念---应用题
计算两点之间的距离
npm npm
文件查询匹配神器 【glob.js】 实用教程
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)