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

 
 
 

猜你喜欢

转载自oywl2008.iteye.com/blog/2387181
今日推荐