SlideShare a Scribd company logo
Data Structures and
Algorithms
Week 4: Trees
Ferdin Joe John Joseph, PhD
Faculty of Information Technology
Thai-Nichi Institute of Technology, Bangkok
Week 4
• Tree ADT and its applications
• Tree Traversals
• Binary Trees
• Implementation in Java
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
2
Tree ADT
• Example: Family trees
Carole's children and grandchildren.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
3
Hierarchical Organization
• Example: Family trees
Jared's parents and grandparents.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
4
Hierarchical Organization
• Example: A university's organization
A university's administrative structure.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
5
Hierarchical Organization
• Example: File Directories
Computer files organized into folders
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
6
Tree Terminology
• A tree is
• A set of nodes
• Connected by edges
• The edges indicate relationships among nodes
• Nodes arranged in levels
• Indicate the nodes' hierarchy
• Top level is a single node called the root
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
7
Tree Terminology
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
8
Tree Terminology
• Nodes at a given level are children of nodes of
previous level
• Node with children is the parent node of those
children
• Nodes with same parent are siblings
• Node with no children is a leaf node
• The only node with no parent is the root node
• All others have one parent each
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
9
Tree Terminology
• Empty trees?
• Some authors specify a general tree must have at least
the root node
• This text will allow all trees to be empty
• A node is reached from the root by a path
• The length of the path is the number of edges that
compose it
• The height of a tree is the number of levels in the
tree
• The subtree of a node is a tree rooted at a child of
that node
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
10
Binary Trees
• Each node has at most two children
Three binary trees.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
11
• A binary tree is either empty or has the following
form
• Where Tleft and Tright are binary trees
Binary Trees
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
12
Binary Trees
• Every nonleaf in a full binary tree has exactly two
children
• A complete binary tree is full to its next-to-last level
• Leaves on last level filled from left to right
• The height of a binary tree with n nodes that is
either complete or full is
log2(n + 1)
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
13
Nodes in a Binary Tree
A node in a binary tree.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
14
Interface for a Node
• Usually the class that represents a node in a tree is
a detail hidden from the client
• It is placed within a package
• Makes it available
• Available to all classes involved in implementation of a
tree
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
15
The Method privateSetTree
• Problem: previous implementation of
privateSetTree not sufficient
• Given: treeA.setTree(a, treeB, treeC);
• Now treeA shares nodes with treeB, treeC
• Changes to treeB also affect treeA
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
16
The Method privateSetTree
• Possible solution
• Have privatSetTree copy the nodes in TreeB and
TreeC
• Now treeA is separate and distinct from treeB and
treeC
• Changes to either treeB or treeC do NOT affect treeA
• Note that copying nodes is expensive
• Other solutions are considered
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
17
The Method privateSetTree
• Another possible solution for
treeA.setTree(a, treeB, treeC);
• Have privateSetTree first link the root node of
treeA to root nodes of treeB, treeC
• Then set treeB.root, treeC.root to null
This still has some
problems
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
18
The Method privateSetTree
1. Create root node r for containing the data
2. If left subtree exists, not empty
• Attach root node to r as left child
3. If right subtree exists, not empty, distinct from left
subtree
• Attach root node r as right child
• If right, left subtrees are the same, attach copy of right
subtree to r instead
4. If left (right) subtree exists, different from the
invoking tree object
• Set its data field root to null
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
19
Methods for Binary Tree
• getRootData
• isEmpty
• clear
• setRootData
• setRootNode
• getRootNode
• BinaryNodeInterface
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
20
Computing Height, Counting Nodes
• Within BinaryTree – methods in context
• getHeight()
• getNumberOfNodes()
• Within BinaryNode – methods in context
• getHeight()
• getHeight(BinaryNode node)
• getNumberOfNodes()
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
21
Traversals
• Make recursive method private
• Call from public method with no parameters
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
22
Traversals
A binary tree.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
23
Traversals
Using a stack to perform an inorder
traversal of the binary tree
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
24
Traversals
Using a stack to traverse the
binary tree in (a) preorder
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
25
Traversals
Using a stack to traverse the binary
tree in (b) postorder.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
26
Traversals
Using a queue
to traverse the
binary tree in
level order.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
27
General Trees
A node for a general tree.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
28
Using a Binary Tree to Represent a General
Tree
A general tree
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
29
Using a Binary Tree to Represent a General
Tree
an equivalent binary tree;
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
30
Using a Binary Tree to Represent a General
Tree
a more conventional view
of the same binary tree.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
31
Traversals
• Traversals of general tree in a
• Preorder: A B E F C G H I D J
• Postorder: E F B G H I C J D A
• Level order: A B C D E F G H I J
• Traversals of binary tree in c
• Preorder: A B E F C G H I D J
• Postorder: F E I H G J D C B A
• Level order: A B E C F G D H J I
• Inorder: E F B G H I C J D A
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
32
Data Scientists’ view
• To connect a series of data corresponding to a
hierarchy
• Manage data from social network
• Blockchain transaction data analysis
• Binary decision sequence management in robotics
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
33
Activity - Graded
• Grading for 3 points
• Write a java code to perform preorder and post
order traversal using stack in a binary tree.
• Bonus points: 1 If the submission is done before
the end of 8 AM 9 Feb 2019.
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
34
Next Week
Binary Search Trees
AVL Trees
Binary Heaps
Red Black Trees
Implementation in Java
Lecture series for Data Structures and
Algorithms, Data Science and Analytics,
Thai-Nichi Institute of Technology
35

More Related Content

What's hot (20)

geekgap.io webinar #1
geekgap.io webinar #1
junior Teudjio
 
Data Structure
Data Structure
sheraz1
 
Stacks in algorithems & data structure
Stacks in algorithems & data structure
faran nawaz
 
Data structures and algorithm analysis in java
Data structures and algorithm analysis in java
Muhammad Aleem Siddiqui
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Introduction to data structure and algorithms
Introduction to data structure and algorithms
Research Scholar in Manonmaniam Sundaranar University
 
7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Recommendation algorithm using reinforcement learning
Recommendation algorithm using reinforcement learning
Arithmer Inc.
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Test for AI model
Test for AI model
Arithmer Inc.
 
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
presentation
presentation
jie ren
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
Johann Petrak
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Data Structure
Data Structure
sheraz1
 
Stacks in algorithems & data structure
Stacks in algorithems & data structure
faran nawaz
 
Data structures and algorithm analysis in java
Data structures and algorithm analysis in java
Muhammad Aleem Siddiqui
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Recommendation algorithm using reinforcement learning
Recommendation algorithm using reinforcement learning
Arithmer Inc.
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
presentation
presentation
jie ren
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
Johann Petrak
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 

Similar to Data Structures and Algorithm - Week 4 - Trees, Binary Trees (20)

Data Science Training and Placement
Data Science Training and Placement
AkhilGGM
 
Built around answering questions
Built around answering questions
Larry Smarr
 
Introduction to Data Structures on C++ Language
Introduction to Data Structures on C++ Language
MuhammadRashid615513
 
TE581-Introduction1-2019aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pptx
TE581-Introduction1-2019aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pptx
NanaAgyeman13
 
Which institute is best for data science?
Which institute is best for data science?
DIGITALSAI1
 
Best Selenium certification course
Best Selenium certification course
KumarNaik21
 
Data science training in hyd ppt (1)
Data science training in hyd ppt (1)
SayyedYusufali
 
Data science training institute in hyderabad
Data science training institute in hyderabad
VamsiNihal
 
Data science training in Hyderabad
Data science training in Hyderabad
saitejavella
 
Data science training Hyderabad
Data science training Hyderabad
Nithinsunil1
 
Data science online training in hyderabad
Data science online training in hyderabad
VamsiNihal
 
Data science training in hyd ppt (1)
Data science training in hyd ppt (1)
SayyedYusufali
 
data science training and placement
data science training and placement
SaiprasadVella
 
online data science training
online data science training
DIGITALSAI1
 
Data science online training in hyderabad
Data science online training in hyderabad
VamsiNihal
 
data science online training in hyderabad
data science online training in hyderabad
VamsiNihal
 
Best data science training in Hyderabad
Best data science training in Hyderabad
KumarNaik21
 
Data science training Hyderabad
Data science training Hyderabad
Nithinsunil1
 
Best Selenium certification course
Best Selenium certification course
KumarNaik21
 
Data wrangling week2
Data wrangling week2
Ferdin Joe John Joseph PhD
 
Data Science Training and Placement
Data Science Training and Placement
AkhilGGM
 
Built around answering questions
Built around answering questions
Larry Smarr
 
Introduction to Data Structures on C++ Language
Introduction to Data Structures on C++ Language
MuhammadRashid615513
 
TE581-Introduction1-2019aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pptx
TE581-Introduction1-2019aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pptx
NanaAgyeman13
 
Which institute is best for data science?
Which institute is best for data science?
DIGITALSAI1
 
Best Selenium certification course
Best Selenium certification course
KumarNaik21
 
Data science training in hyd ppt (1)
Data science training in hyd ppt (1)
SayyedYusufali
 
Data science training institute in hyderabad
Data science training institute in hyderabad
VamsiNihal
 
Data science training in Hyderabad
Data science training in Hyderabad
saitejavella
 
Data science training Hyderabad
Data science training Hyderabad
Nithinsunil1
 
Data science online training in hyderabad
Data science online training in hyderabad
VamsiNihal
 
Data science training in hyd ppt (1)
Data science training in hyd ppt (1)
SayyedYusufali
 
data science training and placement
data science training and placement
SaiprasadVella
 
online data science training
online data science training
DIGITALSAI1
 
Data science online training in hyderabad
Data science online training in hyderabad
VamsiNihal
 
data science online training in hyderabad
data science online training in hyderabad
VamsiNihal
 
Best data science training in Hyderabad
Best data science training in Hyderabad
KumarNaik21
 
Data science training Hyderabad
Data science training Hyderabad
Nithinsunil1
 
Best Selenium certification course
Best Selenium certification course
KumarNaik21
 
Ad

More from Ferdin Joe John Joseph PhD (20)

Invited Talk DGTiCon 2022
Invited Talk DGTiCon 2022
Ferdin Joe John Joseph PhD
 
Week 12: Cloud AI- DSA 441 Cloud Computing
Week 12: Cloud AI- DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 11: Cloud Native- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 10: Cloud Security- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Ferdin Joe John Joseph PhD
 
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Ferdin Joe John Joseph PhD
 
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Ferdin Joe John Joseph PhD
 
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Ferdin Joe John Joseph PhD
 
Hadoop in Alibaba Cloud
Hadoop in Alibaba Cloud
Ferdin Joe John Joseph PhD
 
Cloud Computing Essentials in Alibaba Cloud
Cloud Computing Essentials in Alibaba Cloud
Ferdin Joe John Joseph PhD
 
Transforming deep into transformers – a computer vision approach
Transforming deep into transformers – a computer vision approach
Ferdin Joe John Joseph PhD
 
Week 11: Programming for Data Analysis
Week 11: Programming for Data Analysis
Ferdin Joe John Joseph PhD
 
Week 10: Programming for Data Analysis
Week 10: Programming for Data Analysis
Ferdin Joe John Joseph PhD
 
Week 9: Programming for Data Analysis
Week 9: Programming for Data Analysis
Ferdin Joe John Joseph PhD
 
Week 8: Programming for Data Analysis
Week 8: Programming for Data Analysis
Ferdin Joe John Joseph PhD
 
Week 11: Cloud Native- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 10: Cloud Security- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Ferdin Joe John Joseph PhD
 
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Ferdin Joe John Joseph PhD
 
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Ferdin Joe John Joseph PhD
 
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Ferdin Joe John Joseph PhD
 
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Ferdin Joe John Joseph PhD
 
Transforming deep into transformers – a computer vision approach
Transforming deep into transformers – a computer vision approach
Ferdin Joe John Joseph PhD
 
Ad

Recently uploaded (20)

Module 1Integrity_and_Ethics_PPT-2025.pptx
Module 1Integrity_and_Ethics_PPT-2025.pptx
Karikalcholan Mayavan
 
MEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdf
MEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdf
OlhaTatokhina1
 
Data-Driven-Operational--Excellence.pptx
Data-Driven-Operational--Excellence.pptx
NiwanthaThilanjanaGa
 
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
bhavaniteacher99
 
Grote OSM datasets zonder kopzorgen bij Reijers
Grote OSM datasets zonder kopzorgen bij Reijers
jacoba18
 
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays
 
THE FRIEDMAN TEST ( Biostatics B. Pharm)
THE FRIEDMAN TEST ( Biostatics B. Pharm)
JishuHaldar
 
apidays New York 2025 - Why an SDK is Needed to Protect APIs from Mobile Apps...
apidays New York 2025 - Why an SDK is Needed to Protect APIs from Mobile Apps...
apidays
 
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
Ameya Patekar
 
BODMAS-Rule-&-Unit-Digit-Concept-pdf.pdf
BODMAS-Rule-&-Unit-Digit-Concept-pdf.pdf
SiddharthSean
 
apidays New York 2025 - Using GraphQL SDL files as executable API Contracts b...
apidays New York 2025 - Using GraphQL SDL files as executable API Contracts b...
apidays
 
What is FinOps as a Service and why is it Trending?
What is FinOps as a Service and why is it Trending?
Amnic
 
apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...
apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...
apidays
 
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
Ameya Patekar
 
FME Beyond Data Processing: Creating a Dartboard Accuracy App
FME Beyond Data Processing: Creating a Dartboard Accuracy App
jacoba18
 
apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...
apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...
apidays
 
apidays New York 2025 - Unifying OpenAPI & AsyncAPI by Naresh Jain & Hari Kri...
apidays New York 2025 - Unifying OpenAPI & AsyncAPI by Naresh Jain & Hari Kri...
apidays
 
最新版美国威斯康星大学拉克罗斯分校毕业证(UW–L毕业证书)原版定制
最新版美国威斯康星大学拉克罗斯分校毕业证(UW–L毕业证书)原版定制
Taqyea
 
2.5-DESPATCH-ORDINARY MAILS.pptxlminub7b7t6f7h7t6f6g7g6fg
2.5-DESPATCH-ORDINARY MAILS.pptxlminub7b7t6f7h7t6f6g7g6fg
mk1227103
 
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays
 
Module 1Integrity_and_Ethics_PPT-2025.pptx
Module 1Integrity_and_Ethics_PPT-2025.pptx
Karikalcholan Mayavan
 
MEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdf
MEDIA_LITERACY_INDEX_OF_EDUCATORS_ENG.pdf
OlhaTatokhina1
 
Data-Driven-Operational--Excellence.pptx
Data-Driven-Operational--Excellence.pptx
NiwanthaThilanjanaGa
 
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
bhavaniteacher99
 
Grote OSM datasets zonder kopzorgen bij Reijers
Grote OSM datasets zonder kopzorgen bij Reijers
jacoba18
 
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays Singapore 2025 - What exactly are AI Agents by Aki Ranin (Earthshots ...
apidays
 
THE FRIEDMAN TEST ( Biostatics B. Pharm)
THE FRIEDMAN TEST ( Biostatics B. Pharm)
JishuHaldar
 
apidays New York 2025 - Why an SDK is Needed to Protect APIs from Mobile Apps...
apidays New York 2025 - Why an SDK is Needed to Protect APIs from Mobile Apps...
apidays
 
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
QUALITATIVE EXPLANATORY VARIABLES REGRESSION MODELS
Ameya Patekar
 
BODMAS-Rule-&-Unit-Digit-Concept-pdf.pdf
BODMAS-Rule-&-Unit-Digit-Concept-pdf.pdf
SiddharthSean
 
apidays New York 2025 - Using GraphQL SDL files as executable API Contracts b...
apidays New York 2025 - Using GraphQL SDL files as executable API Contracts b...
apidays
 
What is FinOps as a Service and why is it Trending?
What is FinOps as a Service and why is it Trending?
Amnic
 
apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...
apidays New York 2025 - Fast, Repeatable, Secure: Pick 3 with FINOS CCC by Le...
apidays
 
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
REGRESSION DIAGNOSTIC I: MULTICOLLINEARITY
Ameya Patekar
 
FME Beyond Data Processing: Creating a Dartboard Accuracy App
FME Beyond Data Processing: Creating a Dartboard Accuracy App
jacoba18
 
apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...
apidays Singapore 2025 - Building Finance Innovation Ecosystems by Umang Moon...
apidays
 
apidays New York 2025 - Unifying OpenAPI & AsyncAPI by Naresh Jain & Hari Kri...
apidays New York 2025 - Unifying OpenAPI & AsyncAPI by Naresh Jain & Hari Kri...
apidays
 
最新版美国威斯康星大学拉克罗斯分校毕业证(UW–L毕业证书)原版定制
最新版美国威斯康星大学拉克罗斯分校毕业证(UW–L毕业证书)原版定制
Taqyea
 
2.5-DESPATCH-ORDINARY MAILS.pptxlminub7b7t6f7h7t6f6g7g6fg
2.5-DESPATCH-ORDINARY MAILS.pptxlminub7b7t6f7h7t6f6g7g6fg
mk1227103
 
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays New York 2025 - The Challenge is Not the Pattern, But the Best Integr...
apidays
 

Data Structures and Algorithm - Week 4 - Trees, Binary Trees

  • 1. Data Structures and Algorithms Week 4: Trees Ferdin Joe John Joseph, PhD Faculty of Information Technology Thai-Nichi Institute of Technology, Bangkok
  • 2. Week 4 • Tree ADT and its applications • Tree Traversals • Binary Trees • Implementation in Java Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 2
  • 3. Tree ADT • Example: Family trees Carole's children and grandchildren. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 3
  • 4. Hierarchical Organization • Example: Family trees Jared's parents and grandparents. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 4
  • 5. Hierarchical Organization • Example: A university's organization A university's administrative structure. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 5
  • 6. Hierarchical Organization • Example: File Directories Computer files organized into folders Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 6
  • 7. Tree Terminology • A tree is • A set of nodes • Connected by edges • The edges indicate relationships among nodes • Nodes arranged in levels • Indicate the nodes' hierarchy • Top level is a single node called the root Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 7
  • 8. Tree Terminology Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 8
  • 9. Tree Terminology • Nodes at a given level are children of nodes of previous level • Node with children is the parent node of those children • Nodes with same parent are siblings • Node with no children is a leaf node • The only node with no parent is the root node • All others have one parent each Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 9
  • 10. Tree Terminology • Empty trees? • Some authors specify a general tree must have at least the root node • This text will allow all trees to be empty • A node is reached from the root by a path • The length of the path is the number of edges that compose it • The height of a tree is the number of levels in the tree • The subtree of a node is a tree rooted at a child of that node Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 10
  • 11. Binary Trees • Each node has at most two children Three binary trees. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 11
  • 12. • A binary tree is either empty or has the following form • Where Tleft and Tright are binary trees Binary Trees Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 12
  • 13. Binary Trees • Every nonleaf in a full binary tree has exactly two children • A complete binary tree is full to its next-to-last level • Leaves on last level filled from left to right • The height of a binary tree with n nodes that is either complete or full is log2(n + 1) Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 13
  • 14. Nodes in a Binary Tree A node in a binary tree. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 14
  • 15. Interface for a Node • Usually the class that represents a node in a tree is a detail hidden from the client • It is placed within a package • Makes it available • Available to all classes involved in implementation of a tree Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 15
  • 16. The Method privateSetTree • Problem: previous implementation of privateSetTree not sufficient • Given: treeA.setTree(a, treeB, treeC); • Now treeA shares nodes with treeB, treeC • Changes to treeB also affect treeA Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 16
  • 17. The Method privateSetTree • Possible solution • Have privatSetTree copy the nodes in TreeB and TreeC • Now treeA is separate and distinct from treeB and treeC • Changes to either treeB or treeC do NOT affect treeA • Note that copying nodes is expensive • Other solutions are considered Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 17
  • 18. The Method privateSetTree • Another possible solution for treeA.setTree(a, treeB, treeC); • Have privateSetTree first link the root node of treeA to root nodes of treeB, treeC • Then set treeB.root, treeC.root to null This still has some problems Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 18
  • 19. The Method privateSetTree 1. Create root node r for containing the data 2. If left subtree exists, not empty • Attach root node to r as left child 3. If right subtree exists, not empty, distinct from left subtree • Attach root node r as right child • If right, left subtrees are the same, attach copy of right subtree to r instead 4. If left (right) subtree exists, different from the invoking tree object • Set its data field root to null Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 19
  • 20. Methods for Binary Tree • getRootData • isEmpty • clear • setRootData • setRootNode • getRootNode • BinaryNodeInterface Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 20
  • 21. Computing Height, Counting Nodes • Within BinaryTree – methods in context • getHeight() • getNumberOfNodes() • Within BinaryNode – methods in context • getHeight() • getHeight(BinaryNode node) • getNumberOfNodes() Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 21
  • 22. Traversals • Make recursive method private • Call from public method with no parameters Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 22
  • 23. Traversals A binary tree. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 23
  • 24. Traversals Using a stack to perform an inorder traversal of the binary tree Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 24
  • 25. Traversals Using a stack to traverse the binary tree in (a) preorder Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 25
  • 26. Traversals Using a stack to traverse the binary tree in (b) postorder. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 26
  • 27. Traversals Using a queue to traverse the binary tree in level order. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 27
  • 28. General Trees A node for a general tree. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 28
  • 29. Using a Binary Tree to Represent a General Tree A general tree Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 29
  • 30. Using a Binary Tree to Represent a General Tree an equivalent binary tree; Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 30
  • 31. Using a Binary Tree to Represent a General Tree a more conventional view of the same binary tree. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 31
  • 32. Traversals • Traversals of general tree in a • Preorder: A B E F C G H I D J • Postorder: E F B G H I C J D A • Level order: A B C D E F G H I J • Traversals of binary tree in c • Preorder: A B E F C G H I D J • Postorder: F E I H G J D C B A • Level order: A B E C F G D H J I • Inorder: E F B G H I C J D A Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 32
  • 33. Data Scientists’ view • To connect a series of data corresponding to a hierarchy • Manage data from social network • Blockchain transaction data analysis • Binary decision sequence management in robotics Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 33
  • 34. Activity - Graded • Grading for 3 points • Write a java code to perform preorder and post order traversal using stack in a binary tree. • Bonus points: 1 If the submission is done before the end of 8 AM 9 Feb 2019. Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 34
  • 35. Next Week Binary Search Trees AVL Trees Binary Heaps Red Black Trees Implementation in Java Lecture series for Data Structures and Algorithms, Data Science and Analytics, Thai-Nichi Institute of Technology 35