SlideShare a Scribd company logo
Discrete Structures
Trees
Trees and Forests
• A (free) tree is an undirected graph T
such that
– T is connected
– T has no cycles (circuits)
This definition of tree is different
from the one of a rooted tree
• 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.
• A forest is an undirected graph
without cycles
• The connected components of a
forest are trees
Tree
Forest
Spanning Trees and Forests
• A spanning tree of a connected
graph is a spanning subgraph that
is a tree
• A spanning tree is not unique
unless the graph is a tree
• Spanning trees have applications
to the design of communication
networks
• A spanning forest of a graph is a
spanning subgraph that is a forest
Graph
Spanning tree
Tree Traversal
• Ordered Rooted Tree:
An ordered rooted tree is a rooted tree where
the children of each internal vertex are
ordered.
• Tree Traversal:
Ordered rooted trees are often used to store
information. Tree traversal is the procedure of
visiting different vertices of the tree to read
information stored in that vertex. There are
three different orders of the tree traversal.
Inorder Traversal
• Let T be an ordered rooted tree with root r
and T1, T2, …Tn are the subtrees at r from left to
right. The in order traversal begins by
traversing T1 in inorder, then visiting r. It
continues by traversing T2 in inorder, then T3 in
inorder, . . . , and finally Tn in inorder.
Inorder Tree Walk
• Left, root, right
• Example:
• A, B, D, F, H, K
F
B H
KDA
Inorder Tree Walk
• What does the following code do?
TreeWalk(x)
TreeWalk(left[x]);
print(x);
TreeWalk(right[x]);
• A: prints elements in sorted (increasing) order
• This is called an inorder tree walk
– Preorder tree walk: print root, then left, then right
– Postorder tree walk: print left, then right, then root
Example – Inorder Traversal
• Example 3, p-714
Preorder Traversal
• Let T be an ordered rooted tree with root r
and T1, T2, …Tn are the subtrees at r from left to
right. The preorder traversal begins by visiting
r . It continues by traversing T1 in preorder,
then T2 in preorder, then T3 in preorder, . . . ,
and finally Tn in preorder.
Preorder Traversal
• Root, left, right
• Example:
• F, B, A, D, H, K
F
B H
KDA
Preorder Traversal
• Example 2, p-714
Postorder Traversal
• Let T be an ordered rooted tree with root r
and T1, T2, …Tn are the subtrees at r from left to
right. The preorder traversal begins by
traversing T1 in postorder, then T2 in postorder,
then T3 in postorder, . . . , and finally Tn in
postorder and ends by visiting r.
Postorder Traversal
• Left, right, root
• Example:
• A, D, B, K, H, F
F
B H
KDA
Postorder Traversal
• Example 4, p-714
02/09/15
A Shortcut for Traversing an Ordered
Rooted Tree in Preorder, Inorder, and Postorder.
• See Q. 7 to 15, p-723

More Related Content

PPT
XLSTAT - Statistical Analysis Software
PPTX
Role of bioinformatics in drug designing
PPTX
Mushroom amit
PDF
Pharmacological and Clinical Effects of Andrographis paniculata
PPTX
"Nigerian Medicinal Plants with Potentials for the Prevention and Management ...
PPTX
Bioactivity-guided Fractionation of Selected Botanticals
PPTX
A review on medicinal plants with Antidiabetic activity.
PPTX
Topological sort
XLSTAT - Statistical Analysis Software
Role of bioinformatics in drug designing
Mushroom amit
Pharmacological and Clinical Effects of Andrographis paniculata
"Nigerian Medicinal Plants with Potentials for the Prevention and Management ...
Bioactivity-guided Fractionation of Selected Botanticals
A review on medicinal plants with Antidiabetic activity.
Topological sort

Similar to Trees (20)

PPT
Trees
PPTX
Lecture 9 (DS) - Tree, Tree Traversal.pptx
PPTX
Notes on trees Introduction and graphs.pptx
PPTX
PPTX
Data structure(Part 2)
PPTX
Mca iii dfs u-4 tree and graph
PPTX
Bca ii dfs u-3 tree and graph
PPTX
Bsc cs ii dfs u-3 tree and graph
PPTX
TreesTreesTreesTreesTreesTreesTrees.pptx
PPTX
Data structures and Algorithm analysis_Lecture4.pptx
PPTX
Data structures 3
PPTX
Introduction to Tree_Data Structure.pptx
PPTX
Farhana shaikh webinar_treesindiscretestructure
PPTX
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
PPSX
Trees
PPTX
unit 4 for trees data structure notes it is
PPT
Tree 11.ppt
PPT
BINARY SEARCH TREE
Trees
Lecture 9 (DS) - Tree, Tree Traversal.pptx
Notes on trees Introduction and graphs.pptx
Data structure(Part 2)
Mca iii dfs u-4 tree and graph
Bca ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
TreesTreesTreesTreesTreesTreesTrees.pptx
Data structures and Algorithm analysis_Lecture4.pptx
Data structures 3
Introduction to Tree_Data Structure.pptx
Farhana shaikh webinar_treesindiscretestructure
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
Trees
unit 4 for trees data structure notes it is
Tree 11.ppt
BINARY SEARCH TREE
Ad

More from Ali Saleem (9)

PPTX
Nested quantifiers
PPT
Graphs
PPT
Relations
PPTX
Sequences and summations
PPTX
Nested quantifiers
PPT
Writing a memo, letter, and e mail
PPTX
Challenges for the newborn state
PPT
Objectives or goals of technical communication
PPTX
17 using rules of inference to build arguments
Nested quantifiers
Graphs
Relations
Sequences and summations
Nested quantifiers
Writing a memo, letter, and e mail
Challenges for the newborn state
Objectives or goals of technical communication
17 using rules of inference to build arguments
Ad

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Business Ethics Teaching Materials for college
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Institutional Correction lecture only . . .
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Classroom Observation Tools for Teachers
PDF
RMMM.pdf make it easy to upload and study
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O7-L3 Supply Chain Operations - ICLT Program
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Business Ethics Teaching Materials for college
Week 4 Term 3 Study Techniques revisited.pptx
Institutional Correction lecture only . . .
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial diseases, their pathogenesis and prophylaxis
Classroom Observation Tools for Teachers
RMMM.pdf make it easy to upload and study
Module 4: Burden of Disease Tutorial Slides S2 2025
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Complications of Minimal Access Surgery at WLH
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
Abdominal Access Techniques with Prof. Dr. R K Mishra
O7-L3 Supply Chain Operations - ICLT Program

Trees

  • 2. Trees and Forests • A (free) tree is an undirected graph T such that – T is connected – T has no cycles (circuits) This definition of tree is different from the one of a rooted tree • 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. • A forest is an undirected graph without cycles • The connected components of a forest are trees Tree Forest
  • 3. Spanning Trees and Forests • A spanning tree of a connected graph is a spanning subgraph that is a tree • A spanning tree is not unique unless the graph is a tree • Spanning trees have applications to the design of communication networks • A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree
  • 4. Tree Traversal • Ordered Rooted Tree: An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered. • Tree Traversal: Ordered rooted trees are often used to store information. Tree traversal is the procedure of visiting different vertices of the tree to read information stored in that vertex. There are three different orders of the tree traversal.
  • 5. Inorder Traversal • Let T be an ordered rooted tree with root r and T1, T2, …Tn are the subtrees at r from left to right. The in order traversal begins by traversing T1 in inorder, then visiting r. It continues by traversing T2 in inorder, then T3 in inorder, . . . , and finally Tn in inorder.
  • 6. Inorder Tree Walk • Left, root, right • Example: • A, B, D, F, H, K F B H KDA
  • 7. Inorder Tree Walk • What does the following code do? TreeWalk(x) TreeWalk(left[x]); print(x); TreeWalk(right[x]); • A: prints elements in sorted (increasing) order • This is called an inorder tree walk – Preorder tree walk: print root, then left, then right – Postorder tree walk: print left, then right, then root
  • 8. Example – Inorder Traversal • Example 3, p-714
  • 9. Preorder Traversal • Let T be an ordered rooted tree with root r and T1, T2, …Tn are the subtrees at r from left to right. The preorder traversal begins by visiting r . It continues by traversing T1 in preorder, then T2 in preorder, then T3 in preorder, . . . , and finally Tn in preorder.
  • 10. Preorder Traversal • Root, left, right • Example: • F, B, A, D, H, K F B H KDA
  • 12. Postorder Traversal • Let T be an ordered rooted tree with root r and T1, T2, …Tn are the subtrees at r from left to right. The preorder traversal begins by traversing T1 in postorder, then T2 in postorder, then T3 in postorder, . . . , and finally Tn in postorder and ends by visiting r.
  • 13. Postorder Traversal • Left, right, root • Example: • A, D, B, K, H, F F B H KDA
  • 14. Postorder Traversal • Example 4, p-714 02/09/15
  • 15. A Shortcut for Traversing an Ordered Rooted Tree in Preorder, Inorder, and Postorder. • See Q. 7 to 15, p-723