Sei sulla pagina 1di 53

Data Structures and Algorithms

(CS 210)

Asma Sattar
asma.sattar@nu.edu.pk
Today’s Lecture

Binary Heap
Quiz (15 min)

4/27/2018
Complete Binary Tree

(1) A binary tree that is either full or full through


the next-to-last level

(2) The last level is full from left to right (i.e.,


leaves are as far to the left as possible)

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:

Its shape must be a complete binary tree.

For each node in the heap, the value stored in that


node is greater than or equal to the value in each of
its children.

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

For any node tree.nodes[index]


its left child is in
tree.nodes[index*2 + 1]

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.

template< class ItemType >


struct HeapType
{
void ReheapDown ( int root , int bottom ) ;
void ReheapUp ( int root, int bottom ) ;

ItemType* elements; //ARRAY to be allocated dynamically


int numElements ;
};

4/27/2018
Insertion in heap

Add a new element to the end of an array;


Sift up the new element, while heap property
is broken. Shifting is done as following:
compare node's value with parent's value. If they are
in wrong order, swap them.

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

template< class ItemType >


void HeapType<ItemType>::ReheapUp ( int root, int bottom )

// 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 ;

if ( bottom > root )


{
parent = ( bottom - 1 ) / 2;
if ( elements [ parent ] < elements [ bottom ] )
{
Swap ( elements [ parent ], elements [ bottom ] );
ReheapUp ( root, parent );
}
}
} O(logN)4/27/2018
Contd…
This algorithm is recursive.
In the general case, we swap the (current) “bottom
"node with its parent and reinvoke the function.
Two base cases exist:
1.if we have reached the root node
2.if the heap order property is satisfied.
In either case we exit the function without doing
anything.
How do we find the parent node?

4/27/2018
Contd…

This task is not an easy one in a binary tree linked


together with pointers from parent to child nodes.
 As we saw earlier, however, it is very simple in our
implicit link implementation:
parent = (index - 1) / 2;

4/27/2018
Removing Element From HEAP

4/27/2018
Contd…

Bottom Right most Element.

4/27/2018
ReheapDown

bottom

4/27/2018
Contd…

4/27/2018
ReheapDown

4/27/2018
ReheapDown
// IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS

template< class ItemType >


void HeapType<ItemType>::ReheapDown ( int root, int bottom )

// 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.

In either of these cases, we do nothing.


How can we tell if heap.elements[root] is a leaf ?

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)

 Example : 45, 20, 30, 12, 9, 15, 17, 1, 5, 3, 4


Delete 20 (on board)
Apply reheap down as 4 is less than 20

 Example : 90, 73, 75, 70, 69, 71, 74


Delete 70
Apply reheap up as 74 is greater than 70
4/27/2018
Search element in heap

How can you search specific element =k in


heap
What will be the worst case analysis?

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

Heap O(log2N) O(log2N)

Unsorted Linked List O(1) O(N)


Sorted Linked List O(N) O(1)

Binary Search Tree


Balanced O(log2N) O(log2N)

Skewed O(N) O(N)


Class task : Min heap
Insertion : 8, 3, 15, 20, 4, 70, 2, 100
Dequeue 3 elements according to priority
1st element:
2nd element:
3rd element:

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

DS Malik Chapter 11


Allen Weiss chapter 4
Read Pointer based heap
https://stackoverflow.com/questions/19720438/
pointer-based-binary-heap-implementation

4/27/2018

Potrebbero piacerti anche