2.Binary Heap

download 2.Binary Heap

of 57

Transcript of 2.Binary Heap

  • 7/30/2019 2.Binary Heap

    1/57

    Heaps

    Priority Queues

  • 7/30/2019 2.Binary Heap

    2/57

    9/2/2013 Data Structure: Heaps 2

    Outline

    Binary heaps

    Binomial queues

    Leftist heaps

  • 7/30/2019 2.Binary Heap

    3/57

    Binary Heaps

  • 7/30/2019 2.Binary Heap

    4/57

    9/2/2013 Data Structure: Heaps 4

    Heap order propertyFor every root node N, the key in the parentofN is smaller than or equal to the key in N.

    512

    15 2216 28 2321

    1925 48

    27The smallest value is in the root.

  • 7/30/2019 2.Binary Heap

    5/57

    9/2/2013 Data Structure: Heaps 5

    Operations on priority queues Insert

    Put an element into the queue

    DeleteMin Find the minimal element Return it

    Remove it from the queue

  • 7/30/2019 2.Binary Heap

    6/57

    9/2/2013 Data Structure: Heaps 6

    Binary Heaps A binary heap is an implementation of a

    priority queue.

    A binary heap is a complete binary tree withheap order property.

  • 7/30/2019 2.Binary Heap

    7/57

    9/2/2013 Data Structure: Heaps 7

    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. 3212

    34 2540 56 4043

    2331 33

    30 36 4535

    Height is in log2n,

    where n is the

    number of nodes.

  • 7/30/2019 2.Binary Heap

    8/57

    9/2/2013 Data Structure: Heaps 8

    Array implementation of complete binary treeA

    BD E

    I J KH

    CF G

    L

    A B C D IHGFE J LK1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    1

    2

    4

    3

    5 6 7

    8 9 121110

  • 7/30/2019 2.Binary Heap

    9/57

    9/2/2013 Data Structure: Heaps 9

    Class BinaryHeappublic 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 heap

    private Comparable [ ] array; // The heap array

    }

  • 7/30/2019 2.Binary Heap

    10/57

    9/2/2013 Data Structure: Heaps 10

    percolateUp5

    1215 22

    16 3 2321

    1925 48

    27 36

  • 7/30/2019 2.Binary Heap

    11/57

    9/2/2013 Data Structure: Heaps 11

    Method percolateUpprivate 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;

    }

  • 7/30/2019 2.Binary Heap

    12/57

    9/2/2013 Data Structure: Heaps 12

    Method percolateUpprivate void percolateUp( int hole )

    { while (hole>1 && array[hole].compareTo( array[ hole/2 ])

  • 7/30/2019 2.Binary Heap

    13/57

    9/2/2013 Data Structure: Heaps 13

    PercolateDown32

    1234 25

    40 56 4043

    2331 33

    30 36 4535

  • 7/30/2019 2.Binary Heap

    14/57

    9/2/2013 Data Structure: Heaps 14

    Method percolateDownprivate void percolateDown( int hole )

    { int child;

    while( hole * 2

  • 7/30/2019 2.Binary Heap

    15/57

    9/2/2013 Data Structure: Heaps 15

    Method percolateDownprivate void percolateDown( int hole )

    { int child;Comparable tmp = array[ hole ]; // save the value of the node

    while ( hole * 2

  • 7/30/2019 2.Binary Heap

    16/57

    9/2/2013 Data Structure: Heaps 16

    Insertion5

    1215 22

    16 28 2321

    1925 48

    27 10

  • 7/30/2019 2.Binary Heap

    17/57

    9/2/2013 Data Structure: Heaps 17

    Method insertpublic void insert( Comparable x ) throws Overflow

    { if( isFull( ) ) throw new Overflow( );

    array[++currentSize]=x;

    percolateUp(currentSize);}

  • 7/30/2019 2.Binary Heap

    18/57

    9/2/2013 Data Structure: Heaps 18

    DeleteMin5

    1215 22

    16 28 2321

    1019 48

    27 25

  • 7/30/2019 2.Binary Heap

    19/57

    9/2/2013 Data Structure: Heaps 19

    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;

    }

  • 7/30/2019 2.Binary Heap

    20/57

    9/2/2013 Data Structure: Heaps 20

    Method buildHeapprivate void buildHeap( )

    { for( int i = currentSize / 2; i > 0; i-- )

    percolateDown( i );

    }

    3212

    34 2540 56 4043

    2331 33

    30 36 4535

  • 7/30/2019 2.Binary Heap

    21/57

    9/2/2013 Data Structure: Heaps 21

    Method decreaseKeypublic void decreaseKey(int p, Comparable d)

    throws outOfRange

    { if( p>currentSize ) throw new outOfRange();

    array[ p ] = array[ p ] - d;

    percolateUp( p );

    }

  • 7/30/2019 2.Binary Heap

    22/57

    9/2/2013 Data Structure: Heaps 22

    Method increaseKeypublic void increaseKey(int p, Comparable d)

    throws outOfRange

    { if( p>currentSize ) throw new outOfRange();

    array[ p ] = array[ p ] + d;

    percolateDown( p );

    }

  • 7/30/2019 2.Binary Heap

    23/57

    Binomial Queues

  • 7/30/2019 2.Binary Heap

    24/57

    9/2/2013 Data Structure: Heaps 24

    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 ofanother binomial tree Bk-1.

  • 7/30/2019 2.Binary Heap

    25/57

    9/2/2013 Data Structure: Heaps 25

    Property of Binomial Trees

    A binomial tree of height khas 2k

    nodes. The number of nodes at depth d is the

    binomial coefficient kd.

    11

    11

    2

    113

    3

    1

    1

    46

    4

    1

    11 11 2 11 3 3 11 4 6 4 11 510105 11 61520156 1

  • 7/30/2019 2.Binary Heap

    26/57

    9/2/2013 Data Structure: Heaps 26

    Structure

    A binomial queue is a collection of heap-ordered trees, each of which is a binomialtree.

    6 321

    1316 32

    4026

    28 3543

    4551 46

    50

  • 7/30/2019 2.Binary Heap

    27/57

    9/2/2013 Data Structure: Heaps 27

    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;

    }

  • 7/30/2019 2.Binary Heap

    28/57

    9/2/2013 Data Structure: Heaps 28

    Examples: binomial nodes13

    16 3240

    2628 35

    4345

    51 4650

    1316 32

    40

    2628 35

    4345

    51 4650

    ChildnextSibling

  • 7/30/2019 2.Binary Heap

    29/57

    9/2/2013 Data Structure: Heaps 29

    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; }

    }

  • 7/30/2019 2.Binary Heap

    30/57

    9/2/2013 Data Structure: Heaps 30

    Method combineTreesprivate 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;

    }26

    28 3543

    4551 46

    50

    2628 35

    4345

    51 4650

  • 7/30/2019 2.Binary Heap

    31/57

    9/2/2013 Data Structure: Heaps 31

    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

  • 7/30/2019 2.Binary Heap

    32/57

    9/2/2013 Data Structure: Heaps 32

    Method merge (2)// No trees

    if (t1==null && t2==null && carry==null) {}// Only thisif (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 & rhsif (t1!=null && t2==null && carry!=null){ carry = combineTrees( t1, t2 );theTrees[i]=rhs.theTrees[i]=null; }

  • 7/30/2019 2.Binary Heap

    33/57

    9/2/2013 Data Structure: Heaps 33

    Method merge(3)// this and carry

    if (t1!=null && t2==null && carry!=null){ carry = combineTrees( t1, carry );theTrees[ i ] = null; }

    // rhs and carry

    if (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; }

    }

  • 7/30/2019 2.Binary Heap

    34/57

    9/2/2013 Data Structure: Heaps 34

    Method merge(4)for( int k=0; k < rhs.theTrees.length; k++ )

    rhs.theTrees[ k ] = null;

    rhs.currentSize = 0;

    }

  • 7/30/2019 2.Binary Heap

    35/57

    9/2/2013 Data Structure: Heaps 35

    Method insertpublic void insert( Comparable x )

    throws Overflow

    { BinomialQueue oneItem= new BinomialQueue( );

    oneItem.currentSize = 1;oneItem.theTrees[0] = new BinomialNode( x );

    merge( oneItem );

    }

  • 7/30/2019 2.Binary Heap

    36/57

    9/2/2013 Data Structure: Heaps 36

    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;

    }

  • 7/30/2019 2.Binary Heap

    37/57

    9/2/2013 Data Structure: Heaps 37

    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

  • 7/30/2019 2.Binary Heap

    38/57

    Leftist Heaps

  • 7/30/2019 2.Binary Heap

    39/57

    9/2/2013 Data Structure: Heaps 39

    Null Path Length

    The null path length of thenode 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 ofX.

    11

    1 000

    00

    0

  • 7/30/2019 2.Binary Heap

    40/57

    9/2/2013 Data Structure: Heaps 40

    Leftist Trees

    A leftist tree is a binary tree such that forevery node X in the tree npl(left) npl(right),where leftand rightare left child and right

    child ofX.1

    11 0

    00

    00

    0

  • 7/30/2019 2.Binary Heap

    41/57

    9/2/2013 Data Structure: Heaps 41

    Examples of leftist trees1

    11 0

    01

    00

    0

    11

    1 000

    00

    0 01

    1 000

    10

    00

  • 7/30/2019 2.Binary Heap

    42/57

    9/2/2013 Data Structure: Heaps 42

    Leftist Heap

    A leftist heap is a leftist tree with heap orderproperty.4

    56 21

    1531

    812

    2333

  • 7/30/2019 2.Binary Heap

    43/57

    9/2/2013 Data Structure: Heaps 43

    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;}

  • 7/30/2019 2.Binary Heap

    44/57

    9/2/2013 Data Structure: Heaps 44

    Merge Leftist Heaps4

    5

    6

    2115318

    1223

    33

  • 7/30/2019 2.Binary Heap

    45/57

    9/2/2013 Data Structure: Heaps 45

    Merge Leftist Heaps6

    1531 812

    2333

    812

    23

    158

    1223

    15

  • 7/30/2019 2.Binary Heap

    46/57

    9/2/2013 Data Structure: Heaps 46

    Merge Leftist Heaps4

    5

    216

    3133

    812

    2315

  • 7/30/2019 2.Binary Heap

    47/57

    9/2/2013 Data Structure: Heaps 47

    Method merge1private static LeftHeapNode merge1

    (LeftHeapNode h1, LeftHeapNode h2 ){ if(h1.left ==null ) //Single node

    h1.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;

    }

  • 7/30/2019 2.Binary Heap

    48/57

    9/2/2013 Data Structure: Heaps 48

    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 );else

    return merge1( h2, h1 );}

  • 7/30/2019 2.Binary Heap

    49/57

    9/2/2013 Data Structure: Heaps 49

    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;

    }

  • 7/30/2019 2.Binary Heap

    50/57

    9/2/2013 Data Structure: Heaps 50

    Applications

    Event simulation Merge sort

  • 7/30/2019 2.Binary Heap

    51/57

    9/2/2013 Data Structure: Heaps 51

    Event Simulation

    Events 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 theelement.

    We choose the next event from the root of the

    heapp.

  • 7/30/2019 2.Binary Heap

    52/57

    9/2/2013 Data Structure: Heaps 52

    Heap Sort5

    1234 25

    2330

    5 12 3423 25 30

  • 7/30/2019 2.Binary Heap

    53/57

    9/2/2013 Data Structure: Heaps 53

    Heap Sort12

    2534 30

    235

    12 25 3423 30 5

  • 7/30/2019 2.Binary Heap

    54/57

    9/2/2013 Data Structure: Heaps 54

    Heap Sort23

    2534 12

    305

    23 25 3430 12 5

    S

  • 7/30/2019 2.Binary Heap

    55/57

    9/2/2013 Data Structure: Heaps 55

    Heap Sort25

    3423 12

    305

    25 34 2330 12 5

    H S

  • 7/30/2019 2.Binary Heap

    56/57

    9/2/2013 Data Structure: Heaps 56

    Heap Sort34

    3023 12

    255

    34 30 2325 12 5

  • 7/30/2019 2.Binary Heap

    57/57