SlideShare a Scribd company logo
1 of 27
 In-order
 Post-order
 Pre-order
 Tree
 Binary tree
 Recursive
 Traversal
 Traversal in trees and types
Conclusion
1. In-order
2. Post-order
3. Pre-order
A tree T is a set of nodes storing
elements such that the nodes have a
parent-child relationship
 Root : The top node in the tree.
 Leaves : Nodes having no children.
 Height of a tree : length of the
longest path from the root to a
leaf.
height of this tree= 3
RL
N Root
Binary Trees
(Full Binary Trees)
 Full Binary Tree: A full binary tree is a binary
tree where all the leaves are on the same
level and every non-leaf has two children.
 The first four full binary trees are:
7
Binary Tree
A
B
D
H
C
E F
G I
Left subtree
Root/Parent
Right subtree
Binary Tree
 Recursive
That repeat itself again and again.
A
B
D
H
C
E F
G ILeft subtree
parent
Right subtree
A
B
D
H
C
E F
G I
Left sub tree
parent
Recursive
A
B
D
H
C
E F
G I
parent
Recursive
A
B
D
H
C
E F
G I
parent
Right subtree
Recursive
A
B
D
H
C
E F
G I
parent
Right subtreeLeft subtree
Recursive
Traversal is the process of visiting every
node once in a tree.
Three techniques are used for traversing in trees
 In-order
 Post-order
 Pre-order
16
In-order Traversal:
1. Traverse left sub tree(LST)
2. Visit the root
3. Traverse right sub tree(RST)
Post-order Traversal:
1. Traverse left sub tree(LST)
2. Traverse right sub tree(RST)
3. Visit the root
Pre-order Traversal:
1. Visit the root
2. Traverse left sub tree (LST)
3. Traverse right sub tree(RST)
Node
Pre-order: (N,L,R)
In-order: (L,N,R)
Post-order: (L,R,N)
left
subtree
right
subtreeL
N
R
Traversing in Binary Tree
Ways to print a 3 node tree
= 23 = 6 A
CB
(A,B,C) , (A,C,B)
(B,A,C) , (B,C,A)
(C,B,A) , (C,A,B)
 Assume: visiting a node (traversal)
 Pre-order:
1, 3 ,5, 4, 6, 7, 8, 9, 10, 11, 12
 In-order:
4 ,5 ,6 ,3 ,1 ,8 ,7 ,9 ,11, 10 ,12
 Post-order:
4, 6, 5, 3, 8, 11, 12, 10, 9 ,7 ,1
19
1
3
11
98
4 6
5
7
12
10
Preorder: 14 4 3 9 7 5 15 18 16 17 20
14
154
9
7
183
5
16 20
17
Traversing a Binary Tree
pre-order???
In-order: 3 4 5 7 9 14 15 16 17 18 20
14
154
9
7
183
5
16 20
17
Traversing a Binary Tree
In-order???
Post-order: 3 5 7 9 4 17 16 20 18 15 14
14
154
9
7
183
5
16 20
17
Traversing a Binary Tree
post-order???
23
void preOrder(Tree *tree)
{
if (tree->isEmpty( ))
return;
visit(tree->getRoot( ));
preOrder(tree->getLeftSubtree());
preOrder(tree->getRightSubtree());
}
Code for the Traversal
Code for the Traversal
void postOrder(Tree *tree)
{
if (tree->isEmpty( ))
return;
postOrder(tree->getLeftSubtree( ));
postOrder(tree->getRightSubtree( ));
visit(tree->getRoot( ));
}
void inOrder(Tree *tree)
{
if (tree->isEmpty( ))
return;
inOrder(tree->getLeftSubtree( ));
visit(tree->getRoot( ));
inOrder(tree->getRightSubtree( ));
}
Code for the Traversal
Traversal: In tree s a technique in which we search
elements “once”.
 Three types of traversal used in trees are:
1. Post-order (root, Left sub tree, Right sub tree)
2. In-Order ( Left sub tree, Root, Right sub tree)
3. Pre-order(Left sub tree, Right sub tree, Root)
Data structure

More Related Content

Viewers also liked

Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Artificial Intelligence Presentation
Artificial Intelligence PresentationArtificial Intelligence Presentation
Artificial Intelligence Presentationlpaviglianiti
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShareSlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShareSlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShareSlideShare
 

Viewers also liked (8)

Time complexity
Time complexityTime complexity
Time complexity
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Artificial Intelligence Presentation
Artificial Intelligence PresentationArtificial Intelligence Presentation
Artificial Intelligence Presentation
 
Artificial inteligence
Artificial inteligenceArtificial inteligence
Artificial inteligence
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Similar to Data structure (20)

Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
tutorial-tree (3).ppt
tutorial-tree (3).ppttutorial-tree (3).ppt
tutorial-tree (3).ppt
 
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
 
Lect 22 Zaheer Abbas
Lect 22 Zaheer AbbasLect 22 Zaheer Abbas
Lect 22 Zaheer Abbas
 
7.tree
7.tree7.tree
7.tree
 
Red Black Tree
Red Black TreeRed Black Tree
Red Black Tree
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Tree
TreeTree
Tree
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 

Data structure

  • 1.
  • 2.
  • 3.
  • 5.  Tree  Binary tree  Recursive  Traversal  Traversal in trees and types Conclusion 1. In-order 2. Post-order 3. Pre-order
  • 6. A tree T is a set of nodes storing elements such that the nodes have a parent-child relationship  Root : The top node in the tree.  Leaves : Nodes having no children.  Height of a tree : length of the longest path from the root to a leaf. height of this tree= 3 RL N Root
  • 7. Binary Trees (Full Binary Trees)  Full Binary Tree: A full binary tree is a binary tree where all the leaves are on the same level and every non-leaf has two children.  The first four full binary trees are: 7
  • 8. Binary Tree A B D H C E F G I Left subtree Root/Parent Right subtree
  • 9. Binary Tree  Recursive That repeat itself again and again. A B D H C E F G ILeft subtree parent Right subtree
  • 10. A B D H C E F G I Left sub tree parent Recursive
  • 12. A B D H C E F G I parent Right subtree Recursive
  • 13. A B D H C E F G I parent Right subtreeLeft subtree Recursive
  • 14.
  • 15. Traversal is the process of visiting every node once in a tree. Three techniques are used for traversing in trees  In-order  Post-order  Pre-order
  • 16. 16 In-order Traversal: 1. Traverse left sub tree(LST) 2. Visit the root 3. Traverse right sub tree(RST) Post-order Traversal: 1. Traverse left sub tree(LST) 2. Traverse right sub tree(RST) 3. Visit the root Pre-order Traversal: 1. Visit the root 2. Traverse left sub tree (LST) 3. Traverse right sub tree(RST)
  • 17. Node Pre-order: (N,L,R) In-order: (L,N,R) Post-order: (L,R,N) left subtree right subtreeL N R
  • 18. Traversing in Binary Tree Ways to print a 3 node tree = 23 = 6 A CB (A,B,C) , (A,C,B) (B,A,C) , (B,C,A) (C,B,A) , (C,A,B)
  • 19.  Assume: visiting a node (traversal)  Pre-order: 1, 3 ,5, 4, 6, 7, 8, 9, 10, 11, 12  In-order: 4 ,5 ,6 ,3 ,1 ,8 ,7 ,9 ,11, 10 ,12  Post-order: 4, 6, 5, 3, 8, 11, 12, 10, 9 ,7 ,1 19 1 3 11 98 4 6 5 7 12 10
  • 20. Preorder: 14 4 3 9 7 5 15 18 16 17 20 14 154 9 7 183 5 16 20 17 Traversing a Binary Tree pre-order???
  • 21. In-order: 3 4 5 7 9 14 15 16 17 18 20 14 154 9 7 183 5 16 20 17 Traversing a Binary Tree In-order???
  • 22. Post-order: 3 5 7 9 4 17 16 20 18 15 14 14 154 9 7 183 5 16 20 17 Traversing a Binary Tree post-order???
  • 23. 23 void preOrder(Tree *tree) { if (tree->isEmpty( )) return; visit(tree->getRoot( )); preOrder(tree->getLeftSubtree()); preOrder(tree->getRightSubtree()); } Code for the Traversal
  • 24. Code for the Traversal void postOrder(Tree *tree) { if (tree->isEmpty( )) return; postOrder(tree->getLeftSubtree( )); postOrder(tree->getRightSubtree( )); visit(tree->getRoot( )); }
  • 25. void inOrder(Tree *tree) { if (tree->isEmpty( )) return; inOrder(tree->getLeftSubtree( )); visit(tree->getRoot( )); inOrder(tree->getRightSubtree( )); } Code for the Traversal
  • 26. Traversal: In tree s a technique in which we search elements “once”.  Three types of traversal used in trees are: 1. Post-order (root, Left sub tree, Right sub tree) 2. In-Order ( Left sub tree, Root, Right sub tree) 3. Pre-order(Left sub tree, Right sub tree, Root)