S2 BCA Module2
S2 BCA Module2
Name:………………….
Class:……………….….
MODULE 2
TREES
Tree
Rooted Tree
Siblings
The vertices with same parent are called siblings.
MAC Ramapuram 1
Discrete Mathematics II BCA 2023-2026 Batch
Ancestors
The ancestors of a vertex are the vertices in the path from root to that vertex(excluding
that vertex and including the root) with v as ancestor.
Descendants
The descendants of a vertex are those vertices that have v as an ancestor
Leaf
A vertex is called a leaf if it has no children
Internal Vertices
The vertices that have children are called internal vertices.
Sub-Tree
If a is a vertex in a tree then sub-tree with root a is the sub-graph of the tree consisting
of a and all its descendant.
Q) Find the following from the tree T.
1. Parent of c
2. Children of d
3. Siblings of b
4. Ancestors of k
5. Descendants of b
7. Leaves
8. Internal vertices
M-ary tree
A rooted tree is called a m-ary tree if every internal vertex has no more than m children. A
rooted tree is called full m-ary tree if each internal vertex has exactly m-children. A full m-
ary tree with m=2 is called a full binary tree. That is each internal vertex has exactly 2
children.
MAC Ramapuram 2
Discrete Mathematics II BCA 2023-2026 Batch
In an ordered binary tree, if an internal vertex has 2 children, the first child is called left child
and the second child is called right child.The tree rooted at left child of a vertex is called left
subtree and the tree rooted at right child of a vertex is called the right subtree of the vertex.
Q] What are the left and right children of d in binary tree T? What are the left and right
sub trees of c?
MAC Ramapuram 3
Discrete Mathematics II BCA 2023-2026 Batch
Note:
A rooted m-ary tree of height h is balanced if all leaves are at levels h or h-1.
Q) Which of the rooted trees are balanced?
Q) Find the
1. Root
2. Internal vertices
3. Leaves
4. Children of j
5. Parent of h
6. Siblings of o
7. Ancestors of m
8. Descendants of b
9. Height
10. Level of q
MAC Ramapuram 4
Discrete Mathematics II BCA 2023-2026 Batch
Theorem:
A tree with n vertices has n-1 edges.
Proof:
We prove the theorem by mathematical induction method. A tree with one vertex has zero
edges. ∴ The result is true for n=1.Now we assume that the result is true for n=k. Let T be a
tree with k+1 vertices. Then T must have a vertex of degree 1(leaf). Let W be such a vertex.
Deleting W from the tree T we get a sub tree 𝑇 ′ with k vertices. Then the subtree 𝑇 ′ has k-1
edges. i.e. The tree T has k-1+1=k edges. Hence the theorem is proved.
Theorem
● A full m-ary tree with i internal vertices contains n=mi+1 vertices.
(𝑛−1) ((𝑚−1) 𝑛)+1
● A full m-ary tree with n vertices has i = internal vertices and 𝑙 =
𝑚 𝑚
leaves.
Note
Tree Traversal
Procedures for systematically visiting every vertex of an ordered neutral tree are called
traversal algorithm.
There are 3 most commonly using algorithms
● Preorder Traversal
● Inorder Traversal
● Postorder Traversal
MAC Ramapuram 5
Discrete Mathematics II BCA 2023-2026 Batch
Preorder Traversal
Let T be an ordered rooted tree with root r. If T consist only r, then r is the preorder traversal
of T. Otherwise suppose that 𝑇1 , 𝑇2 , 𝑇3 , ... , 𝑇𝑛 are the subtrees at r from left to right in T.The
preorder traversal begins by visiting r. It continues by traversing 𝑇1 in preorder then 𝑇2 in
preorder and so on , until 𝑇𝑛 is traversed in preorder.
Eg: Consider the rooted tree
MAC Ramapuram 6
Discrete Mathematics II BCA 2023-2026 Batch
Inorder Traversal
Let T be an ordered rooted tree with root r. If T consist only r, then r is the inorder traversal of
T. Otherwise suppose that 𝑇1 , 𝑇2 , 𝑇3 , ... , 𝑇𝑛 are the subtrees at r from left to right. The inorder
traversal begins by traversing 𝑇1 in inorder then visiting r. It continues by traversing 𝑇2 in
inorder then 𝑇3 in inorder, etc and finally 𝑇𝑛 in inorder.
Eg: Consider the rooted tree
MAC Ramapuram 7
Discrete Mathematics II BCA 2023-2026 Batch
Postorder Traversal
Let T be an ordered rooted tree with root r. If T consist only r, then r is the postorder traversal
of T. Otherwise suppose that 𝑇1 , 𝑇2 , 𝑇3 , ... , 𝑇𝑛 are the subtrees at r from left to right. The post
order traversal begins by traversing 𝑇1 in postorder then 𝑇2 in postorder ... then 𝑇𝑛 in post oder
and ends by visiting r.
Eg: Consider the rooted tree
MAC Ramapuram 8
Discrete Mathematics II BCA 2023-2026 Batch
1] What is the ordered rooted tree that represent the expression ((x+y)↑2)+((x-4)/3)
2] What is the ordered rooted tree that represent the expression (x + y)/(x + 3)
MAC Ramapuram 9
Discrete Mathematics II BCA 2023-2026 Batch
3] What is the ordered rooted tree that represent the expression (x + (y / x)) + 5
4] What is the ordered rooted tree that represent the expression x + (y / (x + 3))
MAC Ramapuram 10
Discrete Mathematics II BCA 2023-2026 Batch
Q] Represent the expression ((x+2)↑3)*((y-(3+x))-5) using a binary tree and write the
expression in
1.Prefixnotation
2.Postfixnotation
3. Infix notation
Spanning Trees
Let G be a simple graph a spanning tree of G is a subgraph of G that is a tree containing every
vertices of G.
MAC Ramapuram 11
Discrete Mathematics II BCA 2023-2026 Batch
Cayley’s Theorem
The number of spanning trees of a complete graph is 𝑛𝑛−2
MAC Ramapuram 12
Discrete Mathematics II BCA 2023-2026 Batch
Repeat this procedure, beginning at the last vertex visited, moving back up the path one vertex
at a time, forming new paths that are as long as possible until no more edges can be added.
Because the graph has a finite number of edges and is connected, this process ends with the
production of a spanning tree.
Depth-first search is also called backtracking, because the algorithm returns to vertices
previously visited to add paths.
1] Use depth-first search to find a spanning tree for the graph G shown in Figure
Answer:
The steps used by depth-first search to produce a spanning tree of G is as follows.
We arbitrarily start with the vertex f. A path is built by successively adding edges incident
with vertices not already in the path, as long as this is possible. This produces a path f, g, h, k,
j (note that other paths could have been built). Next, backtrack to k. There is no path beginning
at k containing vertices not already visited. So we backtrack to h. Form the path h, i. Then
backtrack to g, and then to f. From f build the path f, d, e, c, a. Then backtrack to c and form
the path c, b. This produces the spanning tree.
MAC Ramapuram 13
Discrete Mathematics II BCA 2023-2026 Batch
2] Use depth-first search to find a spanning tree for the graph G shown in Figure
Next, for each vertex at level l, visited in order, add each edge incident to this vertex to the tree
as long as it does not produce a simple circuit. Arbitrarily order the children of each vertex at
level 1. This produces the vertices at level 2 in the tree. Follow the same procedure until all the
vertices in the tree have been added. The procedure ends because there are only a finite number
of edges in the graph. A spanning tree is produced because we have produced a tree containing
every vertex of the graph.
1] Using breadth first search to find a spanning tree for the graph.
MAC Ramapuram 14
Discrete Mathematics II BCA 2023-2026 Batch
Answer:
The steps of the breadth-first search procedure are shown in Figure. We choose the vertex e to
be the root. Then we add edges incident with all vertices adjacent to e, so edges from e to b, d,
f, and i are added. These four vertices are at level 1 in the tree. Next, add the edges from these
vertices at level 1 to adjacent vertices not already in the tree. Hence, the edges from b to a and
c are added, as are edges from d to h, from f to j and g, and from i to k. The new vertices a, c,
h, j, g, and k are at level 2. Next, add edges from these vertices to adjacent vertices not already
in the graph. This adds edges from g to l and from k to m.
MAC Ramapuram 15
Discrete Mathematics II BCA 2023-2026 Batch
1.DepthFirstSearch
2. Breadth First Search
Application of Trees
1] Form a binary search tree for the words mathematics, physics, geography, zoology,
meteorology, geology, psychology and chemistry.
MAC Ramapuram 16
Discrete Mathematics II BCA 2023-2026 Batch
2] Construct a binary tree for the words Banana, Peach, Apple, Pear, Coconut, Mango,
Pappaya
3] Construct a binary tree for the words “The, Quick ,Brown ,Fox, Jumps, Over, Crazy ,Dog”
MAC Ramapuram 17
Discrete Mathematics II BCA 2023-2026 Batch
Prefix Codes
One way to ensure that no bit string corresponds to more than one sequence of letters is to
encode letters so that the bit string for a letter never occurs as the first part of the bit string for
another letter. Codes with this property are called prefix codes.
A prefix code can be represented using a binary tree, where the characters are the labels of the
leaves in the tree. The edges of the tree are labeled so that an edge leading to a left child is
assigned a “ 0 ” and an edge leading to a right child is assigned a” 1” . The bit string used to
encode a character is the sequence of labels of the edges in the unique path from the root to the
leaf that has this character as its label.
HUFFMAN CODING
We now introduce an algorithm that takes as input the frequencies of symbols in a string and
produces as output a prefix code that encodes the string using the fewest possible bits, among
all possible binary prefix codes for these symbols. This algorithm is known as Huffman coding.
1) Use Huffman coding to encode the following symbols with the frequencies listed: A: 0.08,
B: 0. 10, C: 0. 12, D: 0. 1 5, E: 0.20, F: 0.35. What is the average number of bits used to encode
a character?
MAC Ramapuram 18
Discrete Mathematics II BCA 2023-2026 Batch
2]Use Huffman coding to encode these symbols with given frequencies: a: 0.20, b: 0. 1 0, c:
0. 1 5, d: 0.25, e: 0.30. What is the average number of bits required to encode a character?
3]Use Huffman coding to encode these symbols with given frequencies: A: 0. 1 0, B: 0.25, C:
0.05, D: 0. 1 5, E: 0.30, F: 0.07, G: 0.08. What is the average number of bits required to
encode a symbol?
MAC Ramapuram 19
Discrete Mathematics II BCA 2023-2026 Batch
Q)Prove that a connected graph is a tree if there exist a unique path between every pair of
vertices
MAC Ramapuram 20
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 21
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 22
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 23
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 24
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 25
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 26
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 27
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 28
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 29
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 30
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 31
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 32
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 33
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 34
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 35
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 36
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 37
Discrete Mathematics II BCA 2023-2026 Batch
MAC Ramapuram 38