AVL Tree Insertion
PROBLEM :
Given a root of the tree you need to perform N AVL tree insertion operations on it. You need to complete the method insertToAVL which takes 2 arguments the first is the root of the tree and the second is the value of the node to be inserted.The function should return the root of the modified tree.
Note: Your code will be checked for each insertion and will produce an output 1 if all the nodes for a particular test case were correctly inserted else 0.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains 2 lines . The first line of each test case contains an integer N denoting the no of nodes to be inserted in the AVL tree and in the next line are N space separated values denoting the values of the nodes to be inserted to the tree.
Output:
For each test case output will be 1 if the node was correctly inserted else 0.
Constraints:
1<=T<=100
1<=N<=100
Example(To be used only for expected output):
Input:
2
2
1 2
3
1 2 3
Output:
1
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* The structure of the node is
struct node
{
int data;
struct node *left;
struct node *right;
int height;
};
*/
/*You are required to complete this method */
int height_tree(struct node *) ;
int max_of(int ,int ) ;
int balence_factor(struct node *) ;
struct node * RightRotation_tree(struct node *) ;
struct node * LeftRotation_tree(struct node *) ;
struct node* insertToAVL( struct node* node, int data)
{
struct node *temp ;
if(node==NULL)
{
temp=(struct node*)malloc(sizeof(struct node)) ;
temp->data=data ;
temp->left=NULL ;
temp->right=NULL ;
temp->height=1 ;
return temp ;
}
if(data==node->data)
return node ;
if(data<node->data)
node->left=insertToAVL(node->left,data) ;
else
node->right=insertToAVL(node->right,data) ;
node->height=max_of(height_tree(node->left),height_tree(node->right))+1 ;
int balence ;
balence=balence_factor(node) ;
if(balence>1&&data<=node->left->data)
{
return RightRotation_tree(node) ;
}
if(balence<-1&&data>node->right->data)
{
return LeftRotation_tree(node) ;
}
if(balence>1&&data>=node->left->data)
{
node->left=LeftRotation_tree(node->left) ;
return RightRotation_tree(node) ;
}
if(balence<-1&&data<=node->right->data)
{
node->right=RightRotation_tree(node->right) ;
return LeftRotation_tree(node) ;
}
return node ;
}
int height_tree(struct node *root)
{
if(root==NULL)
return 0 ;
return root->height ;
}
int max_of(int a,int b)
{
return a>b?a:b ;
}
int balence_factor(struct node *root)
{
if(root==NULL)
return 0 ;
return height_tree(root->left)-height_tree(root->right) ;
}
struct node * RightRotation_tree(struct node *one)
{
struct node *two=one->left ;
struct node *three=two->right ;
two->right=one ;
one->left=three ;
one->height=max_of(height_tree(one->left),height_tree(one->right))+1 ;
two->height=max_of(height_tree(two->left),height_tree(two->right))+1 ;
return two ;
}
struct node * LeftRotation_tree(struct node *one)
{
struct node *two=one->right ;
struct node *three=two->left ;
two->left=one ;
one->right=three ;
one->height=max_of(height_tree(one->left),height_tree(one->right))+1 ;
two->height=max_of(height_tree(two->left),height_tree(two->right))+1 ;
return two ;
}
---------------------------------------------------------------------------------
Given a root of the tree you need to perform N AVL tree insertion operations on it. You need to complete the method insertToAVL which takes 2 arguments the first is the root of the tree and the second is the value of the node to be inserted.The function should return the root of the modified tree.
Note: Your code will be checked for each insertion and will produce an output 1 if all the nodes for a particular test case were correctly inserted else 0.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains 2 lines . The first line of each test case contains an integer N denoting the no of nodes to be inserted in the AVL tree and in the next line are N space separated values denoting the values of the nodes to be inserted to the tree.
Output:
For each test case output will be 1 if the node was correctly inserted else 0.
Constraints:
1<=T<=100
1<=N<=100
Example(To be used only for expected output):
Input:
2
2
1 2
3
1 2 3
Output:
1
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* The structure of the node is
struct node
{
int data;
struct node *left;
struct node *right;
int height;
};
*/
/*You are required to complete this method */
int height_tree(struct node *) ;
int max_of(int ,int ) ;
int balence_factor(struct node *) ;
struct node * RightRotation_tree(struct node *) ;
struct node * LeftRotation_tree(struct node *) ;
struct node* insertToAVL( struct node* node, int data)
{
struct node *temp ;
if(node==NULL)
{
temp=(struct node*)malloc(sizeof(struct node)) ;
temp->data=data ;
temp->left=NULL ;
temp->right=NULL ;
temp->height=1 ;
return temp ;
}
if(data==node->data)
return node ;
if(data<node->data)
node->left=insertToAVL(node->left,data) ;
else
node->right=insertToAVL(node->right,data) ;
node->height=max_of(height_tree(node->left),height_tree(node->right))+1 ;
int balence ;
balence=balence_factor(node) ;
if(balence>1&&data<=node->left->data)
{
return RightRotation_tree(node) ;
}
if(balence<-1&&data>node->right->data)
{
return LeftRotation_tree(node) ;
}
if(balence>1&&data>=node->left->data)
{
node->left=LeftRotation_tree(node->left) ;
return RightRotation_tree(node) ;
}
if(balence<-1&&data<=node->right->data)
{
node->right=RightRotation_tree(node->right) ;
return LeftRotation_tree(node) ;
}
return node ;
}
int height_tree(struct node *root)
{
if(root==NULL)
return 0 ;
return root->height ;
}
int max_of(int a,int b)
{
return a>b?a:b ;
}
int balence_factor(struct node *root)
{
if(root==NULL)
return 0 ;
return height_tree(root->left)-height_tree(root->right) ;
}
struct node * RightRotation_tree(struct node *one)
{
struct node *two=one->left ;
struct node *three=two->right ;
two->right=one ;
one->left=three ;
one->height=max_of(height_tree(one->left),height_tree(one->right))+1 ;
two->height=max_of(height_tree(two->left),height_tree(two->right))+1 ;
return two ;
}
struct node * LeftRotation_tree(struct node *one)
{
struct node *two=one->right ;
struct node *three=two->left ;
two->left=one ;
one->right=three ;
one->height=max_of(height_tree(one->left),height_tree(one->right))+1 ;
two->height=max_of(height_tree(two->left),height_tree(two->right))+1 ;
return two ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment