Heaps - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Heaps

Description:

NOT the same as a 'heap' of memory available to programs same word, ... Heaps. To expand a heap, create a new array and copy the old to the new. Efficiency ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 8
Provided by: UST
Category:
Tags: heaps

less

Transcript and Presenter's Notes

Title: Heaps


1
Heaps
  • Joe Komar

2
Heaps Overview
  • Used to implement priority queues (an Abstract
    Data Type, ADT)
  • Heap is a kind of tree offering insertion and
    deletion in O(logN) time
  • NOT the same as a heap of memory available to
    programs same word, different usage
  • Heap is a binary tree with
  • Each node satisfies the heap condition its
    key is larger than or equal to its children
  • Usually implemented as an array
  • It is complete in the sense that there are no
    holes in the array if there are N items, every
    cell from 0 to N-1 has something in it

3
Heaps
  • Weakly ordered along every path from the root
    to a leaf the nodes are arranged in descending
    order
  • Difficult to traverse the heap in key order
  • Does not allow convenient searching for a
    specified key and, therefore, convenient deletion
    of items
  • Removal of node with maximum key
  • Remove the root (always the max)
  • Move the last node into the root
  • Trickle the last node down until its below a
    larger node and above a smaller node

4
Heaps
  • Insertion of a node
  • Place it in the first open position at the end of
    the array
  • Trickle the node up until in its proper position
    (key smaller than parent and larger than child)
  • trickle using copies rather than swaps
  • Change can change the priority of an item and
    then trickle it up or down appropriately
  • Remember for any node X
  • Its parent is (x-1)/2
  • Its left child is 2x1
  • Its right child is 2x2
  • (see example applet)
  • (see example code)

5
Heaps
  • To expand a heap, create a new array and copy the
    old to the new
  • Efficiency
  • Most time consuming is trickle up or trickle down
    and depends on the height of the heap (number of
    levels)
  • O(logN) why??

6
Heapsort
  • Insert the ordered items into a heap using the
    normal insert algorithm of the heap
  • Repeated calls to the remove routine will return
    them in sorted order
  • Does this in O(NlogN) time, the same as
    quicksort, but not as sensitive to the initial
    arrangement of the data
  • Can ignore all the nodes at the bottom because
    they have no children to be out of order
  • Therefore start with N/2-1 index value and
    trickle down from there on up
  • Can also use only one array and reorder that
    array in place (rather than using two arrays)
  • (see example code)

7
Summary
  • A priority queue is an Abstract Data Type (ADT)
    that can be implemented in a number of ways
  • A Heap is an efficient implementation of a
    priority queue
  • Insertion and deletion of the largest item is in
    O(logN) time
  • Heaps do not support ordered transversal
  • Each node has a key less than its parent and
    greater than its children
  • Trickle up and trickle down algorithms are used
    to insert and remove items respectively
  • Heapsort is an efficient sorting procedure
Write a Comment
User Comments (0)
About PowerShow.com