Heaps - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Heaps

Description:

Heaps. What is a Heap? A heap is another specialized form of a binary tree. In a heap, every descendant of a node is smaller than that node (maxheap) or ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 8
Provided by: cseBu
Category:
Tags: heap | heaps

less

Transcript and Presenter's Notes

Title: Heaps


1
Heaps
2
What is a Heap?
  • A heap is another specialized form of a binary
    tree.
  • In a heap, every descendant of a node is smaller
    than that node (maxheap) or larger than that node
    (minheap).
  • Since this is also a data structure based on
    comparisons, objects in the heap must also be
    Comparable.

3
Heap Operations
  • public void add (Comparable c)
  • public Comparable removeMax()
  • public Comparable getMax()
  • public boolean isEmpty()
  • public int getSize()
  • public void clear()

4
This is not The Heap
  • Unlike the stack, The Heap where java objects
    are stored is not an instance of this ADT.

5
Array Based Implementation
  • Although a heap is a special instance of a binary
    tree, it has the added benefit of also being a
    complete binary tree.
  • This means that we can store the tree in an array
    in a most convenient fashion.

6
Calculating Hierarchy
  • Since the tree is complete, we can locate either
    the children or the parent of any node by using a
    simple computation on the nodes number.
  • The children of a node (if any) are located at 2i
    and 2i1.
  • The parent of a node (excluding the root, of
    course) is at i/2.

7
Basic Concepts
  • To add to the heap, add at the last index and
    bubble up
  • To remove from the heap, swap the last index and
    the first, and bubble down.
Write a Comment
User Comments (0)
About PowerShow.com