Graph and Trees
Graph and Trees
Lecture Outline
• Graphs
– Path and Distance Matrix
• Trees
– Rooted Tree
– Tree Terminologies
– Ordered Rooted Tree
– Tree Traversal
– Infix, Prefix and Postfix Notation
simple path no no No
Path
• A path is a sequence of edges that begins at a
vertex of a graph and travels from vertex to
vertex along edges of the graph. (path does
not contain a repeated edge)
• Find a path between a and e.
a---e length=1
a---b---e length=2
a---b---f---e length=3
a---b---c---f---e length=4
Path
Find a path between a and e.
• a, d, c, f , e is a simple path of length 4, because {a, d}, {d,
c}, {c, f }, and {f, e} are all edges.
• a, e is a simple path of length 1, because {a, e} is the edge
connecting both vertices.
• a, b, f , e is a simple path of length 3, because {a, b}, {b, f},
and {f, e} are all edges.
a---e length=1
a---b---e length=2
a---b---f---e length=3
a---b---c---f---e length=4
Circuit
• Path a, b, c, d, a is a circuit.
Circuit
• Path a, b, c, d, a is a circuit.
Circuit
Connected Graph
[ ]
0 1 1 0
1 0 1 1 a b
1 1 0 1
0 1 1 0
Given Matrix
d c
[ ]
E(a)=2 0 1 1 2
E(b)=1 1 0 1 1
E(c)=1 1 1 0 1
E(d)=2 2 1 1 0
Distance Matrix
Radius =1
Diameter=2
Tree
Tree
• Trees are particularly useful in computer
science, where they are employed in a wide
range of algorithms.
Tree
A Rooted Tree T
Example
• In the rooted tree T (with root a), What is the
subtree rooted at g?
Step
1
Step
1
Step
Step 1
3
Step
3
Step Step
Step Step 2 3
2 2
𝑐 , 𝑎, 𝑏, 𝑓 , 𝑔 , 𝑑, 𝑒
Preorder Traversal
B H
A D K
• F, B, A, D, H, K
Preorder Traversal
Postorder Traversal
• Let T be an ordered rooted tree with root r and are
the subtrees at r from left to right. The postorder
traversal begins by traversing in postorder, then in
postorder, then in postorder, . . . , and finally in
postorder and ends by visiting r.
Post-order Traversal
Step
3
Step
3
Step
Step 3
2
Step
2
Step Step
Step Step 1 2
1 1
𝑓 , 𝑔, 𝑏, 𝑑 , 𝑎, 𝑒, 𝑐
Postorder Traversal
B H
A D K
• A, D, B, K, H, F
Postorder Traversal
In order Traversal
• Let be an ordered rooted tree with root r and are
the subtrees at r from left to right. The in order
traversal begins by traversing in inorder, then visiting
r. It continues by traversing in inorder, then in
inorder, . .. , and finally in inorder.
In-order Traversal
Step
2
Step
2
Step
Step 2
3
Step
3
Step Step
Step Step 1 3
1 1
𝑓 , 𝑏, 𝑔, 𝑎, 𝑑, 𝑐 , 𝑒
In-order Tree Walk
B H
A D K
• A, B, D, F, H, K
Example
Binary Search Trees
• A Binary search tree is a binary tree in
symmetric order.
• Symmetric order means that:
– every node has a key
– every node’s key is
• larger than all keys in its left subtree
• smaller than all keys in its right subtree
Infix, Prefix and Postfix Notation
preorder : xy 2 / x 43
postorder : xy 2 x 4 3 /
inorder : x y 2 x 4 / 3
Infix, Prefix and Postfix form
• Infix Form
– It is the common arithmetic and logical formula notation,
in which operators are written between the operands
• Prefix Form
– Also known as Polish notation.
– A form of notation for logic, arithmetic, and algebra.
– It places operators to the left of their operands.
• Postfix Form
– Also known as Reverse Polish Notation.
– We obtain the postfix form of an expression by traversing
its tree in postorder.
Example
• Chapter # 11
• Topic # 11.1
• Question # 1, 2, 3, 4, 5, 6, 7, 8, 9,10
• Topic # 11.3
• Question # 7 – 18, 23 ,24