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

【0】README

0.1) 本文文字描述部分转自 数据结构与算法分析, 旨在理解 优先队列——二项队列(binominal queue) 的基础知识;
0.2) 本文核心的剖析思路均为原创(insert,merge和deleteMin的操作步骤图片示例), 源代码均为原创
0.3) for original source code, please visit https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter6/p152_binominal_queue


【1】二项队列相关

1.0)Attention: 二项队列中不允许有高度相同的二项树存在该队列中;
1.1)problem+solution:

  • 1.1.1)problem:虽然左式堆和斜堆每次操作花费O(logN)时间, 这有效地支持了合并, 插入和deleteMin, 但还是有改进的余地,因为我们知道, 二叉堆以每次操作花费常数平均时间支持插入。
  • 1.1.2)solution: 二项队列支持所有这三种操作(merge + insert + deleteMin), 每次操作的最坏情形运行时间为O(logN), 而插入操作平均花费常数时间; (干货——优先队列的三种基本操作——merge + insert + deleteMin)

1.2)相关定义

  • 1.2.1) 二项队列定义: 二项队列不同于我们看到的所有优先队列的实现之处在于, 一个二项队列不是一颗堆序的树, 而是堆序树的集合,称为森林;(干货——二项队列的定义和构成,二项队列是二项树的集合,而二项树是一颗堆序树)
  • 1.2.2)二项树定义: 堆序树中的每一颗都是有约束的形式。 (干货——二项树的定义)
  • 1.2.3)二项树的构成:每一个高度上至多存在一颗二项树, 高度为0的二项树是一颗单节点树; 高度为k 的二项树Bk 通过将一颗二项树 Bk-1 附接到另一颗二项树Bk-1 的根上而构成;(干货——二项树的构成)
    这里写图片描述

对上图的分析(Analysis):

  • A1)二项树的性质:

    • A1.1)从图中看到, 二项树Bk 由一个带有儿子B0, B1, …, Bk-1的根组成;
    • A1.2)高度为k 的二项树恰好有2^k 个节点;
    • A1.3) 而在深度d 的节点数是 二项系数 。
  • A2)如果我们把堆序添加到二项树上, 并允许任意高度上最多有一颗二项树,那么我们能够用二项树的集合唯一地表示任意大小的优先队列;


【2】二项队列操作(merge + insert + deleteMin)

2.1)合并操作(merge) (干货——合并操作的第一步就是查看是否有高度相同的二项树,如果有的话将它们merge)

  • step1) H1 没有高度为0的二项树而H2有,所以将H2中高度为0的二项树直接作为H3的一部分;(直接的意思==中间不需要merge);
  • step2) H1 和 H2 中都有高度为1的二项树,将它们进行merge, 得到高度为2的二项树(根为12);
  • step3)现在存在三颗高度为2的二项树(根分别为12, 14, 23),将其中两个进行merge(如merge根为12 和 根为14 的二项树),得到高度为3的二项树;
  • step4)所以,最后,我们得到二项队列, 其集合包括:高度为0的二项树(根为13), 高度为1的二项树(根为23),高度为3的二项树(高度为12);

Attention)

  • A1)显然,merge操作是按照高度升序依次进行的;
  • A2)最后得到的二项队列不存在高度相同的二项树,即使存在,也要将高度相同的二项树进行merge;
  • A3)二项队里中的二项树的高度不必囊括所有的升序实数,即不必一定是0, 1, 2, 3,4 等等; 也可以是0, 1, 3 等;
  • A4)单节点树的高度为0; (干货——树高度从零起跳)
    这里写图片描述

2.2)插入操作(insert) (干货——insert操作是merge操作的特例,而merge操作的第一步就是查看是否有高度相同的二项树,如果有的话将它们merge)

  • 2.2.1)插入操作实际上: 就是特殊情形的合并, 我们只需要创建一颗单节点树并执行一次merge;
  • 2.2.2)更准确地说: 如果元素将要插入的那个优先队列中不存在的最小的二项树是Bi, 那么运行时间与 i + 1 成正比;
    这里写图片描述

对上图的分析(Analysis):

  • A1) 4 插入之后,与B0(根为3)进行merge, 得到一颗高度为1的树B1’(根为3);
  • A2)将B1’ 与 B1(根为1) 进行merge 得到高度为2 的树B2’(根为1), 它是新的优先队列;
  • A3)在插入7之后的下一次插入又是一个坏情形, 因为需要三次merge操作;

2.3)删除最小值操作(deleteMin)

  • step1)找出一颗具有最小根的二项树来完成, 令该树为Bk, 令原始序列为H;
  • step2)从H中除去Bk, 形成新的二项队列H’;
  • step3)再除去Bk的根, 得到一些二项树B0, B1, …, Bk-1, 它们共同形成优先队列H”;
  • step4) 合并H’ 和 H” , 操作结束;

【3】 source code and printing results

3.1)source code at a glance
Attention)二项队列的实现源代码用到了 儿子兄弟表示法

#include "binominal_queue.h" #define MINIMAL 10000int minimal(BinominalQueue bq)
{int capacity;int i;int minimal;int miniIndex;  minimal = MINIMAL;capacity = bq->capacity;for(i=0; i<capacity; i++){if(bq->trees[i] && bq->trees[i]->value < minimal){minimal = bq->trees[i]->value;miniIndex = i;}}return miniIndex;
}// initialize the BinominalQueue with given capacity.
BinominalQueue init(int capacity)
{BinominalQueue queue;           BinominalTree* trees; int i;queue = (BinominalQueue)malloc(sizeof(struct BinominalQueue));if(!queue){Error("failed init, for out of space !");return queue;}   queue->capacity = capacity;trees = (BinominalTree*)malloc(capacity * sizeof(BinominalTree));if(!trees){Error("failed init, for out of space !");return NULL;}   queue->trees = trees;for(i=0; i<capacity; i++){queue->trees[i] = NULL;}return queue;
}  // attention: the root must be the left child of the binominal tree.
int getHeight(BinominalTree root)
{int height;     if(root == NULL){       return 0;       }height = 1; while(root->nextSibling){height++;root = root->nextSibling;}return height;
}// merge BinominalQueue bq2 into bq1.
void outerMerge(BinominalQueue bq1, BinominalQueue bq2)
{int height;int i;for(i=0; i<bq2->capacity; i++){height = -1;if(bq2->trees[i]){height = getHeight(bq2->trees[i]->leftChild);   // attention for the line above// height = height(bq2->trees[i]->leftChild); not height = height(bq2->trees[i]);merge(bq2->trees[i], height, bq1);}                   }       
}// merge tree h1 and h2 = bq->trees[height], 
// who represents the new tree and old one respectively.
BinominalTree merge(BinominalTree h1, int height, BinominalQueue bq)
{           if(h1 == NULL){return h1;}if(bq->trees[height] == NULL) // if the queue don't has the B0 tree.{       bq->trees[height] = h1;return bq->trees[height];}else // otherwise, compare the new tree's height with that of old one.{        if(h1->value > bq->trees[height]->value) // the new should be treated as the parent of the old.{       innerMerge(bq->trees[height], height, h1, bq);}else // the old should be treated as the parent of the new.{innerMerge(h1, height, bq->trees[height], bq);}}  return h1;
} BinominalTree lastChild(BinominalTree root)
{               while(root->nextSibling){       root = root->nextSibling;}return root; 
}// merge tree h1 and h2 = bq->trees[height], 
// who represents the new tree and old one respectively.
BinominalTree innerMerge(BinominalTree h1, int height, BinominalTree h2, BinominalQueue bq)
{if(h1->leftChild == NULL){h1->leftChild = h2;}else{lastChild(h1->leftChild)->nextSibling = h2;// attention for the line above// lastChild(h1->leftChild)->nextSibling = h2 not lastChild(h1)->nextSibling = h2}height++;bq->trees[height-1] = NULL;merge(h1, height, bq);  return h1;
} // insert an element with value into the priority queue.
void insert(ElementType value, BinominalQueue bq)
{TreeNode node;node = (TreeNode)malloc(sizeof(struct TreeNode));if(!node){Error("failed inserting, for out of space !");return ;}node->leftChild= NULL;node->nextSibling = NULL;   node->value = value;    merge(node, 0, bq);         
}// analog print node values in the binominal tree, which involves preorder traversal. 
void printPreorderChildSibling(int depth, BinominalTree root)
{           int i;if(root) {      for(i = 0; i < depth; i++)printf("    ");printf("%d\n", root->value);            printPreorderChildSibling(depth + 1, root->leftChild);                                  printPreorderChildSibling(depth, root->nextSibling);} else{for(i = 0; i < depth; i++)printf("    ");printf("NULL\n");}
}// print Binominal Queue bq
void printBinominalQueue(BinominalQueue bq)
{int i;for(i=0; i<bq->capacity; i++){printf("bq[%d] = \n", i);printPreorderChildSibling(1, bq->trees[i]);}   
}void deleteMin(BinominalQueue bq)
{int i;  BinominalTree minitree; BinominalTree sibling;i = minimal(bq);minitree = bq->trees[i]->leftChild; //minitree->value=51free(bq->trees[i]);bq->trees[i] = NULL;            while(minitree){sibling = minitree->nextSibling;minitree->nextSibling = NULL;merge(minitree, getHeight(minitree->leftChild), bq);        minitree = sibling;}       
}

3.2) printing results

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

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

相关文章

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;它可能不适用于完全独立的群集。 或者在部分还原到一个表而不是整个数据库的情况下。 在这…

XML——XML概述

【0】README 0.1&#xff09;本文描述 转自 core java volume 2&#xff0c; 旨在理解 XML——XML概述 的基础知识&#xff1b; 【1】XML概述相关 1&#xff09;problemsolution &#xff08;干货——引入XML的原因&#xff09; 1.1&#xff09;problem&#xff1a;1.1.1&am…

java入门配置

一、下载安装JDK JDK下载路径&#xff1a;http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 点击 Accept License Agreement &#xff0c;选择自己的系统类型JDK下载&#xff0c;然后安装JDK。 二、安装JDK 双击JDK运行&#xff0c;选择…

华中科技大学计算机学院发表论文规定,华中科技大学等7所大学取消“研究生毕业必须发表论文”要求...

华中科技大学等7所大学取消“研究生毕业必须发表论文”要求北京晨报发表于 2006/07/17 10:51中国7所大学取消“研究生毕业必须发表论文”要求中新网7月16日电据《北京晨报》报道&#xff0c;昨天&#xff0c;记者从大学校长论坛了解到&#xff0c;中国人民大学、北京师范大学、…

计算机网络中的广播啥意思,开启无线广播啥意思

大家好&#xff0c;我是时间财富网智能客服时间君&#xff0c;上述问题将由我为大家进行解答。以路由器为例&#xff0c;开启无线广播是指开启无线路由器功能。路由器(Router&#xff0c;又称路径器)是一种计算机网络设备&#xff0c;它能将数据包通过一个个网络传送至目的地(选…