SlideShare a Scribd company logo
1 of 40
DISCRETE MATHEMATICS AND its APPLICATIONSDISCRETE MATHEMATICS AND its APPLICATIONS
TREESTREES
CHAPTER4TREES
CONTENTCONTENT
4.1 Introduction to Trees
4.2 Application of Trees
4.3 Tree Traversal
4.4 Trees and Sorting (Data structure)
4.5 Spanning Trees
4.6 Minimum Spanning
Trees
4.1 INTRODUCTION TO TREES4.1 INTRODUCTION TO TREES
• Define and recognize a tree
• Define and recognize a rooted tree
• Define and recognize a m-ary tree
CHAPTER4TREES
4.1INTRODUCTIONTOTREES
►A tree is a connected undirected graph with no simple circuit.
►An undirected graph is a tree if and only if there is a unique
simple path between any two of its vertices.
►THEOREM: A tree with n vertices has n – 1 edges.
A TreeA Tree
a b c
d e f
a b c
d e f
a b c
d e f
NOT A TREE NOT A TREEA TREE
EXAMPLES
4.1INTRODUCTIONTOTREES
► A rooted tree is a tree in which one vertex has been designated as
the root and every edge is directed away from the root.
► Different choice of root produce different rooted tree
A Rooted TreeA Rooted Tree
a
b
c
d
e
f a
b
c d
ef
a
b
c
d
e
f
A TREE
A TREE with
root c
A TREE with
root a
EXAMPLES
g
g
g
4.1INTRODUCTIONTOTREES
• Parent – A vertex other than root is a
parent if it has one or more children
 The parent of c is b
• Children – If A is a vertex with successors B
and C, then B and C are the children of A.
 The children of a is b, f and g
• Siblings – Children with the same parent
vertex.
 h, i and j are siblings
• Level – the length of the unique path from
the root to a vertex
 Vertex a is at level 0
 Vertices d and e is at level 3
Properties of Rooted TreesProperties of Rooted Trees
a
b
f g
h
d
EXAMPLES
e
i
jc
k l m
• Height – The maximum
level of all the vertices
 The height of this
tree is 3.
4.1INTRODUCTIONTOTREES
• Ancestor of a vertex (v) – the vertices in
the path from the root to this vertex
excluding this vertex.
 The ancestors of e are c, b and a
• Descendent of a vertex (v) – vertices
that have v as ancestor.
 The descendants of b are c, d and e
• Leaf – A vertex with no children
 The leaves are d, e, f, i, k, l and m
• Internal Vertices – vertices that have
children
 The internal vertices are a, b, c, g,
h and j
Properties of Rooted TreesProperties of Rooted Trees
a
b
f g
h
d
EXAMPLES
e
i
jc
k l m
A subtree rooted at g
• Subtree – A subgraph of the
tree consisting of a root and its
descendent and all edges
incident to these descendent.
parent of
d
child of
c
sibling of
d
ancestor of
d
descendants of g
lea
f
internal
vertex
subtree
roo
t
4.1INTRODUCTIONTOTREES
► A rooted tree is called an m-ary tree if every vertex has no more than
m children.
► The tree is called a full m-ary tree if every internal vertex has exactly
m children.
► A rooted m-ary tree is balanced if all leaves are at levels h or h-1.
mm-ary Tree-ary Tree
a
b c d
f
a
b
c
d
e
f
A full binary TREEA 3-ary TREE
EXAMPLES
g
g
a
b
c d
ef g
A full 4-ary TREE
e
h i j k l m
4.1INTRODUCTIONTOTREES
► THEOREM 1: A full m-ary tree with i internal vertices contains
n = mi + 1 vertices
► THEOREM 2: A full m-ary tree with
i. n vertices has internal vertices and
leaves.
ii. i internal vertices has vertices and leaves.
iii. l leaves has vertices and internal vertices.
► THEOREM 3: There are at most leaves in an m-ary tree of
height h.
mm-ary Tree-ary Tree
1n
i
m
−
=
( )1 1m n
l
m
− +
=
1n mi= + ( )1 1l m i= − +
1
1
ml
n
m
−
=
−
1
1
l
i
m
−
=
−
h
m
4.24.2 APPLICATION OF TREESAPPLICATION OF TREES
• Introduce Binary Search Trees
• Introduce Decision Trees
• Introduce Game Trees
CHAPTER4TREES
4.2APPLICATIONOFTREES
• A binary tree in which each
child of a vertex is designated
as a right or left child
• No vertex has more than one
right child or left child
• Each vertex is labeled with a
key
• Vertices are assigned keys so
that the key of a vertex is
both larger than the keys of
all vertices in its left subtree
and smaller than the keys of
all vertices in its right
subtree.
Binary Search TreesBinary Search Trees
EXAMPLE
Binary search tree for the words
mathematics, physics,
geography, zoology,
meteorology, geology,
psychology, and chemistry
using alphabetical order
geography
chemistry
mathematics
geology
physics
metereology
zoology
psycology
4.2APPLICATIONOFTREES
• A rooted tree in
which each
internal vertex
corresponds to a
decision, with a
subtree at these
vertices for each
possible outcome
of decision.
• The possible
solutions of the
problem
correspond to the
paths to the leaves
of this rooted tree.
Decision TreesDecision Trees
EXAMPLE
A Decision tree that orders the
elements of the list a, b, c
a : b
a : c b : c
b : c c > a > b
a > b a < b
a > c a < c
a : c c > b > a
b > c b < c
a > c > b
b > c b < c
a > b > c b > c > a
a > c a < c
b > a > c
4.2APPLICATIONOFTREES
• Use trees to analyze certain types of games
• Vertices represent the positions can be in as it progresses
• Edges represent legal moves between this position
• Games tree is infinite if the games they represent never end
Game TreesGame Trees
EXAMPLE
A Game tree
that represents
the first two
level of the
tic-tac-toe
game.
4.34.3 TREE TRAVERSALTREE TRAVERSAL
• Determine preorder traversal, inorder
traversal and postorder traversal of an
ordered rooted tree.
• Determine the infix, prefix, and postfix form
of an expression.
CHAPTER4TREES
4.3TREETRAVERSAL
• Ordered trees are often used to restore data/info.
• Tree traversal is a procedure for systematically visiting each
vertex of an ordered rooted tree to access data.
• If the tree is label by Universal Address System we can
totally order the vertices using lexicographic ordering
– Example: 0 < 1 < 1.1 < 1.2 < 1.2.1 < 1.3 < 2 < 3 < 3.1 <
3.1.1 < 3.1.2 < 3.1.2.1 < 3.1.2.2 < 4 < 4.1
• Tree traversal algorithm
– Preorder, inorder and postorder traversal
Tree TraversalTree Traversal
4.3TREETRAVERSAL
• Let T be an ordered rooted tree with root r.
– If T consists only of r, then r is the preorder traversal of T.
– If T1, T2, …, Tn are subtrees at r from left to right in T, then
the preorder traversal begins by visiting r, continues by
traversing T1 in preorder, then T2 in preorder, and so on
until Tn is traversed in preorder.
Preorder TraversalPreorder Traversal
Tn
r STEP 1
Visit r
T2 …
STEP 2
Visit T1 in preorder
T1
STEP n+1
Visit Tn in preorder
STEP 3
Visit T2 in preorder
TIPS
Preorder Traversal:
Visit root, visit
subtrees left to right
EXAMPLE: Preorder TraversalEXAMPLE: Preorder Traversal
b
c
d
h
j k
i
n
p
e
l
m
a
o
f g
4.3TREETRAVERSAL
EXAMPLE: Preorder TraversalEXAMPLE: Preorder Traversal
b
c
d
h
j k
i
n p
e
g
l m
a
o
f
b d
h
j k
i
n p
e
g
l m
a
•
o
f
c
•
j
k
n po
g
l m
ea
•
c
•
b
•
d
•
f
•
h
•
i
•
a
•
c
•
b
•
d
•
f
•
h
•
i
•
e
•
j
•
k
n po
l
•
m
•
a
•
b
•
e
•
j
•
k
•
n
•
o
•
p
•
c
•
d
•
h
•
i
•
l
•
m
•
f
•
The preorder traversal of T
T
ITERATION 1
ITERATION 2
ITERATION 3
ITERATION 4
g
•
g
•
4.3TREETRAVERSAL
• Let T be an ordered rooted tree with root r.
– If T consists only of r, then r is the inorder traversal of T.
– If T1, T2, …, Tn are subtrees at r from left to right in T, then
the inorder traversal begins by traversing T1 in inorder,
then visiting r, continues by traversing T2 in inorder, and
so on until Tn is traversed in inorder.
Inorder TraversalInorder Traversal
Tn
r STEP 2
Visit r
T2 …
STEP 1
Visit T1 in inorder
T1
STEP n+1
Visit Tn in inorder
STEP 3
Visit T2 in inorder
TIPS
Inorder Traversal:
Visit leftmost
subtree, Visit root,
Visit other
subtrees left to
right.
EXAMPLE: Inorder TraversalEXAMPLE: Inorder Traversal
b
c
d
h
j k
i
n
p
e
l
m
a
o
f
g
4.3TREETRAVERSAL
EXAMPLE: Inorder TraversalEXAMPLE: Inorder Traversal
b
c
d
h
j k
i
n p
e
g
l m
a
o
f
b d
h
j k
i
n p
e
g
l m
a
•
o
f
c
•
j
k
n po
g
l m
e f
•
c
•
b
•
d
•
a
•
h
•
i
•
a
•
l
•
b
•
g
•
f
•
h
•
i
•
e
•
j
•
k
n po
m
•
d
•
j
•
e
•
n
•
k
•
o
•
p
•
f
•
a
•
g
•
m
•
h
•
i
•
d
•
b
•
l
•
The inorder traversal of T
T
ITERATION 1
ITERATION 2
ITERATION 3
ITERATION 4
c
•
c
•
4.3TREETRAVERSAL
• Let T be an ordered rooted tree with root r.
– If T consists only of r, then r is the postorder traversal of T.
– If T1, T2, …, Tn are subtrees at r from left to right in T, then
the preorder traversal begins by traversing T1 in postorder,
then T2 in postorder, and so on until Tn is traversed in
postorder and ends by visiting r.
Postorder TraversalPostorder Traversal
Tn
r STEP n+1
Visit r
T2 …
STEP 1
Visit T1 in postorder
T1
STEP n
Visit Tn in postorder
STEP 2
Visit T2 in postorder
TIPS
Postorder Traversal:
Visit subtrees
left to right, Visit
root.
b
c
d
h
j k
i
n
p
e
l
m
a
o
f
EXAMPLE: Postorder TraversalEXAMPLE: Postorder Traversal
g
4.3TREETRAVERSAL
EXAMPLE: Postorder TraversalEXAMPLE: Postorder Traversal
b
c
d
h
j k
i
n p
e
g
l m
a
o
f
b d
h
j k
i
n p
e
g
l m
a
•
o
f
c
•
j
k
n po
g
l m
e a
•
c
•
b
•
d
•
f
•
h
•
i
•
a
•
c
•
b
•
d
•
f
•
h
•
i
•
e
•
j
•
k
n po
l
•
m
•
j
•
n
•
o
•
p
•
k
•
e
•
f
•
b
•
l
•
m
•
d
•
a
•
h
•
i
•
c
•
The post order traversal of T
T
ITERATION 1
ITERATION 2
ITERATION 3
ITERATION 4
g
•
g
•
4.3TREETRAVERSAL
• We can represent complicated expression (propositions,
sets, arithmetic) using ordered rooted trees.
• EXAMPLE: A binary tree representing ((x+y)↑2)+((x-4)/3)
Represent Expression by Rooted TreeRepresent Expression by Rooted Tree
↑
+
+
2
/
_
3
x y x 4
Represent Expression by Rooted TreeRepresent Expression by Rooted Tree
4.3TREETRAVERSAL
• We obtain the Infix form of an expression when we
traverse its rooted tree in Inorder.
– The infix form for expression ((x+y)↑2)+((x-4)/3) is
x + y ↑2 + x – 4 / 3 or ((x+y)↑2)+((x-4)/3)
• We obtain the Prefix form of an expression when we
traverse its rooted tree in Preorder.
– The prefix form for expression ((x+y)↑2)+((x-4)/3) is
+ ↑ + x y 2 / - x 4 3
• We obtain the Postfix form of an expression when we
traverse its rooted tree in Postorder.
– The postfix form for expression ((x+y)↑2)+((x-4)/3) is
x y + 2 ↑ x 4 – 3 / +
Infix, Prefix & Postfix NotationInfix, Prefix & Postfix Notation
4.3TREETRAVERSAL
• Working right to left and performing operations using the
operands on the right.
• EXAMPLE:
The value of the prefix expression + - * 2 3 5 / ↑ 2 3 4 is 3
Evaluating Prefix ExpressionEvaluating Prefix Expression
+ - * 2 3 5 / ↑ 2 3 4
2 ↑ 3 = 8
+ - * 2 3 5 / 8 4
8/4 =2
+ - * 2 3 5 2
2 * 3 = 6
+ - * 2 3 5 2
2 * 3 = 6
+ - 6 5 2
6 – 5 =1
+ 1 2
1 + 2 = 3
4.3TREETRAVERSAL
• Working left to right and performing operations using the
operands on the left.
• EXAMPLE:
The value of the postfix expression 7 2 3 * - 4 ↑ 9 3 / + is 4
Evaluating Postfix ExpressionEvaluating Postfix Expression
7 2 3 * - 4 ↑ 9 3 / +
2 * 3 = 6
7 - 6 = 1
1 ↑ 4 = 1
9/3 = 3
1 + 3 = 4
7 6 - 4 ↑ 9 3 / +
1 4 ↑ 9 3 / +
1 ↑ 4 = 1
1 4 ↑ 9 3 / +
1 9 3 / +
1 3 +
4.54.5 SPANNING TREESSPANNING TREES
• Find spanning trees of a simple graph
CHAPTER4TREES
4.5SPANNINGTREES
• Let G be a simple graph. A spanning tree of G is a subgraph of
G that is a tree containing every vertex of G.
• A simple graph is connected if and only if it has a spanning
tree.
• Applied in IP multitasking.
Spanning TreesSpanning Trees
a b c
e f g
d
a b c
e f g
d
a b c
e f g
d
A simple graph
Not
Spanning
tree
Spanning
tree
4.64.6 MINIMUM SPANNING TREESMINIMUM SPANNING TREES
• Find minimum spanning tree using Prim’s
algorithm
• Find minimum spanning tree using
Kruskal’s algorithm
CHAPTER4TREES
4.6MINIMUMSPANNINGTREES
• A minimum spanning tree in a connected weighted graph is
a spanning tree that has the smallest possible sum of
weights of it edges.
• Two algorithms can be used:
– Prim’s Algorithm
• Robert Prim, 1957
– Kruskal’s Algorithm
• Joseph Kruskal, 1956
Minimum Spanning TreesMinimum Spanning Trees
4.6MINIMUMSPANNINGTREES
1. Chose an edge with the least weight.
2. Include it in spanning tree, T.
3. Select an edge of least weight that is incident with a vertex
of an edge in T.
4. If it does not create a cycle (simple circuit) with the edges in
T, then include it in T; otherwise discard it.
5. Repeat STEPS 3 and 4 until T contains n-1 edges.
• There may be more than one minimum spanning tree
for a given connected weighted simple graph.
• If there are two edges with similar smallest weight,
chose either one.
Prim’s AlgorithmPrim’s Algorithm
4.6MINIMUMSPANNINGTREES
EXAMPLE: Prim’s AlgorithmEXAMPLE: Prim’s Algorithm
CHOICE EDGE WEIGHT SPANNING TREE
1 {a, b} 7
2 {a, d} 10
3 {b, e} 11
4 {e, c} 12
db
c
a e
7
13
10
16
13
12
12
FIGURE 1
10 11
b
a
7
db
a
7 10
15
d
b
a
7 10
db
a
7 10
e11
e11
c
12
The minimum
spanning tree is
given by:
d
b
a
7 10
e11
c
12
Total weight = 40
4.6MINIMUMSPANNINGTREES
1. Arrange the edges in G in increasing order.
2. Chose an edge with the minimum weight.
3. Include it in spanning tree, T.
4. Add an edge of least weight to T.
5. If it does not create a cycle (simple circuit) with the edges in
T, then include it in T; otherwise discard it.
6. Repeat STEPS 4 and 5 until T contains n-1 edges.
• There may be more than one minimum spanning tree
for a given connected weighted simple graph.
• If there are two edges with similar smallest weight,
chose either one.
Kruskal’s AlgorithmKruskal’s Algorithm
4.6MINIMUMSPANNINGTREES
EXAMPLE: Kruskal’s AlgorithmEXAMPLE: Kruskal’s Algorithm
CHOICE EDGE WEIGHT SPANNING TREE
1 {a, b} 7
2 {b, d} 10
3 {b, e} 11
4 {e, c} 12
db
c
a e
7
13
10
16
13
12
12
FIGURE 1
10 11
b
a
7
b
a
7
10
15
b
a
7
10
b
a
7
10
e11
e11
c
12
The minimum
spanning tree is
given by:
b
a
7
10
e11
c
12
Total weight = 40
d
d
d
d
CHAPTER4TREES
SUMMARYSUMMARY
• Tree is a connected undirected graph with no simple circuits
• Trees have been employed to solve problems in a wide variety
of disciplines.

More Related Content

What's hot (20)

Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relations
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
Red black tree
Red black treeRed black tree
Red black tree
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 d
 
Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Isomorphic graph
Isomorphic graphIsomorphic graph
Isomorphic graph
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Poset in Relations(Discrete Mathematics)
Poset in Relations(Discrete Mathematics)Poset in Relations(Discrete Mathematics)
Poset in Relations(Discrete Mathematics)
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 

Viewers also liked

Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIWongyos Keardsri
 
Trees (slides)
Trees (slides)Trees (slides)
Trees (slides)IIUM
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Treecinterviews
 
Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)Fahrul Usman
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphsmaznabili
 
Tree in Discrete structure
Tree in Discrete structureTree in Discrete structure
Tree in Discrete structureNoman Rajput
 
Relations digraphs
Relations  digraphsRelations  digraphs
Relations digraphsIIUM
 
1.4 expression tree
1.4 expression tree  1.4 expression tree
1.4 expression tree Krish_ver2
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression treeShab Bi
 

Viewers also liked (17)

Trees and graphs
Trees and graphsTrees and graphs
Trees and graphs
 
Discrete-Chapter 10 Trees
Discrete-Chapter 10 TreesDiscrete-Chapter 10 Trees
Discrete-Chapter 10 Trees
 
Avltrees
AvltreesAvltrees
Avltrees
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part II
 
Trees (slides)
Trees (slides)Trees (slides)
Trees (slides)
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphs
 
Tree in Discrete structure
Tree in Discrete structureTree in Discrete structure
Tree in Discrete structure
 
Relations digraphs
Relations  digraphsRelations  digraphs
Relations digraphs
 
1.4 expression tree
1.4 expression tree  1.4 expression tree
1.4 expression tree
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
Tree
TreeTree
Tree
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
The Political History Of Bangladesh
The Political History Of BangladeshThe Political History Of Bangladesh
The Political History Of Bangladesh
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression tree
 

Similar to Trees

Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana Shaikh
 
tutorial-tree (3).ppt
tutorial-tree (3).ppttutorial-tree (3).ppt
tutorial-tree (3).pptSrinivasanCSE
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREESTABISH HAMID
 
unit 4 for trees data structure notes it is
unit 4 for trees data structure notes it isunit 4 for trees data structure notes it is
unit 4 for trees data structure notes it islogesswarisrinivasan
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptssuser5c874e
 
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxAhmedEldesoky24
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graphRai University
 

Similar to Trees (20)

Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
tutorial-tree (3).ppt
tutorial-tree (3).ppttutorial-tree (3).ppt
tutorial-tree (3).ppt
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
Trees
TreesTrees
Trees
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
unit 4 for trees data structure notes it is
unit 4 for trees data structure notes it isunit 4 for trees data structure notes it is
unit 4 for trees data structure notes it is
 
Treesandgraphs
TreesandgraphsTreesandgraphs
Treesandgraphs
 
Trees
TreesTrees
Trees
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
 
Tree
TreeTree
Tree
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptx
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 

More from Gaditek

Digital marketing strategy and planning | About Business
Digital marketing strategy and planning | About BusinessDigital marketing strategy and planning | About Business
Digital marketing strategy and planning | About BusinessGaditek
 
Intro to social network analysis | What is Network Analysis? | History of (So...
Intro to social network analysis | What is Network Analysis? | History of (So...Intro to social network analysis | What is Network Analysis? | History of (So...
Intro to social network analysis | What is Network Analysis? | History of (So...Gaditek
 
Marketing ethics and social responsibility | Criticisms of Marketing
Marketing ethics and social responsibility | Criticisms of MarketingMarketing ethics and social responsibility | Criticisms of Marketing
Marketing ethics and social responsibility | Criticisms of MarketingGaditek
 
understanding and capturing customer value | What Is a Price?
understanding and capturing customer value | What Is a Price?understanding and capturing customer value | What Is a Price?
understanding and capturing customer value | What Is a Price?Gaditek
 
The marketing environment | Suppliers | Marketing intermediaries
The marketing environment | Suppliers | Marketing intermediariesThe marketing environment | Suppliers | Marketing intermediaries
The marketing environment | Suppliers | Marketing intermediariesGaditek
 
strategic planning | Customer Relationships | Partnering to Build
strategic planning | Customer Relationships | Partnering to Build strategic planning | Customer Relationships | Partnering to Build
strategic planning | Customer Relationships | Partnering to Build Gaditek
 
Digital marketing | what is marketing?
Digital marketing | what is marketing?Digital marketing | what is marketing?
Digital marketing | what is marketing?Gaditek
 
Fundamentals of Computer Design including performance measurements & quantita...
Fundamentals of Computer Design including performance measurements & quantita...Fundamentals of Computer Design including performance measurements & quantita...
Fundamentals of Computer Design including performance measurements & quantita...Gaditek
 
Dealing with exceptions Computer Architecture part 2
Dealing with exceptions Computer Architecture part 2Dealing with exceptions Computer Architecture part 2
Dealing with exceptions Computer Architecture part 2Gaditek
 
Dealing with Exceptions Computer Architecture part 1
Dealing with Exceptions Computer Architecture part 1Dealing with Exceptions Computer Architecture part 1
Dealing with Exceptions Computer Architecture part 1Gaditek
 
Pipelining of Processors
Pipelining of ProcessorsPipelining of Processors
Pipelining of ProcessorsGaditek
 
Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Gaditek
 
differential equation Lecture#14
differential equation  Lecture#14differential equation  Lecture#14
differential equation Lecture#14Gaditek
 
differential equation Lecture#12
differential equation Lecture#12differential equation Lecture#12
differential equation Lecture#12Gaditek
 
differential equation Lecture#11
differential equation Lecture#11differential equation Lecture#11
differential equation Lecture#11Gaditek
 
differential equation Lecture#13
differential equation Lecture#13differential equation Lecture#13
differential equation Lecture#13Gaditek
 
differential equation Lecture#10
differential equation Lecture#10differential equation Lecture#10
differential equation Lecture#10Gaditek
 
differential equation Lecture#9
differential equation  Lecture#9differential equation  Lecture#9
differential equation Lecture#9Gaditek
 
differential equation Lecture#8
differential equation Lecture#8differential equation Lecture#8
differential equation Lecture#8Gaditek
 
differential equation Lecture#7
differential equation Lecture#7differential equation Lecture#7
differential equation Lecture#7Gaditek
 

More from Gaditek (20)

Digital marketing strategy and planning | About Business
Digital marketing strategy and planning | About BusinessDigital marketing strategy and planning | About Business
Digital marketing strategy and planning | About Business
 
Intro to social network analysis | What is Network Analysis? | History of (So...
Intro to social network analysis | What is Network Analysis? | History of (So...Intro to social network analysis | What is Network Analysis? | History of (So...
Intro to social network analysis | What is Network Analysis? | History of (So...
 
Marketing ethics and social responsibility | Criticisms of Marketing
Marketing ethics and social responsibility | Criticisms of MarketingMarketing ethics and social responsibility | Criticisms of Marketing
Marketing ethics and social responsibility | Criticisms of Marketing
 
understanding and capturing customer value | What Is a Price?
understanding and capturing customer value | What Is a Price?understanding and capturing customer value | What Is a Price?
understanding and capturing customer value | What Is a Price?
 
The marketing environment | Suppliers | Marketing intermediaries
The marketing environment | Suppliers | Marketing intermediariesThe marketing environment | Suppliers | Marketing intermediaries
The marketing environment | Suppliers | Marketing intermediaries
 
strategic planning | Customer Relationships | Partnering to Build
strategic planning | Customer Relationships | Partnering to Build strategic planning | Customer Relationships | Partnering to Build
strategic planning | Customer Relationships | Partnering to Build
 
Digital marketing | what is marketing?
Digital marketing | what is marketing?Digital marketing | what is marketing?
Digital marketing | what is marketing?
 
Fundamentals of Computer Design including performance measurements & quantita...
Fundamentals of Computer Design including performance measurements & quantita...Fundamentals of Computer Design including performance measurements & quantita...
Fundamentals of Computer Design including performance measurements & quantita...
 
Dealing with exceptions Computer Architecture part 2
Dealing with exceptions Computer Architecture part 2Dealing with exceptions Computer Architecture part 2
Dealing with exceptions Computer Architecture part 2
 
Dealing with Exceptions Computer Architecture part 1
Dealing with Exceptions Computer Architecture part 1Dealing with Exceptions Computer Architecture part 1
Dealing with Exceptions Computer Architecture part 1
 
Pipelining of Processors
Pipelining of ProcessorsPipelining of Processors
Pipelining of Processors
 
Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)
 
differential equation Lecture#14
differential equation  Lecture#14differential equation  Lecture#14
differential equation Lecture#14
 
differential equation Lecture#12
differential equation Lecture#12differential equation Lecture#12
differential equation Lecture#12
 
differential equation Lecture#11
differential equation Lecture#11differential equation Lecture#11
differential equation Lecture#11
 
differential equation Lecture#13
differential equation Lecture#13differential equation Lecture#13
differential equation Lecture#13
 
differential equation Lecture#10
differential equation Lecture#10differential equation Lecture#10
differential equation Lecture#10
 
differential equation Lecture#9
differential equation  Lecture#9differential equation  Lecture#9
differential equation Lecture#9
 
differential equation Lecture#8
differential equation Lecture#8differential equation Lecture#8
differential equation Lecture#8
 
differential equation Lecture#7
differential equation Lecture#7differential equation Lecture#7
differential equation Lecture#7
 

Recently uploaded

School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxnuruddin69
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stageAbc194748
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfsmsksolar
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...Health
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 

Recently uploaded (20)

Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 

Trees

  • 1. DISCRETE MATHEMATICS AND its APPLICATIONSDISCRETE MATHEMATICS AND its APPLICATIONS TREESTREES
  • 2. CHAPTER4TREES CONTENTCONTENT 4.1 Introduction to Trees 4.2 Application of Trees 4.3 Tree Traversal 4.4 Trees and Sorting (Data structure) 4.5 Spanning Trees 4.6 Minimum Spanning Trees
  • 3. 4.1 INTRODUCTION TO TREES4.1 INTRODUCTION TO TREES • Define and recognize a tree • Define and recognize a rooted tree • Define and recognize a m-ary tree CHAPTER4TREES
  • 4. 4.1INTRODUCTIONTOTREES ►A tree is a connected undirected graph with no simple circuit. ►An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. ►THEOREM: A tree with n vertices has n – 1 edges. A TreeA Tree a b c d e f a b c d e f a b c d e f NOT A TREE NOT A TREEA TREE EXAMPLES
  • 5. 4.1INTRODUCTIONTOTREES ► A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root. ► Different choice of root produce different rooted tree A Rooted TreeA Rooted Tree a b c d e f a b c d ef a b c d e f A TREE A TREE with root c A TREE with root a EXAMPLES g g g
  • 6. 4.1INTRODUCTIONTOTREES • Parent – A vertex other than root is a parent if it has one or more children  The parent of c is b • Children – If A is a vertex with successors B and C, then B and C are the children of A.  The children of a is b, f and g • Siblings – Children with the same parent vertex.  h, i and j are siblings • Level – the length of the unique path from the root to a vertex  Vertex a is at level 0  Vertices d and e is at level 3 Properties of Rooted TreesProperties of Rooted Trees a b f g h d EXAMPLES e i jc k l m • Height – The maximum level of all the vertices  The height of this tree is 3.
  • 7. 4.1INTRODUCTIONTOTREES • Ancestor of a vertex (v) – the vertices in the path from the root to this vertex excluding this vertex.  The ancestors of e are c, b and a • Descendent of a vertex (v) – vertices that have v as ancestor.  The descendants of b are c, d and e • Leaf – A vertex with no children  The leaves are d, e, f, i, k, l and m • Internal Vertices – vertices that have children  The internal vertices are a, b, c, g, h and j Properties of Rooted TreesProperties of Rooted Trees a b f g h d EXAMPLES e i jc k l m A subtree rooted at g • Subtree – A subgraph of the tree consisting of a root and its descendent and all edges incident to these descendent.
  • 8. parent of d child of c sibling of d ancestor of d descendants of g lea f internal vertex subtree roo t
  • 9. 4.1INTRODUCTIONTOTREES ► A rooted tree is called an m-ary tree if every vertex has no more than m children. ► The tree is called a full m-ary tree if every internal vertex has exactly m children. ► A rooted m-ary tree is balanced if all leaves are at levels h or h-1. mm-ary Tree-ary Tree a b c d f a b c d e f A full binary TREEA 3-ary TREE EXAMPLES g g a b c d ef g A full 4-ary TREE e h i j k l m
  • 10. 4.1INTRODUCTIONTOTREES ► THEOREM 1: A full m-ary tree with i internal vertices contains n = mi + 1 vertices ► THEOREM 2: A full m-ary tree with i. n vertices has internal vertices and leaves. ii. i internal vertices has vertices and leaves. iii. l leaves has vertices and internal vertices. ► THEOREM 3: There are at most leaves in an m-ary tree of height h. mm-ary Tree-ary Tree 1n i m − = ( )1 1m n l m − + = 1n mi= + ( )1 1l m i= − + 1 1 ml n m − = − 1 1 l i m − = − h m
  • 11. 4.24.2 APPLICATION OF TREESAPPLICATION OF TREES • Introduce Binary Search Trees • Introduce Decision Trees • Introduce Game Trees CHAPTER4TREES
  • 12. 4.2APPLICATIONOFTREES • A binary tree in which each child of a vertex is designated as a right or left child • No vertex has more than one right child or left child • Each vertex is labeled with a key • Vertices are assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left subtree and smaller than the keys of all vertices in its right subtree. Binary Search TreesBinary Search Trees EXAMPLE Binary search tree for the words mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry using alphabetical order geography chemistry mathematics geology physics metereology zoology psycology
  • 13. 4.2APPLICATIONOFTREES • A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of decision. • The possible solutions of the problem correspond to the paths to the leaves of this rooted tree. Decision TreesDecision Trees EXAMPLE A Decision tree that orders the elements of the list a, b, c a : b a : c b : c b : c c > a > b a > b a < b a > c a < c a : c c > b > a b > c b < c a > c > b b > c b < c a > b > c b > c > a a > c a < c b > a > c
  • 14.
  • 15. 4.2APPLICATIONOFTREES • Use trees to analyze certain types of games • Vertices represent the positions can be in as it progresses • Edges represent legal moves between this position • Games tree is infinite if the games they represent never end Game TreesGame Trees EXAMPLE A Game tree that represents the first two level of the tic-tac-toe game.
  • 16. 4.34.3 TREE TRAVERSALTREE TRAVERSAL • Determine preorder traversal, inorder traversal and postorder traversal of an ordered rooted tree. • Determine the infix, prefix, and postfix form of an expression. CHAPTER4TREES
  • 17. 4.3TREETRAVERSAL • Ordered trees are often used to restore data/info. • Tree traversal is a procedure for systematically visiting each vertex of an ordered rooted tree to access data. • If the tree is label by Universal Address System we can totally order the vertices using lexicographic ordering – Example: 0 < 1 < 1.1 < 1.2 < 1.2.1 < 1.3 < 2 < 3 < 3.1 < 3.1.1 < 3.1.2 < 3.1.2.1 < 3.1.2.2 < 4 < 4.1 • Tree traversal algorithm – Preorder, inorder and postorder traversal Tree TraversalTree Traversal
  • 18. 4.3TREETRAVERSAL • Let T be an ordered rooted tree with root r. – If T consists only of r, then r is the preorder traversal of T. – If T1, T2, …, Tn are subtrees at r from left to right in T, then the preorder traversal begins by visiting r, continues by traversing T1 in preorder, then T2 in preorder, and so on until Tn is traversed in preorder. Preorder TraversalPreorder Traversal Tn r STEP 1 Visit r T2 … STEP 2 Visit T1 in preorder T1 STEP n+1 Visit Tn in preorder STEP 3 Visit T2 in preorder TIPS Preorder Traversal: Visit root, visit subtrees left to right
  • 19. EXAMPLE: Preorder TraversalEXAMPLE: Preorder Traversal b c d h j k i n p e l m a o f g
  • 20. 4.3TREETRAVERSAL EXAMPLE: Preorder TraversalEXAMPLE: Preorder Traversal b c d h j k i n p e g l m a o f b d h j k i n p e g l m a • o f c • j k n po g l m ea • c • b • d • f • h • i • a • c • b • d • f • h • i • e • j • k n po l • m • a • b • e • j • k • n • o • p • c • d • h • i • l • m • f • The preorder traversal of T T ITERATION 1 ITERATION 2 ITERATION 3 ITERATION 4 g • g •
  • 21. 4.3TREETRAVERSAL • Let T be an ordered rooted tree with root r. – If T consists only of r, then r is the inorder traversal of T. – If T1, T2, …, Tn are subtrees at r from left to right in T, then the inorder traversal begins by traversing T1 in inorder, then visiting r, continues by traversing T2 in inorder, and so on until Tn is traversed in inorder. Inorder TraversalInorder Traversal Tn r STEP 2 Visit r T2 … STEP 1 Visit T1 in inorder T1 STEP n+1 Visit Tn in inorder STEP 3 Visit T2 in inorder TIPS Inorder Traversal: Visit leftmost subtree, Visit root, Visit other subtrees left to right.
  • 22. EXAMPLE: Inorder TraversalEXAMPLE: Inorder Traversal b c d h j k i n p e l m a o f g
  • 23. 4.3TREETRAVERSAL EXAMPLE: Inorder TraversalEXAMPLE: Inorder Traversal b c d h j k i n p e g l m a o f b d h j k i n p e g l m a • o f c • j k n po g l m e f • c • b • d • a • h • i • a • l • b • g • f • h • i • e • j • k n po m • d • j • e • n • k • o • p • f • a • g • m • h • i • d • b • l • The inorder traversal of T T ITERATION 1 ITERATION 2 ITERATION 3 ITERATION 4 c • c •
  • 24. 4.3TREETRAVERSAL • Let T be an ordered rooted tree with root r. – If T consists only of r, then r is the postorder traversal of T. – If T1, T2, …, Tn are subtrees at r from left to right in T, then the preorder traversal begins by traversing T1 in postorder, then T2 in postorder, and so on until Tn is traversed in postorder and ends by visiting r. Postorder TraversalPostorder Traversal Tn r STEP n+1 Visit r T2 … STEP 1 Visit T1 in postorder T1 STEP n Visit Tn in postorder STEP 2 Visit T2 in postorder TIPS Postorder Traversal: Visit subtrees left to right, Visit root.
  • 25. b c d h j k i n p e l m a o f EXAMPLE: Postorder TraversalEXAMPLE: Postorder Traversal g
  • 26. 4.3TREETRAVERSAL EXAMPLE: Postorder TraversalEXAMPLE: Postorder Traversal b c d h j k i n p e g l m a o f b d h j k i n p e g l m a • o f c • j k n po g l m e a • c • b • d • f • h • i • a • c • b • d • f • h • i • e • j • k n po l • m • j • n • o • p • k • e • f • b • l • m • d • a • h • i • c • The post order traversal of T T ITERATION 1 ITERATION 2 ITERATION 3 ITERATION 4 g • g •
  • 27. 4.3TREETRAVERSAL • We can represent complicated expression (propositions, sets, arithmetic) using ordered rooted trees. • EXAMPLE: A binary tree representing ((x+y)↑2)+((x-4)/3) Represent Expression by Rooted TreeRepresent Expression by Rooted Tree
  • 28. ↑ + + 2 / _ 3 x y x 4 Represent Expression by Rooted TreeRepresent Expression by Rooted Tree
  • 29. 4.3TREETRAVERSAL • We obtain the Infix form of an expression when we traverse its rooted tree in Inorder. – The infix form for expression ((x+y)↑2)+((x-4)/3) is x + y ↑2 + x – 4 / 3 or ((x+y)↑2)+((x-4)/3) • We obtain the Prefix form of an expression when we traverse its rooted tree in Preorder. – The prefix form for expression ((x+y)↑2)+((x-4)/3) is + ↑ + x y 2 / - x 4 3 • We obtain the Postfix form of an expression when we traverse its rooted tree in Postorder. – The postfix form for expression ((x+y)↑2)+((x-4)/3) is x y + 2 ↑ x 4 – 3 / + Infix, Prefix & Postfix NotationInfix, Prefix & Postfix Notation
  • 30. 4.3TREETRAVERSAL • Working right to left and performing operations using the operands on the right. • EXAMPLE: The value of the prefix expression + - * 2 3 5 / ↑ 2 3 4 is 3 Evaluating Prefix ExpressionEvaluating Prefix Expression + - * 2 3 5 / ↑ 2 3 4 2 ↑ 3 = 8 + - * 2 3 5 / 8 4 8/4 =2 + - * 2 3 5 2 2 * 3 = 6 + - * 2 3 5 2 2 * 3 = 6 + - 6 5 2 6 – 5 =1 + 1 2 1 + 2 = 3
  • 31. 4.3TREETRAVERSAL • Working left to right and performing operations using the operands on the left. • EXAMPLE: The value of the postfix expression 7 2 3 * - 4 ↑ 9 3 / + is 4 Evaluating Postfix ExpressionEvaluating Postfix Expression 7 2 3 * - 4 ↑ 9 3 / + 2 * 3 = 6 7 - 6 = 1 1 ↑ 4 = 1 9/3 = 3 1 + 3 = 4 7 6 - 4 ↑ 9 3 / + 1 4 ↑ 9 3 / + 1 ↑ 4 = 1 1 4 ↑ 9 3 / + 1 9 3 / + 1 3 +
  • 32. 4.54.5 SPANNING TREESSPANNING TREES • Find spanning trees of a simple graph CHAPTER4TREES
  • 33. 4.5SPANNINGTREES • Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. • A simple graph is connected if and only if it has a spanning tree. • Applied in IP multitasking. Spanning TreesSpanning Trees a b c e f g d a b c e f g d a b c e f g d A simple graph Not Spanning tree Spanning tree
  • 34. 4.64.6 MINIMUM SPANNING TREESMINIMUM SPANNING TREES • Find minimum spanning tree using Prim’s algorithm • Find minimum spanning tree using Kruskal’s algorithm CHAPTER4TREES
  • 35. 4.6MINIMUMSPANNINGTREES • A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of it edges. • Two algorithms can be used: – Prim’s Algorithm • Robert Prim, 1957 – Kruskal’s Algorithm • Joseph Kruskal, 1956 Minimum Spanning TreesMinimum Spanning Trees
  • 36. 4.6MINIMUMSPANNINGTREES 1. Chose an edge with the least weight. 2. Include it in spanning tree, T. 3. Select an edge of least weight that is incident with a vertex of an edge in T. 4. If it does not create a cycle (simple circuit) with the edges in T, then include it in T; otherwise discard it. 5. Repeat STEPS 3 and 4 until T contains n-1 edges. • There may be more than one minimum spanning tree for a given connected weighted simple graph. • If there are two edges with similar smallest weight, chose either one. Prim’s AlgorithmPrim’s Algorithm
  • 37. 4.6MINIMUMSPANNINGTREES EXAMPLE: Prim’s AlgorithmEXAMPLE: Prim’s Algorithm CHOICE EDGE WEIGHT SPANNING TREE 1 {a, b} 7 2 {a, d} 10 3 {b, e} 11 4 {e, c} 12 db c a e 7 13 10 16 13 12 12 FIGURE 1 10 11 b a 7 db a 7 10 15 d b a 7 10 db a 7 10 e11 e11 c 12 The minimum spanning tree is given by: d b a 7 10 e11 c 12 Total weight = 40
  • 38. 4.6MINIMUMSPANNINGTREES 1. Arrange the edges in G in increasing order. 2. Chose an edge with the minimum weight. 3. Include it in spanning tree, T. 4. Add an edge of least weight to T. 5. If it does not create a cycle (simple circuit) with the edges in T, then include it in T; otherwise discard it. 6. Repeat STEPS 4 and 5 until T contains n-1 edges. • There may be more than one minimum spanning tree for a given connected weighted simple graph. • If there are two edges with similar smallest weight, chose either one. Kruskal’s AlgorithmKruskal’s Algorithm
  • 39. 4.6MINIMUMSPANNINGTREES EXAMPLE: Kruskal’s AlgorithmEXAMPLE: Kruskal’s Algorithm CHOICE EDGE WEIGHT SPANNING TREE 1 {a, b} 7 2 {b, d} 10 3 {b, e} 11 4 {e, c} 12 db c a e 7 13 10 16 13 12 12 FIGURE 1 10 11 b a 7 b a 7 10 15 b a 7 10 b a 7 10 e11 e11 c 12 The minimum spanning tree is given by: b a 7 10 e11 c 12 Total weight = 40 d d d d
  • 40. CHAPTER4TREES SUMMARYSUMMARY • Tree is a connected undirected graph with no simple circuits • Trees have been employed to solve problems in a wide variety of disciplines.