SlideShare a Scribd company logo
DATA STRUCTURES AND ALGORITHMS
LAB 9
Bianca Tesila
FILS, April 2014
OBJECTIVES
 Binary Trees
 Binary Search Trees
BINARY TREES & BINARY SEARCH TREES
 What is a tree? How can we check if a graph is a
tree?
 What binary tree traversals do you know? How
do they work?
 What’s the difference between a binary tree and a
binary search tree?
 What traversal should we use in order to get the
values of the nodes in ascending order?
 What happens when we delete a node from a
binary search tree?
BINARY TREES APPLICATIONS
!! Exercise:
Write a function that displays the values of
the nodes located on a given level (sent as a parameter).
Hint:
 What happens when the given level greater than the
tree’s height?
 Make a recursive function in which you decrement
level by 1 as you advance to the next level. When level
equals 0, you’ve reached the given level.
Display (T,level):
if(level==0) then print(T.data)
else Display (T.left, level-1); Display (T.right,
level-1);
BINARY TREES APPLICATIONS
!! Exercise:
Given a binary tree, compute its "maximumHeight"
-- the number of nodes along the longest path from
the root node down to the farthest leaf node.
Hint:
The maximum height of a tree is the maximum of the
heights of its children
BINARY TREES APPLICATIONS
!! Exercise:
Write a function that returns, for the root node, the difference
between the height of the left sub-tree and the height of the right
sub-tree.
Hint:
 you should take into consideration the maximum heights of the sub-trees.
 use the previous exercise
int difHeight() {
if(this == NULL) { return - 1;}
return abs(left_son->maxHeight()-right_son-
>maxHeight());
}
BINARY SEARCH TREES APPLICATIONS
!! Exercise:
Write a function which finds the lowest value in a
binary search tree.
Hint:
 traverse the node from root to left, recursively, until
left is NULL
 the node whose left is NULL is the node with minimum
value
BINARY SEARCH TREES APPLICATIONS
!! Exercise:
Write a function that displays the nodes which
have values greater than x and smaller than
y, where x and y are given as parameters.
Hint:
Take the value of the root into account in order to decide
on which way to go to find the corresponding nodes.
BINARY SEARCH TREES APPLICATIONS
BINARY SEARCH TREES APPLICATIONS
!! Exercise:
Write a function that rotates a binary search tree
that is given as a parameter. It shouldn’t change
the results of the inorder traversal.
BINARY SEARCH TREES APPLICATIONS
Hint:
 if the height of the right subtree is smaller than
the height of the left subtree, it should be right-
rotated.
 if the height of the left subtree is smaller than
the height of the right subtree, it should be left-
rotated.
 if the tree is balanced, nothing happens
HOMEWORK
Finish all the lab assignments.

More Related Content

PPTX
PDF
Sienna 8 countingsorts
PPTX
Heap sort
PPTX
R data types
PPTX
Arrays introduction- JAVA
PPT
types
PPTX
Data Analysis packages
PPTX
R Datatypes
Sienna 8 countingsorts
Heap sort
R data types
Arrays introduction- JAVA
types
Data Analysis packages
R Datatypes

Similar to Data structures and algorithms lab9 (20)

PPTX
Binary search tree definition operation.pptx
PPTX
Saikat techhnology of techtechhnology of techGhorai.pptx
PPTX
VCE Unit 05.pptx
PPT
Trees
PPT
Binary search tree(bst)
PPT
Binary Search Tree
PPTX
Binary Search Tree
PPTX
Binary trees1
PPTX
BST.pptx this isp used for learning binary search trees
PPTX
BST.pptx this is Good for data structure
PPTX
nptel 2nd presentation.pptx
PPTX
Tree all information about tree concept are available .
PPT
Binary trees
DOCX
Biary search Tree.docx
PDF
Presentation of Linear Regression with Multiple Variable-4.pdf
PDF
AD3251-Data Structures Design-Notes-Tree.pdf
PPT
Algorithm and Data Structure - Binary Trees
PPTX
Svm Presentation
PPT
Binary tree
PPTX
learn tree, linked list, queue, stack, and other algo
Binary search tree definition operation.pptx
Saikat techhnology of techtechhnology of techGhorai.pptx
VCE Unit 05.pptx
Trees
Binary search tree(bst)
Binary Search Tree
Binary Search Tree
Binary trees1
BST.pptx this isp used for learning binary search trees
BST.pptx this is Good for data structure
nptel 2nd presentation.pptx
Tree all information about tree concept are available .
Binary trees
Biary search Tree.docx
Presentation of Linear Regression with Multiple Variable-4.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
Algorithm and Data Structure - Binary Trees
Svm Presentation
Binary tree
learn tree, linked list, queue, stack, and other algo
Ad

More from Bianca Teşilă (11)

PDF
Akka Streams - An Adobe data-intensive story
PPTX
Data structures and algorithms lab11
PPTX
Data structures and algorithms lab10
PPTX
Data structures and algorithms lab8
PPTX
Data structures and algorithms lab7
PPTX
Data structures and algorithms lab6
PPTX
Data structures and algorithms lab5
PPTX
Data structures and algorithms lab4
PPTX
Data structures and algorithms lab3
PPTX
Data structures and algorithms lab2
PPTX
Data structures and algorithms lab1
Akka Streams - An Adobe data-intensive story
Data structures and algorithms lab11
Data structures and algorithms lab10
Data structures and algorithms lab8
Data structures and algorithms lab7
Data structures and algorithms lab6
Data structures and algorithms lab5
Data structures and algorithms lab4
Data structures and algorithms lab3
Data structures and algorithms lab2
Data structures and algorithms lab1
Ad

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Cloud computing and distributed systems.
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Machine Learning_overview_presentation.pptx
PPT
Teaching material agriculture food technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Building Integrated photovoltaic BIPV_UPV.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
Cloud computing and distributed systems.
sap open course for s4hana steps from ECC to s4
Machine Learning_overview_presentation.pptx
Teaching material agriculture food technology
MIND Revenue Release Quarter 2 2025 Press Release
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton

Data structures and algorithms lab9

  • 1. DATA STRUCTURES AND ALGORITHMS LAB 9 Bianca Tesila FILS, April 2014
  • 2. OBJECTIVES  Binary Trees  Binary Search Trees
  • 3. BINARY TREES & BINARY SEARCH TREES  What is a tree? How can we check if a graph is a tree?  What binary tree traversals do you know? How do they work?  What’s the difference between a binary tree and a binary search tree?  What traversal should we use in order to get the values of the nodes in ascending order?  What happens when we delete a node from a binary search tree?
  • 4. BINARY TREES APPLICATIONS !! Exercise: Write a function that displays the values of the nodes located on a given level (sent as a parameter). Hint:  What happens when the given level greater than the tree’s height?  Make a recursive function in which you decrement level by 1 as you advance to the next level. When level equals 0, you’ve reached the given level. Display (T,level): if(level==0) then print(T.data) else Display (T.left, level-1); Display (T.right, level-1);
  • 5. BINARY TREES APPLICATIONS !! Exercise: Given a binary tree, compute its "maximumHeight" -- the number of nodes along the longest path from the root node down to the farthest leaf node. Hint: The maximum height of a tree is the maximum of the heights of its children
  • 6. BINARY TREES APPLICATIONS !! Exercise: Write a function that returns, for the root node, the difference between the height of the left sub-tree and the height of the right sub-tree. Hint:  you should take into consideration the maximum heights of the sub-trees.  use the previous exercise int difHeight() { if(this == NULL) { return - 1;} return abs(left_son->maxHeight()-right_son- >maxHeight()); }
  • 7. BINARY SEARCH TREES APPLICATIONS !! Exercise: Write a function which finds the lowest value in a binary search tree. Hint:  traverse the node from root to left, recursively, until left is NULL  the node whose left is NULL is the node with minimum value
  • 8. BINARY SEARCH TREES APPLICATIONS !! Exercise: Write a function that displays the nodes which have values greater than x and smaller than y, where x and y are given as parameters. Hint: Take the value of the root into account in order to decide on which way to go to find the corresponding nodes.
  • 9. BINARY SEARCH TREES APPLICATIONS
  • 10. BINARY SEARCH TREES APPLICATIONS !! Exercise: Write a function that rotates a binary search tree that is given as a parameter. It shouldn’t change the results of the inorder traversal.
  • 11. BINARY SEARCH TREES APPLICATIONS Hint:  if the height of the right subtree is smaller than the height of the left subtree, it should be right- rotated.  if the height of the left subtree is smaller than the height of the right subtree, it should be left- rotated.  if the tree is balanced, nothing happens
  • 12. HOMEWORK Finish all the lab assignments.