Analysis
1
Balanced Binary Tree
• A balanced binary tree is a tree in which all nodes have 2
siblings except
– Nodes at height h have no child and
– Nodes at height h-1 can have any number of children
from 0 to 2
Balanced Balanced
2
Not balanced
Left-justified(Complete Tree) Tree
• A balanced binary tree is left-justified or complete tree if:
– all the leaves are at the same depth, or
– all the leaves at depth h are to the left of all the leaves at depth h-1
• In other words, a left justified binary tree is a balanced binary tree in
which all nodes have two siblings except all nodes at depth h-1 has
number of children less or equals to all nodes in its left siblings (equality
doesn’t work for 1 child)
• A left justified binary tree is also called complete binary tree
4
Heap
A binary heap is a binary tree that satisfies two important
property:
1. Structural property:
It must be left justified balanced tree(Complete Binary
Tree)
2. Partial Order Property:
the values stored in a heap are partially ordered. This
means that there is a relationship between the value
stored at any node and the values of its children.
There are two variants of the heap, depending on the definition
of this relationship.
• A Max-heap
• A Min-heap 5
A Min Heap
• A min-heap has the property that every node stores a value
that is less than or equal to that of its children. Because the
root has a value less than or equal to its children, which in
turn have values less than or equal to their children, the root
stores the minimum of all values in the tree.
6
Heap
• A max-heap has the property that every node stores a value
that is greater than or equal to the value of either of its
children. Because the root has a value greater than or equal
to its children, which in turn have values greater than or
equal to their children, the root stores the maximum of all
values in the tree.
K
77 64 81 63
3 4 5 6
14 48 15 55 67 72 7
7 8 9 10 11 12 13
Array representation(Physical view)
99 91 83 77 64 81 63 14 48 15 55 67 72 7
9
Heap Operations
Main Operations
• Create() : initialize the heap
• insert(): insert a node next to the last node, call siftUp from it,
treating the new node as a violation.
• deleteMax(): delete the root node, move the last node to the
root, then call siftDown from the root.
• findMax(): return the root’s value.
• Heapify(): build a heap from an array.
Helper Operation
• siftUp() :The operation to restore the heap property after the
heap property is violated
• siftDown(): The operation to restore the heap property after
10
the heap property is violated
Heap Implementation: Create
const int MAX=100;
<appropriate type> Heap[MAX];
int n //no of elements in the heap
void create(int & n){
n=0;
}
11
Heap: Insertion
• Insert new element into the heap at the next
available slot (“hole”)
– According to maintaining a complete binary tree
• Then, “SiftUp” the element up the heap while
heap-order property not satisfied
12
Heap Insertion
• Insert element x=87 into heap.
– Insert into next available slot.
99
91 83
77 64 81 63
99 91 83 77 64 81 63 14 48 15 55 67 72 7 87
13
Heap Insertion
• siftup if the heap is not ordered.
99 swap with parent
91 83
77 64 81 63
14 48 15 55 67 72 7 87
99 91 83 77 64 81 63 14 48 15 55 67 72 7 87
14
Heap Insertion
• siftup if the heap is not ordered.
91 83
77 64 81 87
14 48 15 55 67 72 7 63
99 91 83 77 64 81 87 14 48 15 55 67 72 7 63
15
Heap Insertion
• siftup if the heap is not ordered.
99 Stop heap ordered
91 87
77 64 81 83
14 48 15 55 67 72 7 63
99 91 87 77 64 81 83 14 48 15 55 67 72 7 63
16
Heap: Siftup
Template <class T>
void siftup(T Heap[], int pos, int n) {
T temp;
if((pos < 0) || (pos >= n) return; //Bad position
while ((pos <=0) && (Heap[(pos – 1)/2] <Heap[pos] )) {
temp=Heap[pos]; //swap(Heap, pos, (pos – 1)/2);
Heap[pos]=Heap[(pos – 1)/2];
Heap[(pos – 1)/2]=temp;
pos = (pos – 1)/2;
}
}
}
17
Performance Analysis?
Heap: Insertion
Template <class T>
void insert(T Heap[], int & n, T data) {
if(n != MAX){
int cur=n++;
Heap[cur]=data;
siftup(Heap,n,cur);
}
}
Performance Analysis?
18
Heap: DeleteMax
• Maximum element is always at the root
• Exchange root with rightmost leaf.
• Cut the right most leaf (Heap decreases by one in size)
• siftdown the root while heap-order property not satisfied
19
Heap: DeleteMax
• Delete maximum element from heap.
99 Delete Max
91 83
77 64 81 63
14 48 15 55 67 72 7 43
99 91 83 77 64 81 63 14 48 15 55 67 72 7 43
20
Heap: DeleteMax
• Delete maximum element from heap.
– Exchange root with rightmost leaf.
43
91 83
77 64 81 63
14 48 15 55 67 72 7 99
99 91 83 77 64 81 63 14 48 15 55 67 72 7 43
21
Heap: DeleteMax
– Cut the rightmost leaf (Heap decreases by one in size
43
91 83
77 64 81 63
14 48 15 55 67 72 7
43 91 83 77 64 81 63 14 48 15 55 67 72 7 99
22
Heap: DeleteMax
– Sift –down 43 by swapping it with the largest of its
children
43
exchange with left child
91 83
77 64 81 63
14 48 15 55 67 72 7
43 91 83 77 64 81 63 14 48 15 55 67 72 7 99
23
Heap: DeleteMax
– Sift –down 43 by swapping it with the largest of its
children
91
exchange with left child
43 83
77 64 81 63
14 48 15 55 67 72 7
43 91 83 77 64 81 63 14 48 15 55 67 72 7 99
24
Heap Insertion
– Sift –down 43 by swapping it with the largest of its
children
91
exchange with right child
77 83
43 64 81 63
14 48 15 55 67 72 7
91 77 83 43 64 81 63 14 48 15 55 67 72 7 99
25
Heap: DeleteMax
91
Stop Heap ordered
77 83
48 64 81 63
14 43 15 55 67 72 7
91 77 83 48 64 81 63 14 43 15 55 67 72 7 99
26
Heap: Siftdown
Template <class T>
void siftdown(T Heap[], int pos, int n) {
int rc, mc;
T temp;
if((pos < 0) || (pos >= n) return; //Bad position
while (pos < n/2) { // Stop if pos is a leaf
mc= 2*pos + 1; rc = mc + 1;
if ((rc < n) && (Heap[rc] > Heap[mc]))
mc= rc; // Set mc to greater child’s value
if (Heap[mc] <Heap[pos])) return; // Done
temp=Heap[pos]; //swap(Heap, pos, mc);
Heap[pos]=Heap[mc];
Heap[mc]=temp;
pos = mc; // Move down
}
27
} Performance Analysis?
Heap: RemoveMax
Template <class T>
void removeMax(T Heap[], int &n, T & data){
data=Heap[0];
Heap[0]=Heap[--n];
siftdown(0);
}
Performance Analysis?
28
Exercise
Given the numbers
12, 5, 10, 2, 17, 15, 8, 6, 40, 3, 19
1. Build a Binary MaxHeap. For each insertion,
show the physical (assuming array
implementation) and the logical view of the
heap.
2. Assuming the numbers are stored in an array in
that order, heapify the array
29
Heapify
void Heapify(item Heap[], int n) // Heapify contents of Heap
{
for (int i=n/2-1; i>=0; i--) //for all non leaf node
siftdown(i);
}
Performance Analysis?
30
Priority Queue & Heap
When a collection of objects is organized by importance or
priority, we call this a priority queue. A normal queue data
structure will not implement a priority queue efficiently
because search for the element with highest priority will take
(n) time.
The heap is a natural implementation for the priority queue .
Jobs can be added to the heap (using their priority value as
the ordering key) when needed. Method removemax can be
called whenever a new job is to be executed.
31