Heaps - PowerPoint PPT Presentation

About This Presentation
Title:

Heaps

Description:

parent = (index - 1) / 2 // must be integer division ... the new root with both of its children. if either of the nodes children are larger, swap it with the ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 14
Provided by: matt51
Category:
Tags: children | division | heaps | of

less

Transcript and Presenter's Notes

Title: Heaps


1
Heaps
  • CS 367 Introduction to Data Structures

2
Heap
  • A heap is a tree that satisfies the following
    conditions
  • largest element in tree is located at the root
  • each node has a larger value than its children
  • tree is balanced and the leaves on the last level
    are all as far left as possible

3
Valid Heaps
root
root
Z
Z
X
M
X
M
T
N
J
L
T
N
4
Invalid Heaps
root
root
X
X
Z
M
Z
M
T
N
J
L
T
J
Level 2 has a value higher than level 1
Nodes not all to left
5
Heap Applications
  • Great for priority queues
  • highest priority item always at the root
  • just pop it off and re-sort the heap
  • Can be used to sort data (called heap sort)
  • put the data in a heap
  • remove the root and re-sort
  • keep repeating this until the heap is empty
  • data was all removed in descending order

6
Implementing Heaps
  • An elegant implementation is to use an array
  • Re-consider the breadth first search
  • dequeue a node from the head of the queue
  • enqueue the nodes children in queue
  • What if the node wasnt actually removed?
  • instead, its children were just enqueued

7
Implementing Heaps
Z
1
3
4
6
0
2
5
X
M
queue
Z
X
M
T
N
J
L
T
N
J
L
8
Implementing Heaps
  • The previous example shows that a tree can be
    represented by an array
  • the children of a node can be found with the
    following equations
  • child1 index 2 1
  • child2 index 2 2
  • the parent of a node can be found with the
    following equation
  • parent (index - 1) / 2 // must be integer
    division
  • this only works if the tree is balanced and all
    nodes are as far left as possible

9
Implementing Heaps
  • Basic class for a heap
  • class Heap
  • Object heap
  • int end
  • public Heap(int size)
  • heap new Objectsize
  • end 0
  • public boolean insert(Object data)
  • public Object delete()

10
Inserting into Heap
  • Place new node at very end of the array
  • the index indicated by the end variable
  • Compare the added node with its parent
  • if it is greater than the parent, swap the two
    elements
  • repeat this process until it is no longer greater
    than its parent or the root is reached
  • This algorithm percolates an inserted node toward
    the root as long as it is larger than its parent

11
Inserting into Heap
  • private boolean insert(Object data)
  • if(end heap.length) return false
  • heapend data
  • int pos end
  • int parent (pos 1) / 2
  • while((pos ! 0) (heappos gt heapparent)
  • swap(pos, parent)
  • pos parent
  • parent (pos 1) / 2
  • end
  • return true

12
Deleting from Heap
  • Remove the first element in the array
  • this is the root of the tree highest value
  • Move the last element in the array into the root
    position
  • Compare the new root with both of its children
  • if either of the nodes children are larger, swap
    it with the largest one
  • repeat this procedure until the node is larger
    than both of its children or it has become a leaf
  • Now the node trickles down the tree as long as
    either of its children are larger than it

13
Deleting from Heap
  • public Object delete()
  • if(end 0) return null
  • Object data heap0
  • end--
  • if(end 0) return
  • int pos 0 int child 1
  • heappos heapend
  • while((child lt end-1)
  • ((heappos lt heapchild) (heappos lt
    heapchild1)))
  • if(heapchild lt heapchild 1) child
  • swap(pos, child)
  • pos child
  • child pos 2 1
  • if((child end 1) (heappos lt
    heapchild))
  • swap(pos, child)
Write a Comment
User Comments (0)
About PowerShow.com