优先队列——斜堆

【0】README

0.1)本文部分内容转自  http://www.cnblogs.com/skywang12345/p/3638493.html, 旨在理解 优先队列——斜堆 的基础知识
0.2) for original source code , please visit https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter6/p150_skew_heap


【1】优先队列——斜堆相关

1.0)斜堆定义: 斜堆是具有堆序性的二叉树, 与左式堆的差别在于没有零路径属性,故merge操作后,不需要考虑左右子堆的零路径大小,而是无条件交换左右子堆;(干货——斜堆定义)

1.1)定义:斜堆(Skew heap)也叫自适应堆(self-adjusting heap),它是左式堆的一个变种。和左式堆一样,它通常也用于实现优先队列。它的合并操作的时间复杂度也是O(log n)。
1.2)相比于左倾堆,斜堆的节点没有”零距离”这个属性: 除此之外,它们斜堆的合并操作也不同。
1.3)斜堆的合并操作算法如下:

  • case1) 如果一个空斜堆与一个非空斜堆合并,返回非空斜堆。
  • case2) 如果两个斜堆都非空,那么比较两个根节点,取较小堆的根节点为新的根节点。将”较小堆的根节点的右孩子”和”较大堆”进行合并。
  • case3) 合并后,交换新堆根节点的左孩子和右孩子。

Attention)

  • A1)第3步是斜堆和左式堆的合并操作差别的关键所在,如果是左式堆,则合并后要比较左右孩子的零距离大小,若右孩子的零距离 > 左孩子的零距离,则交换左右孩子;最后,在设置根的零距离。
  • A2) 下面是一个关于斜堆的干货荔枝:

【2】source code + printing results

2.1)source code at a glance

#include "skew_heap.h" // swap the left and the right in priority queue.
void swap(PriorityQueue h1)
{PriorityQueue temp;temp = h1->left;h1->left = h1->right;h1->right = temp;
}// analog print directories and files name in the BinaryTree, which involves postorder traversal. 
void printPreorder(int depth, TreeNode root)
{           int i;if(root) {      for(i = 0; i < depth; i++)printf("    ");     printf("%d\n", root->value);printPreorder(depth + 1, root->left); // Attention: there's difference between traversing binary tree and common tree.printPreorder(depth + 1, root->right);}else {for(i = 0; i < depth; i++)printf("    ");     printf("NULL\n");}
}// insert an element with value into the priority queue.
PriorityQueue insert(ElementType value, PriorityQueue pq)
{TreeNode node;          node = (TreeNode)malloc(sizeof(struct TreeNode));if(!node){Error("failed inserting, for out of space !");return pq;}node->left = NULL;node->right = NULL; node->value = value;    if(pq == NULL) // means that just only creating a node with value.{return node;}else{return merge(node, pq);     }
}// return the minimal between a and b.
int minimal(int a, int b)
{return a > b ? b : a;
}// merge the priority queue h1 and h2.
PriorityQueue merge(PriorityQueue h1, PriorityQueue h2)
{       if(h1 == NULL){return h2;}else if(h2 == NULL){return h1;}   if(h1->value > h2->value){return innerMerge(h2, h1);}else{return innerMerge(h1, h2);}   
}// merge the priority queue h1 and h2.
PriorityQueue innerMerge(PriorityQueue h1, PriorityQueue h2)
{ if(h1->left == NULL){h1->left = h2;}else{h1->right = merge(h1->right, h2);swap(h1);       }       return h1;
} int main()
{PriorityQueue h1;PriorityQueue h2;   int data[] =  {21, 10, 23, 14, 3, 26, 17, 8};   int data2[] = {18, 7, 37, 6, 24, 33, 12, 18};   int i;h1 = insert(data[0], NULL);for(i=1; i<8; i++){h1 = insert(data[i], h1);}printf("\n=== after the leftist heap h1 is merged===\n");printPreorder(1, h1);h2 = insert(data2[0], NULL);for(i=1; i<8; i++){h2 = insert(data2[i], h2);}printf("\n=== after the leftist heap h2 is merged===\n");printPreorder(1, h2);h1 = merge(h1, h2);printf("\n=== after both h1 and h2 are merged===\n");printPreorder(1, h1);  return  0;
}

2.2) printing results

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331585.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Class的getResource与ClassLoader的getResource路径/问题

一、Class的getResource(String path)&#xff1a;URL 1、path 不以’/开头时&#xff0c;默认是从此类所在的包下取资源&#xff1b; 2、path 以’/开头时&#xff0c;则是从ClassPath根下获取&#xff1b; System.out.println(Test.class.getResource("")); Syste…

java 轻量级文件数据库_Java:如何创建轻量级数据库微服务

java 轻量级文件数据库基于云的Java数据库应用程序的数量每分钟都在增加。 许多组织部署了数百甚至数千个微服务实例。 但是&#xff0c;相对于运行时环境&#xff0c;大多数应用程序会带来惊人的不必要开销。 反过来&#xff0c;这会使应用程序运行更慢&#xff0c;运行成本更…

html中显示shell脚本的输出,网页从shell脚本中输入并显示结果

首先&#xff0c;不是在BASH脚本中使用$USERNAME。 $USERNAME是一个包含当前用户名的BASH变量。实际上&#xff0c;在BASH中使用UPPERCASE变量通常是一个糟糕的主意。大多数BASH环境变量都是大写字母&#xff0c;可能会导致混淆。让你的变量小写是个好习惯。此外&#xff0c;因…

优先队列——二项队列(binominal queue)

【0】README 0.1&#xff09; 本文文字描述部分转自 数据结构与算法分析&#xff0c; 旨在理解 优先队列——二项队列&#xff08;binominal queue&#xff09; 的基础知识&#xff1b; 0.2&#xff09; 本文核心的剖析思路均为原创&#xff08;insert&#xff0c;merge和del…

Class的getName、getSimpleName与getCanonicalName的区别

一、getName 除了数组外&#xff0c;其他的类都是输出类全名以 String 的形式返回此 Class 对象所表示的实体&#xff08;类、接口、数组类、基本类型或 void&#xff09;名称。 1、此类对象表示的是非数组类型的引用类型&#xff0c; 返回该类的二进制名称&#xff0c;Java…

apache.camel_Apache Camel 3.1 –即将推出更多骆驼核心优化

apache.camel希望一切都很好&#xff0c;您可以安全进入2020年。 Camel团队已经在忙于开发下一个Camel 3.1版本。 目标之一是继续优化骆驼核心&#xff0c;这一次我们花了一些时间来寻找路由引擎中的一些热点。 我们所研究的方面之一也是在Camel路由的每个消息中发生的对象分…

xp系统的计算机管理中用户在哪里,WINDOWSXP的用户管理和系统安全设置

台计算机)⑤回到“添加独立管理单元”对话框&#xff0c;单击“关闭”&#xff0c;回到““控制台->添加/删除管理单元”对话框&#xff0c;再单击“确定”&#xff1b;⑥此时&#xff0c;在控制台窗口左窗格中看到新添加的控制单元“本地计算机策略”&#xff1b;⑦依次展开…

XML——StAX Streaming API for XML(read+write)

【0】README 0.1&#xff09; reshipping from http://www.journaldev.com/1191/how-to-read-xml-file-in-java-using-java-stax-api http://www.journaldev.com/892/how-to-write-xml-file-in-java-using-java-stax-api 0.2&#xff09; for all source code , please visi…

Class的getInterfaces与getGenericInterface区别

一、getInterfaces 返回直接实现的接口&#xff08; 由于编译擦除&#xff0c;没有显示泛型参数&#xff09; Class<?>[] getInterfaces() 确定此对象所表示的类或接口实现的接口。 确定此对象所表示的类或接口实现的接口。 如果此对象表示一个类&am…

maven配置junit5_JUnit 5和Selenium –改善项目配置

maven配置junit5Selenium是一组支持浏览器自动化的工具和库&#xff0c;主要用于Web应用程序测试。 Selenium的组件之一是Selenium WebDriver&#xff0c;它提供客户端库&#xff0c;JSON有线协议&#xff08;与浏览器驱动程序进行通信的协议&#xff09;和浏览器驱动程序。 Se…

形容计算机网络教室的成语,形容教育的成语

形容教育的成语形容教育的成语【不教而杀】 【弦歌之声】 【化及冥顽】 【蒙以养正】【不言之教】 【沂水春风】 【嘉言善状】 【神道设教】【不教之教】 【相夫教子】 【画荻教子】 【磨昏抉聩】【东风化雨】 【因材施教】 【教无常师】 【脱骨换胎】…

Class的 getSuperclass与getGenericSuperclass区别

Class的getInterfaces与getGenericInterface区别 http://www.cnblogs.com/maokun/p/6773076.html一、getSuperclass 返回直接继承的父类&#xff08;由于编译擦除&#xff0c;没有显示泛型参数&#xff09; Class<? super T>getSuperclass() 返回表示此 Cla…

XML——XSLT的一个简单荔枝

【0】intro to XSLT&#xff08;转自&#xff1a;http://www.w3school.com.cn/xsl/xsl_languages.asp&#xff09;0.1&#xff09;起始于 XSL XSL 指扩展样式表语言&#xff08;EXtensible Stylesheet Language&#xff09;。 万维网联盟 (W3C) 开始发展 XSL 的原因是&#xff…

Eclipse系列的隐藏宝藏– 2019年版

Eclipse Collections是一个开放源代码Java Collections框架。 在此博客中&#xff0c;我将演示该框架的五个鲜为人知的功能。 我在去年的Java Advent Calendar中发布了一个类似的博客 。 请参阅博客末尾的资源以获取有关该框架的更多信息。 1. countBy() &#xff1a;当您要查…

css html 方格,使用CSS创建方格背景

这里是一个什么样的格仔背景看起来在图形设计编辑器&#xff0c;如Photoshop或Illustrator的翻版。 (所有的CSS).checkered{height: 240px;background: -webkit-linear-gradient(45deg, rgba(0, 0, 0, 0.0980392) 25%, transparent 25%, transparent 75%, rgba(0, 0, 0, 0.0980…

java嵌套类与内部类

一、嵌套类(Nested Classes) 使用嵌套类减少了命名冲突&#xff0c;一个内部类可以定义在一个类中&#xff0c;一个方法中甚至一个表达式中。 (1)定义 A nested(嵌套) class is any class whose declaration occurs within the body of another class or interface. A top lev…

jsr303 spring_使用Spring和JSR 303进行方法参数验证

jsr303 springSpring提供了一种使用JSR 303 bean验证来验证方法参数的简便方法。 在这篇文章中&#xff0c;我们将看到如何使用此功能。 建立 首先&#xff0c;我们需要通过创建MethodValidationPostProcessor bean添加对方法参数验证的支持&#xff1a; Configuration publi…

优先队列——斐波那契堆(without source code)

【0】README 0.1&#xff09; 本文部分内容转自 数据结构与算法分析&#xff0c;旨在理解 斐波那契堆 的基础知识&#xff1b; 0.2&#xff09; 文本旨在理清 斐波那契堆的 核心idea&#xff0c;还没有写出源代码实现&#xff0c;表遗憾&#xff1b; 0.3&#xff09;从实际角…

计算机专业考研可关注哪些公众号,考研应关注哪些公众号?

考研应关注哪些公众号&#xff1f;2018-11-30深夜睡不着&#xff0c;看到了这个问题&#xff0c;觉得我能帮上忙&#xff0c;就坐起来写了一份回答(^_^)微信公众号有&#xff1a;考研狗之家(特别推荐&#xff0c;各种资源)考研军火库(特别推荐&#xff0c;各种技巧)考研圈考研资…

cassandra 备份_使用sstableloader恢复Cassandra Priam备份

cassandra 备份我之前曾写过关于设置Cassandra和Priam进行备份和集群管理的文章。 但是&#xff0c;我在此处提供的用于备份还原的示例并不适用于所有情况&#xff0c;例如&#xff0c;它可能不适用于完全独立的群集。 或者在部分还原到一个表而不是整个数据库的情况下。 在这…