SlideShare ist ein Scribd-Unternehmen logo
1 von 27
NADARSARASWATHI COLLEGE OF ARTS
AND SCIENCE
DATA STRUCTURE ALOGIRTHM
TECHNIQUES FOR BINARY TREES
TECHNIQUES FOR GRAPHS
PRESENTED BY:
S.SABTHAMI
I.M.Sc(IT)
Binary Tree Traversal
Traversal is the process of visiting every node
once visiting a node entails doing some processing
at that node, but when describing a traversal
strategy
Binary Tree Traversal
Techniques
 Three recursive techniques for binary
tree traversal
 In each technique, the left subtree is
traversed recursively, the right subtree
is traversed recursively, and the root is
visited
 What distinguishes the techniques from
one another is the order of those 3
tasks
Preoder, Inorder, Postorder
 In Preorder, the root
is visited before (pre)
the subtrees traversals
 In Inorder, the root is
visited in-between left
and right subtree
traversal
 In Preorder, the root
is visited after (pre)
the subtrees traversals
 Preorder Traversal:
1. Visit the root
2. Traverse left subtree
3. Traverse right subtree
 Inorder Traversal:
1. Traverse left subtree
2. Visit the root
3. Traverse right subtree
 Postorder Traversal:
1. Traverse left subtree
2. Traverse right subtree
3. Visit the root
Illustrations for Traversals
 Assume: visiting a
node
is printing its label
 Preorder:
A B F G H C D E I J
K
 Inorder:
G F H B A D C E J I
K
 Postorder:
G H F B D J K I E C
A
A
B C
D
E
I
J K
F
G H
Code for the Traversal
Techniques
 The code for visitis up to you to provide,
depending
on the application
 A typical example for visit(…) is to print
out the data
part of its input node
ALGORITHM FOR PREORDER
void preOrder(Tree *tree){
if (tree->isEmpty( )) return;
visit(tree->getRoot( ));
preOrder(tree->getLeftSubtree());
preOrder(tree->getRightSubtree());
}
ALGORITHM FOR INORDER
void inOrder(Tree *tree){
if (tree->isEmpty( )) return;
inOrder(tree->getLeftSubtree( ));
visit(tree->getRoot( ));
inOrder(tree->getRightSubtree( ));
}
ALGORITHM FOR
POSTORDER
void PostOrder(struct Treenode*t)
{
if(t){
PostOrder(t->1child);
PostOrder(t->child);
Visit(t);
}
}
Graph Traversal
Depth-First Traversal
Breadth-First Traversal
Depth-First Traversal
 algorithm dft(x)
visit(x)
FOR each y such that (x,y) is an edge DO
IF <y was not visited yet >
THEN dft(y)
Depth-First Traversal
 A recursive algorithm implicitly
recording a “backtracking” path from
the root to the node currently under
consideration
DEPTH-FIRST TRAVERSAL
• Depth first search is another way
of traversing graphs, which is
closely related to preorder
traversal of a tree.
• the Breath-first search tree is typically
"short and bushy", the DFS tree is
typically "long and stringy".
DEPTH-FIRST TRAVERSAL
DEPTH-FIRST TRAVERSAL
DEPTH-FIRST TRAVERSAL
DEPTH-FIRST TRAVERSAL
BREADTH-FIRST TRAVERSAL
• Each vertex is clearly
• marked at most once,
• added to the list at most once (since that happens only
when it's marked), and
• removed from the list at most once.
◦ Since the time to process a vertex is proportional to the
length of its adjacency list, the total time for the whole
algorithm is O(m).
• A tree T constructed by the algorithm is called a breadth
first search tree.
• The traversal goes a level at a time, left to right within a
level (where a level is defined simply in terms of distance
from the root of the tree).
BREADTH-FIRST TRAVERSAL
• Every edge of G can be classified into one of three
groups.
• Some edges are in T themselves.
• Some connect two vertices at the same level of T.
• The remaining ones connect two vertices on two
adjacent levels. It is not possible for an edge to skip
a level.
• Breadth-first search tree really is a shortest path tree
starting from its root.
BREADTH-FIRST TRAVERSAL
BREADTH-FIRST TRAVERSAL
BREADTH-FIRST TRAVERSAL
BREADTH-FIRST TRAVERSAL
Relation between BFS and
DFS
 Both of these search algorithms now keep a list
of edges to explore; the only difference
between the two is
 while both algorithms adds items to the end of L,
• BFS removes them from the beginning, which
results in maintaining the list as a queue
• DFS removes them from the end, maintaining
the list as a stack.
BFS and DFS in directed
graphs
 The same search routines work essentially
unmodified for directed graphs.
◦ The only difference is that when exploring a
vertex v, we only want to look at edges (v,w)
going out of v; we ignore the other edges
coming into v.
BFS AND DFS IN DIRECTED
GRAPHS
 For BFS in directed graphs each edge of the
graph either
• connects two vertices at the same level
• goes down exactly one level
• goes up any number of levels.
 For DFS, each edge either connects an ancestor
to
• a descendant
• a descendant to an ancestor
• one node to a node in a previously visited
subtree.
Data structure and algorithm

Weitere ähnliche Inhalte

Ähnlich wie Data structure and algorithm

Skiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivitySkiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivityzukun
 
Binary tree traversals (Divide and Conquer)
Binary tree traversals (Divide and Conquer)Binary tree traversals (Divide and Conquer)
Binary tree traversals (Divide and Conquer)Gem WeBlog
 
Analysis &amp; design of algorithm
Analysis &amp; design of algorithmAnalysis &amp; design of algorithm
Analysis &amp; design of algorithmrahela bham
 
DFS & BFS in Computer Algorithm
DFS & BFS in Computer AlgorithmDFS & BFS in Computer Algorithm
DFS & BFS in Computer AlgorithmMeghaj Mallick
 
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 graphRai University
 
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
 
Zippers: Derivatives of Regular Types
Zippers: Derivatives of Regular TypesZippers: Derivatives of Regular Types
Zippers: Derivatives of Regular TypesJay Coskey
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.pptHODElex
 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2infanciaj
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
 
Topological Sort and BFS
Topological Sort and BFSTopological Sort and BFS
Topological Sort and BFSArchanaMani2
 
Tree(Data Structure)
Tree(Data Structure)Tree(Data Structure)
Tree(Data Structure)FariaFerdowsy
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02Krish_ver2
 

Ähnlich wie Data structure and algorithm (20)

BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Skiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivitySkiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivity
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 
Binary tree traversals (Divide and Conquer)
Binary tree traversals (Divide and Conquer)Binary tree traversals (Divide and Conquer)
Binary tree traversals (Divide and Conquer)
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Analysis &amp; design of algorithm
Analysis &amp; design of algorithmAnalysis &amp; design of algorithm
Analysis &amp; design of algorithm
 
Graphs
GraphsGraphs
Graphs
 
DFS & BFS in Computer Algorithm
DFS & BFS in Computer AlgorithmDFS & BFS in Computer Algorithm
DFS & BFS in Computer Algorithm
 
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
 
Trees
TreesTrees
Trees
 
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
 
Zippers: Derivatives of Regular Types
Zippers: Derivatives of Regular TypesZippers: Derivatives of Regular Types
Zippers: Derivatives of Regular Types
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2
 
binary tree
binary treebinary tree
binary tree
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
Topological Sort and BFS
Topological Sort and BFSTopological Sort and BFS
Topological Sort and BFS
 
Tree(Data Structure)
Tree(Data Structure)Tree(Data Structure)
Tree(Data Structure)
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 

Mehr von SabthamiS1

women%20empowerment11.pptx
women%20empowerment11.pptxwomen%20empowerment11.pptx
women%20empowerment11.pptxSabthamiS1
 
big data analytics.pptx
big data analytics.pptxbig data analytics.pptx
big data analytics.pptxSabthamiS1
 
Data minig.pptx
Data minig.pptxData minig.pptx
Data minig.pptxSabthamiS1
 
artificial intelligence.pptx
artificial intelligence.pptxartificial intelligence.pptx
artificial intelligence.pptxSabthamiS1
 
distributed computing.pptx
distributed computing.pptxdistributed computing.pptx
distributed computing.pptxSabthamiS1
 
Network and internet security
Network and internet security Network and internet security
Network and internet security SabthamiS1
 
Advance computer architecture
Advance computer architecture Advance computer architecture
Advance computer architecture SabthamiS1
 

Mehr von SabthamiS1 (12)

women%20empowerment11.pptx
women%20empowerment11.pptxwomen%20empowerment11.pptx
women%20empowerment11.pptx
 
big data analytics.pptx
big data analytics.pptxbig data analytics.pptx
big data analytics.pptx
 
iot.pptx
iot.pptxiot.pptx
iot.pptx
 
dip.pptx
dip.pptxdip.pptx
dip.pptx
 
csc.pptx
csc.pptxcsc.pptx
csc.pptx
 
python.pptx
python.pptxpython.pptx
python.pptx
 
Data minig.pptx
Data minig.pptxData minig.pptx
Data minig.pptx
 
artificial intelligence.pptx
artificial intelligence.pptxartificial intelligence.pptx
artificial intelligence.pptx
 
distributed computing.pptx
distributed computing.pptxdistributed computing.pptx
distributed computing.pptx
 
Network and internet security
Network and internet security Network and internet security
Network and internet security
 
Java
Java Java
Java
 
Advance computer architecture
Advance computer architecture Advance computer architecture
Advance computer architecture
 

Kürzlich hochgeladen

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 17Celine George
 
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...Pooja Bhuva
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
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).pptxmarlenawright1
 
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 POSCeline George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 

Kürzlich hochgeladen (20)

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
 
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...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
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
 
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
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

Data structure and algorithm

  • 1. NADARSARASWATHI COLLEGE OF ARTS AND SCIENCE DATA STRUCTURE ALOGIRTHM TECHNIQUES FOR BINARY TREES TECHNIQUES FOR GRAPHS PRESENTED BY: S.SABTHAMI I.M.Sc(IT)
  • 2. Binary Tree Traversal Traversal is the process of visiting every node once visiting a node entails doing some processing at that node, but when describing a traversal strategy
  • 3. Binary Tree Traversal Techniques  Three recursive techniques for binary tree traversal  In each technique, the left subtree is traversed recursively, the right subtree is traversed recursively, and the root is visited  What distinguishes the techniques from one another is the order of those 3 tasks
  • 4. Preoder, Inorder, Postorder  In Preorder, the root is visited before (pre) the subtrees traversals  In Inorder, the root is visited in-between left and right subtree traversal  In Preorder, the root is visited after (pre) the subtrees traversals  Preorder Traversal: 1. Visit the root 2. Traverse left subtree 3. Traverse right subtree  Inorder Traversal: 1. Traverse left subtree 2. Visit the root 3. Traverse right subtree  Postorder Traversal: 1. Traverse left subtree 2. Traverse right subtree 3. Visit the root
  • 5. Illustrations for Traversals  Assume: visiting a node is printing its label  Preorder: A B F G H C D E I J K  Inorder: G F H B A D C E J I K  Postorder: G H F B D J K I E C A A B C D E I J K F G H
  • 6. Code for the Traversal Techniques  The code for visitis up to you to provide, depending on the application  A typical example for visit(…) is to print out the data part of its input node
  • 7. ALGORITHM FOR PREORDER void preOrder(Tree *tree){ if (tree->isEmpty( )) return; visit(tree->getRoot( )); preOrder(tree->getLeftSubtree()); preOrder(tree->getRightSubtree()); }
  • 8. ALGORITHM FOR INORDER void inOrder(Tree *tree){ if (tree->isEmpty( )) return; inOrder(tree->getLeftSubtree( )); visit(tree->getRoot( )); inOrder(tree->getRightSubtree( )); }
  • 9. ALGORITHM FOR POSTORDER void PostOrder(struct Treenode*t) { if(t){ PostOrder(t->1child); PostOrder(t->child); Visit(t); } }
  • 11. Depth-First Traversal  algorithm dft(x) visit(x) FOR each y such that (x,y) is an edge DO IF <y was not visited yet > THEN dft(y)
  • 12. Depth-First Traversal  A recursive algorithm implicitly recording a “backtracking” path from the root to the node currently under consideration
  • 13. DEPTH-FIRST TRAVERSAL • Depth first search is another way of traversing graphs, which is closely related to preorder traversal of a tree. • the Breath-first search tree is typically "short and bushy", the DFS tree is typically "long and stringy".
  • 18. BREADTH-FIRST TRAVERSAL • Each vertex is clearly • marked at most once, • added to the list at most once (since that happens only when it's marked), and • removed from the list at most once. ◦ Since the time to process a vertex is proportional to the length of its adjacency list, the total time for the whole algorithm is O(m). • A tree T constructed by the algorithm is called a breadth first search tree. • The traversal goes a level at a time, left to right within a level (where a level is defined simply in terms of distance from the root of the tree).
  • 19. BREADTH-FIRST TRAVERSAL • Every edge of G can be classified into one of three groups. • Some edges are in T themselves. • Some connect two vertices at the same level of T. • The remaining ones connect two vertices on two adjacent levels. It is not possible for an edge to skip a level. • Breadth-first search tree really is a shortest path tree starting from its root.
  • 24. Relation between BFS and DFS  Both of these search algorithms now keep a list of edges to explore; the only difference between the two is  while both algorithms adds items to the end of L, • BFS removes them from the beginning, which results in maintaining the list as a queue • DFS removes them from the end, maintaining the list as a stack.
  • 25. BFS and DFS in directed graphs  The same search routines work essentially unmodified for directed graphs. ◦ The only difference is that when exploring a vertex v, we only want to look at edges (v,w) going out of v; we ignore the other edges coming into v.
  • 26. BFS AND DFS IN DIRECTED GRAPHS  For BFS in directed graphs each edge of the graph either • connects two vertices at the same level • goes down exactly one level • goes up any number of levels.  For DFS, each edge either connects an ancestor to • a descendant • a descendant to an ancestor • one node to a node in a previously visited subtree.