data structures and algorithms (cs 210) - cs batch 16-a
TRANSCRIPT
Perfect Binary Tree
Every non-leaf node has two children Leaves are on the same level All levels are filled
Full Binary Tree10/18/17
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
10/18/17
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.
10/18/17
And use the Numbers as Array Indexes to Store the Trees
70
0
60
1
40
3
30
4
12
2
8
5
tree[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
8
tree.nodes
Why Array?? 10/18/17
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] 10/18/17
// 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 ;};
10/18/17
Insertion
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.
10/18/17
// 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)
ReheapUp
10/18/17
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?
10/18/17
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;
10/18/17
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 ;
10/18/17
ReheapDown (cont)
if ( leftChild <= bottom ) // ReheapDown continued { if ( leftChild == bottom )
maxChild = leftChld; else {
if (elements [ leftChild ] <= elements [ rightChild ] )maxChild = rightChild;
elsemaxChild = leftChild;
} if ( elements [ root ] < elements [ maxChild ] ) { Swap ( elements [ root ] , elements [ maxChild ] ); ReheapDown ( maxChild, bottom ) ; } }}
O(logN)10/18/17
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.
10/18/17
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 ?
10/18/17
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.
10/18/17
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 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
10/18/17
Search element in heap
How can you search specific element =k in heap
What will be the worst case analysis?
10/18/17
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 EnqueueDequeue
Observers IsEmptyIsFull
change state
observe state
Different Implementations of Priority Queue
An unsorted Listdequeuing would require searching through the entire list
An Array-Based Sorted ListEnqueuing is expensive
A R eference-Based Sorted ListEnqueuing again is 0(N)
A Binary Search TreeOn average, 0(log2N) steps for both enqueue and dequeue
A Heapguarantees 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 Definitionstemplate<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
EnqueueIncrement length
Put newItem in next available position
elements.ReheapUp(0, length-1)
DequeueSet item to root element from queue
Move last leaf element into root position
Decrement length
elements.ReheapDown(0, length-1)
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);
}
}
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)
Linked List O(N) O(N)
Binary Search Tree
Balanced O(log2N) O(log2N)
Skewed O(N) O(N)
Home task
Insertion : 8, 3, 15, 20, 4, 70, 2, 100 Dequeue 3 elements according to priority1st element:2nd element:3rd element:
10/18/17