Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist...

of 57 /57
Heaps Priority Queues

Embed Size (px)

Transcript of Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist...

  • HeapsPriority Queues

    Data Structure: Heaps

  • *Data Structure: Heaps*Outline

    Binary heaps

    Binomial queues

    Leftist heaps

    Data Structure: Heaps

  • Binary Heaps

    Data Structure: Heaps

  • *Data Structure: Heaps*Heap order propertyFor every root node N, the key in the parent of N is smaller than or equal to the key in N.5The smallest value is in the root.

    Data Structure: Heaps

  • *Data Structure: Heaps*Operations on priority queuesInsertPut an element into the queueDeleteMinFind the minimal elementReturn itRemove it from the queue

    Data Structure: Heaps

  • *Data Structure: Heaps*Binary Heaps

    A binary heap is an implementation of a priority queue.A binary heap is a complete binary tree with heap order property.

    Data Structure: Heaps

  • *Data Structure: Heaps*Complete Binary TreeA binary tree is a complete binary tree if every level in the tree is completely filled, except the leaf level, which is filled from left to right.

    32Height is in log2 n, where n is the number of nodes.

    Data Structure: Heaps

  • *Data Structure: Heaps*Array implementation of complete binary treeAABCDIHGFEJ LK 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15124356789121110

    Data Structure: Heaps

  • *Data Structure: Heaps*Class BinaryHeap public class BinaryHeap{public BinaryHeap( ){this( DEFAULT_CAPACITY );}public BinaryHeap( int capacity ){currentSize = 0;array = new Comparable[ capacity + 1 ];}private static final int DEFAULT_CAPACITY = 100;private int currentSize; // Number of elements in heapprivate Comparable [ ] array; // The heap array}

    Data Structure: Heaps

  • *Data Structure: Heaps*percolateUp512223192536

    Data Structure: Heaps

  • *Data Structure: Heaps*Method percolateUp

    private void percolateUp( int hole ){Comparable x = array[hole];while (hole > 1 && x.compareTo( array[ hole / 2 ] ) < 0){array[ hole ] = array[ hole/2 ]; hole = hole/2;}array[ hole ] = x;}

    Data Structure: Heaps

  • *Data Structure: Heaps*Method percolateUpprivate void percolateUp( int hole ){while (hole>1 && array[hole].compareTo( array[ hole/2 ])
  • *Data Structure: Heaps*PercolateDown321234254056404323313330364535

    Data Structure: Heaps

  • *Data Structure: Heaps*Method percolateDownprivate void percolateDown( int hole ){int child;

    while( hole * 2

  • *Data Structure: Heaps*Method percolateDownprivate void percolateDown( int hole ){int child;Comparable tmp = array[ hole ]; // save the value of the nodewhile ( hole * 2
  • *Data Structure: Heaps*Insertion5192510

    Data Structure: Heaps

  • *Data Structure: Heaps*Method insert

    public void insert( Comparable x ) throws Overflow{if( isFull( ) )throw new Overflow( ); array[++currentSize]=x;percolateUp(currentSize);}

    Data Structure: Heaps

  • *Data Structure: Heaps*DeleteMin51210194825

    Data Structure: Heaps

  • *Data Structure: Heaps*Method deleteMinpublic Comparable findMin( ){if( isEmpty( ) )return null;return array[ 1 ];}

    public Comparable deleteMin( ){if( isEmpty( ) )return null;Comparable minItem = findMin( );array[ 1 ] = array[ currentSize--];percolateDown( 1 );return minItem;}

    Data Structure: Heaps

  • *Data Structure: Heaps*Method buildHeapprivate void buildHeap( ){for( int i = currentSize / 2; i > 0; i-- )percolateDown( i );}321234254056404323313330364535

    Data Structure: Heaps

  • *Data Structure: Heaps*Method decreaseKey

    public void decreaseKey(int p, Comparable d) throws outOfRange{if( p>currentSize )throw new outOfRange();array[ p ] = array[ p ] - d;percolateUp( p );}

    Data Structure: Heaps

  • *Data Structure: Heaps*Method increaseKey

    public void increaseKey(int p, Comparable d) throws outOfRange{if( p>currentSize )throw new outOfRange();array[ p ] = array[ p ] + d;percolateDown( p );}

    Data Structure: Heaps

  • Binomial Queues

    Data Structure: Heaps

  • *Data Structure: Heaps*Binomial TreeA binomial tree is defined recursively as follow:A binomial tree of height 0, denoted by B0, is a one-node tree.A binomial tree of height k, denoted by Bk, is formed by attaching a binomial tree Bk-1 to the root of another binomial tree Bk-1.

    Data Structure: Heaps

  • *Data Structure: Heaps*Property of Binomial TreesA binomial tree of height k has 2k nodes. The number of nodes at depth d is the binomial coefficient k d. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 11 6 15 20 15 6 1

    Data Structure: Heaps

  • *Data Structure: Heaps*StructureA binomial queue is a collection of heap-ordered trees, each of which is a binomial tree.6

    Data Structure: Heaps

  • *Data Structure: Heaps*Binomial Nodesclass BinomialNode{BinomialNode( Comparable theElement ){ this( theElement, null, null );}BinomialNode( Comparable theElement,BinomialNode lt, BinomialNode nt ){element = theElement;Child = lt;nextSibling = nt;}Comparable element;BinomialNode Child; BinomialNode nextSibling;}

    Data Structure: Heaps

  • *Data Structure: Heaps*Examples: binomial nodes262835434551465013163240ChildnextSibling

    Data Structure: Heaps

  • *Data Structure: Heaps*Binomial Queuespublic class BinomialQueue{public BinomialQueue( ){theTrees = new BinomialNode[ MAX_TREES ];makeEmpty( );}...public void makeEmpty( ){currentSize = 0;for( int i=0; i < theTrees.length; i++ )theTrees[ i ] = null;}private static final int MAX_TREES = 14;private int currentSize; private BinomialNode [ ] theTrees; private int capacity( ){return 2*theTrees.length - 1;}}

    Data Structure: Heaps

  • *Data Structure: Heaps*Method combineTrees

    private static BinomialNode combineTrees( BinomialNode t1,BinomialNode t2 ){if( t1.element.compareTo( t2.element ) > 0 )return combineTrees( t2, t1 );t2.nextSibling = t1.Child;t1.leftChild = t2;return t1;}26283543455146502628354345514650

    Data Structure: Heaps

  • *Data Structure: Heaps*Method merge (1)public void merge( BinomialQueue rhs ) throws Overflow{if( this == rhs ) return;if( currentSize+rhs.currentSize>capacity() )throw new Overflow( );currentSize += rhs.currentSize;BinomialNode carry = null;for( int i=0,j=1; j
  • *Data Structure: Heaps*Method merge (2)// No trees if (t1==null && t2==null && carry==null) {}// Only this if (t1!=null && t2==null && carry==null) {}// Only rhs if (t1==null && t2!=null && carry==null){theTrees[i] = t2; rhs.theTrees[i] = null;}// Only carryif (t1==null && t2==null && carry!=null) {theTrees[ i ] = carry; carry = null;}// this & rhs if (t1!=null && t2==null && carry!=null){carry = combineTrees( t1, t2 ); theTrees[i]=rhs.theTrees[i]=null;}

    Data Structure: Heaps

  • *Data Structure: Heaps*Method merge (3)// this and carryif (t1!=null && t2==null && carry!=null){carry = combineTrees( t1, carry );theTrees[ i ] = null;}// rhs and carryif (t1==null && t2!=null && carry!=null){carry = combineTrees( t2, carry );rhs.theTrees[ i ] = null;}// All threeif (t1!=null && t2!=null && carry!=null){theTrees[ i ] = carry;carry = combineTrees( t1, t2 );rhs.theTrees[ i ] = null;}}

    Data Structure: Heaps

  • *Data Structure: Heaps*Method merge (4)

    for( int k=0; k < rhs.theTrees.length; k++ )rhs.theTrees[ k ] = null;rhs.currentSize = 0;}

    Data Structure: Heaps

  • *Data Structure: Heaps*Method insert

    public void insert( Comparable x ) throws Overflow{BinomialQueue oneItem= new BinomialQueue( );oneItem.currentSize = 1;oneItem.theTrees[0] = new BinomialNode( x );merge( oneItem );}

    Data Structure: Heaps

  • *Data Structure: Heaps*Method deleteMin (2)for( int j = minIndex - 1; j >= 0; j-- ){deletedQueue.theTrees[ j ] = deletedTree;deletedTree = deletedTree.nextSibling;deletedQueue.theTrees[ j ].nextSibling = null;}theTrees[ minIndex ] = null; currentSize -= deletedQueue.currentSize + 1;try { merge( deletedQueue ); }catch( Overflow e ) { }return minItem;}

    Data Structure: Heaps

  • *Data Structure: Heaps*Method deleteMinpublic Comparable deleteMin( ){if( isEmpty( ) )return null;int minIndex = findMinIndex( );Comparable minItem =theTrees[minIndex].element;BinomialNode deletedTree = theTrees[ minIndex ].child;BinomialQueue deletedQueue = new BinomialQueue( );deletedQueue.currentSize =(1
  • Leftist Heaps

    Data Structure: Heaps

  • *Data Structure: Heaps*Null Path LengthThe null path length of the node X, npl(X), is the length of the shortest path from X to a node without 2 children.npl(X) = 1 + min (npl(Y1), npl(Y2)), where Y1 and Y2 are children of X.111000000

    Data Structure: Heaps

  • *Data Structure: Heaps*Leftist TreesA leftist tree is a binary tree such that for every node X in the tree npl(left) npl(right), where left and right are left child and right child of X.111000000

    Data Structure: Heaps

  • *Data Structure: Heaps*Examples of leftist trees1110010001110000000110001000

    Data Structure: Heaps

  • *Data Structure: Heaps*Leftist HeapA leftist heap is a leftist tree with heap order property.4562115318122333

    Data Structure: Heaps

  • *Data Structure: Heaps*Leftist Tree Nodesclass LeftHeapNode{LeftHeapNode( Comparable theElement ){this( theElement, null, null );}

    LeftHeapNode( Comparable theElement,LeftHeapNode lt, LeftHeapNode rt ){element = theElement; npl = 0;left = lt;right = rt;}

    Comparable element; int npl; LeftHeapNode left;LeftHeapNode right;}

    Data Structure: Heaps

  • *Data Structure: Heaps*Merge Leftist Heaps4562115318122333

    Data Structure: Heaps

  • *Data Structure: Heaps*Merge Leftist Heaps6153181223338122315

    Data Structure: Heaps

  • *Data Structure: Heaps*Merge Leftist Heaps4

    Data Structure: Heaps

  • *Data Structure: Heaps*Method merge1private static LeftHeapNode merge1 ( LeftHeapNode h1, LeftHeapNode h2 ){if( h1.left == null ) // Single nodeh1.left = h2; // Other fields in h1 OKelse{h1.right = merge( h1.right, h2 );if( h1.left.npl < h1.right.npl )swapChildren( h1 );h1.npl = h1.right.npl + 1;}return h1;}

    Data Structure: Heaps

  • *Data Structure: Heaps*Method mergepublic void merge( LeftistHeap rhs ){if( this == rhs ) return;root = merge( root, rhs.root );rhs.root = null;}private static LeftHeapNode merge( LeftHeapNode h1, LeftHeapNode h2 ){if( h1 == null ) return h2;if( h2 == null ) return h1;if( h1.element.compareTo( h2.element ) < 0 )return merge1( h1, h2 );elsereturn merge1( h2, h1 );}

    Data Structure: Heaps

  • *Data Structure: Heaps*Methods insert, deleteMInpublic void insert( Comparable x ){root=merge(new LeftHeapNode(x),root);}

    public Comparable deleteMin( ){if( isEmpty( ) )return null;Comparable minItem = root.element;root = merge( root.left, root.right );return minItem;}

    Data Structure: Heaps

  • *Data Structure: Heaps*ApplicationsEvent simulationMerge sort

    Data Structure: Heaps

  • *Data Structure: Heaps*Event SimulationEvents have the scheduled time to happen.Advancing time by clock ticks is too slow.We need to find the next event.So, events are put into heaps with time as the element.We choose the next event from the root of the heapp.

    Data Structure: Heaps

  • *Data Structure: Heaps*Heap Sort51234252330 51234232530

    Data Structure: Heaps

  • *Data Structure: Heaps*Heap Sort1234302351225342330 5

    Data Structure: Heaps

  • *Data Structure: Heaps*Heap Sort232534123052325343012 5

    Data Structure: Heaps

  • *Data Structure: Heaps*Heap Sort253423123052534233012 5

    Data Structure: Heaps

  • *Data Structure: Heaps*Heap Sort343023122553430232512 5

    Data Structure: Heaps

  • *Data Structure: Heaps*

    Data Structure: Heaps

    *********************************************************