0% found this document useful (0 votes)
8 views29 pages

09-303 Heaps

Uploaded by

yashabiba23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views29 pages

09-303 Heaps

Uploaded by

yashabiba23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Data Structures

CMPS 303
Dr. Saeed Salem

Unit 9
Priority Queues & Heap
Some slide are adopted from slides by Dr. Saleh Alhazbi and Dr. Osama Halabi
© 2011 The McGraw-Hill Companies, Inc. Copyright © 2004 Pearson Education, Inc.
All rights reserved Publishing as Pearson Addison-Wesley
Why Priority Queues?
First-in first-out queue is not suitable for some applications.
For example
1- scheduling patients for treatment in the ER:
Patients should be scheduled based on the level of emergency. A gunshot victim
should be treated sooner than a guy with a cold, regardless of arrival time.
2- Printing jobs:
assume the printer should print jobs based on the importance of the employees in
the company (managers before employees).

So we need another type of queue where every entity must have a priority
factor to be used when queueing these entities. This type of queues is
called priority queue.

Data Structures CMPS 303 2


Priority Queue ADT
It is an abstract data type similar to regular queue data structure which stores collections
of elements where each element has a "priority" associated with it. In a priority queue, an
element with high priority is served before an element with low priority.
✤ A priority queue stores a collection of entries
✤ Each entry is a pair (key, value)

✤ Main methods of the Priority Queue ADT

insert(k, d): inserts an entry with key k and data d.


removeMax(): removes and return the entry with the max. key (highest
priority), it returns null if the priority queue is empty.
max(): returns, but does not remove, the entry with max. key (highest priority),
or null if the priority queue is empty.
size(): returns how many entries in the priority queue.
isEmpty(): returns true if the priority queue is empty, and false if it is not empty.
Data Structures CMPS 303 3
Example
A sequence of priority queue methods:

Method Returned Priority Queue


value contents
insert(5,A) { (5,A) }
insert(9,C) { (9,C), (5,A) }
insert(3,B) { (9,C), (5,A),(3,B }
max( ) (9,C) { (9,C), (5,A),(3,B }
removeMax( ) (9,C) {(5,A),(3,B }
insert(7,D) {(7,D),(5,A),(3,B }
removeMax( ) (7,D) {(5,A),(3,B }
removeMax( ) (5,A) {(3,B }
removeMax( ) (3,B) {}
removeMax( ) null {}
isEmpty() True {}

Data Structures CMPS 303 4


H1

Priority Queue
Implementation
Priority Queue Implementation
1- Using unsorted array
Array will hold queue entries (each entry has a priority).

7 4 9 2 8 3
The complexity of adding an element is O(1)

The complexity of removing an element is O(n)

because we have to search for the entry with max value to be removed, and
when removing , all elements after that one should be shifted.

Data Structures CMPS 303 6


Priority Queue Implementation
2- Using sorted list

9 8 5 3 2

The complexity of adding a new element is O(n)

because we need to compare in order to find the suitable place to


insert the elements, then shift all other elements

The complexity of removing an element is O(n) because we need also to shift


the elements.

Data Structures CMPS 303 7


Priority Queue Implementation
3- Using sorted linked list

The complexity of adding a new element is O(n), because


we need to compare in order to find the suitable place to
insert the elements
The complexity of removing an element is O(1) because it is the one
with max priority
4- Using heap to implement the priority queue
Data Structures CMPS 303 8
H2
4- Using Heap to
implement Priority
Queue
Heap
A Heap is a binary tree with the following characteristics:

1. It is a complete tree, means all


levels before the last one are
full. Although the last level
need not to be full, it should
be filled from left to right.

2. Each node satisfies the


heap condition, every
node’s key is greater than
or equal to the keys of the
children.
Data Structures CMPS 303 10
✤ Usually, heap is implemented as
an array based on the rules
(illustrated in unit 8)
✤ The root as index 0
✤ Left child of a node a i is in 2*i+1
✤ Right child of a node at i is in 2*i+2
✤ Parent of a node at i is in (i-1)/2

Data Structures CMPS 303


Insert to a heap
✤ The new node is always inserted next to the
right most node at the bottom level, or at
the left most position of a new level if the
bottom level is already full

But this might violate the heap property (parent’s key should be greater than or
equal its children’s keys), so we need to restore this property (upheap process)

Data Structures CMPS 303 12


Remove from a heap
✤ In heap, we always remove the root (it has the max key). So method removeMax of
the priority queue ADT corresponds to the removal of the root key from the heap

✤ The removal algorithm consists of three steps


✦ Replace the root key with the key of the last node w
✦ Remove w
✦ Restore the heap-order property (downheap)

Algorithm downheap restores the heap-order property by swapping key k along a downward
path from the root.
Data Structures CMPS 303 13
Efficiency of inserting and removing from heap

✤ In inserting, we need to use upheap algorithm to restore the heap


property by swapping k along an upward path from the insertion node.
✤ Upheap terminates when the key k reaches the root or a node whose
parent has a key greater than or equal to k.
✤ In removing, we use downheap, it terminates when key k reaches a leaf
or a node whose children have keys less than or equal to k.
✤ Since heap is a binary tree, and its height log (n+1) where n is number of
nodes. The complexity of upheap and downheap operation is O(log n).

Data Structures CMPS 303 14


H1

Heap Implementation
Heap Implementation

✤ The root as index 0


✤ Left child of a node a i is in 2*i+1
✤ Right child of a node at i is in 2*i+2
✤ Parent of a node at i is in (i-1)/2
Data Structures CMPS 303 16
public class Node <E>{
int key;
E data;
public Node(int k, E d)
{
key=k;
data=d;
}
public void display()
{
System.out.print("Key:"+key);
System.out.println(data);
}

Data Structures CMPS 303 17


public class Heap <E>{
private Node<E> heapArray[];
private int size;
public Heap(int mx)
{
size=0;
heapArray=new Node[mx];
}
public boolean isEmpty()
{
return size==0;
}
public int size()
{
return size;
}

Data Structures CMPS 303 18


private void upheap(int i)
{
int parent=(i-1)/2;
Node<E> bottom=heapArray[i];
while (i>0 && heapArray[parent].key<bottom.key)
{
heapArray[i]=heapArray[parent];
i=parent;
parent=(parent-1)/2;
}
heapArray[i]=bottom;
}
public void insert(int k, E d)throws IllegalStateException
{
if (size( ) == heapArray.length) throw new IllegalStateException("Heap is full ");
Node<E> newNode=new Node<>(k,d);
heapArray[size]=newNode;
upheap(size);
size++;
}
Data Structures CMPS 303 19
private boolean hasLeftChild(int i)
{
if ((2*i+1)<size)
return true;
else
return false;
}

private boolean hasRightChild(int i)


{
if ((2*i+2)<size)
return true;
else
return false;
}

Data Structures CMPS 303 20


public void downHeap(int i)
{
int largerChild;
Node<E> top=heapArray[i];
while (hasLeftChild(i)) // node has at least one child
{
int leftChild=2*i+1;
largerChild=leftChild;
if (hasRightChild(i))
{
int rightChild=2*i+2;
if (heapArray[rightChild].key>heapArray[leftChild].key)
largerChild=rightChild; //right child is smaller
}
if (top.key > heapArray[largerChild].k)
break;
heapArray[i]=heapArray[largerChild];
i=largerChild;
}
heapArray[i]=top;
}
Data Structures CMPS 303 21
public Node<E> removeMax()
{
if (isEmpty())
return null;
Node<E> tmp=heapArray[0];
heapArray[0]=heapArray[--size];
heapArray[size]=null;
downHeap(0);
return tmp;
}

public Node<E> max()


{
if (isEmpty())
return null;
return heapArray[0];
}
Data Structures CMPS 303 22
Efficiency of different Priority Queue Implementations

Unsorted Sorted Sorted Heap


array array linked list

Insert O(1) O(n) O(n) Log(n)

removeMax O(n) O(n) O(1) Log(n)

Data Structures CMPS 303 23


Exercise
Construct a heap for these values. The values were received
in this order: 5, 19, 10, 7, 17, 16

15 15, 19 15, 19, 10, 7, 17 15, 19, 10, 7, 17, 16


15, 19, 10 15, 19, 10, 7

19 19 19 19
15 19

15 15 10 15 10 17 10 17 16

7 7 15 7 15 10

Data Structures CMPS 303


Heap Sort
We can use heap structure to sort elements of an array
or array list.
Step 1: insert the Step 2: Remove the elements from
elements to the heap. the heap and insert them back to the
array or the array list.

Data Structures CMPS 303 25


Heap Sort: Another way
Note, we can also change our heap such that the
parent is less than or equal to the children.
Step 1: insert the Step 2: Remove the elements from
elements to the heap. the heap and insert them back to the
array or the array list.

Data Structures CMPS 303 26


Heap Sort Complexity
✤ Complexity of step 1:
✦ Complexity of insertion to a heap is O(log n)
✦ Inserting all elements of the array to the heap is O(n
log n)

✤ Complexity of step 2:
✦ Complexity of removing min. from the heap is O(log n)
✦ Therefore removing all elements will be O(n log n)

Therefore, the complexity of heap sort is O(n log n).


Data Structures CMPS 303 27
Heap Sort Implementation

public void heapSort(int a[])


{
Heap h=new Heap(a.length);
for (int i=0;i<a.length;i++)
h.insert(a[i], null);

for (int i=0;i<a.length;i++)


a[i]=h.removeMax().key;
}

Data Structures CMPS 303 28


END OF

End of
Heaps

You might also like