Lists

Document Sample
Lists Powered By Docstoc
					  Heaps, HeapSort,
  & Priority Queues

Briana B. Morrison
Adapted from Alan Eugenio,
William J. Collins, & Michael Main
Topics

   Heaps
       Implementation
       Insertion
       Deletion
   Applications
       Priority Queue
       HeapSort




                         Heaps   2
Heaps

A heap is a
certain kind of
complete
binary tree.




                  Heaps   3
Heaps
                                            Root
A heap is a
certain kind of
complete
binary tree.



                    When a complete
                    binary tree is built,
                  its first node must be
                           the root.
                            Heaps                  4
Heaps

Complete                Left child
binary tree.            of the
                        root




               The second node is
               always the left child
                   of the root.

                        Heaps          5
Heaps

Complete
                                        Right child
binary tree.                                 of the
                                               root




                 The third node is
               always the right child
                   of the root.

                         Heaps                6
Heaps

Complete
binary tree.




                  The next nodes
                 always fill the next
               level from left-to-right.

                          Heaps            7
Heaps

Complete
binary tree.




                  The next nodes
                 always fill the next
               level from left-to-right.

                          Heaps            8
Heaps

Complete
binary tree.




               Heaps   9
Heaps
                                          45
A heap is a
certain kind of
complete                        35                  23
binary tree.
                           27        21        22        4


                  19
                            Each node in a heap
                             contains a key that
                            can be compared to
                             other nodes' keys.
                   Heaps                                 10
Heaps
                                                   45
A heap is a
certain kind of
complete                                 35                  23
binary tree.
                                    27        21        22        4


* - Max heap requires >=   19
                                     The "heap property"
   Min heap requires <=               requires that each
                                     node's key is >=* the
                                      keys of its children
                            Heaps                                 11
What is a heap? (§7.3.1)

   A heap is a binary tree                         The last node of a heap is
    storing keys at its internal                     the rightmost internal
    nodes and satisfying the                         node of depth h - 1
    following properties:
       Heap-Order: for every internal
        node v other than the root,                                2
        key(v)  key(parent(v))
       Complete Binary Tree: let h                      5                6
        be the height of the heap
           for i = 0, … , h - 1, there are 2i       9         7
            nodes of depth i
           at depth h - 1, the internal
            nodes are to the left of the
            external nodes
                                                                       last node

                                         Heaps                                12
     Maximum and Minimum Heaps
                          55                                   40


                50                  52                  15            30


      25             10        11         5       10


20         22

      (A) M aximum Heap (9 nodes)                (B) M aximum Heap (4 nodes)


                          5                                   10


                10                  50                  15            30



      11             20        52        55       40


25         22


     (C) M inimum Heap (9 nodes)                 (D) M inimum Heap (4 nodes)

                                         Heaps                             13
A HEAP t IS A COMPLETE BINARY TREE
SUCH THAT EITHER t IS EMPTY OR

 1. THE ROOT ITEM IS THE LARGEST
    ITEM IN t, ACCORDING TO SOME
    METHOD FOR COMPARING ITEMS;

 2. THE LEFT AND RIGHT SUBTREES
    OF t ARE HEAPS.

                Heaps          14
                        88



              55                  81



        31         26        77        17



   20        29 22 14 48

A HEAP IS ORDERED FROM TOP TO BOTTOM,
BUT NOT FROM LEFT TO RIGHT.
                         Heaps              15
A HEAP IS NOT A BINARY SEARCH
TREE!




              Heaps             16
BECAUSE A HEAP IS A COMPLETE
BINARY TREE, IT CAN BE STORED IN
AN ARRAY/VECTOR: THE ROOT
ITEM AT INDEX 0, THE LEFT CHILD
OF THE ROOT ITEM AT INDEX 1, AND
SO ON. FOR THE HEAP TWO SLIDES
BACK:
 88 55 81 31 26 77 17 20 29 22 14 48


                      Heaps            17
   Complete Binary Tree for a Vector
                                        5

                                    v[0]
                     1                              3

                v[1]                               v[2]
         9                      6             2            4

       v[3]                  v[4]           v[5]          v[6]
  7             0        8

v[7]          v[8]       v[9]
                                    Heaps                      18
RECALL THAT A COMPLETE BINARY
TREE IS FULL EXCEPT, POSSIBLY, AT
THE LOWEST LEVEL, WHERE ALL
ITEMS ARE AS FAR TO THE LEFT AS
POSSIBLE.




               Heaps           19
Insertion Into A Heap




         Heaps          20
Adding a Node to a Heap

 Put the new node in the                               45
  next available spot.
 Push the new node upward,
  swapping with its parent                   35                   23
  until the new node reaches
  an acceptable location.
                                        27         21        22        4


                               19             42




                                Heaps                                  21
Adding a Node to a Heap

 Put the new node in the                               45
  next available spot.
 Push the new node upward,
  swapping with its parent                   35                   23
  until the new node reaches
  an acceptable location.
                                        42         21        22        4


                               19             27




                                Heaps                                  22
Adding a Node to a Heap

 Put the new node in the                               45
  next available spot.
 Push the new node upward,
  swapping with its parent                   42                   23
  until the new node reaches
  an acceptable location.
                                        35         21        22        4


                               19             27




                                Heaps                                  23
Adding a Node to a Heap

 The parent has a key that                             45
  is >= new node, or
 The node reaches the root.
 The process of pushing the                 42                   23
  new node upward is called
  trickle up, or
                                        35         21        22        4
  reheapification
  upward.
                               19             27




                                Heaps                                  24
                         88             first



               55                  81



         31         26        77        17



    20        29 22 14 48          83           last – 1

THE ITEMS AT POSITIONS FROM first THROUGH
last – 2 FORM A HEAP INTO WHICH 83 IS INSERTED.
                          Heaps                            25
TO BEGIN, STORE THE ITEM AT POSITION last – 1
IN A VARIABLE CALLED value. THE VARIABLE
holeIndex IS THE INDEX CORRESPONDING TO
POSITION last – 1. THE LOCATION AT INDEX
holeIndex IS CALLED THE hole.




                      Heaps                26
                     88 v[0]



       v[1] 55                  81 v[2]



v[3] 31    v[4] 26       77 v[5]      17 v[6]


                                                value
20        29 22 14 48          hole              83
v[7]   v[8] v[9] v[10] v[11]



                      Heaps                             27
WHILE THE ITEM IN value IS GREATER
THAN THE ITEM IN THE PARENT OF hole

(AND hole IS NOT AT THE TOP OF THE HEAP),

MOVE THE ITEM IN THE PARENT OF hole

INTO hole, AND

MOVE hole UP TO ITS PARENT (p = (i-1)/2).


SINCE 83 > 77, WE GET:


                         Heaps              28
                             88



              55                          81



        31              26        hole         17


                                                    value
   20        29    22    14 48            77         83


83 > 81, SO THERE IS ANOTHER LOOP ITERATION:
                                  Heaps                     29
                         88



               55                  hole



         31         26        81          17


                                               value
   20         29 22 14 48          77           83

83 IS NOT GREATER THAN 88, SO THE LOOP IS
EXITED AND 83 IS STORED AT hole.
                          Heaps                        30
                     88



           55                  83



     31         26        81        17


                                         value
20        29 22 14 48          77         83


                DONE!
                      Heaps                      31
ANALYSIS:
IN THE WORST CASE, THE INSERTED ITEM
BELONGS AT THE TOP OF THE HEAP, AND THE
NUMBER OF LOOP ITERATIONS EQUALS THE
HEIGHT OF THE TREE. THE HEIGHT OF A
COMPLETE BINARY TREE IS LOGARITHMIC IN n,
SO worstTime(n) IS LOGARITHMIC IN n.




                   Heaps              32
ON AVERAGE, HALF OF THE ITEMS WILL BE
LESS THAN THE INSERTED ITEM, AND HALF
WILL BE GREATER. IN A HEAP, HALF OF THE
ITEMS ARE LEAVES, AND MOST OF THE
SMALLER ITEMS WILL BE AT OR NEAR LEAF
LEVEL. SO averageTime(n) IS CONSTANT.




                    Heaps                 33
       Example of Heap Before and After
       Insertion of 50
                                        63                                                                         63

                                       v[0]                                                                   v[0]
                     30                               40                                    30                                  40

               v[1]                                  v[2]                             v[1]                                     v[2]
        10                       25             8            38                                         25                8                 38
                                                                               10

       v[3]                     v[4]          v[5]          v[6]              v[3]                    v[4]              v[5]               v[6]
  5             3         18                                              5            3         18           50
v[7]          v[8]        v[9]                                        v[7]           v[8]        v[9]         v[10]
                          (a)                                                                                (b)

                                                                  Heaps                                                               34
  Reorder the tree after Insertion
                    63                                     63
                                                                                                  63
                    v[0]                                   v[0]
        30                       ...          50                        ...                       v[0]

      v[1]
                                                                                     50                        ...
                                             v[1]
...               50                   ...               30                         v[1]
                                                                              ...               30
                  v[4]                                   v[4]
                                                                                                v[4]
             18             25                      18             25
                                                                                           18             25
             v[9]          v[10]                    v[9]          v[10]
                                                                                           v[9]          v[10]
  Step 1 Compare 50 and 25                   Step 2 Compare 50 and 30               Step 3 Compare 50 and 63
   (Exchange v[10] and v[4])                  (Exchange v[4] and v[1])                (50 in correct location)

                                                     Heaps                                                     35
EXERCISE: INSERT 91 INTO THE
FOLLOWING HEAP:
                       88



             55                  83



       31         26        81        17



  20        29 22 14 48          77
                        Heaps              36
   Deletion From A Heap
The Top Item is Always Deleted.
 Known as a “pop” operation.


              Heaps           37
Popping from the Heap

 Move the last node onto the                            45
  root.

                                              42                   23


                                         35         21        22        4


                                19             27




                                 Heaps                                  38
Popping from the Heap

 Move the last node onto the                           27
  root.

                                              42                  23


                                         35        21        22        4


                                19




                                 Heaps                                 39
Popping from the Heap

 Move the last node onto the                              27
  root.
 Push the out-of-place node
  downward, swapping with                        42                  23
  its larger child until the new
  node reaches an
  acceptable location.                      35        21        22        4


                                   19




                                    Heaps                                 40
Popping from the Heap

 Move the last node onto the                              42
  root.
 Push the out-of-place node
  downward, swapping with                        27                  23
  its larger child until the new
  node reaches an
  acceptable location.                      35        21        22        4


                                   19




                                    Heaps                                 41
Popping from the Heap

 Move the last node onto the                              42
  root.
 Push the out-of-place node
  downward, swapping with                        35                  23
  its larger child until the new
  node reaches an
  acceptable location.                      27        21        22        4


                                   19




                                    Heaps                                 42
Popping from the Heap

 The children all have keys                             42
  <= the out-of-place node, or
 The node reaches the leaf.
 The process of pushing the                   35                  23
  new node downward is
  called trickle down, or
                                          27        21        22        4
  reheapification
  downward.
                                 19




                                  Heaps                                 43
FIRST, THE BOTTOM ITEM IN THE HEAP, AT
POSITION last – 1, IS MOVED TO THE VARIABLE
value, THE TOP ITEM IS MOVED TO POSITION last
1, AND hole IS THE LOCATION AT THE TOP OF
THE HEAP.




                    Heaps                   44
Pop one value:
                        88



              55                  83



        31         26        81        17



   20        29 22 14 48          77


                         Heaps              45
                              hole



               55                       83



         31              26        81        17


                                                  value
   20         29    22    14 48         88         77


NOTICE THAT 88 IS NO LONGER PART OF THE HEAP.

                               Heaps                      46
NOW YOU WILL “TRICKLE DOWN”
THE VALUE value, USING hole. IF
value IS SMALLER THAN THE
LARGER OF hole’S CHILDREN, THEN
THE LARGER OF hole’S CHILDREN IS
MOVED TO hole, AND hole BECOMES
THAT VACATED LOCATION:

                Heaps             47
                     83



           55                     hole



     31         26           81          17


                                              value
20        29 22 14 48             88           77


                     Heaps                            48
DURING THE NEXT LOOP
ITERATION, THE LARGER OF hole’S
CHILDREN IS MOVED TO hole, AND
hole BECOMES THE VACATED
LOCATION (BECAUSE value IS
SMALLER):



              Heaps              49
                     83



           55                       81



     31         26           hole        17


                                              value
20        29 22 14 48               88         77


                     Heaps                            50
DURING THE NEXT LOOP
ITERATION, value IS BIGGER THAN
THE LARGEST CHILD, SO EXIT
HEAP), SO THAT CHILD IS MOVED
LOOP THEN COPY value TO hole.




               Heaps              51
                     83



           55                     81



     31         26           77        17



20        29 22 14 48             88


                     Heaps                  52
       Example of Pop
                                        63                                                                          18

                                      v[0]                                                                        v[0]
                     30                               40                                        30                               40

               v[1]                                  v[2]                                 v[1]                                  v[2]
        10                       25            8             38                    10                       25             8                 38

       v[3]                    v[4]          v[5]           v[6]                  v[3]                     v[4]          v[5]               v[6]
  5             3         18                                                 5             3          63

v[7]          v[8]        v[9]                                             v[7]          v[8]        v[9]

                                 Before a deletion                                                    After exchanging the root
                                                                                                     and last element in the heap

                                                                   Heaps                                                               53
  Example of Pop

            40                                       40

         v[0]                                     v[0]
...                     18               ...                      38

                        v[2]                                     v[2]
                   8            38                          8           18

                 v[5]          v[6]                       v[5]          v[6]

      Step 1: Exchange 18 and 40               Step 2: Exchange 18 and 38

                                      Heaps                                  54
 Summary Slide 1
§- Heap
 - an array-based tree that has heap order
 - maximum heap: if v[i] is a parent, then v[i]  v[2i+1]
   and v[i]  v[2i+2] (a parent is  its children)
    - root, v[0], is the maximum value in the vector
 - minimum heap: the parent is  its children.
    - v[0] is the minimum value
 - Insertion: place the new value at the back of the
   heap and filtering it up the tree.


                                                      55
55                         Heaps
 Summary Slide 2
§- Heap (Cont…)
 - Deletion: exchanging its value with the back of the
   heap and then filtering the new root down the tree,
   which now has one less element.
 - Insert and delete running time: O(log2 n)
 - heapifying: apply the filter-down operation to the
   interior nodes, from the last interior node in the tree
   down to the root
   - running time: O(n)
 - The O(n log2 n) heapsort algorithm heapifies a
   vector and erases repeatedly from the heap,
   locating each deleted value in its final position.
                           Heaps                       56

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:8/8/2012
language:
pages:56