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

More Related Content

Similar to 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 connectivity
zukun
 

Similar to 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
 

More from SabthamiS1

More from 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
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Recently uploaded (20)

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
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
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

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.