SlideShare a Scribd company logo
1 of 15
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

What's hot

Sistem bilangan bulat (makul teori bilangan)
Sistem bilangan bulat (makul teori bilangan)Sistem bilangan bulat (makul teori bilangan)
Sistem bilangan bulat (makul teori bilangan)
Ig Fandy Jayanto
 
power point program linear
power point program linearpower point program linear
power point program linear
shendyseptyaneu
 
Ppt geometri analit ruang
Ppt geometri analit ruangPpt geometri analit ruang
Ppt geometri analit ruang
Devi_viani
 
Makalah ankom deret kompleks
Makalah ankom deret kompleksMakalah ankom deret kompleks
Makalah ankom deret kompleks
pedydevil
 

What's hot (20)

Kalkulus turunan dan integral
Kalkulus turunan dan integralKalkulus turunan dan integral
Kalkulus turunan dan integral
 
Sistem Persamaan Linear Tiga Variabel
Sistem Persamaan Linear Tiga VariabelSistem Persamaan Linear Tiga Variabel
Sistem Persamaan Linear Tiga Variabel
 
Jenis jenis-pertidaksamaan
Jenis jenis-pertidaksamaanJenis jenis-pertidaksamaan
Jenis jenis-pertidaksamaan
 
Sistem bilangan bulat (makul teori bilangan)
Sistem bilangan bulat (makul teori bilangan)Sistem bilangan bulat (makul teori bilangan)
Sistem bilangan bulat (makul teori bilangan)
 
power point program linear
power point program linearpower point program linear
power point program linear
 
Sifat-sifat logaritma
Sifat-sifat logaritmaSifat-sifat logaritma
Sifat-sifat logaritma
 
Bab 8 kombinatorial
Bab 8 kombinatorialBab 8 kombinatorial
Bab 8 kombinatorial
 
Persamaan Paraboloida Hiperbolik Berpusat di O (0,0,0)
Persamaan Paraboloida Hiperbolik Berpusat di O (0,0,0)Persamaan Paraboloida Hiperbolik Berpusat di O (0,0,0)
Persamaan Paraboloida Hiperbolik Berpusat di O (0,0,0)
 
Ruang Vektor ( Aljabar Linear Elementer )
Ruang Vektor ( Aljabar Linear Elementer )Ruang Vektor ( Aljabar Linear Elementer )
Ruang Vektor ( Aljabar Linear Elementer )
 
Transformasi Peubah Acak dan Distribusinya
Transformasi Peubah Acak dan Distribusinya Transformasi Peubah Acak dan Distribusinya
Transformasi Peubah Acak dan Distribusinya
 
Sub grup normal dan grup fakto
Sub grup normal dan grup faktoSub grup normal dan grup fakto
Sub grup normal dan grup fakto
 
Jawaban Soal Latihan
Jawaban Soal LatihanJawaban Soal Latihan
Jawaban Soal Latihan
 
Ppt geometri analit ruang
Ppt geometri analit ruangPpt geometri analit ruang
Ppt geometri analit ruang
 
Bab 1. Sistem Bilangan Real
Bab 1. Sistem Bilangan RealBab 1. Sistem Bilangan Real
Bab 1. Sistem Bilangan Real
 
Makalah ankom deret kompleks
Makalah ankom deret kompleksMakalah ankom deret kompleks
Makalah ankom deret kompleks
 
Ppt persamaan kuadrat
Ppt persamaan kuadratPpt persamaan kuadrat
Ppt persamaan kuadrat
 
Deret taylor and mac laurin
Deret taylor and mac laurinDeret taylor and mac laurin
Deret taylor and mac laurin
 
3.metode dua fase
3.metode dua fase3.metode dua fase
3.metode dua fase
 
Deret Geometri Tak Hingga
Deret Geometri Tak Hingga Deret Geometri Tak Hingga
Deret Geometri Tak Hingga
 
Media pembelajaran program linear
Media pembelajaran program linearMedia pembelajaran program linear
Media pembelajaran program linear
 

Similar to Trees

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
AhmedEldesoky24
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 

Similar to Trees (20)

Trees
Trees Trees
Trees
 
Tree
TreeTree
Tree
 
Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
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
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
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
 
Tree
TreeTree
Tree
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
Trees
TreesTrees
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
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Trees krw
Trees krwTrees krw
Trees krw
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 

More from Ali Saleem (9)

Nested quantifiers
Nested quantifiersNested quantifiers
Nested quantifiers
 
Graphs
GraphsGraphs
Graphs
 
Relations
RelationsRelations
Relations
 
Sequences and summations
Sequences and summationsSequences and summations
Sequences and summations
 
Nested quantifiers
Nested quantifiersNested quantifiers
Nested quantifiers
 
Writing a memo, letter, and e mail
Writing a memo, letter, and e mailWriting a memo, letter, and e mail
Writing a memo, letter, and e mail
 
Challenges for the newborn state
Challenges for the newborn  stateChallenges for the newborn  state
Challenges for the newborn state
 
Objectives or goals of technical communication
Objectives or goals of technical communicationObjectives or goals of technical communication
Objectives or goals of technical communication
 
17 using rules of inference to build arguments
17   using rules of inference to build arguments17   using rules of inference to build arguments
17 using rules of inference to build arguments
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

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