Answered step by step
Verified Expert Solution
Link Copied!

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

What are the workflow steps in system development?

Answered: 1 week ago

Question

Multiply and simplify your answer. x - 25 x210x25 x-5 X

Answered: 1 week ago

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago