0% found this document useful (0 votes)
25 views5 pages

Avl Tree r23

Uploaded by

usha.kitsakshar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Avl Tree r23

Uploaded by

usha.kitsakshar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

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

int max(int a, int b)


{
return (a > b) ? a : b;
}
struct Node *newNode(int Val)
{
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
node->Val = Val;

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

struct Node *RotateLeft(struct Node *x)


{
struct Node *y = x->right;
struct Node *TEMP2 = y->left;
y->left = x;
x->right = TEMP2;
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
}
int getBalance(struct Node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}

struct Node *insertNode(struct Node *node, int Val)


{

if (node == NULL)
return (newNode(Val));

if (Val < node->Val)


node->left = insertNode(node->left, Val);
else if (Val > node->Val)
node->right = insertNode(node->right, Val);
else
return node;

node->height = 1 + max(height(node->left),height(node->right));

int balance = getBalance(node);


if (balance > 1 && Val < node->left->Val)
return RotateRight(node);

if (balance < -1 && Val > node->right->Val)


return RotateLeft(node);

if (balance > 1 && Val > node->left->Val)


{
node->left = RotateLeft(node->left);
return RotateRight(node);
}

if (balance < -1 && Val < node->right->Val)


{
node->right = RotateRight(node->right);
return RotateLeft(node);
}

return node;
}

struct Node *minValueNode(struct Node *node)


{
struct Node *current = node;

while (current->left != NULL)


current = current->left;

return current;
}

struct Node *deleteNode(struct Node *root, int Val)


{
if (root == NULL)
return root;

if (Val < root->Val)


root->left = deleteNode(root->left, Val);

else if (Val > root->Val)


root->right = deleteNode(root->right, Val);

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;

root->right = deleteNode(root->right, temp->Val);


}
}

if (root == NULL)
return root;

root->height = 1 + max(height(root->left),height(root->right));

int balance = getBalance(root);


if (balance > 1 && getBalance(root->left) >= 0)

if (balance > 1 && getBalance(root->left) < 0)


{
root->left = RotateLeft(root->left);
return RotateRight(root);
}

if (balance < -1 && getBalance(root->right) <= 0)


return RotateLeft(root);

if (balance < -1 && getBalance(root->right) > 0)


{
root->right = RotateRight(root->right);
return RotateLeft(root);
}

return root;
}

void printPreOrder(struct Node *root)


{
if (root != NULL)
{
printf("%d ", root->Val);
printPreOrder(root->left);
printPreOrder(root->right);
}
}

int main()
{
struct Node *root = NULL;

root = insertNode(root, 56);


root = insertNode(root, 14);
root = insertNode(root, 86);
root = insertNode(root, 25);
root = insertNode(root, 38);
root = insertNode(root, 69);
root = insertNode(root, 89);

printPreOrder(root);

root = deleteNode(root, 14);

printf("AVL after deletion: ");


printPreOrder(root);

return 0; return RotateRight(root);


}
P

You might also like