Title: Binary Heaps
1Binary Heaps
- What is a Binary Heap?
- Array representation of a Binary Heap
- MinHeap implementation
- Operations on MinHeap
- Insert
- Delete
- Converting an array into a binary heap
- Heap Applications
- Heap Sort
- Heap as a priority queue
2What is a Binary Heap?
- A binary heap is a complete binary tree with one
or both of the following heap order properties - MinHeap property Each node must have a key that
is less or equal to the key of each of its
children. - MaxHeap property Each node must have a key that
is greater or equal to the key of each of its
children. - A binary heap satisfying the MinHeap property is
called a MinHeap. - A binary heap satisfying the MaxHeap property is
called a MaxHeap. - A binary heap with all keys equal is both a
MinHeap and a MaxHeap. - Recall A complete binary tree may have missing
nodes only on the right side of the lowest level.
All levels except the bottom one must be fully
populated with nodes
All missing nodes, if any, must be on the right
side of the lowest level
3MinHeap and non-MinHeap examples
Violates MinHeap property 21gt6
Not a Heap
4MaxHeap and non-MaxHeap examples
Violates MaxHeap property 65 lt 67
A MaxHeap
Violates heap structural property
Violates heap structural property
5Array Representation of a Binary Heap
- A heap is a dynamic data structure that is
represented and manipulated more efficiently
using an array. - Since a heap is a complete binary tree, its node
values can be stored in an array, without any
gaps, in a breadth-first order, where - Value(node i1) array i , for i gt 0
- The root is array0
- The parent of arrayi is array(i 1)/2, where
i gt 0 - The left child, if any, of arrayi is
array2i1. - The right child, if any, of arrayi is
array2i2. -
6Array Representation of a Binary Heap (contd.)
- We shall use an implementation in which the heap
elements are stored in an array starting at index
1. - Value(node i ) arrayi , for i gt 1
- The root is array1.
- The parent of arrayi is arrayi/2, where i gt 1
- The left child, if any, of arrayi is array2i.
- The right child, if any, of arrayi is
array2i1. -
7MinHeap Implementation
- The class hierarchy for BinaryHeap is
- The PriorityQueue interface is
- 1 public interface PriorityQueue extends
Container - 2
- 3 public abstract void enqueue(MyComparable
comparable) - 4 public abstract MyComparable findMin()
- 5 public abstract MyComparable dequeueMin()
- 6
8MinHeap Implementation (contd.)
- 1 public class BinaryHeap extends
AbstractContainer implements PriorityQueue - 2
- 3 protected MyComparable array
- 4 // count is inherited from
AbstractContainer - public BinaryHeap(int size)
-
- 7 array new MyComparablesize 1
// array0 is not used - 8
- 9
- 10 public BinaryHeap(MyComparable
comparable) - 11
- 12 this(comparable.length)
- 13
- 14 for(int i 0 i lt
comparable.length i) - 15 arrayi 1 comparablei
- 16 count comparable.length
- 17 buildHeapBottomUp()
- 18
- 19 // . . .
9MinHeap Insertion
- The pseudo code algorithm for inserting in a
MinHeap is - 1 minHeapInsert(e1)
- 2
- 3 if(Heap is full) throw an exception
- 4 insert e1 at the end of the heap
- 5 while(e1 is not in the root node and e1 lt
parent(e1)) - 6 swap(e1 , parent(e1))
-
- The process of swapping an element with its
parent, in order to restore the heap order
property after an insertion is called percolate
up, sift up, or reheapification upward. - Thus, the steps for insertion are
- Insert at the end of the heap.
- As long as the heap order property is violated,
percolate up.
10Percolate-Up Via a Hole
- Instead of repeated swappings, it is more
efficient to use a hole (i.e., an empty node) to
percolate up - To insert an element e, insert a hole at the end
of the heap. - While the heap order property is violated,
percolate the hole up. - Insert e in the hole.
p
p lt e
p
e
e
p gt e
hole
e
p
11MinHeap Insertion Example
Percolate up
12MinHeap Insertion implementation
- public void enqueue(MyComparable comparable)
-
- 3 if(isFull()) throw new
ContainerFullException() - 4 // percolate up via a hole
- 5 int hole count
- while(hole gt 1 arrayhole /
2.isGT(comparable)) -
- 8 arrayhole arrayhole / 2
- 9 hole hole / 2
- 10
- 11 arrayhole comparable
- 12
- public boolean isFull()
-
- 3 return count array.length - 1
- 4
13MinHeap deletion
- The pseudo code algorithm for deleting from a
MinHeap is - 1 minHeapDelete()
- 2 if(Heap is empty) throw an exception
- 3 extract the element from the root
- 4 if(root is a leaf node) delete root
return - 5 copy the element from the last leaf to
the root - 6 delete last leaf
- 7 p root
- 8 while(p is not a leaf node and p gt any
of its children) - 9 swap p with the smaller child
- 10 return
-
- The process of swapping an element with its
smaller child, in order to restore the heap
order property after a deletion is called
percolate down, sift down, or reheapification
downward. - Thus, the steps for deletion are
- Replace the value at the root by the last leaf
value. - Delete the last leaf.
- As long as the heap order property is violated,
percolate down.
14Percolate-Down Via a Hole
- Instead of repeated swappings, it is more
efficient to use a hole (i.e., an empty node) to
percolate down - A hole is created in the root by deleting the
root element. - Copy the value in the last node in a temporary
variable. - Delete the last node.
- While the heap order property is violated,
percolate the hole down. - Insert the value from the temporary variable into
the hole.
e lt min(l , r)
e gt l gt r
l
r
r
e
e gt l lt r
l
l
r
l
r
15MinHeap Deletion Example
delete last node
Percolate down
16MinHeap Deletion Implementation
- 1 public MyComparable dequeueMin()
- 2 if(isEmpty()) throw new
ContainerEmptyException() - 3 MyComparable minItem array1
- 4 array1 arraycount
- 5 count--
- 6 percolateDown(1)
- 7 return minItem
-
- 1 private void percolateDown(int hole)
- 2 int child MyComparable temp
arrayhole - 3 while(hole 2 lt count)
- 4 child hole 2
- 5 if(child 1 lt count arraychild
1.isLT(arraychild) ) - 6 child
- if(arraychild.isLT(temp))
- arrayhole arraychild
- else
- break
17Converting an array into a Binary heap
- The algorithm to convert an array into a binary
heap is - Start at the level containing the last non-leaf
node (i.e., arrayn/2, where n is the array
size). - Make the subtree rooted at the last non-leaf node
into a heap by invoking percolateDown. - Move in the current level from right to left,
making each subtree, rooted at each encountered
node, into a heap by invoking percolateDown. - If the levels are not finished, move to a lower
level then go to step 3. - Stop.
- The above algorithm can be refined to the
following method of the BinaryHeap class - private void buildHeapBottomUp()
-
- for(int icount/2 igt1 i--)
-
- 5 // restore heap property for subtree
rooted at arrayi - 6 percolateDown(i)
- 7
- 8
18Converting an array into a MinHeap (Example)
At each stage convert the highlighted tree into a
MinHeap by percolating down starting at the root
of the highlighted tree.
19Heap Application Heap Sort
- A MinHeap or a MaxHeap can be used to implement
an efficient sorting algorithm called Heap Sort. - The following algorithm uses a MinHeap
- 1 public static void heapSort(MyComparable
array) - 2
- 3 BinaryHeap heap new
BinaryHeap(array) - 4 for(int i 0 i lt array.length
i) - 5 arrayi heap.dequeueMin()
- 6
- Because the dequeueMin algorithm is O(log n),
heapSort is an O(n log n) algorithm. - Apart from needing the extra storage for the
heap, heapSort is among efficient sorting
algorithms.
20Heap Applications Priority Queue
- A heap can be used as the underlying
implementation of a priority queue. - A priority queue is a data structure in which the
items to be inserted have associated priorities. - Items are withdrawn from a priority queue in
order of their priorities, starting with the
highest priority item first. - Priority queues are often used in resource
management, simulations, and in the
implementation of some algorithms (e.g., some
graph algorithms, some backtracking algorithms). - Several data structures can be used to implement
priority queues. Below is a comparison of some
21Priority Queue (Contd.)
- 1 priorityQueueEnque(e1)
- 2
- 3 if(priorityQueue is full) throw an
exception - 4 insert e1 at the end of the
priorityQueue - 5 while(e1 is not in the root node and e1 lt
parent(e1)) - 6 swap(e1 , parent(e1))
- 7
- 1 priorityQueueDequeue()
- 2 if(priorityQueue is empty) throw an
exception - 3 extract the highest priority element
from the root - 4 if(root is a leaf node) delete root
return - 5 copy the element from the last leaf to
the root - 6 delete last leaf
- 7 p root
- 8 while(p is not a leaf node and p gt any
of its children)
X
Heap
Heap
X is the element with highest priority