Question: 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 8,5,2na d2/**
* Helper function to returns the height of the tree
*/
int height(struct node *tree){
if (tree == NULL)
return 0;
return tree->height;
}
/**
* Helper function to update height
*/
void updateHeight (struct node *root){
if (root != NULL){
root->height =1+ max(height(root->left),height(root->right));
}
}/**
* Helper function to check balance
*/
int checkBalance (struct node *tree){
if (tree == NULL){
return 0;
}
return height(tree->left)- height(tree->right);
}
/**
* Helper function to perform left rotation
*/
struct node *rotateLeft(struct node *tree){
struct node *newRoot = tree->right;
struct node *temp = newRoot->right;
newRoot->left = tree;
tree->right = temp;
updateHeight(tree);
updateHeight(newRoot);
return newRoot;
}
/**
* Helper function to perform right rotation
*/
struct node *rotateRight(struct node *tree){
struct node *newRoot = tree->left;
struct node *temp = newRoot->right;
newRoot->right = tree;
tree->left = temp;
updateHeight(tree);
updateHeight(newRoot);
return newRoot;
}
/**
* Helper function to balance tree after insertion
*/
struct node *balanceTree (struct node *tree, int item){
int balance = checkBalance(tree);
// Left-left case
if (balance >1 && item < tree->left->item){
return rotateRight(tree);
}
// Left-Right case
if (balance >1 && item > tree->left->item){
tree->left = rotateLeft(tree->left);
return rotateRight(tree);
}
// Right-Right case
if (balance <-1 && item > tree->right->item){
return rotateLeft(tree);
}
// Right-Left case
if (balance <-1 && item < tree->right->item){
tree->right = rotateRight(tree->right);
return rotateLeft(tree);
}
return tree;
}
/**
* Helper function to insert a node into the tree
*/
void insertNode(struct node **tree, int item){
if (*tree == NULL){
*tree = createNode(item);
} else if (item <(*tree)->item){
insertNode(&(*tree)->left, item);
} else if (item >(*tree)->item){
insertNode(&(*tree)->right, item);
}
*tree = balanceTree(*tree, item);
}/**
* Inserts an item into the set
*/
void SetInsert(Set s, int item){
// TODO
// Case where UNDEFINED cannot be inserted
if (item == UNDEFINED){
return;
}
if (!SetContains(s, item)){
// Duplicate items should not be inserted
insertNode(&(s->tree), item);
s->size +=1;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!