Question

Please C code for AVL trees -> insert(recursive),height,balance factor,left rotate,right rotate,max and init functions..

Please C code for AVL trees -> insert(recursive),height,balance factor,left rotate,right rotate,max and init functions..
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include<stdlib.h>

struct BSTreeNode

{

int data;

struct BSTreeNode *left;

struct BSTreeNode *right;

int heightOfNode;

};

void InOrder(struct BSTreeNode *root)

{

if(root != NULL)

{

InOrder(root->left);

printf("%d ",root->data);

InOrder(root->right);

}

}

int Treeheight(struct BSTreeNode *root)

{

if (root == NULL)

return 0;

return root->heightOfNode;

}

int max(int a, int b)

{

if(a >= b)

return a;

return b;

}

struct BSTreeNode* GetNode(int data)

{

struct BSTreeNode* temp = (struct BSTreeNode*)

malloc(sizeof(struct BSTreeNode));

temp->data = data;

temp->left = NULL;

temp->right = NULL;

temp->heightOfNode = 1;

return temp;

}

struct BSTreeNode *RightRight(struct BSTreeNode *y)

{

struct BSTreeNode *x = y->left;

struct BSTreeNode *T2 = x->right;

x->right = y;

y->left = T2;

y->heightOfNode = max(Treeheight(y->left), Treeheight(y->right))+1;

x->heightOfNode = max(Treeheight(x->left), Treeheight(x->right))+1;

return x;

}

struct BSTreeNode *LeftLeft(struct BSTreeNode *x)

{

struct BSTreeNode *y = x->right;

struct BSTreeNode *T2 = y->left;

y->left = x;

x->right = T2;

x->heightOfNode = max(Treeheight(x->left), Treeheight(x->right))+1;

y->heightOfNode = max(Treeheight(y->left), Treeheight(y->right))+1;

return y;

}

int getBalanceFactor(struct BSTreeNode *root)

{

if (root == NULL)

return 0;

return Treeheight(root->left) - Treeheight(root->right);

}

struct BSTreeNode* AVLInsert(struct BSTreeNode* node, int key)

{

if (node == NULL)

return(GetNode(key));

if (key < node->data)

node->left = AVLInsert(node->left, key);

else if (key > node->data)

node->right = AVLInsert(node->right, key);

else

return node;

node->heightOfNode = 1 + max(Treeheight(node->left),

Treeheight(node->right));

int balancefactor = getBalanceFactor(node);

if (balancefactor > 1 && key < node->left->data)

return RightRight(node);

if (balancefactor < -1 && key > node->right->data)

return LeftLeft(node);

if (balancefactor > 1 && key > node->left->data)

{

node->left = LeftLeft(node->left);

return RightRight(node);

}

if (balancefactor < -1 && key < node->right->data)

{

node->right = RightRight(node->right);

return LeftLeft(node);

}

return node;

}

int main()

{

struct BSTreeNode *root = NULL;

root = AVLInsert(root, 190);

root = AVLInsert(root, 200);

root = AVLInsert(root, 130);

root = AVLInsert(root, 840);

root = AVLInsert(root, 500);

root = AVLInsert(root, 225);

InOrder(root);

return 0;

}

==============================================

SEE OUTPUT

PLEASE COMMENT if there is any concern.

======================================================

Add a comment
Know the answer?
Add Answer to:
Please C code for AVL trees -> insert(recursive),height,balance factor,left rotate,right rotate,max and init functions..
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