Avl Tree r23
Avl Tree r23
h>
#include <stdlib.h>
struct Node
{
int Val;
struct Node *left;
struct Node *right;
int height;
};
int max(int a, int b);
int height(struct Node *N)
{
if (N == NULL)
return 0;
return N->height;
}
node->left = NULL;
node->right = NULL;
node->height = 1;
return (node);
}
struct Node *RotateRight(struct Node *y)
{
struct Node *x = y->left;
struct Node *TEMP2 = x->right;
x->right = y;
y->left = TEMP2;
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
}
if (node == NULL)
return (newNode(Val));
node->height = 1 + max(height(node->left),height(node->right));
return node;
}
return current;
}
else
{
if ((root->left == NULL) || (root->right == NULL))
{
struct Node *temp = root->left ? root->left : root->right;
if (temp == NULL)
{
temp = root;
root = NULL;
}
else
*root = *temp;
free(temp);
}
else
{
struct Node *temp = minValueNode(root->right);
root->Val = temp->Val;
if (root == NULL)
return root;
root->height = 1 + max(height(root->left),height(root->right));
return root;
}
int main()
{
struct Node *root = NULL;
printPreOrder(root);