Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture24.pdf

William Albritton
September 14, 2014
190

 lecture24.pdf

William Albritton

September 14, 2014
Tweet

Transcript

  1. Terminology • A heap is a complete binary tree, in

    which each node has a greater (or equal) priority value than its children • A more accurate name for this is max heap • If each node has a priority value less than its children, it is called a min heap
  2. Example Heap • Here is a min heap of Integers

    9 20 37 52 44 24 21 47 81 33 25
  3. What is a Tree? • A tree is set of

    nodes with the following restrictions: 1. Each node has one parent (except for the root node) 2. Each node has zero or more child nodes
  4. Terminology • A binary tree is a tree in which

    each node has at most 2 children
  5. Terminology • The depth is the number of edges from

    root node to current node • For example, the root is at depth 0, its children at depth 1, their children are at depth 2, etc. • Nodes on the same level have the same depth
  6. Complete Binary Trees • A complete binary tree is a

    tree in which every level is filled, except for the bottom level, where all of the nodes are pushed to the left
  7. Example Max Heap Level 0 Level 1 Level 2 Level

    3 50 14 77 53 82 11 71 58 66 5 20 4 18
  8. Heap Array Implementation • Since a heap is a complete

    binary tree, it is convenient to implement with an array  Next node that is added is put at end of array • If the parent node has array index i  Then left child is at index 2*i+1  And right child is at index 2*i+2
  9. Heap Array Implementation • If left or right child has

    array index i  Then parent is at index (i-1)/2
  10. Convert to Array • Let's convert this tree representation of

    a max heap into the corresponding array representation 99 20 37 2 24 4 16 8 3
  11. Convert to Array • Take each node in the heap,

    from the top level to the bottom level, from left to right and insert into an array starting from index zero 99 20 37 2 24 4 16 8 3
  12. Convert to Array • Node 99 is at index 0

    • Node 20 is at index 1 • Node 37 is at index 2 • Node 16 is at index 3 • Etc. 99 20 37 2 24 4 16 8 3
  13. Convert to Array • Index: 0 1 2 3 4

    5 6 7 8 9 • Element: |99|20|37|16|5|24| 2| 3|12|3| 99 20 37 2 24 5 16 12 3 3
  14. Max Heap Array Example • Index: 0 1 2 3

    4 5 6 7 8 9 • Element: | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0| 9 8 7 3 4 5 6 1 2 0
  15. Max Heap Array Example • Index: 0 1 2 3

    4 5 6 7 8 9 • Element: | 9| 3| 9| 2| 1| 8| 8| 2| 1| 1| 9 3 9 8 8 1 2 1 2 1
  16. Not a Max Heap!! • Index: 0 1 2 3

    4 5 6 7 8 9 • Element: | 9|17| 9| 2| 1| 8| 8| 2| 1| 2| 9 17 9 8 8 1 2 1 2 2
  17. Max Heap Insertion Algorithm • Step 1: To keep the

    tree complete, the new node is inserted at bottom left position in tree • This corresponds to the end of the array
  18. Max Heap Insertion Algorithm • Step 2: To keep the

    node relationship of the max heap intact, the new node is swapped with its parent, until the parent node is larger than the child node (“trickles up” the tree)  Each node in a max heap has a larger value than its two child nodes
  19. Max Heap Insertion • Let’s insert the following values into

    a max heap: 5, 4, 6, 3, 3, 8, 8, 4, 5, 9
  20. Max Heap Insertion Example • Insert value: 5 (empty heap,

    so this is the root node) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 5| | | | | | | | | | 5
  21. Max Heap Insertion Example • Insert value: 4 (at the

    bottom left position) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 5| 4| | | | | | | | | 5 4
  22. Max Heap Insertion Example • Insert value: 6 (at the

    bottom left position) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 5| 4| 6| | | | | | | | 5 4 6
  23. Max Heap Insertion Example • Insert value: 6 (greater than

    parent 5, so swap) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 6| 4| 5| | | | | | | | 6 4 5
  24. Max Heap Insertion Example • Insert value: 3 (at the

    bottom left position) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 6| 4| 5| 3| | | | | | | 6 4 5 3
  25. Max Heap Insertion Example • Insert value: 3 (at the

    bottom left position) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 6| 4| 5| 3| 3| | | | | | 6 4 5 3 3
  26. Max Heap Insertion Example • Insert value: 8 (at the

    bottom left position) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 6| 4| 5| 3| 3| 8| | | | | 6 4 5 8 3 3
  27. Max Heap Insertion Example • Insert value: 8 (greater than

    parent 5, so swap) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 6| 4| 8| 3| 3| 5| | | | | 6 4 8 5 3 3
  28. Max Heap Insertion Example • Insert value: 8 (greater than

    parent 6, so swap) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 4| 6| 3| 3| 5| | | | | 8 4 6 5 3 3
  29. Max Heap Insertion Example • Insert value: 8 (at bottom

    left position position) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 4| 6| 3| 3| 5| 8| | | | 8 4 6 5 3 3 8
  30. Max Heap Insertion Example • Insert value: 8 (greater than

    parent 6, so swap) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 4| 8| 3| 3| 5| 6| | | | 8 4 8 5 3 3 6
  31. Max Heap Insertion Example • Insert value: 4 (at bottom

    left position) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 4| 8| 3| 3| 5| 6| 4| | | 8 4 8 5 3 3 6 4
  32. Max Heap Insertion Example • Insert value: 4 (greater than

    parent 3, so swap) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 4| 8| 4| 3| 5| 6| 3| | | 8 4 8 5 3 4 6 3
  33. Max Heap Insertion Example • Insert value: 5 (at bottom

    left position) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 4| 8| 4| 3| 5| 6| 3| 5| | 8 4 8 5 3 4 6 3 5
  34. Max Heap Insertion Example • Insert value: 5 (greater than

    parent 4, so swap) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 4| 8| 5| 3| 5| 6| 3| 4| | 8 4 8 5 3 5 6 3 4
  35. Max Heap Insertion Example • Insert value: 5 (greater than

    parent 4, so swap) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 5| 8| 4| 3| 5| 6| 3| 4| | 8 5 8 5 3 4 6 3 4
  36. Max Heap Insertion Example • Insert value: 9 (at bottom

    left position) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 5| 8| 4| 3| 5| 6| 3| 4| 9| 8 5 8 5 3 4 6 3 4 9
  37. Max Heap Insertion Example • Insert value: 9 (greater than

    parent 3, so swap) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 5| 8| 4| 9| 5| 6| 3| 4| 3| 8 5 8 5 9 4 6 3 4 3
  38. Max Heap Insertion Example • Insert value: 9 (greater than

    parent 5, so swap) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 9| 8| 4| 5| 5| 6| 3| 4| 3| 8 9 8 5 5 4 6 3 4 3
  39. Max Heap Insertion Example • Insert value: 9 (greater than

    parent 8, so swap) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 9| 8| 8| 4| 5| 5| 6| 3| 4| 3| 9 8 8 5 5 4 6 3 4 3
  40. Max Heap Insertion Big-O • Inserting a node into a

    heap follows a path from the leaf to the root  A maximum of h comparisons have to be made, where h is the height of the tree  The height of a complete binary tree (heap) is log2 n (n is number of nodes)  Therefore, the Big-O is O(log n)
  41. Max Heap Removal Algorithm • Removal from a heap is

    always from the top • Step 1: The top node is deleted and its value returned  The deleted node is replaced with the rightmost, bottommost node  This maintains the heap as a complete binary tree
  42. Max Heap Removal Algorithm • Step 2: To keep largest

    value at top of tree, the new value must be compared to its children  If it is smaller than its largest child node, it is swapped with its largest child node  So the node “trickles down” the tree
  43. Max Heap Removal Example • Starting max heap • Index:

    0 1 2 3 4 5 6 7 8 9 • Element: | 9| 8| 8| 4| 5| 5| 6| 3| 4| 3| 9 8 8 5 5 4 6 3 4 3
  44. Max Heap Removal Example • Remove value: 9 (replace with

    last node 3) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 3| 8| 8| 4| 5| 5| 6| 3| 4| | 3 8 8 5 5 4 6 3 4
  45. Max Heap Removal Example • Remove value: 9 (swap 3

    with largest child 8) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 3| 8| 4| 5| 5| 6| 3| 4| | 8 3 8 5 5 4 6 3 4
  46. Max Heap Removal Example • Remove value: 9 (swap 3

    with largest child 5) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 5| 8| 4| 3| 5| 6| 3| 4| | 8 5 8 5 3 4 6 3 4
  47. Max Heap Removal Example • Remove value: 8 (replace with

    last node 4) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 4| 5| 8| 4| 3| 5| 6| 3| | | 4 5 8 5 3 4 6 3
  48. Max Heap Removal Example • Remove value: 8 (swap 4

    with largest child 8) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 5| 4| 4| 3| 5| 6| 3| | | 8 5 4 5 3 4 6 3
  49. Max Heap Removal Example • Remove value: 8 (swap 4

    with largest child 6) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 8| 5| 6| 4| 3| 5| 4| 3| | | 8 5 6 5 3 4 4 3
  50. Max Heap Removal Example • Remove value: 8 (replace with

    last node 3) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 3| 5| 6| 4| 3| 5| 4| | | | 3 5 6 5 3 4 4
  51. Max Heap Removal Example • Remove value: 8 (swap 3

    with largest child 6) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 6| 5| 3| 4| 3| 5| 4| | | | 6 5 3 5 3 4 4
  52. Max Heap Removal Example • Remove value: 8 (swap 3

    with largest child 5) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 6| 5| 5| 4| 3| 3| 4| | | | 6 5 5 3 3 4 4
  53. Max Heap Removal Example • Remove value: 6 (replace with

    last node 4) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 4| 5| 5| 4| 3| 3| | | | | 4 5 5 3 3 4
  54. Max Heap Removal Example • Remove value: 6 (swap 4

    with largest child 5) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 5| 4| 5| 4| 3| 3| | | | | 5 4 5 3 3 4
  55. Max Heap Removal Example • Remove value: 5 (replace with

    last node 3) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 3| 4| 5| 4| 3| | | | | | 3 4 5 3 4
  56. Max Heap Removal Example • Remove value: 5 (swap with

    largest child 5) • Index: 0 1 2 3 4 5 6 7 8 9 • Element: | 5| 4| 3| 4| 3| | | | | | 5 4 3 3 4
  57. Max Heap Removal Big-O • Removing a node from a

    heap follows a path from the root to the leaf  A maximum of h comparisons have to be made, where h is the height of the tree  The height of a complete binary tree (heap) is log2 n (n is number of nodes)  Therefore, the Big-O is O(log n)
  58. Task Manager • Before the next class, you need to:

    1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the due date 4.Stop playing video games!