Trees
Trees
By:Ms N. S. Khairnar
Tree
A tree is a connected undirected graph with no
simple circuits.
A node that doesn't have a child node is known as a leaf node or terminal node.
A node that have a parent as well as Child node is known as a Branch node.
Tree Terminology..
Parent (2): 1
c
Child(2): 4,3
Ancestor(4): 1,2
Descendants(2): 4,3,5,6
Sibling(2): 7
Level, Height and depth of a tree
Center: 3 and 4
Radius: min{2,3}=2
Diameter: max{2,3}=3
Cut Point / Cut Vertex
● A cut vertex is a vertex in a connected graph that, when
removed, makes the graph disconnected.
● Cut vertices are also known as articulation points.
Cut Point / Cut Vertex..
Tree 1 is subtree.
Tree 2 is not a subtree.
Rooted Tree
● A rooted tree G is a connected acyclic graph with a special node that
is called the root of the tree and every edge directly or indirectly
originates from the root.
Preorder traversal: A → B → D → E → C → F → G
Inorder traversal: D → B → E → A → F → C → G
Postorder traversal: D → E → B → F → G → C → A
Q1 Find preorder, Inorder and postorder Traversal for given tree
B C
D E G H
J K
L
Pre-order Traversal
A B D E C G H J L K
ABDECGHJLK
In-order Traversal
D B E A G C L J H K
DBEAGCLJHK
Post-order Traversal
D E B G L J K H C A
DEBGLJKHCA
Q 2 Find preorder, Inorder and postorder Traversal for given tree
B C
E
Preorder traversal: A→B→C→D→E→F
F Inorder traversal: B→ A→ D → F→ E→ C
Postorder traversal: B→ F→ E→ D→ C→ A
Q 3 Find preorder, Inorder and postorder Traversal for given tree
B C
D G H
E F Preorder: ABDEFCGH
Inorder: BEDFAGCH
Postorder: EFDBGHCA
Q 1 Draw a binary tree from following traversal
Inorder: A
DBFEAGCLJHK
DBFE GCLJHK
Preorder:
ABDEFCGHJLK
Q 1 Draw a binary tree from following traversal
Inorder: A
DBFEAGCLJHK
B C
D FE G LJHK
Preorder:
ABDEFCGHJLK
Q Draw a binary tree from following traversal
Inorder: A
DBFEAGCLJHK
B C
D E G H
Preorder:
ABDEFCGHJLK
LJ K
F
Q Draw a binary tree from following traversal
Inorder: A
DBFEAGCLJHK
B C
D E G H
Preorder:
ABDEFCGHJLK
J K
F
L
Q2 Draw a binary tree from following traversal
Inorder: A
BEDFAGCH
B C
D G H
Postorder:
EFDBGHCA
E F
Optimal Tree
A Tree with minimum weight is called as optimal tree.
For 3,4,5 we can construct following trees.
5 3
4 3
5 4
W(T1)=(5*1)+(4*2)+(3*2) W(T2)=(5*2)+(4*2)+(3*1)
=19 =21
Huffman tree
Consider leaves
W(T) =(4*2)+(4*2)+(2*3)+(3*3)+(7*2)
W(T) = 45
Prefix Code/ Huffman Code
Prefix Codes is the code assigned in such a way that the code
assigned to one character is not the prefix of code assigned to any
other character.
e=00
t=01
w=0001
Is not a prefix code
2 100
3 101
0 1 0 1 4 00
4 01
01 0 1 7 11
00 11
100 101
Q2
A=0.08 B=0.10 C=0.12
D=0.15 E=0.20 F=0.35
Find optimal binary tree, also find prefix code and average
codeword length.
Leaves Prefix code
1
A 000
0.38 0.62 B 001
C 100
0.18 0.20 0.27 0.35
D 101
E 01
0.08 0.10 0.12 0.15
F 11
5
(1 + 3 + 2 + 4) : 10 (1 + 4 + 5 + 2): 12 (3 + 2 + 5 + 4) : 14 (1 + 3 + 2 + 5) : 11
Step 2 - Connect a vertex outside the Minimum Spanning Tree (MST) to a vertex
within the MST using the edge with the minimum weight.
Step 3 - Repeat step 2 until all vertices of the main graph are included in the
Minimum Spanning Tree (MST).
Kruskal’s Algorithm
Edge AB DE BC CD AE AC AD
Wt 1 2 3 4 5 7 10
Preorder traversal A, B, C, D, E, F, G, H, J, K, L, M, P, Q, N
Postorder traversal E, D, C, H, G, Q, P, N, M, L, K, J, F, B, A
Transport Network
• In discrete mathematics, a transportation network is a directed,
weighted, connected graph with a single source and a single sink.
The maximum amount of flow passing from the source to the sink is equal to the total weight of the edges in a minimum cut,
Minimum Cut
Q2 Find Maximum flow in the following transport network
4
Q3 Find Maximum flow in the following transport network
7
Q4 Find Maximum flow in the following transport network