Pile It On: Binary Heaps - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Pile It On: Binary Heaps

Description:

Recursively heapify on the child involved in the swap. HEAPIFY example. 2. 11. 8. 4. 11 ... The right child of A[i] is A[2i 1] ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 14
Provided by: rober52
Category:
Tags: binary | heaps | iis | involved | is | pile

less

Transcript and Presenter's Notes

Title: Pile It On: Binary Heaps


1
Pile It On Binary Heaps
  • Helpful Reading CLR Ch. 7

2
Priority Queue ADT (for now)
  • INSERT(key, data) Insert a key and associated
    data
  • MAXIMUM Return the data associated with the
    largest key in the priority queue
  • EXTRACT-MAX Remove and return the data
    associated with maximum key
  • Also a version of the PQ ADT that supports
    MINIMUM and EXTRACT-MIN

3
Priority Queues
  • Hash tables are not useful for implementing a
    priority queue
  • O(n) to find an arbitrary key in a hash table
    (O(mn) if chaining is used)
  • In particular, it is hard to find minimum or
    maximum key, a fairly common operation
  • PQs have many uses
  • Event-driven simulations
  • Scheduling and optimization algorithms

4
Binary Heaps
  • A binary heap is a good implementation for a
    priority queue
  • A heap is a complete binary tree in which the key
    value contained at some node is always greater
    than the values contained in any of its children

16
16
8
18
11
11
LEGAL
ILLEGAL
4
2
14
2
5
Implementing a Heap
  • Usually a heap is an array that is interpreted as
    a complete binary tree (this is the view taken by
    CLR)
  • First we will discuss the tree view of
    everything, and then show how it can be adapted
    to an array
  • The trees shown will contain keys ONLY -- the
    satellite data is assumed to be contained within
    the node, but not shown
  • Discuss the following operations INSERT,
    MAXIMUM, EXTRACT-MAX

6
INSERT Operation
  • INSERT(key, data)
  • Add a new empty leaf to the tree
  • Starting at the parent of the empty leaf
  • if the key in the node is smaller than the key to
    be inserted, shift that key and its data down
    into the empty leaf, then repeat this process one
    level up, shifting down until right place found
  • when a larger key is encountered, put the new key
    and its data into the empty node and stop
  • Alternate view put the new item at the end and
    swap it up until it is in the right place
  • See CLR p. 151, Figure 7.5

7
INSERT example
16
16
Insert 20
11
8
11
8
4
2
4
2
20
16
20
11
20
11
16
4
2
8
4
2
8
8
EXTRACT-MAX Operation
  • EXTRACT-MAX( )
  • Remember the data in the current root (the data
    is what you will return)
  • Swap the element in the last leaf for the root,
    and then remove the last leaf
  • Call HEAPIFY on the heap to fix it
  • See CLR p. 143, Figure 7.2

16
2
Now call HEAPIFY( ) and return 16s data
11
8
11
8
4
2
4
9
Heap Repair with HEAPIFY
  • HEAPIFY is used on a tree or subtree when the
    children of the root are themselves valid heaps,
    but the root element of the whole tree or subtree
    contains a key smaller than one or both of its
    childrens keys
  • Starting at the root, swap its contents with the
    child containing the larger key, or stop if the
    childrens keys are smaller than the roots
  • Recursively heapify on the child involved in the
    swap

10
HEAPIFY example
2
11
11
8
2
8
4
4
Idea is to let the invalid value settle down
through the heap
11
4
8
2
11
Runtime of Heap Operations
  • Height of tree is Q(lg n) -- tree is always
    complete because of how elements are inserted
  • INSERT is O(lg n) because it may swap up all the
    way to the root
  • HEAPIFY is O(lg n) because it may swap down all
    the way to the root
  • EXTRACT-MAX is O(lg n) because it is just O(1)
    work followed by HEAPIFY
  • MAXIMUM is O(1) since it just returns root

12
Array Implementation of Heaps
  • Can use an array to implement a heap
  • Start at index 1 if your language starts indices
    at 0, leave A0 empty, or else the math fails
    badly
  • The root of a heap is A1
  • The parent of Ai is A floor(i/2)
  • The left child of Ai is A2i
  • The right child of Ai is A2i1
  • Then, all the operations discussed apply, except
    substitute above array index calculations for
    actual tree pointers
  • May reduce constants, but take up space

13
Skip Lists Make Terrific PQs
  • Sorted list, so MAXIMUM is at right end, MINIMUM
    is at left end -- O(1)
  • Easy pointer maintenance when deleting the
    minimum or maximum -- O(1), so EXTRACT-MAX(MIN)
    is O(1)
  • This killer operation is very efficient!
  • INSERT runtime is worrisome (O(n)), but we expect
    with very high probability Q(lg n)
  • We will return to priority queues when we discuss
    graph algorithms
Write a Comment
User Comments (0)
About PowerShow.com