0% found this document useful (0 votes)
61 views38 pages

S2 BCA Module2

This document discusses trees and tree traversal algorithms in discrete mathematics. It defines key tree terminology like root, leaf, internal vertex, subtree, and provides examples. It also covers tree traversal algorithms like preorder, inorder and postorder traversal. Finally, it discusses representing arithmetic expressions as ordered rooted trees and different notation systems like prefix, postfix and infix notations.

Uploaded by

aleenashoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views38 pages

S2 BCA Module2

This document discusses trees and tree traversal algorithms in discrete mathematics. It defines key tree terminology like root, leaf, internal vertex, subtree, and provides examples. It also covers tree traversal algorithms like preorder, inorder and postorder traversal. Finally, it discusses representing arithmetic expressions as ordered rooted trees and different notation systems like prefix, postfix and infix notations.

Uploaded by

aleenashoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Discrete Mathematics II BCA 2023-2026 Batch

Name:………………….
Class:……………….….

MODULE 2
TREES

Tree

An undirected graph is called a tree


if it is connected and has no cycles
or circuits.
i.e. A tree is a connected acyclic
graph.

Rooted Tree

A rooted tree is a tree in


which one vertex has
been taken as the root
and every edge is
directed away from the
root

Parent and Child


Consider a rooted tree T, If there is an edge from u to v, then u is the parent of v
and v is the child of u.

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

6. Sub tree with root 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?

Level of Vertex and Height of the rooted tree


The level of a vertex in a rooted tree is the length of the unique path from the root to that vertex.
The level of a root is defined to be zero. The height of a rooted tree is the maximum of the
levels of vertices. In other words the height of the rooted tree is the length of the longest
path from the root to any vertex.
Q] Find the level of each vertex in the rooted tree and what is the height of the tree?

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

11. Whether Balanced or not

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

● i internal vertices has n = mi+1 vertices and l = (m-1)i+1 leaves.


𝑚𝑙−1 𝑙−1
● l leaves has n = vertices and i = 𝑚−1 internal vertices.
𝑚−1

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

The preorder traversal of T is a,b,e,j,k,n,o,p,f,c,d,g,l,m,h,i

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

The inorder traversal of T is j,e,n,k,o,p,b,f,a,c,l,g,m,d,h,i

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

The postorder traversal of T is j,n,o,p,k,e,f,b,c,l,m,g,h,I,d,a

MAC Ramapuram 8
Discrete Mathematics II BCA 2023-2026 Batch

Q] Find preorder, inorder, postorder traversal of the given tree.

Infix, Prefix and Postfix Notation

We can represent complicated expression such as compound prepositions, combinations of sets


arithmetic expressions using ordered rooted trees. For instants consider the representation of
arithmetic expression involving the operators + (addition), - (subtraction), × (multiplication), /
(division), ↑ (exponentiation). An ordered rooted tree can used to represent such expression
where the internal vertices represent operations and leaves represent the variables or numbers.
Each operator operate on its left and right subtrees.

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))

Q] What is the value of the prefix expression +-*235/↑234

Q] What is the value of the postfix expression 723*-4↑93/+

MAC Ramapuram 10
Discrete Mathematics II BCA 2023-2026 Batch

Q] What is the value of the prefix expressions


i. -*2/843
ii. ↑-*33*425
iii. +- ↑32 ↑23/6-42

Q] What is the value of the postfix expressions


a. 521—-314++*
b. 93/5+72-*
c. 32*2↑53-84/*-

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

Universal Address System

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

Some of the Spanning Trees of the above Graph

Q] Draw spanning Trees of 𝑲𝟑

Q] Draw spanning Trees of 𝑲𝟒

Cayley’s Theorem
The number of spanning trees of a complete graph is 𝑛𝑛−2

MAC Ramapuram 12
Discrete Mathematics II BCA 2023-2026 Batch

Depth First Search Algorithm


We built a spanning tree in depth first search algorithm from a simple connected graph. The
spanning tree will be the underlying undirected graph of this rooted tree. We arbitrarily choose
a vertex of the graph as a root form a path starting at this vertex by successively adding vertices
and edges where each new edge is incident with the last vertex in the path and a vertex not
already in the path. Continue adding vertices and edges to the path as long as possible. If the
path goes through all vertices of the graph, the tree consisting of this path is a spanning tree.
If the path does not go through all vertices, more vertices and edges must be added. Move
back to the next to last vertex in the path, and, if possible, form a new path starting at this
vertex passing through vertices that were not already visited. If this cannot be done, move
back to another vertex in the path, that is, two vertices back in the path, and try again.

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

Breadth First Search Algorithm


We can also produce a spanning tree of a simple graph by the use of breadth-first search. Again,
a rooted tree will be constructed, and the underlying undirected graph of this rooted tree forms
the spanning tree. Arbitrarily choose a root from the vertices of the graph. Then add all edges
incident to this vertex. The new vertices added at this stage become the vertices at level 1 in
the spanning tree. Arbitrarily order them.

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.

2] Draw the spanning trees for the graph by

MAC Ramapuram 15
Discrete Mathematics II BCA 2023-2026 Batch

1.DepthFirstSearch
2. Breadth First Search

Application of Trees

Binary Search Trees


It is an algorithm for searching items when the items are unordered. Binary search tree is a
binary tree in which each vertex has right child or left child. Each vertex is labelled with a key
which is one of the item.
We start with a tree containing just one vertex named the root. The first item is the key of the
root. To add a new item we compare it with the keys of vertices already in the tree. Starting
from the root moving to the left if the item is less than the key of the respective vertex. And
moving to the right if the item is greater than the key of the respective item. If the item is less
than the key of the respective vertex and these vertex has no left child then a new vertex with
item as the key inserted as a new left child.

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”

4] Construct a binary search tree of 30,20,5,60,18

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

The encoding produced encodes,


A by 111 , B by 1 1 0, C by 0 1 1, D by 010, E by 1 0, and F by 00.
The average number of bits used to encode a symbol using this encoding is 3 * 0.08 + 3 * 0.
10 + 3 * 0. 12 + 3 * 0. 15 + 2 * 0.20 + 2* 0.35 = 2.45

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

You might also like