Trees
Trees
(22AS003)
Trees
Department of Applied Sciences
Chitkara University, Punjab
Examples:
General Tree
Directed Trees
Directed Tree: A directed tree is an acyclic directed graph.
Examples:
Fig. 1
Fig. 2
Fig. 2
Fig. 1
The trees shown in both figures represent the same tree but have
different orders.
Path length of a vertex
Path length of a vertex: It is defined to be the number of edges in
the path from the root to the vertex.
• The path length of node b is one.
• The path length of node f is two.
• The path length of node l is three.
• The path length of node q is four.
Note:
There is one and only one path between every
pair of vertices in a tree T.
Forest: A forest is an undirected
graph whose components are all
trees.
i.e., If the root and corresponding
edges connecting the nodes are
deleted from a tree, we obtain a set
of disjoint trees. This set of
disjoint trees is called a forest. Forest
Rooted and Binary trees
Rooted tree: A tree in which a particular vertex is designated as the root of the tree is
called a rooted tree.
• If a directed tree has exactly one node or vertex called root whose incoming degree is 0
and all other vertices have incoming degree one, then the tree is called rooted tree.
Example:
Suppose 8 people enter a Badminton tournament use a rooted tree model of
the tournament to determine how many games must be played to determine a champion if a
player is eliminated after one loss.
Rooted and Binary trees
Binary tree: If every internal vertex of a rooted tree has exactly/at most 2
children, the tree
is called a full binary tree/a binary tree.
• A full binary tree is a tree in which there is exactly one vertex (root) of degree 2
and each of the remaining vertices is of degree 1 or 3.
Example:
• A is the root of the tree and is at level
0.
• The vertices B, C, D are at level 1,
E, F, G, H are at level 2 and I, J, K are at
level 3.
• The height of the tree is 3.
• The vertices E, F and I are
descendants of B. Similarly, H, J and
K are descendants of D. E and F are
children of B and J and K are children
of H.
• The vertices E, I, G, J and K are
leaves of the tree.
• The vertices A, B, F, C, D and H are
internal vertices of the tree.
Rooted and Binary trees
Examples:
For the tree shown below
(i) Which node is the root ?
(ii) Which nodes are leaves ?
(iii) Name the parent node of each node.
(ii) The siblings are as follows : (iii) Node Depth/Height (iv) Node Level
Siblings A 0 A 0
B and C B, C 1 B, C 1
D and E D, E, F 2 D, E, F 2
G and H G H, I, J, K 3 G, H, I, J, K 3
I and J L, M 4 L, M 4
L and M are all siblings.
Properties of Binary Trees
Property 1:
The number n of vertices of a full binary tree is odd and the number of pendant
(𝒏+𝟏)
verities (leaves) of the tree is equal to 𝟐 .
Property 2:
The minimum height of a 𝑛-vertex binary tree is equal to [𝐥𝐨𝐠 𝟐 (𝒏 + 𝟏) – 𝟏],
where [𝑥] denotes the smallest integer greater than or equal to 𝑥.
Note:
• To construct a binary tree with n vertices having the minimum height, the above
property can be made use of.
• To construct a binary tree with n vertices having the maximum height, we should
(𝒏+𝟏)
have exactly 2 vertices at each level, except at zero level. Thus, maximum ℎ = 𝟐
(n is an odd integer).
𝒏+𝟏
• Number of leaf nodes in a tree with 𝒏 vertices is 𝟐 .
Spanning Trees
Spanning Tree: If the subgraph T of a connected graph G is a
tree containing all the vertices of G, then T is called a spanning
tree of G.
Example:
Note: Since G has 5 edges, removal of 2 edges may result in spanning tree. This can be
5
done in 𝐶2 = 10 ways.
Note: Every connected graph has at least one spanning tree. This is obvious when G has no
circuit, as G is its own spanning tree.
If G has a circuit, we can get a spanning tree by deleting an edge from the circuit.
2
1
1
C 3 D E
6 7
F 2 G
Note:
1. The weight of a minimum spanning tree is unique, whereas different minimum
spanning trees are possible, as two or more edges can have the same weight.
2. In Prim’s algorithm edges of minimum weight that are incident on a vertex
already in the spanning tree and not forming a circuit are selected, whereas in
Kruskal’s algorithm edges of minimum weight that are not necessarily incident
on a vertex already in the spanning tree and not forming a circuit are selected.
Construction of minimum spanning tree
Example: Find the minimum spanning tree for the weighted graph shown in Fig., by using
Kruskal’s algorithm.
Note:
• The edges of the minimum spanning tree are AE, CD,
AC and AB, whose total length is 15.
• There are 5 other alternative minimum spanning trees
of total length 15 whose edges are listed below:
(1) AE, CD, AC, BC; (2) AE, CD, AC, BE;
(3) AE, CD, CE, AB; (4) AE, CD, CE, BC;
(5) AE, CD, CE, BE.
Construction of minimum spanning tree
Example: Find the minimum spanning tree for the weighted graph shown in Fig., by using
Kruskal’s algorithm.
Note:
The required minimum spanning tree consists of the 8 edges EF, AD, HI,
BD, CF, EH, BC and GH.
The total length of the minimum spanning tree = 22.
Rooted and Binary trees
Example: Draw two different binary trees with five nodes having only one leaf.
Example:
Draw two different binary trees with five nodes having maximum number of leaves.
Complete Binary tree
Inorder: F, L, I, M, D, G, B, A, E, J, H, K, C.
Postorder :
Stage (1) B C A
Stage (2) D B E C A
Stage (3) F G D B H E C A
Stage (4) I F G D B J K H E C A
Stage (5) L, M, I, F, G, D, B, J, K, H, E, C, A.
Postorder: L, M, I, F, G, D, B, J, K, H, E, C, A.
Traversing binary trees
Example: In which order does (i) a preorder, (ii) inorder (iii) a postorder traversal
visit the vertices of the ordered rooted tree.
Preorder :
Stage (1):
ABCD
Stage (2):
ABEFCDGHI
Stage (3):
ABEJKFCDGLMH I
Stage (4):
A, B, E, J, K, N, O, P, F, C, D, G, L, M, H, I.
Inorder : Postorder :
Stage (1): Stage (1):
BACD BCDA
Stage (2): Stage (2):
EBFACGDH I EFBCGHIDA
Stage (3): Stage (3):
J EK B FAC LG M D H I JKEFBCLMGHIDA
Stage (4): Stage (4):
J, E, N, K, O, P, B, F, A, C, L, G, M, D, H, I. J, N, O, P, K, E, F, B, C, L, M, G, H, I, D, A.
Traversing binary trees
Infix, Prefix and Postfix forms: We can represent expressions in three different ways
by using binary trees.
*
BSF: S A B C D
DFS
Depth First Search (DFS):
• Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push
it in a stack.
• Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will
pop up all the vertices from the stack, which do not have adjacent vertices.)
• Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
DFS: S A D B C
Shortest path algorithms
Shortest path : A shortest path between two vertices in a weighted graph is a path of
least weight. In an unweighted graph, a shortest path means one with the least
number of edges.
Dijkstra’s Algorithm : This algorithm assigns numerical labels to the vertices of the
graph by an iterative procedure.
Shortest path: A – B – E – D – F.
Shortest path algorithms: Dijkstra Algorithm
Example: Use Dijkstra’s algorithm to find the shortest path between the vertices A
and H in the weighted graph given
Shortest path: A – B – C – H.