SlideShare a Scribd company logo
1 of 17
Week 6
Binary Trees
CC 104 – Data Structures & Algorithms
RIANNEL B. TECSON, MIS
Instructor
Flow of the Learning Session
 Short Review of the Previous Lesson
 Basic Concepts on Tree Data Structure
 Lesson Proper & Discussion
 Binary Search Tree Traversal
 Pre-order, In-order, Post-order
 Evaluation
 Guided Practice
 Binary Search Tree Visualization & Animation
 Independent Practice
 Assessment
 Online Quiz
2
Recall:
Linear data structures
 Here are some of the data structures we have studied so far:
 Arrays
 Lists
 Queues
 Stacks
 Binary Trees
 These all have the property that their elements can be
adequately displayed in a straight line
 Binary trees are one of the simplest nonlinear data
structures
3
Objectives
At the end of the lesson, students should be able to:
 Construct a Binary Search Tree (BST);
 Traverse a BST using Pre-order, In-order, and Post-
order modes
 Use Online Visualization applications to create a
Binary Search Tree and perform Tree Traversals
4
Parts of a binary tree
 A binary tree is composed of zero or more nodes
 Each node contains:
 A value (some sort of data item)
 A reference or pointer to a left child (may be null), and
 A reference or pointer to a right child (may be null)
 A binary tree may be empty (contain no nodes)
 If not empty, a binary tree has a root node
 Every node in the binary tree is reachable from the root node by a unique
path
 A node with neither a left child nor a right child is called a leaf
 In some binary trees, only the leaves contain a value
5
Picture of a binary tree
6
a
b c
d e
g h i
l
f
j k
Size and depth
 The size of a binary tree is the number
of nodes in it
 This tree has size 12
 The depth of a node is its distance
from the root
 a is at depth zero
 e is at depth 2
 The depth of a binary tree is the depth
of its deepest node
 This tree has depth 4
7
a
b c
d e f
g h i j k
l
Balance
 A binary tree is balanced if every level above the lowest is “full”
(contains 2n nodes)
 In most applications, a reasonably balanced binary tree is desirable
8
a
b c
d e f g
h i j
A balanced binary tree
a
b
c
d
e
f
g h
i j
An unbalanced binary tree
Binary search in an array
 Look at array location (lo + hi)/2
9
2 3 5 7 11 13 17
0 1 2 3 4 5 6
Searching for 5:
(0+6)/2 = 3
hi = 2;
(0 + 2)/2 = 1 lo = 2;
(2+2)/2=2
7
3 13
2 5 11 17
Using a binary
search tree
Tree traversals
 A binary tree is defined recursively: it consists of a
root, a left subtree, and a right subtree
 To traverse (or walk) the binary tree is to visit each
node in the binary tree exactly once
 Tree traversals are naturally recursive
 Since a binary tree has three “parts,” there are six
possible ways to traverse the binary tree:
 root, left, right (Preorder)
 left, root, right (Inorder)
 left, right, root (Postorder)
 root, right, left
 right, root, left
 right, left, root
10
Tree traversals using “flags”
 The order in which the nodes are visited during a tree
traversal can be easily determined by imagining there is a
“flag” attached to each node, as follows:
 To traverse the tree, collect the flags:
14
preorder inorder postorder
A
B C
D E F G
A
B C
D E F G
A
B C
D E F G
A B D E C F G D B E A F C G D E B F G C A
Root-Left-Right Left-Root-Right Left-Right-Root
Practice:
Binary Search Tree Traversal
 https://algorithm-visualizer.org/brute-
force/binary-tree-traversal
 https://yongdanielliang.github.io/animation/web/B
ST.html
15
Binary Search Tree Traversals Applications:
Questions?
 Clarifications?
16
Assessment:
Binary Search Tree Traversal
 https://docs.google.com/forms/d/e/1FAIpQLSf8RT
BMIkGGapJ4jGxg7z2P-
pE3QaROdxiRHSsrRxm6gcxU2g/viewform?authuse
r=1
17
Answer the problems in Google Form below:
Copying a binary tree
 In postorder, the root is visited last
 Here’s a postorder traversal to make a complete copy of a given binary
tree:
public BinaryTree copyTree(BinaryTree bt) {
if (bt == null) return null;
BinaryTree left = copyTree(bt.leftChild);
BinaryTree right = copyTree(bt.rightChild);
return new BinaryTree(bt.value, left, right);
}
18
Other traversals
 The other traversals are the reverse of these three standard
ones
 That is, the right subtree is traversed before the left subtree is
traversed
 Reverse preorder: root, right subtree, left subtree
 Reverse inorder: right subtree, root, left subtree
 Reverse postorder: right subtree, left subtree, root
19
The End
20

More Related Content

Similar to Binary Trees.ppt (20)

Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Binary tree
Binary treeBinary tree
Binary tree
 
Trees
TreesTrees
Trees
 
nptel 2nd presentation.pptx
nptel 2nd presentation.pptxnptel 2nd presentation.pptx
nptel 2nd presentation.pptx
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
Data Structures
Data StructuresData Structures
Data Structures
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Tree
TreeTree
Tree
 
Tree
TreeTree
Tree
 
lecture-i-trees.ppt
lecture-i-trees.pptlecture-i-trees.ppt
lecture-i-trees.ppt
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
binary_trees1
binary_trees1binary_trees1
binary_trees1
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
 

More from Riannel Tecson

PC Components_Hardware_Software_CSS11.ppt
PC Components_Hardware_Software_CSS11.pptPC Components_Hardware_Software_CSS11.ppt
PC Components_Hardware_Software_CSS11.pptRiannel Tecson
 
C# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.pptC# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.pptRiannel Tecson
 
Writing the Design and Methodology in Research
Writing the Design and Methodology in ResearchWriting the Design and Methodology in Research
Writing the Design and Methodology in ResearchRiannel Tecson
 
capstone101 Requirements Methodology and
capstone101 Requirements Methodology andcapstone101 Requirements Methodology and
capstone101 Requirements Methodology andRiannel Tecson
 
Writing the Review of Related Literature.pptx
Writing the Review of Related Literature.pptxWriting the Review of Related Literature.pptx
Writing the Review of Related Literature.pptxRiannel Tecson
 
Week 1 and 2 Getting started with DBMS.pptx
Week 1 and 2 Getting started with DBMS.pptxWeek 1 and 2 Getting started with DBMS.pptx
Week 1 and 2 Getting started with DBMS.pptxRiannel Tecson
 
Network Tools, Materials and Equipment.pptx
Network Tools, Materials and Equipment.pptxNetwork Tools, Materials and Equipment.pptx
Network Tools, Materials and Equipment.pptxRiannel Tecson
 
Week 5 Update Anomalies.pptx
Week 5 Update Anomalies.pptxWeek 5 Update Anomalies.pptx
Week 5 Update Anomalies.pptxRiannel Tecson
 
Does technology shaed soceity.ppt
Does technology shaed soceity.pptDoes technology shaed soceity.ppt
Does technology shaed soceity.pptRiannel Tecson
 
Chapter 03 Application Software.ppt
Chapter 03 Application Software.pptChapter 03 Application Software.ppt
Chapter 03 Application Software.pptRiannel Tecson
 
Week 1 mediaandinformationliteracycommunication.pdf
Week 1 mediaandinformationliteracycommunication.pdfWeek 1 mediaandinformationliteracycommunication.pdf
Week 1 mediaandinformationliteracycommunication.pdfRiannel Tecson
 
MIL Week 1 Lesson 1.pptx
MIL Week 1 Lesson 1.pptxMIL Week 1 Lesson 1.pptx
MIL Week 1 Lesson 1.pptxRiannel Tecson
 

More from Riannel Tecson (13)

PC Components_Hardware_Software_CSS11.ppt
PC Components_Hardware_Software_CSS11.pptPC Components_Hardware_Software_CSS11.ppt
PC Components_Hardware_Software_CSS11.ppt
 
C# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.pptC# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.ppt
 
Writing the Design and Methodology in Research
Writing the Design and Methodology in ResearchWriting the Design and Methodology in Research
Writing the Design and Methodology in Research
 
capstone101 Requirements Methodology and
capstone101 Requirements Methodology andcapstone101 Requirements Methodology and
capstone101 Requirements Methodology and
 
Writing the Review of Related Literature.pptx
Writing the Review of Related Literature.pptxWriting the Review of Related Literature.pptx
Writing the Review of Related Literature.pptx
 
Week 1 and 2 Getting started with DBMS.pptx
Week 1 and 2 Getting started with DBMS.pptxWeek 1 and 2 Getting started with DBMS.pptx
Week 1 and 2 Getting started with DBMS.pptx
 
Network Tools, Materials and Equipment.pptx
Network Tools, Materials and Equipment.pptxNetwork Tools, Materials and Equipment.pptx
Network Tools, Materials and Equipment.pptx
 
Week 5 Update Anomalies.pptx
Week 5 Update Anomalies.pptxWeek 5 Update Anomalies.pptx
Week 5 Update Anomalies.pptx
 
Does technology shaed soceity.ppt
Does technology shaed soceity.pptDoes technology shaed soceity.ppt
Does technology shaed soceity.ppt
 
Chapter 03 Application Software.ppt
Chapter 03 Application Software.pptChapter 03 Application Software.ppt
Chapter 03 Application Software.ppt
 
Media Use Log.pptx
Media Use Log.pptxMedia Use Log.pptx
Media Use Log.pptx
 
Week 1 mediaandinformationliteracycommunication.pdf
Week 1 mediaandinformationliteracycommunication.pdfWeek 1 mediaandinformationliteracycommunication.pdf
Week 1 mediaandinformationliteracycommunication.pdf
 
MIL Week 1 Lesson 1.pptx
MIL Week 1 Lesson 1.pptxMIL Week 1 Lesson 1.pptx
MIL Week 1 Lesson 1.pptx
 

Recently uploaded

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

Binary Trees.ppt

  • 1. Week 6 Binary Trees CC 104 – Data Structures & Algorithms RIANNEL B. TECSON, MIS Instructor
  • 2. Flow of the Learning Session  Short Review of the Previous Lesson  Basic Concepts on Tree Data Structure  Lesson Proper & Discussion  Binary Search Tree Traversal  Pre-order, In-order, Post-order  Evaluation  Guided Practice  Binary Search Tree Visualization & Animation  Independent Practice  Assessment  Online Quiz 2
  • 3. Recall: Linear data structures  Here are some of the data structures we have studied so far:  Arrays  Lists  Queues  Stacks  Binary Trees  These all have the property that their elements can be adequately displayed in a straight line  Binary trees are one of the simplest nonlinear data structures 3
  • 4. Objectives At the end of the lesson, students should be able to:  Construct a Binary Search Tree (BST);  Traverse a BST using Pre-order, In-order, and Post- order modes  Use Online Visualization applications to create a Binary Search Tree and perform Tree Traversals 4
  • 5. Parts of a binary tree  A binary tree is composed of zero or more nodes  Each node contains:  A value (some sort of data item)  A reference or pointer to a left child (may be null), and  A reference or pointer to a right child (may be null)  A binary tree may be empty (contain no nodes)  If not empty, a binary tree has a root node  Every node in the binary tree is reachable from the root node by a unique path  A node with neither a left child nor a right child is called a leaf  In some binary trees, only the leaves contain a value 5
  • 6. Picture of a binary tree 6 a b c d e g h i l f j k
  • 7. Size and depth  The size of a binary tree is the number of nodes in it  This tree has size 12  The depth of a node is its distance from the root  a is at depth zero  e is at depth 2  The depth of a binary tree is the depth of its deepest node  This tree has depth 4 7 a b c d e f g h i j k l
  • 8. Balance  A binary tree is balanced if every level above the lowest is “full” (contains 2n nodes)  In most applications, a reasonably balanced binary tree is desirable 8 a b c d e f g h i j A balanced binary tree a b c d e f g h i j An unbalanced binary tree
  • 9. Binary search in an array  Look at array location (lo + hi)/2 9 2 3 5 7 11 13 17 0 1 2 3 4 5 6 Searching for 5: (0+6)/2 = 3 hi = 2; (0 + 2)/2 = 1 lo = 2; (2+2)/2=2 7 3 13 2 5 11 17 Using a binary search tree
  • 10. Tree traversals  A binary tree is defined recursively: it consists of a root, a left subtree, and a right subtree  To traverse (or walk) the binary tree is to visit each node in the binary tree exactly once  Tree traversals are naturally recursive  Since a binary tree has three “parts,” there are six possible ways to traverse the binary tree:  root, left, right (Preorder)  left, root, right (Inorder)  left, right, root (Postorder)  root, right, left  right, root, left  right, left, root 10
  • 11. Tree traversals using “flags”  The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node, as follows:  To traverse the tree, collect the flags: 14 preorder inorder postorder A B C D E F G A B C D E F G A B C D E F G A B D E C F G D B E A F C G D E B F G C A Root-Left-Right Left-Root-Right Left-Right-Root
  • 12. Practice: Binary Search Tree Traversal  https://algorithm-visualizer.org/brute- force/binary-tree-traversal  https://yongdanielliang.github.io/animation/web/B ST.html 15 Binary Search Tree Traversals Applications:
  • 14. Assessment: Binary Search Tree Traversal  https://docs.google.com/forms/d/e/1FAIpQLSf8RT BMIkGGapJ4jGxg7z2P- pE3QaROdxiRHSsrRxm6gcxU2g/viewform?authuse r=1 17 Answer the problems in Google Form below:
  • 15. Copying a binary tree  In postorder, the root is visited last  Here’s a postorder traversal to make a complete copy of a given binary tree: public BinaryTree copyTree(BinaryTree bt) { if (bt == null) return null; BinaryTree left = copyTree(bt.leftChild); BinaryTree right = copyTree(bt.rightChild); return new BinaryTree(bt.value, left, right); } 18
  • 16. Other traversals  The other traversals are the reverse of these three standard ones  That is, the right subtree is traversed before the left subtree is traversed  Reverse preorder: root, right subtree, left subtree  Reverse inorder: right subtree, root, left subtree  Reverse postorder: right subtree, left subtree, root 19