07-heaps06

Embed Size (px)

Citation preview

  • 8/8/2019 07-heaps06

    1/13

    1

    1

    Heaps

    Heaps

    Properties

    Deletion, Insertion, Construction

    Implementation of the Heap

    Implementation of Priority Queue

    using a Heap

    An application: HeapSort

    2

    Heaps (Min-heap)Complete binary tree that stores a collection of keys

    (or key-element pairs) at its internal nodes and that satisfies

    the additional property:

    key(parent) key(child)4

    6

    207

    811

    5

    9

    1214

    15

    2516

    REMEMBER:

    complete binary tree

    all levels are full, except the

    last one, which is left-filled

    3

    Max-heap

    key(parent) key(child)

    40

    26

    2017

    811

    35

    19

    1214

    15

    131

    4

    We store the keys in the internal nodes only

    5

    915

    16

    After adding the leaves the resulting tree is full

  • 8/8/2019 07-heaps06

    2/13

    2

    5

    Height of a Heap

    Theorem: A heap storing nkeys has height O(log n)

    Proof:

    Let hbe the height of a heap storing nkeys

    Since there are 2ikeys at depth i= 0, , h- 2 and at least one

    key at depth h- 1, we have n 1 + 2 + 4 + + 2h-2 + 1

    Thus, n 2h-1 , i.e., h log n+ 1

    1

    2

    2h-2

    at least 1

    keys

    0

    1

    h-2

    h-1

    depth

    h6

    We could use a heap to implement a priority queue

    We store a (key, element) item at each internal

    node

    (2, Sue)

    (6, Mark)(5, Pat)

    (9, Jeff) (7, Anna)

    removeMin():

    Remove the root

    Re-arrange the heap!

    Notice that .

    7

    Removal From a Heap

    The removal of thetop key leaves a hole

    We need to fix theheap

    First, replace the holewith the last key inthe heap

    Then, begin Downheap

    8

    Downheap

    Downheap compares the parent with the smallest

    child. If the child is smaller, it switches the two.

  • 8/8/2019 07-heaps06

    3/13

    3

    9

    Downheap Continues

    10

    Downheap Continues

    11

    End of Downheap

    Downheap terminates when the key is greater than thekeys of both its children or the bottom of the heap isreached.

    12

    Heap Insertion

    The key to insert is 6

  • 8/8/2019 07-heaps06

    4/13

    4

    13

    Heap Insertion

    14

    Upheap

    Swap parent-child keys out of order

    15

    Upheap Continues

    16

    End of Upheap

    Upheap terminates when new key is greater than the keyof its parent or the top of the heap is reached

    total #swa s h - 1 , which is O lo n

  • 8/8/2019 07-heaps06

    5/13

    5

    17

    Heap Construction

    We could insert the Items one at the time with

    a sequence of Heap Insertions:

    log k = O(n log n)k=1

    n

    But we can do better .

    18

    We can construct a heap

    storing ngiven keys using

    a bottom-up construction

    Bottom-up Heap

    Construction

    19

    Construction of a Heap

    Idea: Recursively re-arrange each sub-

    tree in the heap starting with the leaves

    begin here

    HEAP

    HEAPHEAP

    1st2nd3rd

    4th

    5th6th

    begin here

    20

    Example 1 (Max-Heap)

    3 6

    2

    4 5

    10 87 3

    1 9 6

    6

    3

    2

    4 5

    10 87 6

    1 9 3

    2

    4 5

    10 89 6

    1 7 3

    7 9 5 10

    I am now drawing theleaves anymore here

    2

    4 10

    5 89 6

    1 7 3

    --- keys already in the tree ---

  • 8/8/2019 07-heaps06

    6/13

  • 8/8/2019 07-heaps06

    7/13

  • 8/8/2019 07-heaps06

    8/13

    8

    29

    Let j = h-i, then i = h-j and

    (h i)2i = j2h-j = 2h j2-j

    Consider j2-j : j2-j = 1/2 + 2 1/4 + 3 1/8 + 4 1/16 +

    = 1/2 + 1/4 + 1/8 + 1/16 +

  • 8/8/2019 07-heaps06

    9/13

    9

    33

    2i > nifTRUELeaf? T[i]

    T 0ifT[1]The Root

    i > 1ifT[i div 2]Parent of T[i]

    2i + 1 nifT[2i+1]Right child of

    T[i]

    2i nifT[2i]Left child of T[i]

    n = 111

    2

    4 5 6 7

    8 9 10 11

    I

    3

    Reminder ..

    34

    Implementation of a Priority

    Queue with a Heap

    35

    O(log n)

    O(log n)O(log 1)

    (remove root + downheap)

    (upheap)

    36

    Algorithm PriorityQueueSort(S, P):

    Input: A sequence S storing n elements, on which a

    total order relation is defined, and a Priority

    Queue P that compares keys with the same relation

    Output: The Sequence S sorted by the total

    order relation

    while S.isEmpty() do

    e S.removeFirst()

    P.insertItem(e, e)

    while P.isEmpty() doe P.removeMin()

    S.insertLast(e)

    Application: Sorting Heap Sort

    PriorityQueueSort where the PQ is implemented with a HEAP

    Build Heap

    Remove from heap

  • 8/8/2019 07-heaps06

    10/13

    10

    37

    Application: Sorting

    Heap Sort

    Construct initial heap O(n)

    remove root O(1)

    re-arrange O(log n)

    remove root O(1)

    re-arrange O(log (n-1))

    L M

    L

    n

    times

    38

    When there are i nodes left in the PQ: log i

    TOT =i=1

    n

    log i

    = O(n log n)

    39

    HeapSort in Place

    Example on the board .

    40

    How about implementing a double PQ ??

    removeMin() AND removeMax()

    With a Min-heap:

    removeMin() is O(log n)

    removeMax() is ?

    With a Max-Heap:

    removeMin() is ?

    removeMax() is O(log n)

  • 8/8/2019 07-heaps06

    11/13

  • 8/8/2019 07-heaps06

    12/13

  • 8/8/2019 07-heaps06

    13/13