1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63 1
DATA STRUCTURE
TREES & GRAPHS
UNIT II
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 2
Learning Objectives
Trees
Binary trees
AVL Trees
Binary Tree Applications
Threaded Tree
B Trees
B* Trees
B+ Trees
Graphs
spanning Trees
Shortest Path
Transitive Closure
Topological Sort
Critical Path
Dijkstras Algorithm
Warshalls Algorithm
Spanning Tree
Kruskals Algorithm
Prims Algorithm
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63 3
TREES
2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 4
Learning Objectives
Linear Lists and Trees
Basic Terminology
Binary Tree
Traversal Methods
Basic Operations
Stack Based Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 5
Learning Objectives
Trees
Linear Lists and Trees
Basic Terminology
Binary Tree
Traversal Methods
Basic Operations
Stack Based Traversal
Binary trees: Definition, traversal, threaded binary tree, set representation and
operations, Decision tree, Game tree, BTree: Definition, B+ tree, B* trees, AVL
trees.
Graphs
Representation, traversal, connected components, spanning trees, shortest path
and
transitive closure, topological sort, activity network, critical path, path enumeration.
Dijkstras Algorithm, Floyd Warshalls Algorithm, Coloring of Graphs, TreesBinary
Search Tree, Tree Traversals, Spanning Tree, Minimum Spanning Tree Algorithms,
Kruskals Algorithm, Prims Algorithm, Algorithms of discrete Mathematics
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 6
Linear Lists And Trees
Linear lists are useful for serially ordered data.
(e
0
, e
1
, e
2
, , e
n1
)
Days of week.
Months in a year.
Students in a class.
Trees are useful for hierarchically ordered data.
Employees of a corporation.
President, vice presidents, managers, and so on.
Classes that represent entities in an Object Oriented
Application.
3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 7
Hierarchical Data And Trees
The element at the top of the hierarchy is
the root.
Elements next in the hierarchy are the
children of the root.
Elements next in the hierarchy are the
grandchildren of the root, and so on.
Elements that have no children are leaves.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 8
Definition
A tree t is a finite nonempty set of elements
called nodes.
One of these elements is called the root.
The remaining elements, if any, are
partitioned into trees, which are called the
subtrees of the tree t.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 9
Subtree, Leaves, Parent, Grandparent, Siblings,
Ancestors, Descendants, Levels
Person
S_MCA
Employee
Permanent Temporary
Student
S_MBA
root
4
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 10
Basic Terminology
Node (Vertex, Element):
Main component of a tree structure
Stores data and links to other nodes.
Parent (Father)
Immediate predecessor of a node
Child (Son):
Immediate successor of a node
Sibling
Nodes having the same parent
Link (Edge, Branch):
A pointer to a node in a tree.
Means of traversing the tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 11
Cont..
Root:
Specially designated node that has no parent.
Node at the topmost level in the tree hierarchy
Leaf (Terminal node, External node):
A node that has no child nodes
At the bottommost level in the tree hierarchy
Level:
Rank of hierarchy of a node.
Root is said to be at level 0.
If a node is at level l then its child nodes are level l+1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 12
Path: (from n
l
to n
k
)
Sequence of nodes n
l
, n
l+1,
n
l+2
n
k
such that n
i
is
parent of n
i+1
for 1< i <= k
Length of this path is the number of links traversed in
this path.
Depth of n
i
is the length of unique path from root to n
i
Height of n
i
is the length of longest path from n
i
to a leaf.
Height of a tree:
Height of a tree equal to the depth of its deepest leaf
which in turn is equal to the depth of the tree.
If there is a path from n
i
to n
j
then n
i
is an ancestor
of n
j
and n
j
is a descendant of n
i
.
Cont..
5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 13
Forest:
A set of disjoint trees
Free Tree
Has no node designated as a root
A connected acyclic graph
Ordered Tree:
Child nodes are ordered from youngest to oldest
Binary Tree
Cont..
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 14
Binary Tree
Finite collection of elements / nodes that is either
empty or partitioned into three disjoint
subsets.
The first subset is termed as the root element.
The remaining elements (if any) are partitioned
into two binary trees.
These are called the left and right subtrees of
the binary tree.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 15
Properties of Binary Trees
If a binary tree contains m nodes at level l; it can
contain at most 2m nodes at level l+1.
Maximum number of nodes at level l is 2^l.
The total number of nodes in a complete binary
tree= sum of number of nodes at each level
between 0 and d (depth)
= 2^0 + 2^1 + 2^2 + + 2^d= 2^(d+1) 1
6
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 16
The subtrees of a binary tree are ordered;
Are different when viewed as binary trees.
Are the same when viewed as trees.
a
b
a
b
Cont..
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 17
Binary Tree Traversal Methods
In a traversal of a binary tree, each element of
the binary tree is visited exactly once.
During the visit of an element, all action (make a
clone, display, evaluate the operator, etc.)
with respect to this element is taken.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 18
Binary Tree Traversal Methods
Preorder
Inorder
Postorder
Level order
7
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 19
Preorder Example (visit = print)
a
b c
a b c
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 20
Preorder Example (visit = print)
a
b c
d
e
f
g
h i
j
a b d g h e i c f j
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 21
Preorder Of Expression Tree
+
a
b

c
d
+
e
f
*
/
Gives prefix form of expression!
/ * + a b  c d + e f
8
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 22
Preorder Traversal
void preOrder(BinaryTreeNode t)
{
if(t != null)
{
visit(t);
preOrder(t.leftChild);
preOrder(t.rightChild);
}
}
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 23
Inorder Example (visit = print)
a
b c
b a c
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 24
Inorder Example (visit = print)
a
b c
d
e
f
g
h i
j
g d h b e i a f j c
9
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 25
Inorder Of Expression Tree

+
a
b c
d
+
e
f
*
/
Gives infix form of expression (sans parentheses)!
e a + b * c d / + f 
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 26
Inorder Traversal
void inOrder(BinaryTreeNode t)
{
if (t != null)
{
inOrder(t.leftChild);
visit(t);
inOrder(t.rightChild);
}
}
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 27
Postorder Example (visit = print)
a
b c
b c a
10
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 28
Postorder Example (visit = print)
a
b c
d
e
f
g
h i
j
g h d i e b j f c a
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 29
Postorder Of Expression Tree
+
a
b

c
d
+
e
f
*
/
Gives postfix form of expression!
a b + c d  * e f + /
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 30
Postorder Traversal
Void postOrder(BinaryTreeNode t)
{
if if (t != null)
{
postOrder(t.leftChild);
postOrder(t.rightChild);
visit(t);
}
}
11
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 31
Traversal Applications
a
b c
d
e
f
g
h i
j
Make a clone.
Determine height.
Determine number of nodes.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 32
Make A Clone Using Traversal
TreePtr Clone (srcTree, destTree)
If srcTree Is Not NULL
clone the root in the visit step.
Set destTree:Left to Clone(srcTree:Left,
destTree:Left)
Set destTree:Right to Clone(srcTree:Right,
destTree:Right)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 33
Determine Height
If tree is NULL
height = 1
Else
Height= Max (Height of Left Subtree, Height
of Right Subtree) +1
12
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 34
Determine Number of Nodes Using Traversal
initialize a counter to 0,
add 1 to the counter in the visit step.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 35
Searching Using Traversal
Search for data in the tree N
If (N contains data or if N is NULL)
Return N
Else If (data greater than contents of N)
Return the results of Searching the N:right
Else If (data lesser than contents of N)
Return the results of Searching the N:left
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 36
Make Empty
Make the tree N Empty
If N is not Empty
Make the left branch of N empty
Make the right branch of N empty
Free N
13
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 37
Inserting a Node
Insert (TPtr, data )
If TPtr is NULL
Set TPtr= Create a Node with contents data
Else If data is greater than contents of TPtr
Set TPtr= Insert (TPtr:Right, data)
Else If data is lesser than contents of TPtr
Set TPtr= Insert (TPtr:Left, data)
Return TPtr
*Try nonrecursive addition
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 38
Deleting a Node
Three possible cases
The node to be deleted has no child nodes
Free the node
Set the appropriate pointer of father NULL
The node to be deleted has one child node
Adjust the pointer of parent to bypass the node to be
deleted
Free the node
The node to be deleted has two child nodes
Replace the data in the node to be deleted with the
smallest data in the right subtree
Recursively delete the replacing node from the right
subtree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 39
TreePtr DeleteNode ( TreePtr T, int Data)
TreePtr tempPtr;
If T is not NULL
If Data is lesser than T:Data
Set T:Left to DeleteNode (T:Left, Data)
Else If Data is greater than T:Data
Set T:Right to DeleteNode (T:Right, Data)
Else // node found
If (T:Left AND T:Right)
Set tempPtr to FindMin (T:Right)
Set T:Data to tempPtr:Data
Set T:Right to DeleteNode (T:Right, T:Data)
Else
Set tempPtr to T
If T:Left is NULL
Set T to T:Right
Else
Set T to T:Left
Free tempPtr
Return T
Recursive
Delete
14
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 40
Stack Based Traversal (Preorder)
Set N to Root
While (N)
Process (N)
If N:Right
Push N:Right on Stack
Set N to N:Left
If N is NULL
Pop Stack and place the result in N
Done if Stack is Empty
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 41
PreOrder
a
b c
d
e
f
g
h i
j Output
Stack
a
c
b
e
d
h
g h
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 42
a
b c
d
e
f
g
h i
j Output
Stack
a
c
b
e
d
g h e
PreOrder
15
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 43
a
b c
d
e
f
g
h i
j Output
Stack
a
c
b
i
d
g h e i
PreOrder
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 44
a
b c
d
e
f
g
h i
j Output
Stack
a
c
b
d
g h e i c
PreOrder
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 45
a
b c
d
e
f
g
h i
j Output
Stack
a
b
d
g h e i c f
j
j
N:Left is Null
Stack is Empty
N:Left is Null
Stack is Empty
PreOrder
16
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 46
Stack Based Traversal (Inorder)
Set N to Root
While (N)
While (N)
Push N on Stack
Set N to N:Left
Do
Set N to Pop Stack
Done if Stack is Empty
Process (N)
If N:Right is Not NULL
Set N to N:Right
Step out of Loop
While (N is Not NULL)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 47
Stack Based Traversal (Postorder)
Will require to push each node N as well as its
Right Child N
R
on to the Stack while a
temporary pointer navigate down the Left
Branch.
Need to Distinguish between N and N
R
Can do so by adding a flag field to the structure
that holds data
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 48
Set N to Root
While N is Not NULL
Push N on Stack
If N:Right is Not NULL
Set N:Data:Flag to 1
Push N:Right on Stack
Set N to N:Left
Do
Pop Stack and place the result in N
If N:Data:Flag is 1
Set N:Data:Flag to 0
Step out of loop
Process (N)
While (TRUE)
Cont..
17
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 49
Level Order
Set N to Root
Insert N into the Queue
While Queue is not Empty
Perform Dequeue on Queue and place the result
in N
If N is Not NULL
Process (N)
Insert N:Left into the Queue
Insert N:Right into the Queue
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 50
LevelOrder Example (visit = print)
a
b c
d
e
f
g
h i
j
a b c d e f g h i j
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 51
What we Studied
Linear Lists and Trees
Basic Terminology
Binary Tree
Traversal Methods
Basic Operations
Stack Based Traversal
18
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63 52
AVL Trees
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 53
Learning Objectives
BST
Balanced Trees
Unbalanced Trees
AVL Trees
AVL vs. Balanced Trees
Basic Operations & Implementation
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 54
BST
Searching in a BST is most efficient when the tree is balanced
In other words, a balanced tree gives the best search/insert
times for a given number of nodes
If a tree is balanced then it implies there is no binary tree of
lesser height that can have the same number of nodes as
the tree itself.
A tree is said to be perfectly balanced if the following rule
applies for all the nodes of the tree
height of left subtree = height of right subtree
19
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 55
Balanced Trees
Since the number of nodes in a full binary tree of height h is
given by the expression 2
h+1
1
A binary tree of n elements is balanced if:
2
h
1 < n 2
h+1
1
So, finding an element, inserting, and removing in a balanced
tree containing n elements are O(log n) operations
Unfortunately, binary search trees can become unbalanced
and, in the worst case, these operations then become O(n)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 56
Unbalanced Tree: Example
Worst case search!!!
Requires 
operations
Worst case search!!!
Requires 
operations
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 57
AVL Trees
AVL trees are named after two Russian
mathematicians G.M. AdelsonVelskii and E.M.
Landis, who discovered them in 1962.
An AVL tree is
A binary search tree
In which the heights of the left and right subtrees of
the root differ by at most 1, and
In which the left and right subtrees of the root are again
AVL trees
Some adjustments might be needed after insertion /
deletion to maintain AVL properties.
20
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 58
AVL vs Balanced
An AVL tree is a type of binary search tree
which is nearly as good as a balanced tree for time
complexity of the operations, and
whose structure we can maintain as insertions and
deletions proceed
i.e. an AVL tree may or (in certain cases) may not be
balanced
But, the structure is such that it supports efficient
search / insert operations.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 59
AVL Trees: Examples
Legal AVL, but not
balanced
AVL
NonLegal
AVL
AVL and balanced
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 60
Implementing AVL Trees
To implement algorithms for inserting and deleting from an AVL
tree, we associate a balance factor with each node
This is difference between heights of left and right subtrees
(Note that the difference cannot have a magnitude
greater than 1).
Allowed values for the balance factor are:
0 => Height (L
T
) = Height (R
T
),
1 => Height (L
T
) > Height (R
T
) or
1 => Height (L
T
) < Height (R
T
)
depending on whether the left subtree of the node has height
greater than, less than or equal to that of the right subtree
Height of a NULL tree is 1 by definition
21
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 61
Inserting into an AVL Tree
Though the basic insertion algorithm is similar to that
of a binary search tree; as nodes are randomly
added or removed the balance factor can get
disturbed.
Balance factor will get disturbed when
the new node is added to a subtree of the root
that is higher than the other subtree and
the new node increases the height of the subtree
In this case we have to carry out some balancing
operations in the neighborhood of the root
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 62
AVL Tree: Insertion
10 10
20
10
20
30
Consider the insert sequence: 10, 20, 30
As nodes are added the change in the balance factor is evident
Addition of 30 leads to violation of AVL property
Needs Balancing Operations
0
1
0
0
1
2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 63
Balanced > Unbalanced
Unless keys appear in the perfect order, imbalance is bound to
occur.
These imbalance conditions can be reduced to one of four
reference cases, listed as:
1.Addition in left subtree of left child (LeftLeft Imbalance).
2.Addition in right subtree of left child (LeftRight
Imbalance).
3.Addition in right subtree of right child (RightRight
Imbalance).
4.Addition in left subtree of right child (RightLeft
Imbalance).
22
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 64
Case I: LeftLeft Imbalance
10
5
10
5
2
0  (1) = 1
1 (1) = 0
1  (1) = 0
(0)  (1) = 1
(1)  (1) = 2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 65
Case II: LeftRight Imbalance
10
5
7
(1) (1) = 0
1  (0) = 1
(1)  (1) = 2
10
5
0  (1) = 1
1 (1) = 0
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 66
Case III: RightRight Imbalance
10
20
10
20
30
(1) 0= 1
1 (1) = 0
1  (1) = 0
(1) (0) = 1
(1)  (1) = 2
23
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 67
Case IV: RightLeft Imbalance
10
20
10
20
15
(1) 0= 1
(1)(1)=0
(1) (1) = 0
0  (1) = 1
(1)  (1) = 2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 68
Generalization
Careful inspection reveals that
Case  I and Case III are mirror images of each other
Similarly, Case  II and Case IV are mirror images of
each other
Thus, the process for restoring balance can be
reduced to two generalized cases
Straight line addition (I & III)
The socalled Dogleg pattern (II & IV)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 69
Restoring Balance: Case  I
10
5
2
10
5
2
24
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 70
Restoring Balance: Case  II
30
20
10
10
20
30
Thus, a single rotation, clockwise /
anticlockwise restores balance in
case of straight line addition
Thus, a single rotation, clockwise /
anticlockwise restores balance in
case of straight line addition
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 71
Restoring Balance: Case III
10
5
7
10
7
5
10
7
5
Strategy:
1. Transfer extra weight in left branch
2. Rotate clockwise
Strategy:
1. Transfer extra weight in left branch
2. Rotate clockwise
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 72
Restoring Balance: Case IV
10
20
15
10
15
20
20
15
10
Strategy:
1. Transfer extra weight in right branch
2. Rotate anticlockwise
Strategy:
1. Transfer extra weight in right branch
2. Rotate anticlockwise
25
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 73
Restoring Balance
Balance, in either of the cases can be restored by
means of a process termed rotation
In cases I & III balance is restored by means of single
rotation
Whereas, in cases II & IV double rotation is used for the
same purpose.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 74
Principles
Imbalance will occur only on the path from root
to the inserted node.
As only these nodes have their subtrees altered
Rebalancing needs to be done at the deepest
unbalanced node
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 75
LeftLeft Imbalance: Before Rotation
B and C have the same
height
A is one level higher
Therefore
Make 1 the new root
2 its right child; and
B and C the subtrees of
2
Note the levels
2
1
A
B
C
(Solve rightright imbalance, by symmetry)
(Solve rightright imbalance, by symmetry)
New node added here
26
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 76
LeftLeft Imbalance: After Rotation
Result: A more balanced
and legal AVL tree
Note the levels
2
1
A
B C
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 77
LeftRight Imbalance: Before Rotation
Cant use the leftleft
balance trick
Because now its the
middle subtree, i.e. it is
B, thats too deep.
So we consider whats
inside B...
3
1
A
C
B
(Solve rightleft imbalance by symmetry)
(Solve rightleft imbalance by symmetry)
New node added here
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 78
LR Imbalance: Inadequacy of Single Rotation
Lets try to apply single
rotation in case of Leftright
Imbalance:
Steps
Make 1 the new root
3 its right child
B & C the subtrees of 3
3
1
A
C
B
27
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 79
LeftRight Imbalance: Double Rotation
B will have two subtrees
containing at least one
item (just added)
We do not know which is
too deep  set them
both to 0.5 levels below
subtree A so that
either can be
adjusted half a level
up / down
3
1
A
C
2
B1 B2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 80
LeftRight Imbalance: Double Rotation
Neither 1 nor 3 worked as root
node so make 2 the root
Rearrange the subtrees in the
correct order
No matter how deep B1 or B2
(+/ 0.5 levels) we get a
legal AVL tree again
A C
B1 B2
1 3
2
Equivalent to:
SingleRotateForRight (1)
SingleRotateForLeft (3)
Equivalent to:
SingleRotateForRight (1)
SingleRotateForLeft (3)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 81
Approach Towards Solution
Spot the where imbalance has occurred:
youll need to make sure youre updating your balance
factors, all the way from each new node to root.
Spot which rebalancing operation to use:
from the imbalanced node,
draw subtree triangles left and right.
Then break the deeper subtree itself into two subtrees.
The location of the deeper subsubtree will tell you which
rule to use (see diagrams on previous slides)
28
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 82
Example
Example question:
Sketch the various stages involved in inserting the
following keys, in the order given, into an AVL
tree:
342, 206, 444, 523, 607, 301, 142, 183, 102, 157, 149
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 83
Algorithm: Insert
AVLTree AVLInsert ( ElementType X, AVLTree T)
BEGIN
IF ( T IS NULL) THEN
Allocate Memory to T
T:Data < X
T:Height < 0
T:Left < T:Height < NULL
ELSEIF ( X < T:Data )
T < AVLInsert (X, T:Left);
IF ( Height(T:Left) Height(T:Right) = 2) THEN
IF ( X < T:Left:Data )
T < SingleRotateForLeft (T)
ELSE
T < DoubleRotateForLeft (T)
ENDIF
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 84
Algorithm: Insert Contd.
ELSEIF ( X > T:Data )
//Solve by symmetry
ENDIF
T:Height= MAX ( Height (T:Left), Height
(T:Right) ) + 1;
return T;
END
29
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 85
Algorithm: Height
INT Height ( AVLTree T)
BEGIN
IF ( T IS NULL)
RETURN 1;
ENDIF
RETURN T:Height
END
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 86
Algorithm: SingleRotateForLeft
AVLTREE SingleRotateForLeft (AVLTree T)
BEGIN
AVLTree temp;
temp < T:Left;
T:Left < temp:Right;
temp:Right < T;
T:Height < MAX (Height(T:Left), Height(T:Right)) + 1;
//adjust height of temp;
return temp; //the new root
END
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 87
Algorithm: DoubleRotateForLeft
AVLTree DoubleRotateForLeft (AVLTree
T)
BEGIN
T:Left < SingleRotateForRight (
T:Left );
return SingleRotateForLeft ( T );
END
30
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 88
What we Studied
BST
Balanced Trees
Unbalanced Trees
AVL Trees
AVL vs. Balanced Trees
Basic Operations & Implementation
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63 89
BINARY TREES:
APPLICATIONS
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 90
Learning Objectives
Decision Trees
Binary Expression Trees
Evaluation of Arithmetic Operations
Building Expression Trees
31
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 91
Decision Trees
Binary tree associated with a decision process
internal nodes: questions with yes/no answer
external nodes: decisions
Example: Library decision
Cards Free?
Books Available?
Issue Book Waiting List
Refuse
Yes
Yes No
No
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 92
Binary Expression Trees
Binary tree associated with an arithmetic expression
internal nodes: operators
external nodes: operands
Example: arithmetic expression tree for the
expression
(2 (a 1) + (3 b))
+
2
a 1
3 b
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 93
Characteristics
A special kind of binary tree in which:
1. Each leaf node contains a single operand
2. Each nonleaf node contains a single binary
operator
3. The left and right subtrees of an operator node
represent subexpressions that must be evaluated
before applying the operator at the root of the
subtree.
32
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 94
Inductive Definition
A single atomic operand is represented by a single node binary
tree
Consider expressions E and F that are represented by binary
trees S and T, respectively, and op is a binary operator, then
(E op F) is represented by the binary tree U consisting a
new root labeled op with left child S and right child T.
Consider an expression E, represented by tree S and op is a
prefix unary operator, then
(op E) is represented by the binary tree T consisting a
new root labeled op with empty left child and right child
S.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 95
Significance of Level (Depth)
Level of a node indicates its order of evaluation
Parentheses are not required to indicate
precedence.
Operations at lesser depth of the tree are
evaluated later than those below them.
The operation at the root is always the last operation
performed.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 96
Implementation
TreeNode:Info can contain either of the following:
Operator
Operand
Thus the member Info must be capable of holding either of the
two members
struct Info
{
OpType whichType;
union 
{
char operation ;
int operand ;
}
};
struct Info
{
OpType whichType;
union 
{
char operation ;
int operand ;
}
};
enum
33
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 97
Printing Arithmetic Expressions
Application of tree traversals:
Preorder: yields Prefix
Inorder: yields Infix
Postorder: yields Postfix
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 98
Inorder: Infix
Specialization of an inorder traversal
print operand or operator when visiting
node
print ( before traversing left subtree
print ) after traversing right subtree
Algorithm printExpression(v)
if isInternal (v)
print(()
inOrder (leftChild (v))
print(v.element ())
if isInternal (v)
inOrder (rightChild (v))
print ())
Algorithm printExpression(v)
if isInternal (v)
print(()
inOrder (leftChild (v))
print(v.element ())
if isInternal (v)
inOrder (rightChild (v))
print ())
+
2
a 1
3 b
((2 (a 1)) + (3 b))
Why
Why
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 99
Evaluate Arithmetic Expression
Specialization of a postorder traversal
recursive method returning the value of a subtree
when visiting an internal node, combine the values of the
subtrees
Algorithm evalExpr(v)
if isExternal (v)
return v.element ()
else
x evalExpr(leftChild (v))
y evalExpr(rightChild (v))
operator stored at v
return x y
Algorithm evalExpr(v)
if isExternal (v)
return v.element ()
else
x evalExpr(leftChild (v))
y evalExpr(rightChild (v))
operator stored at v
return x y
34
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 100
Building Expression Trees
a b + c d e + * *
I:
Read a
Read b
a b
II:
Read +
a b
+
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 101
Building Expression Tree
III:
Read c
Read d
Read e
a b
+
c d e
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 102
Building Expression Tree
IV:
Read +
a b
+
c
d e
+
35
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 103
Building Expression Tree
V:
Read *
a b
+
c
d e
+
*
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 104
Building Expression Tree
VI:
Read *
a b
+
c
d e
+
*
*
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 105
Building Expression Trees
While hasMoreSymbols (Expression)
Symbol= readNextSymbol (Expression)
If isOperand (Symbol)
Node= CreateNode (Symbol)
Push (Stk, Node)
ElseIf isOperator (Symbol)
T1= Pop (Stk)
T2= Pop (Stk)
Node= CreateNode (Symbol)
Node:Left= T1
Node:Right= T2
Push (Stk, Node)
Endif
While hasMoreSymbols (Expression)
Symbol= readNextSymbol (Expression)
If isOperand (Symbol)
Node= CreateNode (Symbol)
Push (Stk, Node)
ElseIf isOperator (Symbol)
T1= Pop (Stk)
T2= Pop (Stk)
Node= CreateNode (Symbol)
Node:Left= T1
Node:Right= T2
Push (Stk, Node)
Endif
36
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 106
What we Studied
Decision Trees
Binary Expression Trees
Evaluation of Arithmetic Operations
Building Expression Trees
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63
107
Threaded Trees
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 108
Learning Objectives
Binary Tree Traversal
Thread as Solution
Threaded Tree Traversal
Basic Operations
37
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 109
Binary Tree: Traversal
Traversal:
Recursive
Nonrecursive
Both procedures use a stack to store information
about nodes.
Problem:
Some additional time has to be spent to maintain the
stack
Some more space has to be set aside for the stack itself.
In the worst case, the stack may hold information about
almost every node of the tree
A serious concern for very large trees.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 110
Solution: threads
In order to improve efficiency:
The stack is incorporated as part of the tree.
This is done by using threads in a given node.
Threads are pointers to the predecessor and
successor of the node according to a certain
sequence, and
The trees whose nodes use threads are called threaded
trees.
Requirements:
Four pointer fields
Two for children,
Two for predecessor and successor
would be needed for each node in the tree, which again
takes up valuable space.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 111
Utilizing Space
Binary trees have a lot of wasted space
All the leaf nodes each have 2 null pointers
We can use these pointers to help us in tree traversals by
setting them up as threads.
We make the pointers point to the next / previous node in a
traversal
Thus each pointer can act as an actual link or a thread, so
we keep a boolean for each pointer that tells us about the
usage.
38
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 112
Explanation
Thus the problem is solved by overloading existing pointer fields.
An operator is called overloaded if it can have different
meanings;
The * operator in C is a good example, since it can be used
as the multiplication operator or for dereferencing pointers.
In threaded trees, left or right pointers are pointers to
children, but they can also be used as pointers to
predecessors and successors, thereby overloaded with
meaning.
An operator is called overloaded if it can have different
meanings;
The * operator in C is a good example, since it can be used
as the multiplication operator or for dereferencing pointers.
In threaded trees, left or right pointers are pointers to
children, but they can also be used as pointers to
predecessors and successors, thereby overloaded with
meaning.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 113
Threads: Usage
For an overloaded operator, context is always a
disambiguating factor: Example!!!
In threaded trees, however, a new field has to be used to
indicate the current meaning of the pointers.
The left pointer is either a pointer to the left child or to the
predecessor.
Analogously, the right pointer will point either to the right child
or to the successor.
The meaning of predecessor and successor differs depending
on the sequence under scrutiny.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 114
Variations
Depending upon traversal:
Inthreaded
Prethreaded
Postthreaded
Depending upon threading technique:
Left <trav type> threaded
Right <trav type> threaded
<trav type> threaded
39
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 115
Threaded Tree Example
8
7 5
3
11
13
1
6
9
A Right Inthreaded Tree
A Right Inthreaded Tree
: Link
: Thread
: Link
: Thread
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 116
Traversal: Right Inthreaded Tree
Start at the leftmost node in the tree,
print it,
and follow its right child
If you follow a thread to the right, output the node and
continue to its right
If you follow a link to the right, go to the leftmost node,
print it, and continue
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 117
Traversal: Right Inthreaded Tree
8
7 5
3
11
13
1
6
9
Start at leftmost node, print it
Output
1
40
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 118
Traversal: Right Inthreaded Tree
8
7 5
3
11
13
1
6
9
Follow thread to right, print node
Output
1
3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 119
Traversal: Right Inthreaded Tree
8
7 5
3
11
13
1
6
9
Follow link to right, go to
leftmost node and print
Output
1
3
5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 120
Traversal: Right Inthreaded Tree
8
7 5
3
11
13
1
6
9
Follow thread to right, print node
Output
1
3
5
6
41
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 121
Traversal: Right Inthreaded Tree
8
7 5
3
11
13
1
6
9
Follow link to right, go to
leftmost node and print
Output
1
3
5
6
7
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 122
Traversal: Right Inthreaded Tree
8
7 5
3
11
13
1
6
9
Follow thread to right, print node
Output
1
3
5
6
7
8
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 123
Traversal: Right Inthreaded Tree
8
7 5
3
11
13
1
6
9
Follow link to right, go to
leftmost node and print
Output
1
3
5
6
7
8
9
42
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 124
Traversal: Right Inthreaded Tree
8
7 5
3
11
13
1
6
9
Follow thread to right, print node
Output
1
3
5
6
7
8
9
11
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 125
Traversal: Right Inthreaded Tree
8
7 5
3
11
13
1
6
9
Follow link to right, go to
leftmost node and print
Output
1
3
5
6
7
8
9
11
13
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 126
Right Inthreaded Tree
43
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 127
Traversal: alternate approach
Ptr < LeftmostChild (Root)
While (Ptr)
Do
Process (Ptr)
Ptr < InorderSuccessor
(Ptr)
Done
Ptr < LeftmostChild (Root)
While (Ptr)
Do
Process (Ptr)
Ptr < InorderSuccessor
(Ptr)
Done
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 128
Traversal: Alternate Approach
InorderSuccessor ( Node )
Begin
Succ < Node:Right
If ! Rthread
While ( Succ:Left )
Succe < Succ:Left
Return Succ
End
InorderSuccessor ( Node )
Begin
Succ < Node:Right
If ! Rthread
While ( Succ:Left )
Succe < Succ:Left
Return Succ
End
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 129
Adding a Node
Adding a node to a threaded tree requires
handling of two special cases
Adding a left child
Adding a right child
Why!!!
Consider adding the following data set
40, 30, 25, 45, 35, 50, 27
44
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 130
Insertion Example
40
After: 40
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 131
Insertion Example
40
30
After: 40, 30
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 132
Insertion Example
40
30
After: 40, 30, 25
25
45
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 133
40
30
After: 40, 30, 25, 45
25
45
Insertion Example
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 134
40
30
After: 40, 30, 25, 45, 35
25
45
35
Insertion Example
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 135
40
30
After: 40, 30, 25, 45, 35, 50
25
45
35 50
Insertion Example
46
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 136
40
30
After: 40, 30, 25, 45, 35, 50, 27
25
45
35
27
50
Insertion Example
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 137
Adding Left Child
AddLeft ( Father, Data )
Begin
// Allocate Memory & Set Data
NewNode:Left < NULL
NewNode:RThread <  //!!!
NewNode:Right <  //!!!
Father:Left < NewNode
End
AddLeft ( Father, Data )
Begin
// Allocate Memory & Set Data
NewNode:Left < NULL
NewNode:RThread <  //!!!
NewNode:Right <  //!!!
Father:Left < NewNode
End
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 138
Adding Right Child
AddRight ( Father, Data )
Begin
// Allocate Memory & Set Data
NewNode:Left < NULL
NewNode:Rthread <  //!!!
NewNode:Right <  //!!!
Father:Right < NewNode
Father:Rthread <  //!!!
End
AddRight ( Father, Data )
Begin
// Allocate Memory & Set Data
NewNode:Left < NULL
NewNode:Rthread <  //!!!
NewNode:Right <  //!!!
Father:Right < NewNode
Father:Rthread <  //!!!
End
47
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 139
Inthreaded Tree
8
7 5
3
11
13
1
6
9
Left Child:
Link / Inorder Predecessor
Right Child:
Link / Inorder Successor
Left Child:
Link / Inorder Predecessor
Right Child:
Link / Inorder Successor
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 140
Prethreaded Tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 141
Inthreaded Tree
48
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 142
Postthreaded Tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 143
What we Studied
Binary Tree Traversal
Thread as Solution
Threaded Tree Traversal
Basic Operations
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63
144
BTrees
Shalini Singh Jaspal, Lecturer, BVICAM 144
49
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 145
Learning Objectives
BTree
Motivations
Basic Operations
B* Tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 146
Motivation for BTrees
So far we have assumed that we can store an entire data
structure in main memory
What if we have so much data that it doesnt fit?
We will have to use disk storage but when this happens our
time complexity fails
The problem is that BigOh analysis assumes that all
operations take roughly equal time
This is not the case when disk access is involved
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 147
Motivation (cont.)
Assume that a disk spins at 3600 RPM
In 1 minute it makes 3600 revolutions,
One revolution occurs in 1/60 of a second, or 16.7ms
On an average a disk access (half way round the disk)
will take 8ms
Comparing with CPU instructions:
120 disk accesses a second 10
6
instructions
In other words, one disk access takes about the same
time as 10,000 instructions
It is worth executing lots of instructions to avoid a disk
access!!!
50
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 148
Motivation (cont.)
Assume that we use an AVL tree to create a database having
a record count of the order of 10
6
We still end up with a very deep tree with lots of different disk
accesses;
log
2
1,000,000 is about 20,
so this takes about 0.2 seconds (if there is only one user
of the program)
We know we cant improve on the log n for a binary tree
Solution
Use more branches and thus lesser height!
As branching increases, depth decreases
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 149
B  Trees
Definition: A balanced search tree in which every
node has between m/2 and m children, where
m>1 is a
fixed integer. m is the order. The root may have as
few
as 2 children.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 150
Definition of a Btree
A Btree of order m is an mway tree (i.e., a tree where each
node may have up to m children) in which:
1. The number of keys in each nonleaf node is one less
than the number of its children and these keys partition
the keys in the children in the fashion of a search tree
2. All leaves are on the same level
3. All nonleaf nodes except the root have at least m / 2
children
4. The root is either a leaf node, or it has from two to m
children
5. A node contains no more than m 1 keys
The number m should always be odd
51
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 151
An Example BTree
51 62 42
6 12
26
55 60 70 64 90 45
1 2 4 7 8 13 15 18 25
27 29 46 48 53
A Btree of order 5
containing 26 items
Note that all the leaves are at the same level Note that all the leaves are at the same level
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 152
Suppose we start with an empty Btree and keys
arrive in the following order:1 12 8 2 25 6 14 28
17 7 52 16 48 68 3 26 29 53 55 45
We want to construct a Btree of order 5
The first four items go into the root:
To put the fifth item in the root would violate condition
5
Therefore, when 25 arrives, pick the middle key to
make a new root
Constructing a Btree
1 2 8 12
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 153
Constructing a Btree (contd.)
1 2
8
12 25
6, 14, 28 get added to the leaf nodes:
1 2
8
12 14 6 25 28
52
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 154
Constructing a Btree (contd.)
Adding 17 to the right leaf node would overfill it, so we take the
middle key, promote it (to the root) and split the leaf
8 17
12 14 25 28 1 2 6
7, 52, 16, 48 get added to the leaf nodes
8 17
12 14 25 28 1 2 6 16 48 52 7
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 155
Constructing a Btree (contd.)
Adding 68 causes us to split the right most leaf, promoting 48 to the
root, and adding 3 causes us to split the left most leaf, promoting 3
to the root; 26, 29, 53, 55 then go into the leaves
3 8 17 48
52 53 55 68 25 26 28 29 1 2 6 7 12 14 16
Adding 45 causes a split of 25 26 28 29
and promoting 28 to the root then causes the root to split
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 156
Constructing a Btree (contd.)
17
3 8 28 48
1 2 6 7 12 14 16 52 53 55 68 25 26 29 45
53
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 157
Inserting into a BTree
Attempt to insert the new key into a leaf
If this would result in that leaf becoming too big, split the leaf
into two, promoting the middle key to the leafs parent
If this would result in the parent becoming too big, split the
parent into two, promoting the middle key
This strategy might have to be repeated all the way to the top
If necessary, the root is split in two and the middle key is
promoted to a new root, making the tree one level higher
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 158
Exercise in Inserting a BTree
Insert the following keys to a 5way Btree:
3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31,
35, 56
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 159
Removal from a Btree
During insertion, the key always goes into a leaf.
For deletion we wish to remove from a leaf.
There are three possible ways we can do this:
1. If the key is already in a leaf node, and removing it
doesnt cause that leaf node to have too few keys, then
simply remove the key to be deleted.
2. If the key is not in a leaf then it is guaranteed (by the
nature of a Btree) that its predecessor or successor
will be in a leaf  in this case can we delete the key
and promote the predecessor or successor key to the
nonleaf deleted keys position.
54
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 160
Removal from a Btree (2)
If (1) or (2) lead to a leaf node containing less than
the minimum number of keys then we have to look
at the siblings immediately adjacent to the leaf in
question:
if one of them has more than the min number of keys
then we can promote one of its keys to the parent and
take the parent key into our lacking leaf
if neither of them has more than the min number of
keys then the lacking leaf and one of its neighbours can
be combined with their shared parent (the opposite of
promoting a key) and the new leaf will have the correct
number of keys; if this step leave the parent with too few
keys then we repeat the process up to the root itself, if
required
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 161
Exercise in Removal from a BTree
Given 5way Btree created by these data (last
exercise):
3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31,
35, 56
Add these further keys: 2, 6,12
Delete these keys: 4, 5, 7, 3, 14
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 162
Analysis of BTrees
The maximum number of items in a Btree of order m
and height h:
root m 1
level 1 m(m 1)
level 2 m
2
(m 1)
. . .
level h m
h
(m 1)
So, the total number of items is
(1 + m + m
2
+ m
3
+ + m
h
)(m 1) =
[(m
h+1
1)/ (m 1)] (m 1) = m m
h h+1 +1
1 1
When m = 5 and h = 2 this gives 5
3
1 = 124
55
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 163
Reasons for using BTrees
When searching tables held on disc, the cost of each disc
transfer is high but doesn't depend much on the amount of
data transferred, especially if consecutive items are
transferred
If we use a Btree of order 101, say, we can transfer each
node in one disc read operation
A Btree of order 101 and height 3 can hold 101
4
1
items (approximately 100 million) and any item can be
accessed with 3 disc reads (assuming we hold the root in
memory)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 164
Cont..
If we take m = 3, we get a 23 tree, in which nonleaf nodes
have two or three children (i.e., one or two keys)
BTrees are always balanced (since the leaves are all at
the same level), so 23 trees make a good type of
balanced tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 165
Comparing Trees
Binary trees
Can become unbalanced and lose their good time
complexity (big O)
AVL trees are strict binary trees that overcome the
balance problem
Heaps remain balanced but only prioritise (not order) the
keys
Multiway trees
BTrees can be mway, they can have any (odd) number
of children
One BTree, the 23 (or 3way) BTree, approximates a
permanently balanced binary tree, exchanging the AVL
trees balancing operations for insertion and (more
complex) deletion operations
56
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 166
B* Trees
Requires nonroot nodes to be at least 2/3 full
instead of 1/2.
To maintain this, instead of immediately splitting up
a node when it gets full, its keys are shared with
the node next to it.
When both are full, then the two of them are split
into three.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 167
What we Studied
BTree
Motivations
Basic Operations
B* Tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63
168
B+ Trees
57
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 169
Learning Objectives
B+ Tree
Problem with B Tree
Solution as B+ Tree
Basic Operations
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 170
13 13
Problem With B Trees
Accessing keys from Btree in sorted order Requires
Backtracking
6 6 9 9
1 1 3 3 7 7
<6 <6
<6 && >9 <6 && >9
>6 >6
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 171
Solution: B+ Trees
B+ Trees:
Facilitate Sequential Operations
String all leaf nodes together
Replicate keys from nonleaf nodes to make sure each
key appears at leaf level.
58
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 172
Properties of Btrees
B Trees
Are multiway trees i.e. each node contains a set of keys
and pointers.
Contain only data pages.
Are dynamic i.e., the height of the tree grows and
contracts as records are added and deleted.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 173
B+ Trees
Combines features of ISAM and B Trees.
It contains index pages and data pages.
The data pages always appear as leaf nodes in the tree.
The root node and intermediate nodes are always index
pages.
B+ trees grow and contract like their B Tree counterparts
The index pages are constructed through the process of
inserting and deleting records and the contents and the
number of index pages reflects the growth and shrinkage in
height.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 174
An Example B+ Tree
Order: 5
Data Pages Data Pages
59
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 175
B+ Tree
The key value determines a record's placement in a
B+ tree.
The leaf pages are maintained in sequential order
AND a doubly linked list connects each leaf page with
its sibling page(s).
This doubly linked list speeds data movement as
the pages grow and contract.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 176
B+ Tree: Adding Records
Cases to consider while adding records:
Case IV
Case III
Case II
Case I
NOT FULL FULL
FULL FULL
FULL NOT FULL
NOT FULL NOT FULL
Index Page Leaf Page
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 177
B+ Tree: Adding Records
Case I & II:
Place the record in sorted position in the appropriate
leaf page
Case III:
1. Split the leaf page
2. Place Middle Key in the index page in sorted order.
3. Left leaf page contains records with keys below the
middle key.
4. Right leaf page contains records with keys equal to or
greater than the middle key.
60
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 178
B+ Tree: Adding Records
Case IV:
1. Split the leaf page.
2. Records with keys < middle key go to the left leaf page.
3. Records with keys >= middle key go to the right leaf
page.
4. Split the index page.
5. Keys < middle key go to the left index page.
6. Keys > middle key go to the right index page.
7. The middle key goes to the next (higher level) index.
IF the next level index page is full, continue splitting
the index pages.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 179
Insert Algorithm: Case I
inserting a record into a leaf page that is not full
insert a record with a key value of 28 into the B+
tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 180
Insert Algorithm: Case III
Adding a record when the leaf page is full but
the index page is not
insert a record with a key value of 70
61
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 181
Insert Algorithm: Case IV
Both the leaf page and the index page are full
Add a record containing a key value of 95
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 182
Rotation
B+ trees can incorporate rotation to reduce the
number of page splits.
A rotation occurs when a leaf page is full, but one of
its sibling pages is not full.
Rather than splitting the leaf page, we move a record
to its sibling, adjusting the indices as necessary.
Typically, the left sibling is checked first (if it exists)
and then the right sibling. Why!!!
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 183
Rotation Example
Before addition of record with key 70
62
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 184
Rotation Example
Using rotation we shift the record with the lowest
key to its sibling.
Since this key appeared in the index page we also
modify the index page.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 185
Deleting Keys from a B+ tree
Like Insertion we must consider three scenarios when we
delete a record from a B+ tree.
Each scenario causes a different action in the delete algorithm.
Case IV
Case III
Case II
Case I
Not Below Fill Factor Below Fill Factor
Below Fill Factor Below Fill Factor
Below Fill Factor Not Below Fill Factor
Not Below Fill Factor Not Below Fill Factor
Index Page Leaf Page
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 186
Deletion
Case I & II:
Delete the record from the leaf page. Arrange keys in
ascending order to fill void.
If the key of the deleted record appears in the index
page, use the next key to replace it.
Case III:
Combine the leaf page and its sibling / Shift data from
sibling.
Change the index page to reflect the change.
63
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 187
Deletion
Case IV
1. Combine the leaf page and its sibling.
2. Adjust the index page to reflect the change.
3. Combine the index page with its sibling.
Continue combining index pages until you reach a page
with the correct fill factor or you reach the root page.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 188
B+ tree: Deletion Illustrations
Consider the B+ tree after we added 95 as a
key
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 189
Delete  Case I
Delete 70 from the B+ Tree
64
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 190
Delete Case I B
Delete the record containing 25 from the B+ tree.
This record is found in the leaf node containing 25,
28, and 30.
The fill factor will be 50% after the deletion;
however, 25 appears in the index page.
Thus, when we delete 25 we must replace it with
28 in the index page.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 191
Delete Case I B
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 192
Delete Case IV
Delete 60 from the B+ tree. Points to consider:
The leaf page containing 60 (60 65) will be
below the fill factor after the deletion. Thus,
we must combine leaf pages.
With recombined pages, we must readjust the
index pages to reflect the change.
65
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 193
Delete : Case IV
Before delete 60
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 194
After Delete 60
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 195
After Delete 50
55 65
55
Before delete 55
66
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 196
Delete Case III
65
65
Delete 55: Invalid tree
Sol: Merge Nodes
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 197
Delete Case III
65
75 85
After Delete 55
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 198
What we Studied
B+ Tree
Problem with B Tree
Solution as B+ Tree
Basic Operations
67
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63
199
GRAPHS
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 200
Learning Objectives
Graphs
Terminology
Representation
Basic operations
DFS Traversal
BFS Traversal
Transitive Closure
Warshells Algorithm
Topological Sort
Shortest Path
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 201
Dijkstras Algorithm
Minimum Spanning Tree
Prims Algorithm
Kruskals Algorithm
Critical Path Analysis
Learning Objectives Cont..
68
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 202
Graphs
Is a nonlinear data structure
Used in order to represent a scenario where there can be a
many to many relationship between parent to child nodes
Graphs serve as models for various processes and structures
like:
Airline routes
Flowchart of a program
Message transmission in a network
Cities and highways
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 203
Mathematical Definition
A graph G consists of two sets
V whose members are vertices of G
E whose members are pairs of vertices form V , and are
termed as edges of G
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 204
Example
The graph in the figure given above is represented as
V {v1, v2, v3, v4}
E { (v1,v4), (v4,v3), (v3, v2), (v2,v1) }
v1
v2
v3 v4
69
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 205
Terminology
In the pair of vertices representing an edge are
unordered => the graph is said to be undirected graph
ordered => the graph is said to be directed graph or a
diagraph
Weighted Graph: A graph having a weight / cost associated
with each edge
Adjacent Vertices: Vertex v1 is said to be adjacent to v2 if there
is an edge from v1 to v2
Path: A sequence of distinct vertices each adjacent to the next
Cycle: A path containing at least three vertices such that the last
vertex is adjacent to first.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 206
Computer Representation
While writing a program a graph can be implemented
using:
Matrices: Adjacency Matrix representation
Linked Lists: Adjacency List representation
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 207
Adjacency Matrix Representation
Adjacency Matrix
A twodimensional array of boolean values
Assuming each vertex is represented by an index number
An element A[I][J] is set to true if and only if the vertex I
is adjacent to vertex J
If the graph is undirected then A[I][J] = A[J][I]
70
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 208
Adjacency Matrix Representation
The structure:
struct Graph {
int CountOfVertices;
Boolean AdjMatrix [MAX][MAX];
};
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 209
Adjacency Matrix Representation
v1
v2
v3 v4
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
V4
V3
V2
V1
0
1
2
3
S
o
u
r
c
e
V
e
r
t
i
c
e
s
Destination Vertices
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 210
Linked Representation
Represented using two lists
A list of vertices; and
For each vertex, a list of adjacent vertices
The lists in turn can be represented as
contiguous lists; or
linked lists
A common approach is a mixed representation
where:
Vertices are represented by a contiguous list; and
List of vertices adjacent to each vertex are stored as a
linked list.
71
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 211
Linked Representation: Mixed Approach
v1
v2
v3 v4
V4
V3
V2
V1
0
1
2
3
v4
v1
v2
v2 v3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 212
Linked Representation: Structure
struct SNode {
LabelType Label; // can be used to name
vertices
struct SNodeNode * First;
};
struct DNode {
int Index;
struct DNode *next;
};
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 213
Linked Representation: Structure
struct Graph {
int count; //number of
vertices;
struct SNode Vertices [MAX];
};
72
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 214
Basic Operations on Graphs
Creation / Building
Add a node
Remove a node
Add an edge
Remove an edge
Simple Operations
Generate Adjacency list of a node
Calculate indegree for a node
Calculate outdegree for a node
Traversal
Depth First
Breadth First
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 215
Adding / Removing a Node
Matrix Representation:
Will require adjusting the size of adjacency matrix
Mixed Representation
Will require adjusting the size of contiguous list that
represents vertices
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 216
Adding / Removing an Edge
Matrix Representation:
Will require setting the corresponding element in the
matrix to 0/1
Mixed Representation
Will require adding / removing a link to the list
representing adjacent nodes.
73
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 217
Graph Traversals
Depth First
Breadth First
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 218
DFS Traversal
Roughly analogous to preorder traversal of a tree
Start
1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 219
DFS Traversal
Start
1
2
74
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 220
DFS Traversal
Start
1
2 3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 221
Start
1
2
3
4
DFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 222
Start
1
2
3
4 5
DFS Traversal
75
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 223
Start
1
2
3
4 5
6
DFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 224
Start
1
2
3
4 5
6 7
DFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 225
Start
1
2
3
4 5
6 7
8
DFS Traversal
76
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 226
Start
1
2
3
4 5
6 7
8
9
DFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 227
Push (Stk, VStart)
While Stk Is Not Empty
Vtemp := Pop (Stk)
If VTemp is Not Marked
Process VTemp
Mark VTemp
For Each VAdj Adjacent to VTemp
Push (Stk, VAdj)
End For
End IF
End While
Push (Stk, VStart)
While Stk Is Not Empty
Vtemp := Pop (Stk)
If VTemp is Not Marked
Process VTemp
Mark VTemp
For Each VAdj Adjacent to VTemp
Push (Stk, VAdj)
End For
End IF
End While
DFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 228
BFS Traversal
Roughly analogous to levelorder traversal of a tree
Start
1
77
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 229
Start
1
2
3
4
BFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 230
Start
1
2
3
4
5
BFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 231
Start
1
2
3
4
5
6
BFS Traversal
78
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 232
Start
1
2
3
4
5
6
7
BFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 233
Start
1
2
3
4
5
6
7
8
BFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 234
Start Start
1
2
3
4
5
6
7
8
9
BFS Traversal
79
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 235
Enqueue (Q, VStart)
While Q Is Not Empty
Dequeue (Q, VTemp)
If VTemp is Not Processed
Process VTemp
Mark VTemp
For Each VAdj Adjacent to VTemp
Enqueue (Q, VAdj)
End For
End IF
End While
Enqueue (Q, VStart)
While Q Is Not Empty
Dequeue (Q, VTemp)
If VTemp is Not Processed
Process VTemp
Mark VTemp
For Each VAdj Adjacent to VTemp
Enqueue (Q, VAdj)
End For
End IF
End While
BFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63
236
TRANSITIVE CLOSURE
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 237
Transitive Closure
The transitive closure of a graph G:
A graph G* is the transitive closure of G if and only if
For any two edges {a,b}, {b,c} there exists {a,c}
i.e., {a,b} G*, if path exists from a to b in G.
Finding Transitive Closure
Matrix Multiplication Method
Warshalls Algorithm
Can be used to check reach ability, i.e. what other
nodes can be reached from what node
80
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 238
Matrix Multiplication Method
Given the adjacency Matrix of a Graph G as A
Paths of length d: A
d
= A x A x . A (d times)
We can define the path matrix of order d as:
A matrix where M[i][j]= Count of paths of length d or less
between V
i
and V
j
path
d
= A + A
2
+ A
3
+ + A
d
Transitive Closure: Substitute 1 for each nonzero
entry in the path matrix of order N
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 239
Warhsalls Algorithm
Aim
Calculate the transitive closure
Basic Working
Initialize the Path Matrix using the Adjacency Matrix
(i.e. P[I][J] is set if there is a direct path from Vi to Vj)
During k
th
iteration set V[I][J]
if there is a path from Vi to Vj
either directly or via Vk
Result
A matrix whose element P[I][J] is set if there is a path
from Vi to Vj wither directly or indirectly
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 240
Copy A to P
For K=0 to N1
For I=0 to N1
For J=0 to N1
P[I][J]= P[I][J]  ( P[I][K] & P[K][J])
End For
End For
End For
Copy A to P
For K=0 to N1
For I=0 to N1
For J=0 to N1
P[I][J]= P[I][J]  ( P[I][K] & P[K][J])
End For
End For
End For
Warhsalls Algorithm
81
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 241
Example: Warshalls Algorithm
v1
v2
v3 v4
0 1 1 0
0 0 1 0
0 0 0 1
1 0 0 0
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 242
0 1 1 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 1 0
0 0 1 0
1 0 0 1
1 0 0 0
After K = 0
Example: Warshalls Algorithm
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 243
0 1 1 0
0 0 1 0
1 0 0 1
1 0 0 0
1 1 1 1
1 0 1 1
1 0 0 1
1 0 0 0
After K = 1
Example: Warshalls Algorithm
82
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 244
1 1 1 1
1 0 1 1
1 0 0 1
1 0 0 0
1 1 1 1
1 0 1 1
1 0 0 1
1 0 0 0
After K = 2
Example: Warshalls Algorithm
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 245
1 1 1 1
1 0 1 1
1 0 0 1
1 0 0 0
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
After K = 3
Example: Warshalls Algorithm
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63
246
TOPOLOGICAL SORT
83
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 247
Topological Sort
Ordering of vertices in a directed acyclic graph,
such that:
if there is a path from V
i
to V
j
then Vi precedes after
V
j
in the sorting
Topological Sorting is not possible for a graph
containing cycles.
As, for two vertices v and w in a cycle
V precedes w and w precedes v
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 248
Applications & Strategy
Topological Sorting can be used to:
Cyclic dependencies in formulae
Glossary of terms so that no term appears before it is
defined
Strategy:
Find a vertex with no incoming edges (indegree zero)
Process it
Remove it from graph
Continue with the same strategy till there are no more
vertices in graph.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 249
Example
V1
V2
V3 V4
V5
V6 V7
84
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 250
Results
 0 1 1 2 3 2 V7
0 1 1 2 3 3 3 V6
  0 0 1 2 2 V5
    0 1 2 V4
   0 1 1 3 V3
     0 1 V2
      0 V1
Indegrees before removing V#
V6 V7 V5 V3 V4 V2 V1 O/P
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 251
Algorithm: Topological Sort
Calculate InDegree for all vertices >
IDeg [N]
For I = 1 to N
If Ideg [I] == 0
Enqueue (Q, V[I])
End If
End For
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 252
While Q Is Not Empty
Dequeue (Q, VTemp)
TopSort [Count++] < VTemp
For Each VAdj Adjacent to VTemp
Decrement InDegree of VAdj
If InDegree of VAdj == 0
Enqueue (Q, VAdj)
End IF
End For
End While
Cont..
85
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 253
If Count < N
Graph has a Cycle
Else
Return TopSort
End If
Cont..
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63
254
SHORTEST PATH
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 255
Shortest Path
Applications
Finding shortest route from location X to location Y
Finding communication paths with least cost in a
computer network
Finding transit paths with least cost in a railway network
And many more
Problems
Finding shortest paths from a distinguished vertex S to
every other vertex in a weighted graph G
Finding shortest paths between all possible pairs of
vertices in a weighted graph G
86
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 256
Dijkstras Algorithm
Used in order to find the shortest paths from a distinguished
vertex S to every other vertex in a weighted graph G
Termed as Single Source Shortest Path algorithm
Technique:
Select the vertex V having the least D
v
among all the
unknown vertices
Declare the smallest path from S to V to be known
For all unknown W adjacent to V, set D
w
= D
v
+ C
v,w
if this
value is lesser than the current D
w
Set V to be the node that precedes W on the path from S
to W
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 257
Dijkstras SSSP
v5
v1 v4
v2
v3
30
8
10
4
4
6 6
0 0 V5
0 0 V4
0 0 V3
0 0 V2
0 0 1 V1
P D Kno
wn
Initially
Initially
5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 258
Dijkstras SSSP
Start Point V1 =>
D(V1)=0
Unknown Vertex with Smallest Distance > V1
Declare V1 as Known
Consider Unknown Vertices adjacent to V1 (V2, V5)
D(V1) + D(V1, V2) < D(V2) => Update D(V2) & P(V2)
D(V1) + D(V1, V5) < D(V5) => Update D(V5) & P(V5)
87
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 259
Dijkstras SSSP: After V1 is Known
v5
v1 v4
v2
v3
30
8
10
4
4
6
V1 30 0 V5
0 0 V4
0 0 V3
V1 4 0 V2
0 0 1 V1
P D Kno
wn
6
5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 260
Dijkstras SSSP
Unknown Vertex with Smallest Distance
V2
Declare V2 as Known
Consider Unknown Vertices adjacent to V2 (V3, V4)
D(V2) + D(V2, V3) < D(V3) => Update D(V3) & P(V3)
D(V2) + D(V2, V4) < D(V4) => Update D(V4) & P(V4)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 261
Dijkstras SSSP: After V2 is Known
v5
v1 v4
v2
v3
30
8
10
4
4
6
V1 30 0 V5
V2 10 0 V4
V2 14 0 V3
V1 4 1 V2
0 0 1 V1
P D Kno
wn
6
5
88
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 262
Dijkstras SSSP
Unknown Vertex with Smallest Distance
V4
Declare V4 as Known
Consider Unknown Vertices adjacent to V4 (V5)
D(V4) + D(V4, V5) < D(V5) => Update D(V5) & P(V5)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 263
Dijkstras SSSP: After V4 is Known
V4 18 0 V5
V2 10 1 V4
V2 14 0 V3
V1 4 1 V2
0 0 1 V1
P D Kno
wn
v5
v1 v4
v2
v3
30
8
10
4
4
6 6
5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 264
Dijkstras SSSP
Unknown Vertex with Smallest Distance
V3
Declare V3 as Known
Consider Unknown Vertices adjacent to V3 (V5)
D(V3) + D(V3, V5) > D(V5) =>
No Updation required for V5
89
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 265
Dijkstras SSSP: After V3 is Known
V4 18 0 V5
V2 10 1 V4
V2 14 1 V3
V1 4 1 V2
0 0 1 V1
P D Kno
wn
v5
v1 v4
v2
v3
30
8
10
4
4
6 6
5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 266
Dijkstras SSSP
Unknown Vertex with Smallest Distance
V5
Declare V5 as Known
Consider Unknown Vertices adjacent to V5
None
Shortest Paths to all nodes starting from V1 are known
Done
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 267
Dijkstras SSSP: Data
Struct NodeInfo
{
LabelType Label;
Boolean Known;
DistanceType Distance;
Vertex Previous;
};
90
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 268
Dijkstras SSSP: Initialization
Create an array of type NodeInfo
For all elements
Set Known to False
Set Distance to Infinity
Set Previous to NULL
For the Start node
Set Known to True
Set Distance to 0
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 269
Dijkstras SSSP: Algorithm
Void Dijkstra (Graph G, Vertex Start)
Begin
NodeInfo Nodes [COUNT];
Initialize (Nodes, Start);
While (TRUE)
VLoc= SmallestDistanceUnknownVertex ();
if ( VLoc == 1 )
break;
Nodes [VLoc]. Known = TRUE;
Void Dijkstra (Graph G, Vertex Start)
Begin
NodeInfo Nodes [COUNT];
Initialize (Nodes, Start);
While (TRUE)
VLoc= SmallestDistanceUnknownVertex ();
if ( VLoc == 1 )
break;
Nodes [VLoc]. Known = TRUE;
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 270
Dijkstras SSSP: Algorithm
For Each VAdj AdjacentTo VLoc
If ( ! Nodes[VAdj].Known )
Temp=Nodes[Vloc].Distance+Cost(Vloc,
Vadj)
IF ( Temp < Nodes[Vadj].Distance)
Nodes[Vadj].Distance= Temp;
Nodes[Vadj].Previous= Vloc;
End IF
End IF
End For
End While
End
For Each VAdj AdjacentTo VLoc
If ( ! Nodes[VAdj].Known )
Temp=Nodes[Vloc].Distance+Cost(Vloc,
Vadj)
IF ( Temp < Nodes[Vadj].Distance)
Nodes[Vadj].Distance= Temp;
Nodes[Vadj].Previous= Vloc;
End IF
End IF
End For
End While
End
91
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 271
To Do
Use Recursion to print the actual path
Use Recursion to print the actual path
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 272
Shortest Paths for All Pairs of
Vertices: Floyd Warshalls
Aim:
Find shortest weighted path between each pair of
vertices in of directed graph
Algorithm:
FloydWarshalls
Strategy:
As an extension of Warshalls algorithm, apart from
checking whether there is a path from Vi to Vj via Vk
Calculate the new path length D[I][J] as D[I][K] + D[K][J]
Use the shorter of the two and set previous vertex
accordingly.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 273
All Pair Shortest Path
D: Distance / Cost Matrix
P: Next Node
Initialization
If there is no edge connecting Vi & Vj then set D[I][J] >
Else set D[I][J] to the weight of the edge (Vi, Vj)
Set P[I][J] to NULL
92
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 274
All Pair Shortest Path: Illustration
A B
C
D
2 5
2
3
   
   
   
   
3
2
5
2
Initialization
Initialization
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 275
All Pair Shortest Path: Illustration
Update D[I][J] if
D[I][0] + D[0][J] < D[I][J]
The above condition is false for all (Vi, Vj)
No Updations are made
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 276
All Pair Shortest Path: Illustration
A B
C
D
2 5
2
3
   
   
   
   
3
2
5
2
After First Iteration
After First Iteration
2 5
2
3
93
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 277
All Pair Shortest Path: Illustration
Update D[I][J] if
D[I][1] + D[1][J] < D[I][J]
D[2][1] + D[1][0] = 5 < D[2][0]
Thus;
Update D[2][0] to 5
Set P[2][0] to 1
D[3][1] + D[1][0] = 8 < D[3][0]
Thus
Update D[3][0] to 8, Set P[3][0] to 1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 278
All Pair Shortest Path: Illustration
A B
C
D
2 5 8
2 5
3
   1
   1
   
   
3
2
5
2
After Second Iteration
After Second Iteration
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 279
All Pair Shortest Path: Illustration
Update D[I][J] if
D[I][2] + D[2][J] < D[I][J]
D[3][2] + D[2][0] = 7 < D[3][0]
Thus;
Update D[3][0] to 7
Set P[2][0] to 2
D[3][2] + D[2][1] = 4 < D[3][1]
Thus
Update D[3][1] to 4, Set P[3][1] to 2
94
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 280
All Pair Shortest Path: Illustration
A B
C
D
2 4 7
2 5
3
  2 2
   1
   
   
3
2
5
2
After Third Iteration
After Third Iteration
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 281
All Pair Shortest Path: Illustration
Update D[I][J] if
D[I][3] + D[3][J] < D[I][J]
The above condition is false for all (Vi, Vj)
No Updations are made
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 282
All Pair Shortest Path: Illustration
A B
C
D
2 4 7
2 5
3
  2 2
   1
   
   
3
2
5
2
After Fourth Iteration
After Fourth Iteration
95
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 283
All Pair Shortest Path: Illustration
Finding Cost of traveling from D to A
D[3][0]= 7 => Path Exists
Finding Path from D to A
P[3][0]= 2
P[2][0]= 1
P[1][0]= NULL but D[1][0] is not Infinity
Thus, there is a direct edge from B to A
Path: 3, 2, 1, 0 or D, C, B, A
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 284
All Source Shortest Path: Algorithm
Void FloydWarshall
( TwoDimArray D, TwoDimArray P, Int N)
Begin
For K=0 to N1
For I=0 to N1
For J=0 to N1
If ( D[I][J] < D[I][K] + D[K][J] )
Begin
D[I][J] = D[I][K] + D[K][J]
P[I][J]= K
End
End
Void FloydWarshall
( TwoDimArray D, TwoDimArray P, Int N)
Begin
For K=0 to N1
For I=0 to N1
For J=0 to N1
If ( D[I][J] < D[I][K] + D[K][J] )
Begin
D[I][J] = D[I][K] + D[K][J]
P[I][J]= K
End
End
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 285
To Do
Use Recursion to print the actual path
Use Recursion to print the actual path
96
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63
286
MINIMUM SPANNING TREES
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 287
Minimum Spanning Tree
Definition
A tree formed from edges of graph G that connects all the
the vertices of G at lowest total cost.
A minimum spanning tree exists if and only if G is connected
The number of edges in a minimum spanning tree is
Count(Vertices) 1
Applications
Finding minimum cost of laying down cables
Finding minimum cost of constructing roads
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 288
Common Algorithms
Prims
Kruskals
97
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 289
Prims Algorithm
The actual algorithm is a slight variation of Dijkstras
algorithm
D
v
is the weight of shortest arc connecting V to a
known vertex.
After V is selected for each unknown W adjacent to V
D
w
= Min (D
w
, C
w,v
)
The final tree is obtained from the collection of
selected edges in the table
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 290
Prims: Illustration
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
 0 V6
 0 V5
 0 V4
 0 V3
 0 V2
 0 1 V1
9
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 291
Prims: Illustration
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
 0 V6
V1 11 0 V5
V1 9 0 V4
V1 5 0 V3
V1 10 0 V2
 0 1 V1
9
98
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 292
Prims: Illustration
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
 0 V6
V1 11 0 V5
V1 9 0 V4
V1 5 1 V3
V1 10 0 V2
 0 1 V1
9
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 293
Prims: Illustration
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
 0 V6
V3 8 0 V5
V3 7 0 V4
V1 5 1 V3
V1 10 0 V2
 0 1 V1
9
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 294
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
 0 V6
V3 8 0 V5
V3 7 1 V4
V1 5 1 V3
V1 10 0 V2
 0 1 V1
9
Prims: Illustration
99
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 295
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
V4 8 0 V6
V3 8 0 V5
V3 7 1 V4
V1 5 1 V3
V1 10 0 V2
 0 1 V1
9
Prims: Illustration
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 296
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
V4 8 0 V6
V3 8 1 V5
V3 7 1 V4
V1 5 1 V3
V1 10 0 V2
 0 1 V1
9
Prims: Illustration
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 297
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
V4 8 0 V6
V3 8 1 V5
V3 7 1 V4
V1 5 1 V3
V5 6 0 V2
 0 1 V1
9
Prims: Illustration
100
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 298
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
V4 8 0 V6
V3 8 1 V5
V3 7 1 V4
V1 5 1 V3
V5 6 1 V2
 0 1 V1
9
Prims: Illustration
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 299
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
V4 8 1 V6
V3 8 1 V5
V3 7 1 V4
V1 5 1 V3
V5 6 1 V2
 0 1 V1
9
Prims: Illustration
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 300
To Do
Modify Dijkstras algorithm to get Prims
algorithm for Minimum Spanning Tree.
Modify Dijkstras algorithm to get Prims
algorithm for Minimum Spanning Tree.
101
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 301
Kruskals Algorithm
Arrange all edges in increasing order of weight
Starting with the first edge (edge with minimum
weight)
Accept an edge into the tree if it doesnt lead to a cycle
Else reject it
Move on to the next edge
Break when number of edges = Count(Vertices)1
Note: Tree can be maintained as a graph in adjacency
matrix form to check for cycles if required.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 302
Kruskals Algorithm: Data
Struct EdgeInfo
{
Int StartIndex;
Int EndIndex;
WeightType Weight;
Int Status;
};
Struct EdgeInfo
{
Int StartIndex;
Int EndIndex;
WeightType Weight;
Int Status;
};
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 303
Kruskals: Illustration
v1
v2
v3
v4 v5
v6
8
10
11
7
5
8
11
10
6
21
21 V4 V2
11 V5 V4
11 V5 V1
10 V6 V5
10 V2 V1
9 V4 V1
8 V5 V3
8 V6 V4
7 V4 V3
6 V5 V2
5 V3 V1
Count of edges = 5
= Count (Vertices) 1
Hence: Done
Count of edges = 5
= Count (Vertices) 1
Hence: Done
102
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 304
4
2
10
1
3
2 7
5
8 4
6
1
v1 v2
v3
v4
v5
v6
v7
10 V5 v2
8 V6 v4
7 V5 v4
6 V7 V5
5 V6 V3
4 V7 V4
4 V3 V1
3 V4 V2
2 V4 V3
2 V2 V1
1 V7 V6
1 V4 V1
Count of edges = 6
= Count (Vertices) 1
Hence: Done
Count of edges = 6
= Count (Vertices) 1
Hence: Done
Kruskals: Illustration
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63
305
CRITICAL PATH ANALYSIS
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 306
Critical Path Analysis
A common set of problems that are faced while planning
allocating resources for projects:
Estimating the earliest completion time for the project.
Determining the activities that can be delayed (and by
how long) without affecting the maximum completion time
The above said calculations can be made by modeling the
project and its subactivities as an acyclic graph
The technique is termed as critical path analysis.
103
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 307
ActivityNode Graph
A graph used to model various activities involved in a
project.
Each Node (Vertex) in the graph represents
An activity that needs to be performed towards the
completion of the project.
The time required to complete the activity.
Each edge of the graph represents the relationship
between two activities.
An edge (v, w) means activity v must be completed
before activity w begins
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 308
EventNode Graph
To perform the calculations the ActivityNode graph
is transformed to an EventNode graph
In an EventNode graph
each node corresponds to completion of an activity and
all its dependent activities.
As in activitynode graph, here also, the events
reachable from a node v cannot commence until
the even v is over.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 309
Activity Node Graph to Event Node Graph
Rename ActivityNodes to EventNodes.
Label Edges with the corresponding Event and the
time required.
When an activity v depends on multiple activities,
Introduce dummy edge/nodes that represent an
intermediate state.
This state signifies that all those activities whose
completion is a prerequisite for starting the activity v
are over.
104
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 310
Illustration: AN Graph
start
B(2)
A(3)
C(3)
D(2)
E(1)
F(3)
G(2)
K(1)
H(1)
Finish
An ActivityNode Graph
An ActivityNode Graph
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 311
1
2
3
6
4
6
5
8
7
8
7
9
10 10
Here, n is an event introduced prior to an event n that, in turn,
depends on multiple events
Time to reach n from all the activities on which it depends is
zero.
Here, n is an event introduced prior to an event n that, in turn,
depends on multiple events
Time to reach n from all the activities on which it depends is
zero.
A/3
B/2
C/3
D/2
E/1
F/3
G/2
K/4
H/1
0
0
0
0
0
0
0
0
0
Illustration: AN Graph
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 312
Earliest Completion Time
The earliest completion time of the project is
given by
the length of the longest path
from the first event
to the last event.
The earliest completion time for an activity is given
by the relation:
EC
1
= 0
EC
w
= max (EC
v
+ c
v,w
) (For all the edges v,w existing in
the graph)
105
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 313
Illustration: EC Time
1
2
3
6
4
6
5
8
7
8
7
9
10 10
A/3
B/2
C/3
D/2
E/1
F/3
G/2
K/4
H/1
0
0
0
0
0
0
0
0
0
0
3
2
6
3
5
3
6
5
9
7
7
9 10
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 314
Latest Completion Time
The latest completion time for all the events is also an
important factor that decides whether a project can
be completed in time or not.
The latest completion time for an activity is:
The latest time that an event E
i
can take to complete
without affecting the final completion time
The value is given by the relation:
LC
n
= Ec
n
*
LC
v
= min (LC
w
 c
v,w
) (For all the edges v,w existing in the
graph)
We want to finish the project earliest possible
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 315
Illustration: LC Time
1
2
3
6
4
6
5
8
7
8
7
9
10 10
A/3
B/2
C/3
D/2
E/1
F/3
G/2
K/4
H/1
0
0
0
0
0
0
0
0
0
0
3
2
6
3
5
3
6
5
9
7
7
9 10
9
9
9
6
7
6
5
4
4
3
0
9
6
10
106
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 316
Slack Time
Slack time for each edge in in an event node graph
implies
The amount of time
That the completion of the corresponding activity can
be delayed
Without delaying the overall completion.
Slack time for an activity is given by:
Slack
(v,w)
= LC
w
(EC
v
+ c
v,w
)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 317
A/3
B/2
C/3
D/2
E/1
F/3
G/2
K/4
H/1
Illustration: Slack Time
1
2
3
6
4
6
5
8
7
8
7
9
10 10
A/3/0
B/2/2
C/3/0
D/2/1
E/1/2
F/3/0
G/2/2
K/4/2
H/1/0
0
3
2
6
3
5
3
6
5
9
7
7
9 10
9
9
9
6
7
6
5
4
4
3
0
9
6
10
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 318
Critical Path
Critical Activities
Activities with zero slack
Must complete on time to ensure timely completion of
project
Critical Path
Path on which all the activities are zeroslack activities.
Thus implying that all the activities on the path must
complete on their scheduled time
As, there is no buffer period
There will be atleast on such path on the graph
107
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 319
What we Studied
Graphs
Terminology
Representation
Basic operations
DFS Traversal
BFS Traversal
Transitive Closure
Warshells Algorithm
Topological Sort
Shortest Path
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 320
Dijkstras Algorithm
Minimum Spanning Tree
Prims Algorithm
Kruskals Algorithm
Critical Path Analysis
What we Studied Cont..
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 321
Conclusion
Trees
Binary trees
AVL Trees
Binary Tree Applications
Threaded Tree
B Trees
B* Trees
B+ Trees
Graphs
spanning Trees
Shortest Path
Transitive Closure
Topological Sort
Critical Path
Dijkstras Algorithm
Warshalls Algorithm
Spanning Tree
Kruskals Algorithm
Prims Algorithm
108
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 322
Review Questions (Objective)
1. How many different trees are possible with 10 nodes ?
2. How many null branches are there in a binary tree with 20
nodes?
3. There are 8, 15, 13, 14 nodes were there in 4 different
trees. Which of them could have formed a full binary tree?
4. What is a graph.give example.
5. In an AVL tree, at what condition the balancing is to be
done?
6. In tree construction which is the suitable efficient data
structure
7. Define full tree and complete tree.
8. Define threaded tree
9. Define Binary Tree
10. Define AVL Tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 323
Review Questions( Short Type)
1. What is binary tree.Discuss its properties.Give algorithm to traverse the tree in
inorder
preorder
post order
2. How a binary tree is different from binary search tree.
3. Discuss the various methods of representing graph in memory aother than
adjacency matrix in brief.
4. Write a C function to insert an element into an AVL tree.
5. Define Threaded Binary tree. Write an algrothim for preorder trrnversal of
threaded binary tree without a stack.
6. Define Binary trees.How it can be represented in the memory.
7. What are the ways of traversing the binary trees.Describe them
8. How the insertions take place in the Binary search trees.Give algorithm..
9. What are the various ways to store tree in memory.Discuss in brief.
10. Define binary search tree with example.
11. What is the maximum total number of nodes in a tree that has N levels? Note
that the root is level (zero)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 324
Cont..
12. How many different binary trees and binary search trees can be made
from three nodes that contain the key values 1, 2 & 3?
13. How will inorder, preorder and postorder traversals print the elements
of a tree?
14. Draw the binary tree with threads to indicate the post order traversal for
the expression A  B + C * E/F
15. Draw a binary Tree for the expression : A * B  (C + D) * (P / Q)
16. Draw the Btree of order 3 created by inserting the following data
arriving in sequence  92 24 6 7 11 8 22 4 5 16 19 20 78
17. Construct a binary tree whose nodes are in two orders as under :
inorder : B, C, E, D, F, A, G, H
Preorder : A, B, C, D, E, F, G, H
109
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 325
Review Questions (Long Type)
1. What is Graph ? Can a tree can be Graph ? Name various applications of
Graph.
2. Write a function to delete a node from a binary search tree.
3. Write a short note on Optimal Binary Search Tree.
4. What do you mean by graph? How are graphs represented in C.
20.Give a brief description of (a) traversing (b) sorting (c) searching.
5. A binary tree T has 9 nodes .The inorder and preorder traversals of T yield the
following sequence of nodes
Inorder : E A C K F H D B G
Preorder : F A E K C D H G B
6. Draw the tree T
7. Consider the algebric expression E=(2x+y)(5ab)^3
8. Draw the Tree T which correspond to expression E.
9. Draw all the non similar trees T where:
T is a binary tree with 3 nodes
T is a 2tree with 4 external nodes.
10. Write the algorithms of searching in the binary search trees.
11. Give the algorithm of deletions in the binary search trees. Explain with
example.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 326
Cont..
12. Suppose the following list of letters is inserted in order into an empty binary
search tree:
J,R,D,G,T,E,M,H,P,A,F,Q
a) Find the final tree T b)Find inorder traversal of T
13. Suppose a binary tree T is in memory .Write a procedure which deletes all the
terminal nodes in T.
14. Write short notes on
Complete binary tree
Weight of a tree
Binary search tree
Heap
15. What do u mean by tree traversal.Explain the different tree traversals giving
suitable examples.
16. Define graph.How a graph is different from a tree.Write an algorithm to find the
shortest distance between two nodes of a graph.
17. What do you mean by height balanced tree.How an height balanced tree is
different from a binary search tree.What do u meen by rebalancing of height
balanced tree. Explain with example.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 327
Cont..
18. Write short notes on
Simple graph
Multi graph
Acyclic graph
Connected graph.
110
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi63. 328
References
Fundamentals of Data Structures, E. Horowitz and S. Sahani,
Galgotia Booksource Pvt. Ltd., (1999)
Data Structures and Algorithm Analysis in C (Second Edition)
by Mark Allen Weiss
Data Structures: A Pseudocode Approach with C, Second Edition
Richard Gilberg, Behrouz Forouzan
Data Structures and program design in C, R. L. Kruse, B. P.
Leung, C. L. Tondo, PHI.
Data Structure, Schaums outline series, TMH, 2002
Data Structures using C and C++, Y. Langsam et. al., PHI (1999).
Data Structures, N. Dale and S.C. Lilly, D.C. Heath and Co. (1995).
Data Structure & Algorithms, R. S. Salaria, Khanna Book
Publishing Co. (P) Ltd., 2002.