0% found this document useful (0 votes)
18 views44 pages

Trees

The document discusses various types of trees including rooted trees, binary trees, spanning trees, and minimum spanning trees. It provides examples and properties for each type. It also describes Prim's algorithm for constructing a minimum spanning tree, giving an example of applying the algorithm to find the minimum spanning tree of a weighted graph. The minimum spanning tree has the smallest total weight among all spanning trees of the graph.

Uploaded by

Himanshu Sharma
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)
18 views44 pages

Trees

The document discusses various types of trees including rooted trees, binary trees, spanning trees, and minimum spanning trees. It provides examples and properties for each type. It also describes Prim's algorithm for constructing a minimum spanning tree, giving an example of applying the algorithm to find the minimum spanning tree of a weighted graph. The minimum spanning tree has the smallest total weight among all spanning trees of the graph.

Uploaded by

Himanshu Sharma
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/ 44

Discrete Structures

(22AS003)

Trees
Department of Applied Sciences
Chitkara University, Punjab

Prepared by Dr. Swati


Trees
Trees
Tree: A graph which has no cycle is called an acyclic graph. A tree
is an acyclic graph having no cycle.
• A connected (undirected) graph without any circuits is called a
tree.
• Obviously a tree has to be a simple graph, since loops and
parallel edges form circuits.

Examples:

Note: Trivial Tree is a tree with only one vertex.


Trees

Some Properties of Trees:


• An undirected graph is a tree, if and only if, there is a unique simple
path between every pair of vertices.
• A tree with n vertices has (n – 1) edges.
• In a tree with more than one vertex, there are at least two vertices of
degree 1.
• There is only one path between a pair of vertices of a tree.
• Any circuit-less graph with n vertices and (n – 1) edges is a tree.
• Any connected graph with n vertices and (n – 1) edges is a tree.

General Tree
Directed Trees
Directed Tree: A directed tree is an acyclic directed graph.

Examples:

Fig. 1
Fig. 2

• The node which has outdegree 0 is called an external


node/terminal node or a leaf.
• The node which has outdegree greater than or equal to one are called
the internal nodes or branch nodes.
Ordered Trees
Ordered Trees: If in a tree at each level, an ordering is defined,
then such a tree is called an ordered tree.
Examples:

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.

Note: 𝑇1 is a binary tree, whereas 𝑇2 is a full binary tree.

Note: In the definition of binary and full binary trees, if 2 is


replaced by m, the trees are called 𝒎-ary tree and full 𝒎-ary
tree.
Rooted and Binary trees
Terminology:
• The length of the path from the root of a rooted tree to any vertex v is
called the level or depth of v or height of v.
• The root is said to be at level zero. The maximum level of any vertex
is called the depth or height of the tree.
• Ancestor of a vertex other than root are the vertices in the path from
root to this vertex excluding itself and including the root.
• Descendent of v are those vertices that have ‘v’ as an ancestor..
• Also the vertices that are reachable from v through a single edge are
called the children of v.
• If a vertex v has no children, then v is called a leaf or a terminal
vertex or a pendant vertex. The degree of a leaf is 1. A non-pendant
vertex is called an internal vertex. Root is also considered an internal
vertex.
Rooted and Binary trees

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.

(i) The node A is the root node.


(ii) The nodes G, H, I, L, M, N, O are leaves.
(iii) Nodes Parent
B, C A
D, E B
F C
G, H D
I, J E
K F
L, M J
N, O K
Rooted and Binary trees
Examples: For the tree shown below
(i) List the children of each node. (ii) List the siblings.
(iii) Find the depth of each node. (iv) Find the level of each node.

(i) The children of each node is as follows :


Node Children
A B, C
B D, E
C F
D G, H
E I, J
F K
K L, M

(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.

Minimum Spanning Tree: If G is a connected weighted graph,


the spanning tree of G with the smallest total weight (viz., the
sum of the weights of its edges) is called the minimum spanning
tree of G.
Construction of minimum spanning tree
Prim’s Algorithm:
Step 1
Any edge of the given graph G with the smallest weight is chosen and put into the
spanning tree.
Step 2
Graph edges of minimum weight that are incident to a vertex already in the tree
and not forming a circuit with the edges already in the tree are added
successively.
Step 3
The procedure is stopped when (n – 1) edges have been added.
Example: Use Prim’s algorithm to find a minimum spanning tree for the weighted graph
given as
Construction of minimum spanning tree
Example: Use Prim’s algorithm to find a minimum spanning tree for the weighted graph
given as

Minimum spanning tree


1
2

2
1

The total weight of the


minimum spanning tree
1 + 2 + 2 + 1 = 6.
Construction of minimum spanning tree
Example: Use Prim’s algorithm to find a minimum spanning tree for the weighted graph
given as

Minimum spanning tree


A 5 B

1
C 3 D E

6 7
F 2 G

GE(7) The total weight of the


AB(5) minimum spanning tree
5 + 1 + 3 + 6 + 2 + 7 = 24.
Construction of minimum spanning tree
Kruskal’s Algorithm:
Step 1
The edges of the given graph G are arranged in the order of increasing weights.
Step 2
An edge G with minimum weight is selected as an edge of the required
spanning tree.
Step 3
Edges with minimum weight that do not from a circuit with the already
selected edges are successively added.
Step 4
The procedure is stopped after (n – 1) edges have been selected.

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

Complete Binary tree:


Complete binary tree is a binary tree if all its levels, except possibly the last.

Complete binary tree

Neither complete Full and


complete Complete but Full but not
nor full
not full complete
Rooted and Binary trees
Binary Expression Trees:
Algebraic expression can be conveniently expressed by its expression tree.
An expression having binary operators can be decomposed into
< left operand or expression > (operator) < right operand or expression >
depending upon precedence of evaluation.
The expression tree is a binary tree whose root contains the operator and whose left
subtree contains the left expression and right subtree contains the right expression.
Example: Construct the binary expression tree for the expression (𝒂 + 𝒃) ∗ (𝒄/𝒅).
Rooted and Binary trees
Binary Expression Trees:
< left operand or expression > (operator) < right operand or expression >

Example: Represent the expression ((𝑎 – 𝑐) ∗ 𝑑)/(𝑎 + (𝑏 – 𝑑) as a binary tree.


Rooted and Binary trees
Binary Expression Trees:
< left operand or expression > (operator) < right operand or expression >

Example: Determine the value of expression tree shown

Value = 7 Value = (𝑎 − 𝑏)/((𝑐 × 𝑑) + 𝑒)


Traversing binary trees
Traversing binary trees: A traversal a tree is a process to traverse (walk along)
a tree in a systematic manner so that each vertex is visited and processed exactly
once. There are three methods of traversal of a binary tree, namely, preorder, inorder
and postorder traversals.
Preorder : The preorder traversal of T visits the
root A first and then traverses 𝑻𝟏 and 𝑻𝟏 in
preorder. The preorder traversal of 𝑻𝟏 visits the
root B and then D and E.
Inorder : The inorder traversal of T traverses
𝑻𝟏 in inorder first, then visits the root A and
finally traverses 𝑻𝟐 in inorder. But the inorder
traversal of 𝑻𝟏 processes D, B and E in that order
and the inorder traversal of 𝑻𝟐 processes C and
then F. Preorder traversal of T is A B D E C F.
Postorder : The postorder traversal of T Inorder traversal of T is D B E A C F.
processes 𝑻𝟏 , then 𝑻𝟐 in postorder and finally Postorder traversal of T is D E B F C A.
visits A. But the postorder traversal of 𝑻𝟏
processes D, E and B in that order and the
postorder traversal of 𝑻𝟐 processes F and then C.
Traversing binary trees
Example: List the order in which the vertices of the tree given are processed using
preorder, inorder and postorder traversal.
Preorder :
Stage 1:(level 0 vertex A) Stage 2:(level 1 vertices B and C)

Stage (3) (level 2 vertices D and E)

Stage (5) (level 4 vertices I, J, K)


Preorder: A, B, D, F, I, L, M, G, C, E, H, J, K.

Stage (4) (level 3 vertices F, G, H)


Traversing binary trees
Example: List the order in which the vertices of the tree given are processed using
preorder, inorder and postorder traversal.
Inorder :
Stage (1) B A C
Stage (2) D B A E C
Stage (3) F D G B A E H C
Stage (4) F I D G B A E J H K C
Stage (5) F, L, I, M, D, G, B, A, E, J, H, K, C.

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.
*

Infix Notation: The standard way of


representing an expression in which the
operator is placed between its operands is
called the infix form of the expression.

Expression =((𝐴 + 𝐵) ∗ (𝐶/𝐷))


Prefix Notation: The prefix from of an
algebraic expression represented by a binary Expression =∗ + 𝐴𝐵/𝐶𝐷
tree corresponds to the preorder traversal of
the tree.

Postfix Notation: The postfix from of an


algebraic expression represented by binary Expression =𝐴𝐵 + 𝐶𝐷/∗
tree corresponds to the postorder traversal of
the tree.
Traversing binary trees
Graph Traversal Method : Graph traversal is a technique used for searching a vertex
in a graph. There are two graph traversal methods.
Breadth First Search (BFS): Breadth First Search (BFS) algorithm traverses a graph
in a breadthward motion and uses a queue to remember to get the next vertex to start
a search, when a dead end occurs in any iteration.
• Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it
in a queue.
• Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

Depth First Search (DFS):


Depth First Search (DFS) algorithm traverses a graph in a depthward motion and
uses a stack to remember to get the next vertex to start a search, when a dead end
occurs in any iteration.
• 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.
BSF/DSF
Breadth First Search (BFS):
• Rule 1 − Visit the adjacent unvisited vertex.
Mark it as visited. Display it. Insert it in a
queue.
• Rule 2 − If no adjacent vertex is found,
remove the first vertex from the queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until the
queue is empty.

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.
BFS
Breadth First Search (BFS):
• Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it
in a queue.
• Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

1. Initialize the queue.

2. Start from visiting S (starting


node), and mark it as visited.
BFS
Breadth First Search (BFS):
• Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it
in a queue.
• Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

3. We then see an unvisited adjacent


node from S. In this example, we
have three nodes but alphabetically
we choose A, mark it as visited and
enqueue it.

4. Next, the unvisited adjacent node


from S is B. We mark it as visited and
enqueue it.
BFS
Breadth First Search (BFS):
• Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it
in a queue.
• Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

5. Next, the unvisited adjacent node


from S is C. We mark it as visited and
enqueue it.

6. Now, S is left with no unvisited


adjacent nodes. So, we dequeue and
find A.
BFS
Breadth First Search (BFS):
• Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it
in a queue.
• Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

7. From A we have D as unvisited


adjacent node. We mark it as visited and
enqueue it.

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.

1. Initialize the queue.

2. Mark S as visited and put it onto the


stack. Explore any unvisited adjacent
node from S. We have three nodes and we
can pick any of them. For this example,
we shall take the node in an alphabetical
order.
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.

3. Mark A as visited and put it onto the


stack. Explore any unvisited adjacent
node from A. Both S and D are adjacent
to A but we are concerned for unvisited
nodes only.

4. Visit D and mark it as visited and put onto


the stack. Here, we have B and C nodes,
which are adjacent to D and both are
unvisited. However, we shall again choose in
an alphabetical order.
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.

5. We choose B, mark it as visited and put


onto the stack. Here B does not have any
unvisited adjacent node. So, we
pop B from the stack.

6. We check the stack top for return to the


previous node and check if it has any
unvisited nodes. Here, we find D to be on the
top of the stack.
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.

7. Only unvisited adjacent node is


from D is C now. So we visit C, mark it
as visited and put it onto the stack.

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.

Warshall’s Algorithm: Warshall’s algorithm determines the shortest distances


between all pairs of vertices in a graph. It is popular because it is easier to describe
than the other algorithm and it can be applied to a directed graph too without any
change.
Shortest path algorithms: Dijkstra Algorithm
Example: Dijkstra’s algorithm to find the shortest path between the vertices A and

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.

You might also like