The%20Heap%20ADT - PowerPoint PPT Presentation

About This Presentation
Title:

The%20Heap%20ADT

Description:

Operations on heaps. All heap operations are based on the following idea: ... Traverse the heap from the root to the bottom, or from the bottom to the root ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 13
Provided by: csC5
Learn more at: https://cs.ccsu.edu
Category:
Tags: 20adt | 20heap | heap

less

Transcript and Presenter's Notes

Title: The%20Heap%20ADT


1
The Heap ADT
  • A heap is a complete binary tree where each
    nodes datum is greater than or
  • equal to the data of all of the nodes in the left
    and right subtrees.
  • Example 1

  • 17
  • 15
    10
  • 9 14
    8
    3
  • 4 7 6
    11 5 2 1
  • The relational ordering between data in parent /
    children nodes can be different
  • than gt, for example can be a less than
    ordering.

2
Height of a heap
  • Proposition The height of a heap H storing n
    keys is log(n) 1.
  • Justification The number of internal nodes in a
    complete binary tree is at most
  • 1 2 4 ... 2h-1 2h - 1
  • However, if the lowest level is not
    complete, the number of internal nodes can be as
    little as
  • 1 2 4 ... 2h-2 1 2h-1
  • That is, the number of internal nodes in a
    heap is 2h-1 lt n lt 2h - 1, which implies that
    the height, h, is
  • log(n 1) lt h lt log(n) 1

3
Operations on heaps
  • All heap operations are based on the following
    idea
  • Make a simple structural modification of the heap
    (different for different operations), which may
    result in a violation of the heap condition.
  • Traverse the heap from the root to the bottom, or
    from the bottom to the root (depending on the
    operation) to recover the heap condition
    everywhere.
  • The most important operations on heaps are
  • insertItem (item) Inserts item
  • removeItem () Removes the root
    and returns the datum stored
  • replaceItem (item) Replaces the root
    with item unless this violates the

  • heap condition
  • deleteItem (item) Deletes
    (arbitrary) item

4
Linear representation of a heap
  • The easiest way to represent a complete binary
    tree is by means of a one-
  • dimensional array of size 2h1 - 1, where h is
    the height of the tree. The most
  • attractive property of this representation is
    that given a position of a node, j,
  • positions of its parent and children can be
    easily defined (j / 2 is the position of
  • the parent, j 2 is the position of the left
    child, and j 2 1 is the position of the
  • right child.
  • Our example 1 heap can be represented as follows
  • k 1 2 3 4 5 6 7
    8 9 10 11 12 13 14
  • Ak 17 15 10 9 14 8 3 4
    7 6 11 5 2 1

5
Linear representation of a heap (contd.)
  • The insertItem operation in this representation
    requires the size of the array to
  • be increased by one, and Asize newNode

  • 17
  • 15
    10
  • 9 14
    8
    3
  • 4 7 6
    11 5 2 1
    13
  • k 1 2 3 4 5 6 7
    8 9 10 11 12 13 14
    15
  • Ak 17 15 10 9 14 8 3 4
    7 6 11 5 2 1
    13
  • Now 13 must be walked upheap to fix the heap
    condition. Its parent is in 15/2,
  • i.e. the 7th position then, the parents parent
    is in 7/2 position, etc.

6
The upheap method
  • Algorithm upheap (An, j, precedes)
  • Input array An representing the heap,
  • index of the childs node j,
  • precedes, a comparator object
    defining the ordering relationship
  • Output array An with data originally in j
    walked up until accordingly ordered with its
    parent (according to the precedes function)
  • int l j
  • int key Al
  • int k l/2
  • while (k gt 1) and precedes(Ak, key)
  • Al Ak
  • l k
  • k l/2
  • Al key

7
The insertItem method
  • Algorithm insertItem (H, Item, precedes)
  • Input heap Hsize,
  • item to be inserted
  • precedes, a comparator object
    defining the ordering relationship
  • Output boolean variable success set to
    true if insertItem is successful
  • if H.full()
  • success false
  • else
  • success true
  • size size 1
  • Hsize Item
  • upheap(Hsize, size, precedes)
  • return success
  • The efficiency if insertItem operation is O(log
    n), where n is the number of tree
  • nodes.

8
Linear representation of a heap (contd.)
  • The removeItem operation decreases the size of
    the array by one and makes
  • A1 Asize

  • 1
  • 15
    10
  • 9 14
    8
    3
  • 4 7 6
    11 5 2
  • k 1 2 3 4 5 6 7
    8 9 10 11 12 13 14
  • Ak 17 15 10 9 14 8 3 4
    7 6 11 5 2 1
  • Now 1 must be walked downheap to fix the heap
    condition. Its children are in
  • positions 12 and 12 1. Compare to the larger
    of the two children and if this
  • child is greater, exchange the two. Continue
    until the heap condition is fixed.

9
The downheap method
  • Algorithm downheap (An, j, precedes)
  • Input array An representing the heap,
  • index of the parents node j,
  • precedes, a comparator object
    defining the ordering relationship
  • Output array An with data originally in j
    walked down until accordingly ordered w.r.t both
    of its children (according to the precedes
    function)
  • boolean foundSpot false
  • int l j
  • int key Al
  • int k 2 l // get the left child
    first
  • while (k lt n) and (! foundSpot)
  • if (k lt n) and (! precedes (Ak1,
    Ak)
  • k k 1
  • if (! precedes (Ak, key))
  • Al Ak, l k, k
    2 l
  • else foundSpot true //
    end while
  • Al key

10
The removeItem method
  • Algorithm removeItem (H, precedes)
  • Input heap Hsize,
  • precedes, a comparator object
    defining the ordering relationship
  • Output boolean variable success set to
    true if removeItem is successful
  • if H.empty()
  • success false
  • else
  • success true
  • item H1
  • H1 Hsize
  • size size - 1
  • downheap(Hsize, 1, precedes)
  • return success
  • The efficiency of removeItem operation is O(log
    n), where n is the number of
  • tree nodes.

11
Linear representation of a heap (contd.)
  • The replaceItem operation does not change the
    size of the array but replaces
  • the root node with a new one, and recovers the
    heap condition, if necessary.

  • 12
  • 15
    10
  • 9 14
    8
    3
  • 4 7 6
    11 5 2 1
  • k 1 2 3 4 5 6 7
    8 9 10 11 12 13 14
  • Ak 12 15 10 9 14 8 3 4
    7 6 11 5 2 1
  • Now 12 must be walked downheap to fix the heap
    condition.

12
Linear representation of a heap (contd.)
  • The deleteItem operation decreases the size of
    the array by one and makes
  • Aitem Asize the heap condition must be
    fixed after that.
  • Assume we want to remove 15.

  • 17
  • 1
    10
  • 9 14
    8
    3
  • 4 7 6
    11 5 2
  • k 1 2 3 4 5 6 7
    8 9 10 11 12 13
  • Ak 17 1 10 9 14 8 3 4
    7 6 11 5 2
  • Now 1 must be walked downheap to fix the heap
    condition.
Write a Comment
User Comments (0)
About PowerShow.com