Question

For AVL tree, write C functions to rotate-right, rotate-left, insert and delete, to check if a BS...

For AVL tree, write C functions to rotate-right, rotate-left, insert and delete, to
check if a BST is a AVL tree.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

struct Node *rightRotate(struct Node *y)
{
struct Node *x = y->left;
struct Node *T2 = x->right;
  
// Perform rotation
x->right = y;
y->left = T2;
  
// Update heights
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
  
// Return new root
return x;
}
  
  
  
struct Node *leftRotate(struct Node *x)
{
struct Node *y = x->right;
struct Node *T2 = y->left;
  
// Perform rotation
y->left = x;
x->right = T2;
  
// Update heights
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;
  
// Return new root
return y;
}


struct Node* insert(struct Node* node, int key)
{
/* 1. Perform the normal BST insertion */
if (node == NULL)
return(newNode(key));
  
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Equal keys are not allowed in BST
return node;
  
/* 2. Update height of this ancestor node */
node->height = 1 + max(height(node->left),
height(node->right));
  
/* 3. Get the balance factor of this ancestor
node to check whether this node became
unbalanced */
int balance = getBalance(node);
  
// If this node becomes unbalanced, then
// there are 4 cases
  
// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);
  
// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);
  
// Left Right Case
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
  
// Right Left Case
if (balance < -1 && key < node->right->key)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}
  
/* return the (unchanged) node pointer */
return node;
}

int getBalance(Node *N)
{
if (N == NULL)
return 0;
return height(N->left) –
height(N->right);
}

Node* deleteNode(Node* root, int key)
{

// STEP 1: PERFORM STANDARD BST DELETE
if (root == NULL)
return root;

// If the key to be deleted is smaller
// than the root’s key, then it lies
// in left subtree
if ( key < root->key )
root->left = deleteNode(root->left, key);

// If the key to be deleted is greater
// than the root’s key, then it lies
// in right subtree
else if( key > root->key )
root->right = deleteNode(root->right, key);

// if key is same as root’s key, then
// This is the node to be deleted
else
{
// node with only one child or no child
if( (root->left == NULL) ||
(root->right == NULL) )
{
Node *temp = root->left ?
root->left :
root->right;

// No child case
if (temp == NULL)
{
temp = root;
root = NULL;
}
else // One child case
*root = *temp; // Copy the contents of
// the non-empty child
free(temp);
}
else
{
// node with two children: Get the inorder
// successor (smallest in the right subtree)
Node* temp = minValueNode(root->right);

// Copy the inorder successor’s
// data to this node
root->key = temp->key;

// Delete the inorder successor
root->right = deleteNode(root->right,
temp->key);
}
}

// If the tree had only one node
// then return
if (root == NULL)
return root;

// STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
root->height = 1 + max(height(root->left),
height(root->right));

// STEP 3: GET THE BALANCE FACTOR OF
// THIS NODE (to check whether this
// node became unbalanced)
int balance = getBalance(root);

// If this node becomes unbalanced,
// then there are 4 cases

// Left Left Case
if (balance > 1 &&
getBalance(root->left) >= 0)
return rightRotate(root);

// Left Right Case
if (balance > 1 &&
getBalance(root->left) < 0) { root->left = leftRotate(root->left);
return rightRotate(root);
}

// Right Right Case
if (balance < -1 && getBalance(root->right) <= 0) return leftRotate(root); // Right Left Case if (balance < -1 && getBalance(root->right) > 0)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}

return root;
}


/* Returns true if binary tree with root as root is height-balanced which means AVL tree*/
bool isBalanced(struct node *root)
{
int lh; /* for height of left subtree */
int rh; /* for height of right subtree */
  
/* If tree is empty then return true */
if(root == NULL)
return 1;
  
/* Get the height of left and right sub trees */
lh = height(root->left);
rh = height(root->right);
  
if( abs(lh-rh) <= 1 &&
isBalanced(root->left) &&
isBalanced(root->right))
return 1;
  
/* If we reach here then tree is not height-balanced */
return 0;
}
  
/* UTILITY FUNCTIONS TO TEST isBalanced() FUNCTION */
  
/* returns maximum of two integers */
int max(int a, int b)
{
return (a >= b)? a: b;
}

Add a comment
Know the answer?
Add Answer to:
For AVL tree, write C functions to rotate-right, rotate-left, insert and delete, to check if a BS...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT