Answered step by step
Verified Expert Solution
Question
1 Approved Answer
can you help me find where the problem is as i cant seem to have the function does not seem to rebalcne the height of
can you help me find where the problem is as i cant seem to have the function does not seem to rebalcne the height of the tree after inserting na d
Helper function to returns the height of the tree
int heightstruct node tree
if tree NULL
return ;
return treeheight;
Helper function to update height
void updateHeight struct node root
if root NULL
rootheight maxheightrootleftheightrootright;
Helper function to check balance
int checkBalance struct node tree
if tree NULL
return ;
return heighttreeleft heighttreeright;
Helper function to perform left rotation
struct node rotateLeftstruct node tree
struct node newRoot treeright;
struct node temp newRootright;
newRootleft tree;
treeright temp;
updateHeighttree;
updateHeightnewRoot;
return newRoot;
Helper function to perform right rotation
struct node rotateRightstruct node tree
struct node newRoot treeleft;
struct node temp newRootright;
newRootright tree;
treeleft temp;
updateHeighttree;
updateHeightnewRoot;
return newRoot;
Helper function to balance tree after insertion
struct node balanceTree struct node tree int item
int balance checkBalancetree;
Leftleft case
if balance && item treeleftitem
return rotateRighttree;
LeftRight case
if balance && item treeleftitem
treeleft rotateLefttreeleft;
return rotateRighttree;
RightRight case
if balance && item treerightitem
return rotateLefttree;
RightLeft case
if balance && item treerightitem
treeright rotateRighttreeright;
return rotateLefttree;
return tree;
Helper function to insert a node into the tree
void insertNodestruct node tree int item
if tree NULL
tree createNodeitem;
else if item treeitem
insertNode&treeleft, item;
else if item treeitem
insertNode&treeright, item;
tree balanceTreetree item;
Inserts an item into the set
void SetInsertSet s int item
TODO
Case where UNDEFINED cannot be inserted
if item UNDEFINED
return;
if SetContainss item
Duplicate items should not be inserted
insertNode&stree item;
ssize ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started