SlideShare a Scribd company logo
1 of 12
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

Similar to Data Structures Lab on Binary Trees and Binary Search Trees

Similar to Data Structures Lab on Binary Trees and Binary Search Trees (18)

VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Trees
TreesTrees
Trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
nptel 2nd presentation.pptx
nptel 2nd presentation.pptxnptel 2nd presentation.pptx
nptel 2nd presentation.pptx
 
Binary trees
Binary treesBinary trees
Binary trees
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
AD3251-Data Structures Design-Notes-Tree.pdf
AD3251-Data Structures  Design-Notes-Tree.pdfAD3251-Data Structures  Design-Notes-Tree.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
 
Svm Presentation
Svm PresentationSvm Presentation
Svm Presentation
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
data structures module III & IV.pptx
data structures module III & IV.pptxdata structures module III & IV.pptx
data structures module III & IV.pptx
 
Tree
TreeTree
Tree
 
tree Data Structures in python Traversals.pptx
tree Data Structures in python Traversals.pptxtree Data Structures in python Traversals.pptx
tree Data Structures in python Traversals.pptx
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 
Review session2
Review session2Review session2
Review session2
 

More from Bianca Teşilă

Akka Streams - An Adobe data-intensive story
Akka Streams - An Adobe data-intensive storyAkka Streams - An Adobe data-intensive story
Akka Streams - An Adobe data-intensive storyBianca Teşilă
 
Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11Bianca Teşilă
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10Bianca Teşilă
 
Data structures and algorithms lab8
Data structures and algorithms lab8Data structures and algorithms lab8
Data structures and algorithms lab8Bianca Teşilă
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7Bianca Teşilă
 
Data structures and algorithms lab6
Data structures and algorithms lab6Data structures and algorithms lab6
Data structures and algorithms lab6Bianca Teşilă
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5Bianca Teşilă
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4Bianca Teşilă
 
Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3Bianca Teşilă
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2Bianca Teşilă
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1Bianca Teşilă
 

More from Bianca Teşilă (11)

Akka Streams - An Adobe data-intensive story
Akka Streams - An Adobe data-intensive storyAkka Streams - An Adobe data-intensive story
Akka Streams - An Adobe data-intensive story
 
Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
 
Data structures and algorithms lab8
Data structures and algorithms lab8Data structures and algorithms lab8
Data structures and algorithms lab8
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7
 
Data structures and algorithms lab6
Data structures and algorithms lab6Data structures and algorithms lab6
Data structures and algorithms lab6
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4
 
Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1
 

Recently uploaded

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Data Structures Lab on Binary Trees and Binary Search Trees

  • 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.