Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

Trees

Tree: DeIinition, Rooted tree, properties oI trees, binary


search tree, tree traversal.
TerminoIogy of 0rophs:
CycIes, Connecfivify ond Trees
A path that begins and ends at the same node is called a cycle.
Example.
Two nodes are connected iI there is a path between them.
A graph is connected iI every pair oI its nodes is connected.
A graph is acyclic iI it doesn`t have any cycle.
A graph is called a tree iI it is connected and acyclic.
Example:
ompIes/AppIicofions of Trees
Family tree
Possibility tree
Prime number factorization tree
Spanning tree of a telecom network
(considered later in this handout)
Hydrocarbon molecules
!roperfies of Trees
Theorem 1.There is one and only one path between every pair oI
vertices in tree,T.
ProoI: Since T is a connected graph ,there must exist at least one path
between every pair oI vertices in T. let there are two distinct paths
between two vertices u and v oI T,but union oI these two path contain
a cycle and then T cannot be a tree.
Theorem 2.II in a graph G there is one and only one path between
every pair oI vertices, G is a tree.
Theorem 3: A tree T with n vertices has n-1 edges.
Theorem 3. For any positive integer n, II G is a connected graph with n
vertices and n-1 edges, then G is a tree.
!roperfies of Trees
A graph with n vertices is called a tree iI
1. G is Connected and has n-1 edges.
2.G is a acyclic and has n-1 edges
3.G is a acyclic and has n-1 edges.
4.There is exactly one path between every pair oI vertices in G.
Rooted Tree
A rooted tree is a tree in which a particular vertex is distinguished Irom other
and is called the root. In contrast to natural trees,which have their roots at the
bottom, in graph theory rooted drawn with their roots at the top.
We give deIinition oI some terms related to it
Level
Height (maximum level)
Children
Parent
LeaI(or terminal vertex)
Interal vertex
Descendants
Ancestor
Rooted Tree
By Shashi Prabha
xample
a).What is the root oI T?
b).Find the leaves and the internal vertices oI T
c).What are the levels oI c and e.
d). Find the children oI a and e.
e).Find the descendants oI the vertices a and c.
Binary Search Tree
A binary tree is a rooted tree in which each vertex has atmost two childern.
ach child in a binary tree is designated either a eft chi/ or a right chi/,
and an internal vertex has at most one leIt and one right child. A fu binary
is a tree in which each internal vertex has exactly two childern.
Given an internal vertex v oI a binary tree T, the eft subtree oI v is the binary
tree whose root is the leIt child oI v,whose vertices consist oI leIt child oI v
and all its descendants,and whose edges consist oI all those edges oI T that
connect the vertices oI the leIt subtree together.The right subtree oI v is
deIined analogously.
xample:
What are leIt & right childern oI b shown in Fig.? What are the leIt and
right subtrees oI a?
a
b
c
g
j
h
k
e
d
I
Solution
The leIt child oI b is d and right child is e.The leIt subtree oI vertex a consists
oI the vertices b,d,e and I and the right subtree oI consists oI the vertices c,
g,h,j & k whose Iigures are shown in Iig.
Properties oI Binary Tree
Theorem
The number oI vertices in a binary tree is always odd.
In any binary tree T on n vertices, the number oI pendent vertices is
equal (n1)/2.
The number oI internal vertices in a binary tree is one less than the
number oI pendant vertices.
The maximum number oI vertices on level n oI a binary tree is 2
n
where
n_0.
The maximum number oI vertices in binary tree oI height h is 2
h1
-
1,h_0.
Minimum height oI height oI binary tree on n vertices is log
2
(n1)-1
and maximum possible height is (n-1)/2.
Tree Traversal
A traversal oI tree is a process to traverse a tree in a systematic way so that each vertex is
visited exactly once. Three commonly used traversals are preorder, postorder and
inorder. We describe here these 3 process that may be used to traverse a binary.
!reor/er Traversa:The preorder traversal oI binary tree is deIined recursively as Iollows
i).Visit the root
ii).Traverse the leIt subtree in preorder.
iii).Traverse the right subtree in preorder.
!ostor/er Traversa:The postorder traversal oI binary tree is deIined recursively as Iollows
i).Traverse the leIt subtree in postorder.
ii).Traverse the right subtree in postorder.
iii).Visit the root
Tree Traversal
nor/er Traversa: The inorder traversal oI binary tree is deIined
recursively as Iollows.
i).Traverse in inorder the leIt subtree.
ii).Visit the root
iii).Traverse in inorder the right subtree.

You might also like