0% found this document useful (0 votes)
28 views

Trees

Uploaded by

40Neha Pagariya
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)
28 views

Trees

Uploaded by

40Neha Pagariya
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/ 63

Trees

By:Ms N. S. Khairnar
Tree
A tree is a connected undirected graph with no
simple circuits.

Theorem 1. An undirected graph is a tree if and only


if there is a unique simple path between any two of
its vertices.
Identify which is/are trees?

Graph a and c are


trees, b is Graph, not
a tree.
Properties of a Tree
1. There is only one path between each pair of vertices of a
tree.
2. A tree T with n vertices has n-1 edges.
3. A tree with 2 or more vertices has at least 2 leaves.
Tree Terminology

Branch Node/ Internal Vertex

Leaf Node/ Terminal Vertex

A node that doesn't have a child node is known as a leaf node or terminal node.

A node that doesn't have a parent node is known as a root node.

A node that have a parent as well as Child node is known as a Branch node.
Tree Terminology..
Parent (2): 1

c
Child(2): 4,3

Ancestor(4): 1,2

Descendants(2): 4,3,5,6

Sibling(2): 7
Level, Height and depth of a tree

Height of the tree = the longest path from the root


node to the leaf node.
Eccentricity of a vertex
● The eccentricity of a vertex in a graph is the maximum
distance between that vertex and all other vertices in the
graph.
● It is represented by the symbol e(V).
● Vertex with minimum eccentricity is called as center of a
tree.
Center, Radius and Diameter of a tree
e(0) = 3
e(1) = 3
e(2) = 3
e(3) = 2
e(4) = 2
e(5) = 3
Note: every Tree has either one or
two centers.

Center: 3 and 4
Radius: min{2,3}=2
Diameter: max{2,3}=3
Cut Point / Cut Vertex
● A cut vertex is a vertex in a connected graph that, when
removed, makes the graph disconnected.
● Cut vertices are also known as articulation points.
Cut Point / Cut Vertex..

Note: Every Internal


node/ Branch node is a
cut point.
Subtree
A subtree of a tree is a tree that is made up of a node in the original tree
and all of its descendants.

Tree 1 is subtree.
Tree 2 is not a subtree.
Rooted Tree
● A rooted tree G is a connected acyclic graph with a special node that
is called the root of the tree and every edge directly or indirectly
originates from the root.

● If a directed tree has exactly one node(root) whose incoming degrees


is 0 and all other vertices have incoming degree one, then the tree is
called rooted tree.
M-ary Tree
● Tree in which each and every node has at most M children is called
M-ary Tree.

● Tree in which each and every node has exactly M or 0 children is


called Full M-ary Tree.
M-ary Tree..
Total Number of nodes in Full M-ary tree are:
n=Mi+1

Where, n is total no of nodes


M represents m-ary tree
i is no of internal nodes.
l is no of leaves

For Binary Tree; n=2i+1 and n=i+l


Binary Tree
● Tree in which each and every node has at most 2 children is called
Binary Tree.

● Tree in which each and every node has exactly 2 or 0 children is


called Full Binary Tree.
Q. Find total number of nodes in a full binary tree with 20 leaves.

Given: l=20, i=?

For Full Binary tree, n=2i+1 ---------(1)


and n=i+l
i=n-l
i=n-20
Put this value in equation (1)
n=2(n-20)+1
n=39
Tree Traversal Techniques
○ Preorder traversal: vertex left right
○ Inorder traversal: left vertex right
○ Postorder traversal: left right vertex

Preorder traversal: A → B → D → E → C → F → G

Inorder traversal: D → B → E → A → F → C → G

Postorder traversal: D → E → B → F → G → C → A
Q1 Find preorder, Inorder and postorder Traversal for given tree

B C

D E G H

J K

L
Pre-order Traversal

A B D E C G H J L K

ABDECGHJLK
In-order Traversal

D B E A G C L J H K

DBEAGCLJHK
Post-order Traversal

D E B G L J K H C A

DEBGLJKHCA
Q 2 Find preorder, Inorder and postorder Traversal for given tree

B C

E
Preorder traversal: A→B→C→D→E→F

F Inorder traversal: B→ A→ D → F→ E→ C

Postorder traversal: B→ F→ E→ D→ C→ A
Q 3 Find preorder, Inorder and postorder Traversal for given tree

B C

D G H

E F Preorder: ABDEFCGH
Inorder: BEDFAGCH
Postorder: EFDBGHCA
Q 1 Draw a binary tree from following traversal

Inorder: A
DBFEAGCLJHK
DBFE GCLJHK

Preorder:
ABDEFCGHJLK
Q 1 Draw a binary tree from following traversal

Inorder: A
DBFEAGCLJHK
B C

D FE G LJHK
Preorder:
ABDEFCGHJLK
Q Draw a binary tree from following traversal

Inorder: A
DBFEAGCLJHK
B C

D E G H
Preorder:
ABDEFCGHJLK
LJ K
F
Q Draw a binary tree from following traversal

Inorder: A
DBFEAGCLJHK
B C

D E G H
Preorder:
ABDEFCGHJLK
J K
F

L
Q2 Draw a binary tree from following traversal

Inorder: A
BEDFAGCH
B C

D G H
Postorder:
EFDBGHCA
E F
Optimal Tree
A Tree with minimum weight is called as optimal tree.
For 3,4,5 we can construct following trees.

5 3

4 3
5 4

W(T1)=(5*1)+(4*2)+(3*2) W(T2)=(5*2)+(4*2)+(3*1)
=19 =21
Huffman tree
Consider leaves

W(T) =(4*2)+(4*2)+(2*3)+(3*3)+(7*2)
W(T) = 45
Prefix Code/ Huffman Code
Prefix Codes is the code assigned in such a way that the code
assigned to one character is not the prefix of code assigned to any
other character.

e=00
t=01
w=0001
Is not a prefix code

{000,001,01,10,11} Is a prefix code


0 1 Leaves Prefix code

2 100

3 101
0 1 0 1 4 00

4 01

01 0 1 7 11
00 11

100 101
Q2
A=0.08 B=0.10 C=0.12
D=0.15 E=0.20 F=0.35
Find optimal binary tree, also find prefix code and average
codeword length.
Leaves Prefix code
1
A 000
0.38 0.62 B 001

C 100
0.18 0.20 0.27 0.35
D 101

E 01
0.08 0.10 0.12 0.15
F 11

W(T)= (0.08*3)+ Average codeword length= (0.08*3)+


(0.10*3)+(0.12*3)+(0.15*3)+(0.20*2)+(0.35*2) (0.10*3)+(0.12*3)+(0.15*3)+(0.20*2)+(0.35*2)
= 2.76 bits
Binary Search Tree
● A Binary Search Tree is a data structure used in computer science for organizing and
storing data in a sorted manner.
● Here Each vertex contains a distinct key value.
● left child containing values less than the parent node and the right child containing
values greater than the parent node.
Q1
Draw a BST for 50, 15, 62, 5, 20, 58, 91,3, 8, 37,60, 24.
Minimum Spanning Tree
● A minimum spanning tree (MST) is a subset of the edges of a connected,
edge-weighted graph that connects all the vertices together without any
cycles and with the minimum possible total edge weight.

● It is a spanning Tree with minimum weight.

● For this Graph which are possible spanning trees?

5
(1 + 3 + 2 + 4) : 10 (1 + 4 + 5 + 2): 12 (3 + 2 + 5 + 4) : 14 (1 + 3 + 2 + 5) : 11

Minimum Spanning Tree


Algorithms for MST
Prims Algorithm

Step 1 - First, choose a vertex from the graph. Let's choose B.

Step 2 - Connect a vertex outside the Minimum Spanning Tree (MST) to a vertex
within the MST using the edge with the minimum weight.

Step 3 - Repeat step 2 until all vertices of the main graph are included in the
Minimum Spanning Tree (MST).
Kruskal’s Algorithm
Edge AB DE BC CD AE AC AD

Wt 1 2 3 4 5 7 10

Step 1 - First, sort all the edges from low weight


to high.

Step 2 - Now, take the edge with the lowest


weight and add it to the spanning tree. If the
edge to be added creates a cycle, then reject the
edge.

Step 3 - Continue to add the edges until we


reach all vertices, and a minimum spanning tree
is created.
Q1 Convert a Generic Tree(N-ary Tree) to Binary Tree

● In a Binary Tree, the main node is


the same as the main node in a
Generic Tree.
● The first or leftmost child of a
node in the Generic Tree
becomes the left child in the
Binary Tree.

● Any other sibling of a node in the


Generic Tree becomes the right
child in the Binary Tree.
Q1 Answer
Q2 Convert a Generic Tree(N-array Tree) to Binary Tree
Q2 Answer
Q3 Find preorder and postorder Traversal of following tree
Q3 Answer

Preorder traversal A, B, C, D, E, F, G, H, J, K, L, M, P, Q, N
Postorder traversal E, D, C, H, G, Q, P, N, M, L, K, J, F, B, A
Transport Network
• In discrete mathematics, a transportation network is a directed,
weighted, connected graph with a single source and a single sink.

• A source is a vertex with no incoming edges, and a sink is a vertex


with no outgoing edges.
Q1 Find Maximum flow in the following transport network
Maximum Flow is =10 (7+3 or 6+4)
Max Flow Min Cut Theorem
In discrete mathematics, the max-flow min-cut theorem states that the maximum amount of flow that can pass
through a network from a source to a sink is equal to the minimum sum of a cut. A cut is the smallest total
weight of edges that would disconnect the source from the sink if removed.

The maximum amount of flow passing from the source to the sink is equal to the total weight of the edges in a minimum cut,
Minimum Cut
Q2 Find Maximum flow in the following transport network

4
Q3 Find Maximum flow in the following transport network

7
Q4 Find Maximum flow in the following transport network

You might also like