SlideShare a Scribd company logo
Week 6
Binary Trees
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
Introduction:
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
Recall:
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
Lesson Proper:
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 found in the 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

What's hot

Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
File organization 1
File organization 1File organization 1
File organization 1
Rupali Rana
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
B trees
B treesB trees
Indexing structure for files
Indexing structure for filesIndexing structure for files
Indexing structure for files
Zainab Almugbel
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
Anandhasilambarasan D
 
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
chauhankapil
 
b+ tree
b+ treeb+ tree
b+ tree
bitistu
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Abhishek L.R
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
Dhananjaysinh Jhala
 
B+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletionB+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletion
HAMID-50
 
Reduction & Handle Pruning
Reduction & Handle PruningReduction & Handle Pruning
Reduction & Handle Pruning
MdAshikJiddney
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
Intro C# Book
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
Trupti Agrawal
 
Recovery system
Recovery systemRecovery system
Recovery system
lalithambiga kamaraj
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
Elavarasi K
 
Extendible hashing
Extendible hashingExtendible hashing
Extendible hashing
Indika Munaweera Kankanamge
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
Priyanka Rana
 

What's hot (20)

Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
File organization 1
File organization 1File organization 1
File organization 1
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
B trees
B treesB trees
B trees
 
Indexing structure for files
Indexing structure for filesIndexing structure for files
Indexing structure for files
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
b+ tree
b+ treeb+ tree
b+ tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
B+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletionB+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletion
 
Reduction & Handle Pruning
Reduction & Handle PruningReduction & Handle Pruning
Reduction & Handle Pruning
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Recovery system
Recovery systemRecovery system
Recovery system
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Extendible hashing
Extendible hashingExtendible hashing
Extendible hashing
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 

Similar to Binary Search Tree Traversal.ppt

Binary Trees.ppt
Binary Trees.pptBinary Trees.ppt
Binary Trees.ppt
Riannel Tecson
 
Binary trees
Binary treesBinary trees
Binary trees
Simratpreet Singh
 
Binary trees
Binary treesBinary trees
Binary trees
Rajendran
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
MuhammadShafi89
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
Dabbal Singh Mahara
 
Binary tree
Binary treeBinary tree
Binary tree
Maria Saleem
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
skilljiolms
 
TREES.pptx
TREES.pptxTREES.pptx
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
ssuser50179b
 
Binary trees
Binary treesBinary trees
Binary trees
Ssankett Negi
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
Tribhuvan University
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
Mahmoud Alfarra
 
Trees
TreesTrees
Trees
9590133127
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
 
binary_trees1
binary_trees1binary_trees1
binary_trees1
Mohamed Elsayed
 
Tree
TreeTree

Similar to Binary Search Tree Traversal.ppt (20)

Binary Trees.ppt
Binary Trees.pptBinary Trees.ppt
Binary Trees.ppt
 
Binary trees
Binary treesBinary trees
Binary trees
 
Binary trees
Binary treesBinary trees
Binary trees
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Binary trees
Binary treesBinary trees
Binary trees
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
 
Trees
TreesTrees
Trees
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
 
binary_trees1
binary_trees1binary_trees1
binary_trees1
 
Tree
TreeTree
Tree
 

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.ppt
Riannel 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.ppt
Riannel 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 Research
Riannel Tecson
 
capstone101 Requirements Methodology and
capstone101 Requirements Methodology andcapstone101 Requirements Methodology and
capstone101 Requirements Methodology and
Riannel 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.pptx
Riannel 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.pptx
Riannel Tecson
 
Network Tools, Materials and Equipment.pptx
Network Tools, Materials and Equipment.pptxNetwork Tools, Materials and Equipment.pptx
Network Tools, Materials and Equipment.pptx
Riannel Tecson
 
Week 5 Update Anomalies.pptx
Week 5 Update Anomalies.pptxWeek 5 Update Anomalies.pptx
Week 5 Update Anomalies.pptx
Riannel Tecson
 
Does technology shaed soceity.ppt
Does technology shaed soceity.pptDoes technology shaed soceity.ppt
Does technology shaed soceity.ppt
Riannel Tecson
 
Chapter 03 Application Software.ppt
Chapter 03 Application Software.pptChapter 03 Application Software.ppt
Chapter 03 Application Software.ppt
Riannel Tecson
 
Media Use Log.pptx
Media Use Log.pptxMedia Use Log.pptx
Media Use Log.pptx
Riannel Tecson
 
Week 1 mediaandinformationliteracycommunication.pdf
Week 1 mediaandinformationliteracycommunication.pdfWeek 1 mediaandinformationliteracycommunication.pdf
Week 1 mediaandinformationliteracycommunication.pdf
Riannel Tecson
 
MIL Week 1 Lesson 1.pptx
MIL Week 1 Lesson 1.pptxMIL Week 1 Lesson 1.pptx
MIL Week 1 Lesson 1.pptx
Riannel 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

Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 

Recently uploaded (20)

Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 

Binary Search Tree Traversal.ppt

  • 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. Introduction: 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. Recall: 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. Lesson Proper: 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 found in the 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