You are on page 1of 110

# 1

## Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63 1

DATA STRUCTURE
TREES & GRAPHS
UNIT II
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 2
Learning Objectives
Trees
Binary trees
AVL Trees
Binary Tree Applications
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 Delhi-63 3
TREES
2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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, B-Tree: 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, Trees-Binary
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 Delhi-63. 6
Linear Lists And Trees
Linear lists are useful for serially ordered data.
(e
0
, e
1
, e
2
, , e
n-1
)
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 Delhi-63. 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 Delhi-63. 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
sub-trees of the tree t.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 9
Sub-tree, 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 Delhi-63. 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
A pointer to a node in a tree.
Means of traversing the tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 sub-trees of
the binary tree.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 16
The sub-trees 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 Delhi-63. 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 Delhi-63. 18
Binary Tree Traversal Methods
Preorder
Inorder
Postorder
Level order
7
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 19
Preorder Example (visit = print)
a
b c
a b c
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 23
Inorder Example (visit = print)
a
b c
b a c
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 27
Postorder Example (visit = print)
a
b c
b c a
10
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 40
Stack Based Traversal (Pre-order)
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 Delhi-63. 41
Pre-Order
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 Delhi-63. 42
a
b c
d
e
f
g
h i
j Output
Stack
a
c
b
e
d
g h e
Pre-Order
15
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 43
a
b c
d
e
f
g
h i
j Output
Stack
a
c
b
i
d
g h e i
Pre-Order
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 44
a
b c
d
e
f
g
h i
j Output
Stack
a
c
b
d
g h e i c
Pre-Order
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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
Pre-Order
16
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 46
Stack Based Traversal (In-order)
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 Delhi-63. 47
Stack Based Traversal (Post-order)
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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 50
Level-Order 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 Delhi-63. 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 Delhi-63 52
AVL Trees
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 56
Unbalanced Tree: Example
Worst case search!!!
Requires --------
operations
Worst case search!!!
Requires --------
operations
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 57
AVL Trees
AVL trees are named after two Russian
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 Delhi-63. 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 Delhi-63. 59
AVL Trees: Examples
Legal AVL, but not
balanced
AVL
Non-Legal
AVL
AVL and balanced
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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
Needs Balancing Operations
0
-1
0
0
-1
-2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 (Left-Left Imbalance).
2.Addition in right subtree of left child (Left-Right
Imbalance).
3.Addition in right subtree of right child (Right-Right
Imbalance).
4.Addition in left subtree of right child (Right-Left
Imbalance).
22
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 64
Case I: Left-Left 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 Delhi-63. 65
Case II: Left-Right 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 Delhi-63. 66
Case III: Right-Right 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 Delhi-63. 67
Case IV: Right-Left 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 Delhi-63. 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 so-called Dog-leg pattern (II & IV)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 69
Restoring Balance: Case - I
10
5
2
10
5
2
24
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 70
Restoring Balance: Case - II
30
20
10
10
20
30
Thus, a single rotation, clockwise /
anticlockwise restores balance in
Thus, a single rotation, clockwise /
anticlockwise restores balance in
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 72
Restoring Balance: Case IV
10
20
15
10
15
20
20
15
10
Strategy:
1. Transfer extra weight in right branch
2. Rotate anti-clockwise
Strategy:
1. Transfer extra weight in right branch
2. Rotate anti-clockwise
25
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 75
Left-Left 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 right-right imbalance, by symmetry)
(Solve right-right imbalance, by symmetry)
26
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 76
Left-Left 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 Delhi-63. 77
Left-Right Imbalance: Before Rotation
Cant use the left-left
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 right-left imbalance by symmetry)
(Solve right-left imbalance by symmetry)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 78
LR Imbalance: Inadequacy of Single Rotation
Lets try to apply single
rotation in case of Left-right
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 Delhi-63. 79
Left-Right Imbalance: Double Rotation
B will have two subtrees
containing at least one
We do not know which is
too deep - set them
both to 0.5 levels below
subtree A so that
either can be
up / down
3
1
A
C
2
B1 B2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 80
Left-Right 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 Delhi-63. 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 re-balancing 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 sub-subtree will tell you which
rule to use (see diagrams on previous slides)
28
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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;
return temp; //the new root
END
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63 89
BINARY TREES:
APPLICATIONS
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 100
Building Expression Trees
a b + c d e + * *
I:
a b
II:
a b
+
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 101
Building Expression Tree
III:
a b
+
c d e
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 102
Building Expression Tree
IV:
a b
+
c
d e
+
35
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 103
Building Expression Tree
V:
a b
+
c
d e
+
*
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 104
Building Expression Tree
VI:
a b
+
c
d e
+
*
*
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 105
Building Expression Trees
While hasMoreSymbols (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)
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 Delhi-63. 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 Delhi-63
107
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 108
Learning Objectives
Binary Tree Traversal
Basic Operations
37
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 109
Binary Tree: Traversal
Traversal:
Recursive
Non-recursive
Both procedures use a stack to store information
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 Delhi-63. 110
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
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 Delhi-63. 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
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 Delhi-63. 112
Explanation
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 Delhi-63. 113
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 Delhi-63. 114
Variations
Depending upon traversal:
39
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 115
8
7 5
3
11
13
1
6
9
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 116
Start at the leftmost node in the tree,
print it,
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 Delhi-63. 117
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 Delhi-63. 118
8
7 5
3
11
13
1
6
9
Output
1
3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 119
8
7 5
3
11
13
1
6
9
leftmost node and print
Output
1
3
5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 120
8
7 5
3
11
13
1
6
9
Output
1
3
5
6
41
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 121
8
7 5
3
11
13
1
6
9
leftmost node and print
Output
1
3
5
6
7
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 122
8
7 5
3
11
13
1
6
9
Output
1
3
5
6
7
8
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 123
8
7 5
3
11
13
1
6
9
leftmost node and print
Output
1
3
5
6
7
8
9
42
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 124
8
7 5
3
11
13
1
6
9
Output
1
3
5
6
7
8
9
11
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 125
8
7 5
3
11
13
1
6
9
leftmost node and print
Output
1
3
5
6
7
8
9
11
13
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 126
43
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 128
Traversal: Alternate Approach
InorderSuccessor ( Node )
Begin
Succ <- Node:Right
While ( Succ:Left )
Succe <- Succ:Left
Return Succ
End
InorderSuccessor ( Node )
Begin
Succ <- Node:Right
While ( Succ:Left )
Succe <- Succ:Left
Return Succ
End
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 129
handling of two special cases
Why!!!
Consider adding the following data set
40, 30, 25, 45, 35, 50, 27
44
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 130
Insertion Example
40
After: 40
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 131
Insertion Example
40
30
After: 40, 30
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 132
Insertion Example
40
30
After: 40, 30, 25
25
45
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 133
40
30
After: 40, 30, 25, 45
25
45
Insertion Example
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 134
40
30
After: 40, 30, 25, 45, 35
25
45
35
Insertion Example
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 137
Begin
// Allocate Memory & Set Data
NewNode:Left <- NULL
NewNode:Right <- ------- //!!!
Father:Left <- NewNode
End
Begin
// Allocate Memory & Set Data
NewNode:Left <- NULL
NewNode:Right <- ------- //!!!
Father:Left <- NewNode
End
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 138
Begin
// Allocate Memory & Set Data
NewNode:Left <- NULL
NewNode:Right <- ------- //!!!
Father:Right <- NewNode
End
Begin
// Allocate Memory & Set Data
NewNode:Left <- NULL
NewNode:Right <- ------- //!!!
Father:Right <- NewNode
End
47
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 139
8
7 5
3
11
13
1
6
9
Left Child:
Right Child:
Left Child:
Right Child:
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 140
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 141
48
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 142
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 143
What we Studied
Binary Tree Traversal
Basic Operations
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63
144
B-Trees
Shalini Singh Jaspal, Lecturer, BVICAM 144
49
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 145
Learning Objectives
B-Tree
Motivations
Basic Operations
B* Tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 146
Motivation for B-Trees
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 Big-Oh 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 Delhi-63. 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 Delhi-63. 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
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 Delhi-63. 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 Delhi-63. 150
Definition of a B-tree
A B-tree of order m is an m-way tree (i.e., a tree where each
node may have up to m children) in which:
1. The number of keys in each non-leaf 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 non-leaf 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 Delhi-63. 151
An Example B-Tree
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 B-tree 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 Delhi-63. 152
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 B-tree 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 B-tree
1 2 8 12
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 153
Constructing a B-tree (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 Delhi-63. 154
Constructing a B-tree (contd.)
Adding 17 to the right leaf node would over-fill 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 Delhi-63. 155
Constructing a B-tree (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 Delhi-63. 156
Constructing a B-tree (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 Delhi-63. 157
Inserting into a B-Tree
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 Delhi-63. 158
Exercise in Inserting a B-Tree
Insert the following keys to a 5-way B-tree:
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 Delhi-63. 159
Removal from a B-tree
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 B-tree) 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
non-leaf deleted keys position.
54
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 160
Removal from a B-tree (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 Delhi-63. 161
Exercise in Removal from a B-Tree
Given 5-way B-tree 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 Delhi-63. 162
Analysis of B-Trees
The maximum number of items in a B-tree 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 Delhi-63. 163
Reasons for using B-Trees
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 B-tree of order 101, say, we can transfer each
node in one disc read operation
A B-tree 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 Delhi-63. 164
Cont..
If we take m = 3, we get a 2-3 tree, in which non-leaf nodes
have two or three children (i.e., one or two keys)
B-Trees are always balanced (since the leaves are all at
the same level), so 2-3 trees make a good type of
balanced tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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
Multi-way trees
B-Trees can be m-way, they can have any (odd) number
of children
One B-Tree, the 2-3 (or 3-way) B-Tree, 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 Delhi-63. 166
B* Trees
Requires non-root nodes to be at least 2/3 full
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 Delhi-63. 167
What we Studied
B-Tree
Motivations
Basic Operations
B* Tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63
168
B+ Trees
57
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 169
Learning Objectives
B+ Tree
Problem with B Tree
Solution as B+ Tree
Basic Operations
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 170
13 13
Problem With B Trees
Accessing keys from B-tree 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 Delhi-63. 171
Solution: B+ Trees
B+ Trees:
Facilitate Sequential Operations
String all leaf nodes together
Replicate keys from non-leaf nodes to make sure each
key appears at leaf level.
58
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 172
Properties of B-trees
B Trees
Are multi-way 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 Delhi-63. 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 Delhi-63. 174
An Example B+ Tree
Order: 5
Data Pages Data Pages
59
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 176
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 Delhi-63. 177
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 Delhi-63. 178
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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 183
Rotation Example
Before addition of record with key 70
62
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 189
Delete - Case I
Delete 70 from the B+ Tree
64
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 191
Delete Case I B
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 193
Delete : Case IV
Before delete 60
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 194
After Delete 60
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 195
After Delete 50
55 65
55
Before delete 55
66
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 196
Delete Case III
65
65
Delete 55: Invalid tree
Sol: Merge Nodes
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 197
Delete Case III
65
75 85
After Delete 55
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63
199
GRAPHS
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 202
Graphs
Is a non-linear 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 206
Computer Representation
While writing a program a graph can be implemented
using:
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 207
A two-dimensional 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
If the graph is undirected then A[I][J] = A[J][I]
70
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 208
The structure:
struct Graph {
int CountOfVertices;
};
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 209
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 Delhi-63. 210
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
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
71
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 211
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 Delhi-63. 212
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 Delhi-63. 213
struct Graph {
int count; //number of
vertices;
struct SNode Vertices [MAX];
};
72
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 214
Basic Operations on Graphs
Creation / Building
Remove a node
Remove an edge
Simple Operations
Generate Adjacency list of a node
Calculate in-degree for a node
Calculate out-degree for a node
Traversal
Depth First
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 215
Matrix Representation:
Mixed Representation
Will require adjusting the size of contiguous list that
represents vertices
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 216
Matrix Representation:
Will require setting the corresponding element in the
matrix to 0/1
Mixed Representation
73
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 217
Graph Traversals
Depth First
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 218
DFS Traversal
Roughly analogous to pre-order traversal of a tree
Start
1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 219
DFS Traversal
Start
1
2
74
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 220
DFS Traversal
Start
1
2 3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 221
Start
1
2
3
4
DFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 222
Start
1
2
3
4 5
DFS Traversal
75
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 223
Start
1
2
3
4 5
6
DFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 224
Start
1
2
3
4 5
6 7
DFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 225
Start
1
2
3
4 5
6 7
8
DFS Traversal
76
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 226
Start
1
2
3
4 5
6 7
8
9
DFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 227
Push (Stk, VStart)
While Stk Is Not Empty
Vtemp := Pop (Stk)
If VTemp is Not Marked
Process VTemp
Mark VTemp
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
End For
End IF
End While
DFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 228
BFS Traversal
Roughly analogous to level-order traversal of a tree
Start
1
77
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 229
Start
1
2
3
4
BFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 230
Start
1
2
3
4
5
BFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 231
Start
1
2
3
4
5
6
BFS Traversal
78
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 232
Start
1
2
3
4
5
6
7
BFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 233
Start
1
2
3
4
5
6
7
8
BFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 234
Start Start
1
2
3
4
5
6
7
8
9
BFS Traversal
79
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 235
Enqueue (Q, VStart)
While Q Is Not Empty
Dequeue (Q, VTemp)
If VTemp is Not Processed
Process VTemp
Mark VTemp
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
End For
End IF
End While
BFS Traversal
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63
236
TRANSITIVE CLOSURE
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 non-zero
entry in the path matrix of order N
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 240
Copy A to P
For K=0 to N-1
For I=0 to N-1
For J=0 to N-1
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 N-1
For I=0 to N-1
For J=0 to N-1
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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63
246
TOPOLOGICAL SORT
83
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 (in-degree 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 Delhi-63. 249
Example
V1
V2
V3 V4
V5
V6 V7
84
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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
In-degrees before removing V#
V6 V7 V5 V3 V4 V2 V1 O/P
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 252
While Q Is Not Empty
Dequeue (Q, VTemp)
TopSort [Count++] <- VTemp
If InDegree of VAdj == 0
End IF
End For
End While
Cont..
85
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 253
If Count < N
Graph has a Cycle
Else
End If
Cont..
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63
254
SHORTEST PATH
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 267
Dijkstras SSSP: Data
Struct NodeInfo
{
LabelType Label;
Boolean Known;
DistanceType Distance;
Vertex Previous;
};
90
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 270
Dijkstras SSSP: Algorithm
Temp=Nodes[Vloc].Distance+Cost(Vloc,
End IF
End IF
End For
End While
End
Temp=Nodes[Vloc].Distance+Cost(Vloc,
End IF
End IF
End For
End While
End
91
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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:
Floyd-Warshalls
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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 284
All Source Shortest Path: Algorithm
Void FloydWarshall
( TwoDimArray D, TwoDimArray P, Int N)
Begin
For K=0 to N-1
For I=0 to N-1
For J=0 to N-1
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 N-1
For I=0 to N-1
For J=0 to N-1
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 Delhi-63. 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 Delhi-63
286
MINIMUM SPANNING TREES
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 288
Common Algorithms
Prims
Kruskals
97
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63
305
CRITICAL PATH ANALYSIS
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 sub-activities as an acyclic graph
The technique is termed as critical path analysis.
103
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 307
Activity-Node 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 Delhi-63. 308
Event-Node Graph
To perform the calculations the Activity-Node graph
is transformed to an Event-Node graph
In an Event-Node graph
each node corresponds to completion of an activity and
all its dependent activities.
As in activity-node 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 Delhi-63. 309
Activity Node Graph to Event Node Graph
Rename Activity-Nodes to Event-Nodes.
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 pre-requisite for starting the activity v
are over.
104
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Activity-Node Graph
An Activity-Node Graph
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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 zero-slack 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 at-least on such path on the graph
107
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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 Delhi-63. 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 Delhi-63. 321
Conclusion
Trees
Binary trees
AVL Trees
Binary Tree Applications
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 Delhi-63. 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.
9. Define Binary Tree
10. Define AVL Tree
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. 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
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 Delhi-63. 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 B-tree 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 Delhi-63. 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)(5a-b)^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 2-tree 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 Delhi-63. 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 Delhi-63. 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 Delhi-63. 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.