0% found this document useful (0 votes)
38 views6 pages

Lab 11 Data Structure 16 December

This document discusses searching for a node in a binary search tree and balancing an unbalanced tree. It describes using if statements to compare the root node to the search key and check which side of the tree to search. For balancing, it explains checking where the tree is unbalanced and performing the appropriate rotation - left, right, left-right, or right-left. Deletion of nodes is also covered, addressing cases for nodes with no, one, or two children.

Uploaded by

Mahreen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Lab 11 Data Structure 16 December

This document discusses searching for a node in a binary search tree and balancing an unbalanced tree. It describes using if statements to compare the root node to the search key and check which side of the tree to search. For balancing, it explains checking where the tree is unbalanced and performing the appropriate rotation - left, right, left-right, or right-left. Deletion of nodes is also covered, addressing cases for nodes with no, one, or two children.

Uploaded by

Mahreen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Node search

Search node:
In this task we have write program that would search specific element for this I use if statement
that would compare the root element and the element we want to search if the root element is
equal to the search element then we would return the element again I use if statement that would
check if the key element is greater than root data then it will search in the right of search tree else
it will search in right of search tree and in this way the element is found.

In lab tasks:
In this lab we have to write program that would balance tree if the tree is unbalance we have to
balance it for this we check the node where is it unbalance and which kind of unbalance is it
either right left, left right ,right right and left left. If it is unbalance in right left so first we make
right rotation and then left or if it’s left right unbalance then we first make left then right rotation.
If the tree is unbalance in right right right then we make left rotation and if the tree is unbalance
in left then we make right rotation. For this we write program for each cases and after that we run
it and check where it is unbalance and then we call the cases accordingly.
Left rotation:

Right rotation:
Left left rotation

Right right rotation:

Left right rotation:

Right left rotation:


POST LAB:
DELETION OF NODE:
In deletion node we have to tackle three cases that is if the tree have no child one child or two child for
each of this we have to write program for this I use if statement that would check if root is null or not if
the data we want to delete is greater than root data then it delete the element the right data of tree
else if the data we want to delete is less then it will delete the left element. For one child I use if
statement that will check if the left and right is null it means the tree have one child and it would be
deleted for one child I also use if statement that would check both left and right data and for two child I
also use if statement that would check both left and right if both have data it mean it have two child In
deletion node we have to tackle three cases that is if the tree have no child one child or two child for
each of this we have to write program for this I use if statement that would check if root is null or not if
the data we want to delete is greater than root data then it delete the element the right data of tree
else if the data we want to delete is less then it will delete the left element. For one child I use if
statement that will check if the left and right is null it means the tree have one child and it would be
deleted for one child I also use if statement that would check both left and right data and for two child I
also use if statement that would check both left and right if both have data it mean it have two child.

You might also like