`
oywl2008
  • 浏览: 1053276 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

binary heap

 
阅读更多

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: If A is a parentnode of B then the key of node A is ordered with respect to the key of node B with the same ordering applying across the heap. Either the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node (this kind of heap is called max heap) or the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node (min heap). Heaps are crucial in several efficient graph algorithms such as Dijkstra's algorithm, and in the sorting algorithm heapsort. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree (see figure).

<iframe id="iframe_0.9335351248770227" style="box-sizing: border-box; border-width: initial; border-style: none; width: 240px; height: 178px;" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Max-Heap.svg/240px-Max-Heap.svg.png?_=3773073%22%20style=%22border:none;max-width:880px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9335351248770227',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>

 

Heaps are usually implemented in an array, and do not require pointers between elements.

Full and almost full binary heaps may be represented in a very space-efficient way using an array alone. The first (or last) element will contain the root. The next two elements of the array contain its children. The next four contain the four children of the two child nodes, etc. Thus the children of the node at position n would be at positions 2n and 2n+1 in a one-based array, or 2n+1 and 2n+2 in a zero-based array. This allows moving up or down the tree by doing simple index computations. Balancing a heap is done by swapping elements which are out of order. As we can build a heap from an array without requiring extra memory (for the nodes, for example), heapsort can be used to sort an array in-place.

The operations commonly performed with a heap are:

  • create-heap: create an empty heap
  • heapify: create a heap out of given array of elements
  • find-max or find-min: find the maximum item of a max-heap or a minimum item of a min-heap, respectively (aka, peek)
  • delete-max or delete-min: removing the root node of a max- or min-heap, respectively
  • increase-key or decrease-key: updating a key within a max- or min-heap, respectively
  • insert: adding a new key to the heap
  • merge: joining two heaps to form a valid new heap containing all the elements of both.
  • meld(h1,h2): Return the heap formed by taking the union of the item-disjoint heaps h1 and h2. Melding destroys h1 and h2.
  • size: return the number of items in the heap.
  • isEmpty(): returns true if the heap is empty, false otherwise.
  • buildHeap(list): builds a new heap from a list of keys.
  • ExtractMin() [or ExtractMax()]: Returns the node of minimum value from a min heap [or maximum value from a max heap] after removing it from the heap
  • Union(): Creates a new heap by joining two heaps given as input.
  • Shift-up: Move a node up in the tree, as long as needed (depending on the heap condition: min-heap or max-heap)
  • Shift-down: Move a node down in the tree, similar to Shift-up

Different types of heaps implement the operations in different ways, but notably, insertion is often done by adding the new element at the end of the heap in the first available free space. This will tend to violate the heap property, and so the elements are then reordered until the heap property has been reestablished. Construction of a binary (or d-ary) heap out of a given array of elements may be performed faster than a sequence of consecutive insertions into an originally empty heap using the classic Floyd's algorithm, with the worst-case number of comparisons equal to 2N − 2s2(N) − e2(N) (for a binary heap), wheres2(N) is the sum of all digits of the binary representation of N and e2(N) is the exponent of 2 in the prime factorization of N

(reference from:https://en.wikipedia.org/wiki/Heap_(data_structure))

Binary heap

There are several types of heaps, but in the current article we are going to discuss the binary heap. For short, let's call it just "heap". It is used to implement priority queue ADTand in the heapsort algorithm. Heap is a complete binary tree, which answers to the heap property.

http://www.algolist.net/Data_structures/Binary_heap

Implementation in java

路径:commons-collections-3.2.1-src/src/java/org/apache/commons/collections/BinaryHeap.java

 

 

 

 

http://www.cnblogs.com/davidwang456/p/3773073.html

 
 
 
分享到:
评论

相关推荐

    二叉堆(binary heap)

    本人做的一个二叉堆的课件,附带STL中的priority_queue

    java-二叉树binaryTree

    6. **二叉堆(Binary Heap)**:一种特殊的完全二叉树,可以用于实现优先队列。二叉堆有两种类型:最大堆(父节点的值大于或等于其子节点的值)和最小堆(反之)。 7. **树的序列化和反序列化**:将二叉树转换为...

    binomial heap的算法描述

    和二叉堆(Binary Heap)相比,二项堆并不支持快速合并操作。二项堆的合并操作是其最显著的特性,由于采用了基于指针的设计,可以通过简单的剪枝和嫁接树结构来实现大量节点的快速合并。此外,二项堆的结构比较灵活...

    几种堆(Bin,Fib,Pair)在Dijkstra算法上的效率试验

    几种堆(BinaryHeap, FibHeap, PairHeap) 在Dijkstra算法上的效率试验 实验算法:标准Dijkstra算法,用到Heap的Insert, DeleteMn, DecreaseKey方法。 数据存储:链表式前向星存边

    java-二叉堆(堆)binaryHeap

    二叉堆,也称为堆数据结构,是一种特殊的树形数据结构,它满足特定的属性:在二叉堆中,每个节点的值要么大于或等于其子节点(最大堆),要么小于或等于其子节点(最小堆)。这个特性使得二叉堆在处理与优先级相关的...

    Java Methods-Heaps and Priority Queues.ppt

    堆的实现方式有很多种,常见的有二叉堆(Binary Heap)和斐波那契堆(Fibonacci Heap)。 二、优先队列(Priority Queue) 优先队列是一种数据结构,它能够根据优先级来存储和提取数据。优先队列的实现方式有很多...

    服务计算概论作业报告.doc

    在服务计算概论中,二进制堆(Binary Heap)是一种常用的数据结构。它是一种特殊的堆结构,用于实现优先队列和堆排序算法。二进制堆的实现需要考虑到堆的性质、堆的操作和堆的优化等方面。 在上面的代码中,我们...

    数据结构常用算法c++实现

    Binary Heap Fibonacci Heap Priority Queue (list based) Bubble sort Selection sort Insertion sort Radix sort Quick sort Merge sort Heap sort Double linked list Skip list Self-organized linked-list ops...

    binary_heap_for_A_star

    binary_heap_for_A_star

    算法设计与分析:3-Lec7-Heap.pdf

    - 二叉堆(Binary Heap):利用树形结构来代替链表,可以高效地支持Insert和ExtractMin操作; - 斐波那契堆(Fibonacci Heap):通过简单地切割边来实现Decrease Key操作,通过控制“多子树”结构来保持“灌木状”树...

    数据结构Advanced-Data-Structures

    Binary heap 315 Binomial heap 321 Fibonacci heap 326 2-3 heap 331 Pairing heap 331 Beap 334 Leftist tree 335 Skew heap 338 Soft heap 341 d-ary heap 343 Tries 346 Trie 346 Radix tree 353 Suffix tree ...

    DemoBinaryHeap.pdf

    在计算机科学领域,二叉堆(Binary Heap)是一种重要的数据结构,主要被用于实现优先队列(Priority Queue)和在排序算法中,如希尔排序(Shell Sort)和堆排序(Heap Sort)。二叉堆是一种特殊的树形数据结构,满足...

    几种堆(BinaryHeap, FibHeap, PairHeap)在Dijkstra算法上的效率试

    本篇文章将探讨三种不同类型的堆:二叉堆(Binary Heap)、斐波那契堆(Fibonacci Heap)和配对堆(Pairing Heap),以及它们在Dijkstra算法中的效率差异。 **二叉堆**是最基础的堆结构,分为最大堆和最小堆。最大...

    Binary_Heap_Demo.zip_DEMO

    这个“Binary_Heap_Demo.zip_DEMO”压缩包包含了Java实现的二叉堆示例代码,已经过测试,可以直接运行。 二叉堆分为两种类型:最大堆和最小堆。在最大堆中,每个节点的值都大于或等于其子节点的值,而在最小堆中则...

    两种GPU上改进的最短路径算法.pdf

    它采用了二叉堆(Binary Heap)的数据结构,能够高效地处理大量数据并保持优先级顺序。Heap-APSP算法在测试中显示出了非常高的加速比,可以达到46到56倍的速度提升,而且这个加速性能并不受节点度数的影响,表现出了...

    datastructure_斐波那契堆_二项堆_

    在这个主题中,我们将深入探讨两种重要的数据结构——斐波那契堆(Fibonacci Heap)和二项堆(Binary Heap)。它们在算法设计,特别是图论和最优化问题中扮演着至关重要的角色,比如Prim's最小生成树算法和Dijkstra'...

    算法面试通关40讲完整课件 11-13 优先队列(PriorityQueue)

    1. **堆数据结构**:优先队列通常基于堆实现,最常见的是二叉堆(Binary Heap)。二叉堆是一种近乎完全二叉树,分为两种主要类型:最小堆(Min Heap)和最大堆(Max Heap)。在最小堆中,每个节点的值都小于或等于其...

    陈广 C#数据结构视频 第5章 树(1)

    "5-3.swf"可能讲解二叉堆(Binary Heap),一种特殊的二叉树,常用于实现优先队列。"5-6.swf"可能涵盖树的合并和分割操作,这对于理解树的动态变化和优化非常重要。"5-1.swf"和"5-4.swf"可能会讨论树的构造和基本...

    c++数据结构与算法实现

    BinaryHeap.h: Binary heap TestBinaryHeap.cpp: Test program for binary heaps LeftistHeap.h: Leftist heap TestLeftistHeap.cpp: Test program for leftist heaps BinomialQueue.h: Binomial queue ...

    20151910042-刘鹏-DSA实验09-优先队列结构实验1

    1. **堆**:最常见的优先队列实现方式是使用二叉堆(Binary Heap)。二叉堆分为最大堆和最小堆,最大堆保证根节点的值大于或等于其子节点,最小堆则相反。插入和删除元素(通常是最大/最小元素)的操作可以在对数...

Global site tag (gtag.js) - Google Analytics