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

Similar to Data structures and algorithms lab9 (20)

PPT
bst.ppt
plagcheck
 
PPTX
data structures module III & IV.pptx
rani marri
 
PPT
mitochondria moment and super computer integration.ppt
AMMAD45
 
PPT
Trees
9590133127
 
PPTX
Week 8 (trees)
amna izzat
 
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
PDF
CS-102 BST_27_3_14v2.pdf
ssuser034ce1
 
PPTX
Lecture 09 - Binary Search Trees.pptx mission Sk it
AmazingWorld37
 
PPT
Algorithm and Data Structure - Binary Trees
donotreply20
 
PPT
Binary trees
Amit Vats
 
PPT
1.5 binary search tree
Krish_ver2
 
PPT
4.2 bst 03
Krish_ver2
 
PPT
BinarySearchTrees in data structures.ppt
umaranirs
 
PPTX
Data structures and Algorithm analysis_Lecture4.pptx
AhmedEldesoky24
 
PPT
Trees gt(1)
Gopi Saiteja
 
PPTX
Team-hawks_DSA_binary_tree[1] [Read-Only].pptx
XEON14
 
PPTX
Search tree & graph
Michael Jo
 
PPT
1.1 binary tree
Krish_ver2
 
PPTX
learn tree, linked list, queue, stack, and other algo
unknowwqer
 
PPTX
Unit-VStackStackStackStackStackStack.pptx
nakshpub
 
bst.ppt
plagcheck
 
data structures module III & IV.pptx
rani marri
 
mitochondria moment and super computer integration.ppt
AMMAD45
 
Trees
9590133127
 
Week 8 (trees)
amna izzat
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
CS-102 BST_27_3_14v2.pdf
ssuser034ce1
 
Lecture 09 - Binary Search Trees.pptx mission Sk it
AmazingWorld37
 
Algorithm and Data Structure - Binary Trees
donotreply20
 
Binary trees
Amit Vats
 
1.5 binary search tree
Krish_ver2
 
4.2 bst 03
Krish_ver2
 
BinarySearchTrees in data structures.ppt
umaranirs
 
Data structures and Algorithm analysis_Lecture4.pptx
AhmedEldesoky24
 
Trees gt(1)
Gopi Saiteja
 
Team-hawks_DSA_binary_tree[1] [Read-Only].pptx
XEON14
 
Search tree & graph
Michael Jo
 
1.1 binary tree
Krish_ver2
 
learn tree, linked list, queue, stack, and other algo
unknowwqer
 
Unit-VStackStackStackStackStackStack.pptx
nakshpub
 

More from Bianca Teşilă (11)

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

Recently uploaded (20)

PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Ad

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.