Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
(CS 210)
Asma Sattar
asma.sattar@nu.edu.pk
Today’s Lecture
Binary Heap
Quiz (15 min)
4/27/2018
Complete Binary Tree
4/27/2018
Avl tree is not always complete tree
4/27/2018
What is a Heap?
A heap is a binary tree that satisfies these
special SHAPE and ORDER properties:
4/27/2018
Same shape if
same number
of elements in
both tree
4/27/2018
Are these Both Heaps?
treePtr
C 50
A T 20 30
18 10
4/27/2018
Is this a Heap?
tree
70
60 12
40 30 8 10
4/27/2018
Where is the Largest Element in a Heap
Always Found?
tree
70
60 12
40 30 8
4/27/2018
We Can Number the Nodes Left to Right
by Level This Way
tree
70
60 12
1 2
40 30 8
3 4 5
4/27/2018
And use the Numbers as Array Indexes
to Store the Trees
tree.nodes
[0] tree
70
[1] 60 70
0
[2] 12
60 12
[3] 40
1 2
[4] 30 40 30 8
[5] 3 4 5
8
[6]
Why Array?? 4/27/2018
And use the Numbers as Array Indexes to
Store the Trees
4/27/2018
With Array Representation
right child is in
tree.nodes[index*2 + 2]
its parent is in
tree.nodes[(index – 1)/2]
Leaf nodes:
tree.nodes[numElements/2] to
tree.nodes[numElements - 1] 4/27/2018
// HEAP SPECIFICATION
// Assumes ItemType is either a built-in simple data
// type or a class with overloaded relational operators.
4/27/2018
Insertion in heap
4/27/2018
Max heap
Insert
9, 6, 7, 4, 12, 15, 20
4/27/2018
Adding new element in Heap
4/27/2018
ReheapUp
bottom
4/27/2018
4/27/2018
ReheapUp
4/27/2018
ReheapUp
// IMPLEMENTATION continued
// Pre: bottom is the index of the node that may violate the heap
// order property. The order property is satisfied from root to
// next-to-last node.
// Post: Heap order property is restored between root and bottom
{
int parent ;
4/27/2018
Contd…
4/27/2018
Removing Element From HEAP
4/27/2018
Contd…
4/27/2018
ReheapDown
bottom
4/27/2018
Contd…
4/27/2018
ReheapDown
4/27/2018
ReheapDown
// IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS
// Pre: root is the index of the node that may violate the
// heap order property
// Post: Heap order property is restored between root and bottom
{
int maxChild ;
int rightChild ;
int leftChild ;
leftChild = root * 2 + 1 ;
rightChild = root * 2 + 2 ;
4/27/2018
ReheapDown (cont)
if ( leftChild <= bottom ) // ReheapDown continued
{
if ( leftChild == bottom )
maxChild = leftChld;
else
{
if (elements [ leftChild ] <= elements [ rightChild ] )
maxChild = rightChild;
else
maxChild = leftChild;
}
if ( elements [ root ] < elements [ maxChild ] )
{
Swap ( elements [ root ] , elements [ maxChild ] );
ReheapDown ( maxChild, bottom ) ;
}
}
}
4/27/2018
O(logN)
Contd…
This algorithm is recursive. In the general case, we
swap the value in the root node with its larger child,
and then repeat the process.
On the recursive call, we specify maxChild as the
root of the heap; this shrinks the size of the tree still
to be processed, satisfying the smaller-caller
question.
4/27/2018
4/27/2018
Contd…
Two base cases exist:
1.if heap.elements[root] is a leaf
2.if the heap order property is already satisfied.
4/27/2018
Contd…
If the calculated position of the left child is
greater than bottom, then it is a leaf node.
The node with F is the first leaf node. The index of
the node with F is 5
So its left child would be in index position 11 if it
exists.
Because 11 is greater than 9 (bottom), F’s left
child is not in the heap, so F is a leaf node.
4/27/2018
Delete a specific element from heap
Replace that element with the last element
Delete last element
Heap will remain complete (shape property satisfied)
Apply Re-heap down or Re-heap up on the node depending
upon situation (discuss)
4/27/2018
Real-life Priority Queue
Priority Queue
A priority queue is an ADT with the
property that only the highest-priority
element can be accessed at any time.
Queue
Enque an item
Item returned has been in the queue
the longest amount of time.
Priority Queue
Enque a pair <item, priority>
Item returned has the highest priority.
ADT Priority Queue Operations
Transformers
MakeEmpty change state
Enqueue
Dequeue
Observers
observe state
IsEmpty
IsFull
Different Implementations of Priority
Queue
An unsorted List
dequeuing would require searching through the entire list
An Array-Based Sorted List
Enqueuing is expensive
A Sorted linked List
Enqueuing again is 0(N)
A Binary Search Tree
On average, 0(log2N) steps for both enqueue and dequeue but
in worst case O(N)
A Heap
guarantees 0(log2N) steps, even in the worst case
Class PQType Declaration
template<class ItemType>
class PQType
{
public:
PQType(int);
~PQType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType newItem);
void Dequeue(ItemType& item);
private:
int length;
ItemType elements;
int maxItems;
};
Class PQType Function Definitions
template<class ItemType>
PQType<ItemType>::PQType(int max)
{
maxItems = max;
elements = new ItemType[max];
length = 0;
}
template<class ItemType>
void PQType<ItemType>::MakeEmpty()
{
length = 0;
}
template<class ItemType>
PQType<ItemType>::~PQType()
{
delete [] elements;
}
Class PQType Function Definitions
Enqueue
Increment length
Put newItem in next available position
elements.ReheapUp(0, length-1)
Dequeue
Set item to root element from queue
Move last leaf element into root position
Decrement length
elements.ReheapDown(0, length-1)
Enqueue
Code for Enqueue
template<class ItemType>
void PQType<ItemType>::Enqueue(ItemType newItem)
{
if (length == maxItems)
//Overflow
else
{
length++;
elements[length-1] = newItem;
ReheapUp(0, length-1);
}
}
Dequeue
Dequeue (cont.)
c) ReheapDown
Code for Dequeue
template<class ItemType>
void PQType<ItemType>::Dequeue(ItemType& item)
{
if (length == 0)
//underflow
else
{
item = elements[0];
elements[0] = elements[length-1];
length--;
ReheapDown(0, length-1);
}
}
Comparison of Priority Queue
Implementations
Enqueue Dequeue
4/27/2018
Home task
Insertion : 8, 3, 15, 20, 4, 70, 2, 100
Dequeue 3 elements according to priority
1st element:
2nd element:
3rd element:
4/27/2018
Quiz
Question no 1:
Complete the C/C++ code for the deletion of node in binary search tree that has
two child nodes and pointer is given to node that need to be deleted.
For example 7 to be deleted.
Note: Only write the code for deletion of specific node.
nodePtr
makeDeletion(TreeNode *&nodePtr)
{ }
Question no 2:
Build an AVL tree with the following values:
15, 20, 24, 10, 13, 7, 30, 36, 25
4/27/2018
Reading References
4/27/2018