Dm. Unit-4
Dm. Unit-4
Discrete Mathematics
Trees
General Trees
A graph which has no cycle is called an acyclic graph. A tree is an acyclic graph or
graph having no cycles.
A tree or general trees is defined as a non-empty finite set of elements called vertices
or nodes having the property that each node can have minimum degree 1 and
maximum degree n. It can be partitioned into n+1 disjoint subsets such that the first
subset contains the root of the tree and remaining n subsets includes the elements of
the n subtree.
Directed Trees:
A directed tree is an acyclic directed graph. It has one node with indegree 1, while all
other nodes have indegree 1 as shown in fig:
The node which has outdegree 0 is called an external node or a terminal node or a
leaf. The nodes which have outdegree greater than or equal to one are called internal
node.
Ordered Trees:
If in a tree at each level, an ordering is defined, then such a tree is called an ordered
tree.
Example: The trees shown in the figures represent the same tree but have different
orders.
Properties of Trees:
Notes Prepared by:
Asst.Prof.Vidya A. Huse([email protected])
According to DBATU Syllabus 2022-23 UNIT 4:Trees
Discrete Mathematics
2. If a graph G there is one and only one path between each pair of vertices G is a
tree.
Rooted Trees:
If a directed tree has exactly one node or vertex called root whose incoming degrees
is 0 and all other vertices have incoming degree one, then the tree is called rooted
tree.
Binary Trees:
If the outdegree of every node is less than or equal to 2, in a directed tree than the tree
is called a binary tree. A tree consisting of the nodes (empty tree) is also a binary tree.
A binary tree is shown in fig:
Basic Terminology:
Root: A binary tree has a unique node called the root of the tree.
Left Child: The node to the left of the root is called its left child.
Right Child: The node to the right of the root is called its right child.
Parent: A node having a left child or right child or both are called the parent of the
nodes.
Siblings: Two nodes having the same parent are called siblings.
Leaf: A node with no children is called a leaf. The number of leaves in a binary tree
can vary from one (minimum) to half the number of vertices (maximum) in a tree.
Descendant: A node is called descendant of another node if it is the child of the node
or child of some other descendant of that node. All the nodes in the tree are
descendants of the root.
Left Subtree: The subtree whose root is the left child of some node is called the left
subtree of that node.
B, C A
D, E B
F C
G, H D
I, J E
K F
L, M J
N, O K
Right Subtree: The subtree whose root is the right child of some node is called the
right subtree of that node.
Level of a Node: The level of a node is its distance from the root. The level of root is
defined as zero. The level of all other nodes is one more than its parent node. The
maximum number of nodes at any level N is 2N.
Depth or Height of a tree: The depth or height of a tree is defined as the maximum
number of nodes in a branch of a tree. This is more than the maximum level of the
tree, i.e., the depth of root is one. The maximum number of nodes in a binary tree of
depth d is 2d-1, where d ≥1.
External Nodes: The nodes which have no children are called external nodes or
terminal nodes.
Internal Nodes: The nodes which have one or more than one children are called
internal nodes or non-terminal nodes.
Full Binary Tree: Full binary tree is a binary tree in which all the leaves are on the same
level and every non-leaf node has two children.
2. If some node has a child, then there is 2. If some node has a child, then it
right child.
3. The trees shown in fig are the same, 3. The trees shown in fig are
a left child of 2.
1. Preorder Traversal
2. Postorder Traversal
3. Inorder Traversal
3. Inorder Traversal: The inorder traversal of a binary tree is a recursive process. The
inorder traversal of a tree is
Example: Determine the preorder, postorder and inorder traversal of the binary tree as
shown in fig:
Solution: The preorder, postorder and inorder traversal of the tree is as follows:
Preorder 1 2 3 4 5 6 7 8 9 10 11
Postorder 3 5 4 2 7 10 9 11 8 6 1
Inorder 3 2 5 4 1 7 6 9 10 8 11
Algorithms:
(a)Algorithm to draw a Unique Binary Tree when Inorder and Preorder Traversal of
the tree is Given:
1. We know that the root of the binary tree is the first node in its preorder. Draw
the root of the tree.
2. To find the left child of the root node, first, use the inorder traversal to find the
nodes in the left subtree of the binary tree. (All the nodes that are left to the
root node in the inorder traversal are the nodes of the left subtree). After that,
the left child of the root is obtained by selecting the first node in the preorder
traversal of the left subtree. Draw the left child.
3. In the same way, use the inorder traversal to find the nodes in the right subtree
of the binary tree. Then the right child is obtained by selecting the first node in
the preorder traversal of the right subtree. Draw the right child.
4. Repeat the steps 2 and 3 with each new node until every node is not visited in
the preorder. Finally, we obtain a unique tree.
Example: Draw the unique binary tree when the inorder and preorder traversal is given
as follows:
Inorder B A D C F E J H K G I
Preorder A B C D E F G H J K I
Solution: We know that the root of the binary tree is the first node in preorder
traversal. Now, check A, in the inorder traversal, all the nodes that are of left A, are
nodes of left subtree and all the nodes that are right of A, are nodes of right subtree.
Read the next node in the preorder and check its position against the root node, if its
left of the root node, then draw it as a left child, otherwise draw it a right child. Repeat
the above process for each new node until all the nodes of the preorder traversal are
read and finally we obtain the binary tree as shown in fig:
(b) Algorithm to draw a Unique Binary Tree when Inorder and Postorder Traversal of
the tree is Given:
1. We know that the root of the binary tree is the last node in its postorder. Draw
the root of the tree.
2. To find the right child of the root node, first, use the inorder traversal to find the
nodes in the right subtree of the binary tree. (All the nodes that are right to the
root node in the inorder traversal are the nodes of the right subtree). After that,
the right child of the root is obtained by selecting the last node in the postorder
traversal of the right subtree. Draw the right child.
3. In the same way, use the inorder traversal to find the nodes in the left subtree
of the binary tree. Then the left child is obtained by selecting the last node in
the postorder traversal of the left subtree. Draw the left child.
4. Repeat the steps 2 and 3 with each new node until every node is not visited in
the postorder. After visiting the last node, we obtain a unique tree.
Example: Draw the unique binary tree for the given Inorder and Postorder traversal is
given as follows:
Inor 4 6 10 12 8 2 1 5 7 11 13 9 3
der
Pos 12 10 8 6 4 2 13 11 9 7 5 3 1
tord
er
Solution: We know that the root of the binary tree is the last node in the postorder
traversal. Hence, one in the root node.
Now, check the inorder traversal, we know that root is at the center, hence all the
nodes that are left to the root node in inorder traversal are the nodes of left subtree
and, all that are right to the root node are the nodes of the right subtree.
Now, visit the next node from back in postorder traversal and check its position in
inorder traversal, if it is on the left of root then draw it as left child and if it is on the
right, then draw it as the right child.
Repeat the above process for each new node, and we obtain the binary tree as shown
in fig:
1. Starting from the root node, the root of the tree is also the root of the binary
tree.
2. The first child C1(from left) of the root node in the tree is the left child C1 of the
root node in the binary tree, and the sibling of the C1 is the right child of C1 and
so on.
It is not necessary that a node in a 'Binary Search Tree' point to the nodes whose value
Inserting into a Binary Search Tree: Consider a binary tree T. Suppose we have
given an ITEM of information to insert in T. The ITEM is inserted as a leaf in the
tree. The following steps explain a procedure to insert an ITEM in the binary
search tree T.
2. If ITEM>ROOT NODE, proceed to the right child, and it becomes a root node for
the right subtree.
4. Repeat the above steps until we meet a node which has no left and right
subtree.
5. Now if the ITEM is greater than the node, then the ITEM is inserted as the right
child, and if the ITEM is less than the node, then the ITEM is inserted as the left
child.
Example: Show the binary search tree after inserting 3, 1,4,6,9,2,5,7 into an initially
empty binary search tree.
Solution: The insertion of the above nodes in the empty binary search tree is shown in
fig:
Deletion in a Binary Search Tree: Consider a binary tree T. Suppose we want to delete
a given ITEM from binary search tree. To delete an ITEM from a binary search tree we
have three cases, depending upon the number of children of the deleted node.
1. Deleted Node has no children: Deleting a node which has no children is very
simple, as replace the node with null.
2. Deleted Node has Only one child: Replace the value of a deleted node with the
only child.
3. Deletion node has only two children: In this case, replace the deleted node with
the node that is closest in the value to the deleted node. To find the nearest
value, we move once to the left and then to the right as far as possible. This
node is called the immediate predecessor. Now replace the value of the deleted
node with the immediate predecessor and then delete the replaced node by
using case1 or case2.
Example: Show that the binary tree shown in fig (viii) after deleting the root node.
Solution: To delete the root node, first replace the root node with the closest elements
of the root. For this, first, move one step left and then to the right as far as possible to
the node.Then delete the replaced node. The tree after deletion shown in fig:
Spanning Tree
A subgraph T of a connected graph G is called spanning tree of G if T is a tree and T
include all vertices of G.
Kruskal's Algorithm to find a minimum spanning tree: This algorithm finds the
minimum spanning tree T of the given connected weighted graph G
1. Input the given connected weighted graph G with n vertices whose minimum
spanning tree T, we want to find.
4. Add each of the graphs G in T which does not form a cycle until n-1 edges are
added.
Example1: Determine the minimum spanning tree of the weighted graph shown in fig:
Solution: Using kruskal's algorithm arrange all the edges of the weighted graph in
increasing order and initialize spanning tree T with all the six vertices of G. Now start
adding the edges of G in T which do not form a cycle and having minimum weights
until five edges are not added as there are six vertices.
(B, E) 2 Added
(C, D) 3 Added
(A, D) 4 Added
(C, F) 4 Added
(B, C) 5 Added
Step1:
Step2:
Step3:
Step4:
Step5:
Step6: Edge (A, B), (D, E) and (E, F) are discarded because they will form the cycle in a
graph.
So, the minimum spanning tree form in step 5 is output, and the total cost is 18.
Example2: Find all the spanning tree of graph G and find which is the minimal
spanning tree of G shown in fig:
Solution: There are total three spanning trees of the graph G which are shown in fig:
To find the minimum spanning tree, use the KRUSKAL'S ALGORITHM. The minimal
spanning tree is shown in fig:
(E, F) 1 Added
(A, B) 2 Added
(C, D) 2 Added
(B, C) 3 Added
(D, E) 3 Added
The first one is the minimum spanning having the minimum weight = 11.
Prim's Algorithm
Prim's algorithm, discovered in 1930 by mathematicians, Vojtech Jarnik and Robert C.
Prim, is a greedy algorithm that finds a minimum spanning tree for a connected
weighted graph. It finds a tree of that graph which includes every vertex and the
total weight of all the edges in the tree is less than or equal to every possible
Algorithm
● Initialize the minimal spanning tree with a single vertex, randomly
● Repeat steps 3 and 4 until all the vertices are included in the tree.
● Select an edge that connects the tree with a vertex not yet in the tree,
so that the weight of the edge is minimal and inclusion of the edge does
● Add the selected edge and the vertex that it connects to the tree.
Problem
Suppose we want to find minimum spanning tree for the following graph G using
Prim’s algorithm.
Solution
(1+2+3+5+9)=20.
00:11/11:48
52.9M
953