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

PDF
Data Structures and Algorithm - Week 9 - Search Algorithms
PDF
Data Structures and Algorithm - Week 11 - Algorithm Analysis
PDF
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees
PDF
Week 1 - Data Structures and Algorithms
PDF
Data Structures and Algorithm - Week 3 - Stacks and Queues
PDF
Data Structures and Algorithm - Week 5 - AVL Trees
PDF
Data Structures and Algorithm - Week 6 - Red Black Trees
PDF
Week 2 - Data Structures and Algorithms
Data Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 8 - Minimum Spanning Trees
Week 1 - Data Structures and Algorithms
Data Structures and Algorithm - Week 3 - Stacks and Queues
Data Structures and Algorithm - Week 5 - AVL Trees
Data Structures and Algorithm - Week 6 - Red Black Trees
Week 2 - Data Structures and Algorithms

What's hot (20)

PDF
geekgap.io webinar #1
PPS
Data Structure
PPT
Stacks in algorithems & data structure
PDF
Data structures and algorithm analysis in java
PPTX
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
PPTX
Introduction to data structure and algorithms
PPTX
7. Tree - Data Structures using C++ by Varsha Patil
PDF
Recommendation algorithm using reinforcement learning
PPTX
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
PDF
Test for AI model
PDF
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
PPTX
8. Graph - Data Structures using C++ by Varsha Patil
PDF
presentation
PPTX
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
PPTX
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
PDF
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
PPTX
3. Stack - Data Structures using C++ by Varsha Patil
PPTX
5. Queue - Data Structures using C++ by Varsha Patil
PDF
Searching and Sorting Techniques in Data Structure
geekgap.io webinar #1
Data Structure
Stacks in algorithems & data structure
Data structures and algorithm analysis in java
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
Introduction to data structure and algorithms
7. Tree - Data Structures using C++ by Varsha Patil
Recommendation algorithm using reinforcement learning
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
Test for AI model
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
8. Graph - Data Structures using C++ by Varsha Patil
presentation
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
Semantics2018 Zhang,Petrak,Maynard: Adapted TextRank for Term Extraction: A G...
3. Stack - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
Searching and Sorting Techniques in Data Structure
Ad

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

PDF
Data Science Training and Placement
PPTX
Built around answering questions
PDF
Introduction to Data Structures on C++ Language
PPTX
TE581-Introduction1-2019aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pptx
PPTX
Which institute is best for data science?
PPTX
Best Selenium certification course
PPTX
Data science training in hyd ppt (1)
PPTX
Data science training institute in hyderabad
PPTX
Data science training in Hyderabad
PPTX
Data science training Hyderabad
PPTX
Data science online training in hyderabad
PPTX
Data science training in hyd ppt (1)
PPTX
data science training and placement
PPTX
online data science training
PPTX
Data science online training in hyderabad
PPTX
data science online training in hyderabad
PPTX
Best data science training in Hyderabad
PDF
Data science training Hyderabad
PPTX
Best Selenium certification course
PDF
Data wrangling week2
Data Science Training and Placement
Built around answering questions
Introduction to Data Structures on C++ Language
TE581-Introduction1-2019aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pptx
Which institute is best for data science?
Best Selenium certification course
Data science training in hyd ppt (1)
Data science training institute in hyderabad
Data science training in Hyderabad
Data science training Hyderabad
Data science online training in hyderabad
Data science training in hyd ppt (1)
data science training and placement
online data science training
Data science online training in hyderabad
data science online training in hyderabad
Best data science training in Hyderabad
Data science training Hyderabad
Best Selenium certification course
Data wrangling week2
Ad

More from Ferdin Joe John Joseph PhD (20)

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

Recently uploaded (20)

PDF
Foundation of Data Science unit number two notes
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPT
Quality review (1)_presentation of this 21
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
Business Acumen Training GuidePresentation.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
Lecture1 pattern recognition............
Foundation of Data Science unit number two notes
Data_Analytics_and_PowerBI_Presentation.pptx
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Fluorescence-microscope_Botany_detailed content
IB Computer Science - Internal Assessment.pptx
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
Miokarditis (Inflamasi pada Otot Jantung)
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Galatica Smart Energy Infrastructure Startup Pitch Deck
Business Ppt On Nestle.pptx huunnnhhgfvu
climate analysis of Dhaka ,Banglades.pptx
1_Introduction to advance data techniques.pptx
Quality review (1)_presentation of this 21
Introduction to Knowledge Engineering Part 1
Business Acumen Training GuidePresentation.pptx
Reliability_Chapter_ presentation 1221.5784
Lecture1 pattern recognition............

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