SlideShare ist ein Scribd-Unternehmen logo
1 von 23
BINARY TREE
BINARY TREE 
• A binary tree is a special form of tree in which a 
node can have atmost two children 
• A node in a binary tree may not necessarily have 
the maximum of two children i.e either 1,2 or 0 
child. 
• A tree which does not contain any node is called 
empty binary node.
Binary Tree 
Full binary tree Complete binary tree
Full Binary Tree 
A binary tree is said to be full binary tree 
if each node has exactly zero or two 
children. 
Also known as proper binary tree
Complete Binary Tree 
A complete binary tree is either a full 
binary tree or one in which every level is 
fully occupied except possibly for the 
bottomost level where all the nodes 
must be as far left as possible.
PROPERTIES OF BINARY TREE 
• 1.The maximum number of nodes in a binary tree 
on a given level (say L) is 2L a,where L>=0 . 
• 2.The maximum number of nodes in an binary tree 
with height h is 2h+1-1 
• 3.the minimum number of nodes possible in a 
binary tree of height h is h+1
• 4.if n is the number of nodes and e is the number of 
edges in an non-empty binary tree then n=e+1. 
• 5.if n0 is the number of leaf nodes (no child) and n2 is 
the number of nodes with two children in a non-empty 
binary tree then n0= n2+1 
• 6.for a complete binary tree T with n nodes ,the 
height is floor[log2(n+1) -1]
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 
8
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 
9 
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 
1 
• Assume: visiting a node 
is printing its label 
3 
7 
• Preorder: 
5 
8 9 
1 3 5 4 6 7 8 9 10 11 12 
10 
4 6 
• Inorder: 
11 
12 
4 5 6 3 1 8 7 9 11 10 12 
• Postorder: 
4 6 5 3 8 11 12 10 8 9 7 1 10
Illustrations for Traversals (Contd.) 
• Assume: visiting a node 
15 
8 
20 
is printing its data 
• Preorder: 15 8 2 6 3 7 
2 
11 
27 
11 10 12 14 20 27 22 30 
6 
10 
12 
22 30 
• Inorder: 3 6 7 2 8 10 11 
3 7 
14 
12 14 15 20 22 27 30 
• Postorder: 3 7 6 2 10 14 
12 11 8 22 30 27 20 15 11
Tree Traversals 
Pre-Order(NLR) 
1, 3, 5, 9, 6, 8
Tree Traversals 
In-Order(LNR) 
5, 3, 9, 1, 8, 6
Tree Traversals 
Post-Order(LRN) 
5, 9, 3, 8, 6, 1
PRE-ORDER USING STACK 
• PREORD(INFO,LEFT,RIGHT,ROOT): A binary tree 
T is in memory .The algorithm does a 
preorder traversal of T, applying an 
operation PROCESS to each of its node. An 
array STACK is used to temporarily hold the 
addresses of nodes.
• 1.[Initiaaly push NULL onto STACK and initialize 
PTR] 
Set TOP1.STACK[1]NULL and PTRROOT 
2.Repeat steps 3 to 5 while PTR!=NULL 
3.Apply PROCESS to INFO[PTR] 
4.[Right Child?] 
If RIGHT[PTR]!=NULL [Push on STACK] 
TOPTOP+1 
STACK[TOP]RIGHT[PTR] 
[End of if structure]
• 5.[Left Child ?] 
If LEFT[PTR]!=NULL, 
Set PTRLEFT[PTR] 
Else [pop from stack] 
PTRSTACK[TOP] 
TOPTOP-1 
[End of if strucutre] 
[End of step 2 loop] 
6.Exit
IN-ORDER 
• INORDER(INFO,LEFT,RIGHT,ROOT):A binary tree is 
in memory. This algorithm does an inorder traversal 
.applying PROCESS to each of its node.An array 
STACK is used to temporarily hold the addresses of 
node.
1.[Push NULL onto STACK and initialize PTR] 
TOP1,STACK[1]NULL and PTRROOT 
2.Repeat while PTR!=NULL [Push left most path onto 
stack] 
a)TOPTOP+1 and STACK[TOP]PTR [Saves 
nodes] 
b)PTRLEFT[PTR] [updates PTR] 
[End of loop] 
3.PTRSTACK[TOP] 
TOPTOP-1 [pops node from STACK]
4.Repeat steps 5 to 7 while PTR!=NULL [Backtracking] 
5.Apply PROCESS to INFO[PTR] 
6.[Right child?] 
If RIGHT[PTR]!=NULL 
a)PTRRIGHT[PTR] 
b)GOTO step 2 
7.PTRSTACK[TOP] 
TOP=TOP-1 [Pops node] 
[end of step 4 loop] 
8.Exit
POSTORDER 
• POSTORD(INFO,LEFT,RIGHT,ROOT):A binary 
tree T is in memory.This algorithm does a 
postorder traversal of T,applying an 
operation PROCESS to each of its node.An 
array STACK is used to temporarily hold the 
address of nodes
1.[push NULL onto STACK and initialize PTR] 
Set TOP1,STACK[1]NULL,PTRROOT 
2.[Push left-most path onto STACK] 
Repeat steps 3 to 5 while(PTR!=NULL) 
3.TOPTOP+1 
STACK[TOP]PTR [ Pushes PTR on STACK] 
4.if RIGHT[PTR]!=NULL, then [Push on STACK] 
TOPTOP+1 and STACK[TOP]=-RIGHT[PTR]. 
[end of If strucutre]
Set PTRLEFT[PTR] [Updates pointer PTR] 
[End of Step 2 loop] 
6.Set PTRSTACK[TOP] and TOPTOP-1 [Pops node from STACK] 
7.Repeat while PTR>0. 
A)Apply PROCESS to INFO[PTR] 
B)Set PTRSTACK[TOP] and TOPTOP-1 [pops node from 
STACK] 
8.If PTR<0 ,then: 
a)PTR-PTR 
b)Goto step 2. 
[End of If structure] 
9.EXIT

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Quick sort
Quick sortQuick sort
Quick sort
 
Transaction states and properties
Transaction states and propertiesTransaction states and properties
Transaction states and properties
 
stack presentation
stack presentationstack presentation
stack presentation
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Selection sort and insertion sort
Selection sort and insertion sortSelection sort and insertion sort
Selection sort and insertion sort
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Extensible hashing
Extensible hashingExtensible hashing
Extensible hashing
 
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
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 

Andere mochten auch

computer notes - Traversal of a binary tree
computer notes - Traversal of a binary treecomputer notes - Traversal of a binary tree
computer notes - Traversal of a binary tree
ecomputernotes
 
Graph representation
Graph representationGraph representation
Graph representation
Tech_MX
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
leminhvuong
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentation
Hafsa.Naseem
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
geeksrik
 

Andere mochten auch (17)

Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
computer notes - Traversal of a binary tree
computer notes - Traversal of a binary treecomputer notes - Traversal of a binary tree
computer notes - Traversal of a binary tree
 
Binary tree
Binary tree Binary tree
Binary tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
Altar de Muertos
Altar de Muertos Altar de Muertos
Altar de Muertos
 
Cse Binary tree presentation
Cse Binary tree presentationCse Binary tree presentation
Cse Binary tree presentation
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search tree
 
6. binary tree
6. binary tree6. binary tree
6. binary tree
 
Graph representation
Graph representationGraph representation
Graph representation
 
Traversals | Data Structures
Traversals | Data StructuresTraversals | Data Structures
Traversals | Data Structures
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentation
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Binary tree
Binary treeBinary tree
Binary tree
 

Ähnlich wie binary tree

lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
KamranAli649587
 
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
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
Shahzad Ali
 
ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
khitishlpu
 

Ähnlich wie binary tree (20)

Trees
TreesTrees
Trees
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
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
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
tree.ppt
tree.ppttree.ppt
tree.ppt
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
tutorial-tree (3).ppt
tutorial-tree (3).ppttutorial-tree (3).ppt
tutorial-tree (3).ppt
 
Binary tree
Binary treeBinary tree
Binary tree
 
PVEB Tree.pptx
PVEB Tree.pptxPVEB Tree.pptx
PVEB Tree.pptx
 
7.tree
7.tree7.tree
7.tree
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
 
ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
Tree chapter
Tree chapterTree chapter
Tree chapter
 

Mehr von Shankar Bishnoi (6)

Heap tree
Heap treeHeap tree
Heap tree
 
Avl trees 2
Avl trees 2Avl trees 2
Avl trees 2
 
Avl tree
Avl treeAvl tree
Avl tree
 
trees
trees trees
trees
 
binary search tree
binary search treebinary search tree
binary search tree
 
tree in Data Structures
tree in Data Structurestree in Data Structures
tree in Data Structures
 

Kürzlich hochgeladen

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Kürzlich hochgeladen (20)

Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 

binary tree

  • 2. BINARY TREE • A binary tree is a special form of tree in which a node can have atmost two children • A node in a binary tree may not necessarily have the maximum of two children i.e either 1,2 or 0 child. • A tree which does not contain any node is called empty binary node.
  • 3. Binary Tree Full binary tree Complete binary tree
  • 4. Full Binary Tree A binary tree is said to be full binary tree if each node has exactly zero or two children. Also known as proper binary tree
  • 5. Complete Binary Tree A complete binary tree is either a full binary tree or one in which every level is fully occupied except possibly for the bottomost level where all the nodes must be as far left as possible.
  • 6. PROPERTIES OF BINARY TREE • 1.The maximum number of nodes in a binary tree on a given level (say L) is 2L a,where L>=0 . • 2.The maximum number of nodes in an binary tree with height h is 2h+1-1 • 3.the minimum number of nodes possible in a binary tree of height h is h+1
  • 7. • 4.if n is the number of nodes and e is the number of edges in an non-empty binary tree then n=e+1. • 5.if n0 is the number of leaf nodes (no child) and n2 is the number of nodes with two children in a non-empty binary tree then n0= n2+1 • 6.for a complete binary tree T with n nodes ,the height is floor[log2(n+1) -1]
  • 8. 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 8
  • 9. 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 9 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
  • 10. Illustrations for Traversals 1 • Assume: visiting a node is printing its label 3 7 • Preorder: 5 8 9 1 3 5 4 6 7 8 9 10 11 12 10 4 6 • Inorder: 11 12 4 5 6 3 1 8 7 9 11 10 12 • Postorder: 4 6 5 3 8 11 12 10 8 9 7 1 10
  • 11. Illustrations for Traversals (Contd.) • Assume: visiting a node 15 8 20 is printing its data • Preorder: 15 8 2 6 3 7 2 11 27 11 10 12 14 20 27 22 30 6 10 12 22 30 • Inorder: 3 6 7 2 8 10 11 3 7 14 12 14 15 20 22 27 30 • Postorder: 3 7 6 2 10 14 12 11 8 22 30 27 20 15 11
  • 12. Tree Traversals Pre-Order(NLR) 1, 3, 5, 9, 6, 8
  • 13. Tree Traversals In-Order(LNR) 5, 3, 9, 1, 8, 6
  • 15. PRE-ORDER USING STACK • PREORD(INFO,LEFT,RIGHT,ROOT): A binary tree T is in memory .The algorithm does a preorder traversal of T, applying an operation PROCESS to each of its node. An array STACK is used to temporarily hold the addresses of nodes.
  • 16. • 1.[Initiaaly push NULL onto STACK and initialize PTR] Set TOP1.STACK[1]NULL and PTRROOT 2.Repeat steps 3 to 5 while PTR!=NULL 3.Apply PROCESS to INFO[PTR] 4.[Right Child?] If RIGHT[PTR]!=NULL [Push on STACK] TOPTOP+1 STACK[TOP]RIGHT[PTR] [End of if structure]
  • 17. • 5.[Left Child ?] If LEFT[PTR]!=NULL, Set PTRLEFT[PTR] Else [pop from stack] PTRSTACK[TOP] TOPTOP-1 [End of if strucutre] [End of step 2 loop] 6.Exit
  • 18. IN-ORDER • INORDER(INFO,LEFT,RIGHT,ROOT):A binary tree is in memory. This algorithm does an inorder traversal .applying PROCESS to each of its node.An array STACK is used to temporarily hold the addresses of node.
  • 19. 1.[Push NULL onto STACK and initialize PTR] TOP1,STACK[1]NULL and PTRROOT 2.Repeat while PTR!=NULL [Push left most path onto stack] a)TOPTOP+1 and STACK[TOP]PTR [Saves nodes] b)PTRLEFT[PTR] [updates PTR] [End of loop] 3.PTRSTACK[TOP] TOPTOP-1 [pops node from STACK]
  • 20. 4.Repeat steps 5 to 7 while PTR!=NULL [Backtracking] 5.Apply PROCESS to INFO[PTR] 6.[Right child?] If RIGHT[PTR]!=NULL a)PTRRIGHT[PTR] b)GOTO step 2 7.PTRSTACK[TOP] TOP=TOP-1 [Pops node] [end of step 4 loop] 8.Exit
  • 21. POSTORDER • POSTORD(INFO,LEFT,RIGHT,ROOT):A binary tree T is in memory.This algorithm does a postorder traversal of T,applying an operation PROCESS to each of its node.An array STACK is used to temporarily hold the address of nodes
  • 22. 1.[push NULL onto STACK and initialize PTR] Set TOP1,STACK[1]NULL,PTRROOT 2.[Push left-most path onto STACK] Repeat steps 3 to 5 while(PTR!=NULL) 3.TOPTOP+1 STACK[TOP]PTR [ Pushes PTR on STACK] 4.if RIGHT[PTR]!=NULL, then [Push on STACK] TOPTOP+1 and STACK[TOP]=-RIGHT[PTR]. [end of If strucutre]
  • 23. Set PTRLEFT[PTR] [Updates pointer PTR] [End of Step 2 loop] 6.Set PTRSTACK[TOP] and TOPTOP-1 [Pops node from STACK] 7.Repeat while PTR>0. A)Apply PROCESS to INFO[PTR] B)Set PTRSTACK[TOP] and TOPTOP-1 [pops node from STACK] 8.If PTR<0 ,then: a)PTR-PTR b)Goto step 2. [End of If structure] 9.EXIT