版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.Binomial heapIn computer science, a binomial heap is a heap similar to a binary heap but also supports quick merging of two heaps. This is achieved by using a special tree structure. It is important as an implementation of the mergeable heap abstract data type(also called meldable heap), which is a
2、 priority queue supporting merge operation.Binomial tree A binomial heap is implemented as a collection of binomial trees (compare with a binary heap, which has a shape of a single binary tree). A binomial tree is defined recursively:· A binomial tree of order 0 is a single node· A binomia
3、l tree of order k has a root node whose children are roots of binomial trees of orders k1, k2, ., 2, 1, 0 (in this order).Binomial trees of order 0 to 3: Each tree has a root node with subtrees of all lower ordered binomial trees, which have been highlighted. For example, the order 3 binomial tree i
4、s connected to an order 2, 1, and 0 (highlighted as blue, green and red respectively) binomial tree.A binomial tree of order k has 2k nodes, height k.Because of its unique structure, a binomial tree of order k can be constructed from two trees of order k1 trivially by attaching one of them as the le
5、ftmost child of root of the other one. This feature is central to the merge operation of a binomial heap, which is its major advantage over other conventional heaps.The name comes from the shape: a binomial tree of order has nodes at depth . Structure of a binomial heap A binomial heap is implemente
6、d as a set of binomial trees that satisfy the binomial heap properties:· Each binomial tree in a heap obeys the minimum-heap property: the key of a node is greater than or equal to the key of its parent.· There can only be either one or zero binomial trees for each order, including zero or
7、der.The first property ensures that the root of each binomial tree contains the smallest key in the tree, which applies to the entire heap.The second property implies that a binomial heap with n nodes consists of at most logn + 1 binomial trees. In fact, the number and orders of these trees are uniq
8、uely determined by the number of nodes n: each binomial tree corresponds to one digit in the binary representation of number n. For example number 13 is 1101 in binary, , and thus a binomial heap with 13 nodes will consist of three binomial trees of orders 3, 2, and 0 (see figure below).Example of a
9、 binomial heap containing 13 nodes with distinct keys.The heap consists of three binomial trees with orders 0, 2, and 3.Implementation Because no operation requires random access to the root nodes of the binomial trees, the roots of the binomial trees can be stored in a linked list, ordered by incre
10、asing order of the tree.Merge As mentioned above, the simplest and most important operation is the merging of two binomial trees of the same order within two binomial heaps. Due to the structure of binomial trees, they can be merged trivially. As their root node is the smallest element within the tr
11、ee, by comparing the two keys, the smaller of them is the minimum key, and becomes the new root node. Then the other tree become a subtree of the combined tree. This operation is basic to the complete merging of two binomial heaps.function mergeTree(p, q) if p.root.key <= q.root.key return p.addS
12、ubTree(q) else return q.addSubTree(p)To merge two binomial trees of the same order, first compare the root key. Since 7>3, the black tree on the left(with root node 7) is attached to the grey tree on the right(with root node 3) as a subtree. The result is a tree of order 3.The operation of mergin
13、g two heaps is perhaps the most interesting and can be used as a subroutine in most other operations. The lists of roots of both heaps are traversed simultaneously, similarly as in the merge algorithmIf only one of the heaps contains a tree of order j, this tree is moved to the merged heap. If both
14、heaps contain a tree of order j, the two trees are merged to one tree of order j+1 so that the minimum-heap property is satisfied. Note that it may later be necessary to merge this tree with some other tree of order j+1 present in one of the heaps. In the course of the algorithm, we need to examine
15、at most three trees of any order (two from the two heaps we merge and one composed of two smaller trees).Because each binomial tree in a binomial heap corresponds to a bit in the binary representation of its size, there is an analogy between the merging of two heaps and the binary addition of the si
16、zes of the two heaps, from right-to-left. Whenever a carry occurs during addition, this corresponds to a merging of two binomial trees during the merge.Each tree has order at most log n and therefore the running time is O(log n).function merge(p, q) while not( p.end() and q.end() ) tree = mergeTree(
17、p.currentTree(), q.currentTree() if not heap.currentTree().empty() tree = mergeTree(tree, heap.currentTree() heap.addTree(tree) else heap.addTree(tree) heap.next() p.next() q.next()This shows the merger of two binomial heaps. This is accomplished by merging two binomial trees of the same order one b
18、y one. If the resulting merged tree has the same order as one binomial tree in one of the two heaps, then those two are merged again.Insert Inserting a new element to a heap can be done by simply creating a new heap containing only this element and then merging it with the original heap. Due to the
19、merge, insert takes O(log n) time, however it has an amortized time of O(1) (i.e. constant).Find minimum To find the minimum element of the heap, find the minimum among the roots of the binomial trees. This can again be done easily in O(log n) time, as there are just O(log n) trees and hence roots t
20、o examine.By using a pointer to the binomial tree that contains the minimum element, the time for this operation can be reduced to O(1). The pointer must be updated when performing any operation other than Find minimum. This can be done in O(log n) without raising the running time of any operation.D
21、elete minimum To delete the minimum element from the heap, first find this element, remove it from its binomial tree, and obtain a list of its subtrees. Then transform this list of subtrees into a separate binomial heap by reordering them from smallest to largest order. Then merge this heap with the
22、 original heap. Since each tree has at most log n children, creating this new heap is O(log n). Merging heaps is O(log n), so the entire delete minimum operation is O(log n).function deleteMin(heap) min = heap.trees().first() for each current in heap.trees() if current.root < min then min = curre
23、nt for each tree in min.subTrees() tmp.addTree(tree) heap.removeTree(min) merge(heap, tmp)Decrease key After decreasing the key of an element, it may become smaller than the key of its parent, violating the minimum-heap property. If this is the case, exchange the element with its parent, and possibl
24、y also with its grandparent, and so on, until the minimum-heap property is no longer violated. Each binomial tree has height at most log n, so this takes O(log n) time.Delete To delete an element from the heap, decrease its key to negative infinity (that is, some value lower than any element in the
25、heap) and then delete the minimum in the heap.Performance All of the following operations work in O(log n) time on a binomial heap with n elements:· Insert a new element to the heap· Find the element with minimum key· Delete the element with minimum key from the heap· Decrease ke
26、y of a given element· Delete given element from the heap· Merge two given heaps to one heapFinding the element with minimum key can also be done in O(1) by using an additional pointer to the minimum.二项堆在计算机科学中,二项堆是一个二叉堆类似的堆结构,但是支持两个二项堆快速合并。这是通过使用一个特殊的树结构。实现可合并堆的抽象数据类型(也称为meldable堆)是重要的,这个抽
27、象数据结构就是支持合并操作的优先级队列。二项树二项堆是二项树的集合(与二项堆相比二叉堆则只有一颗二项树)。二项式树的递归定义: (1)0阶二项树是一个单一的节点 (2)k阶二项树有根订单K-1,K-2,.,2,1,0(这个顺序)二叉树根节点的孩子。 上图是0阶到3阶的二项树:每一棵树都有一个根节。连接在根节点上的是所有低阶的子树,这在图中被高亮显示。例如,3阶二叉树连接到2阶,1和0(蓝色,绿色和红色高亮显示)二叉树。k阶二项树高度为K,共有2k个节点。由于其独特的结构,k阶的二叉树可以由两棵k-1阶的二叉树构成,通过将其中一棵二叉树的根节点作为另外一棵二叉树的最左子节点。这个特点对
28、二叉堆的合并操作来说至关重要,这也是二项堆相对于传统堆拥有的主要优势。二项树的名字来源于形状:n阶二叉树在深度n处共有个节点。二项堆结构二项式堆实现为一组满足堆性质的二项树集合:(1)在堆的每二项式服从的最小堆属性:一个节点的关键是大于或等于它的父键。 (2)任意阶的二项树只能有1个或者0个。包括0阶二叉树。第一属性确保每个二叉树的根包含树中最小的键,它适用于整个堆中的二叉树。第二个属确保n个节点的二项堆最多包含lgn + 1棵二叉树。事实上,二叉树的数目和阶数是由二叉堆的节点个数决定的:每颗二项树和n的二进制表示中的一位对应。例如13的二进制表示是1101,因此包含13个节点的二项堆包括三棵
29、二叉树 ,阶数分别为3,2和0(见下图)。实现因为没有操作需要随机访问二叉树的根节点,因此二叉树的根节点可以按照对应阶数的增序存储在一个链表中。合并正如上面所提到的,最简单的和最重要的操作是在两个二叉堆中合并两个阶数相同的二叉树。由于二叉树的结构,它们能够被轻易的合并。由于二叉树的根节点具有最小关键字,通过比较两个根节点的关键字大小,其中较小的成为最小关键字,并成为新的根节点。另外一棵树成为合并后的树的一颗子树。这个操作对于合并两个二项堆而言是基础的。function mergeTree(p, q) if p.root.key <= q.root.key return p.addSubT
30、ree(q) else return q.addSubTree(p) 对应上图,为了合并两棵阶数相同的二项树,首先比较根节点的关键字。因为7> 3,在左边的黑色树(根节点7)连接到灰树(与根结点3)右边的子树。结果是棵3阶树。合并两个堆的操作也许是最有趣的,可以用来作为在大多数其他操作的子程序。和合并算法相似的是两个二项堆的根节点链表同时被遍历。如果只有一个堆包含j阶的树,这棵树被移动到合并后的堆。如果两个堆都包含j阶的树,两棵树被合并成一颗满足最小堆性质的阶数为j+1的二叉树。注意这棵树以后也可能要和某个堆中j+1阶的二叉树合并。在算法的过程中,我们需要研究最三棵树的任何顺序
31、的情况。因为二项堆中的每棵二项树与表示二项堆大小的二进制表示中的一位对应,合并两个二项堆与两个二进制数相加是相似的。由右至左,每当一个进位产生时都意味着两棵二项树的合并。每棵树的阶数不超过logn,因此运行时间是O(log n)的。 function merge(p, q) while not( p.end() and q.end() ) tree = mergeTree(p.currentTree(), q.currentTree() if not heap.currentTree().empty() tree = mergeTree(tree, heap.currentTree() heap.addTree(tree) else heap.addTree(tree) he
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 浅析弱电安防系统综合防雷解决方案考核试卷
- 石棉在流体控制中的应用考核试卷
- 煤炭加工工艺对低质煤利用的影响考核试卷
- DB11T 589-2010 保健按摩操作规范
- 森林小屋课件教学课件
- 员工知识产权培训总结报告
- 员工技能培训道场方案
- 英语培训课件教学课件
- 淮阴工学院《沥青与沥青混合材料1》2022-2023学年第一学期期末试卷
- ATM机相关行业投资方案范本
- 安全教育记录范文(25篇)
- 2024年供应链管理竞赛考试题库
- 三年级语文下册第二单元群文阅读教学设计
- 习思想教材配套练习题 第七章 社会主义现代化建设的教育、科技、人才战略
- led显示屏工艺流程
- 建设项目设计管理方案
- 第13课《警惕可怕的狂犬病》 课件
- 仓库货物条码管理培训
- 第六章-中国早期社会学中的社区学派-《中国社会学史》必备
- 水产品质量安全知识讲座
- 技术协议范本通用模板
评论
0/150
提交评论