AVL Tree Deletion
PROBLEM :
Given a root of the tree you need to perform AVL tree deletion operations on it. You need to complete the method delelteNode which takes 2 arguments the first is the root of the tree and the second is the value of the node to be deleted.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. And last line denotes the element to be deleted.
Output:
For each test case output will be 1 if the node was correctly deleted else 0.
Constraints:
1<=T<=100
1<=N<=100
Example(To be used only for expected output):
Input:
2
2
1 2
1
3
1 2 3
2
Output:
1
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*Complete the following function
Node is as follows:
struct Node
{
int data;
Node *left;
Node *right;
int height;
};
*/
int FindMin_tree(Node *root)
{
while(root->left!=NULL)
root=root->left ;
return root->data ;
}
int max_of(int a,int b)
{
return a>b?a:b ;
}
int height_tree(Node *root)
{
if(root==NULL)
return 0 ;
return root->height ;
}
int balence_factor(Node *root)
{
if(root==NULL)
return 0 ;
return height_tree(root->left)-height_tree(root->right) ;
}
Node * RightRotation_tree(Node *one)
{
Node *two=one->left ;
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 ;
}
Node * LeftRotation_tree(Node *one)
{
Node *two=one->right ;
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 ;
}
Node* deleteNode(Node* root, int ele)
{
if(root==NULL)
return NULL ;
if(root->data>ele)
root->left= deleteNode(root->left,ele) ;
else if(root->data<ele)
root->right= deleteNode(root->right,ele) ;
else
{
if(root->left==NULL&&root->right==NULL)
{
Node *temp ;
temp=root ;
free(temp) ;
return NULL ;
}
else if(root->left!=NULL&&root->right==NULL)
{
Node *temp ;
temp=root->left ;
free(root) ;
return temp ;
}
else if(root->left==NULL&&root->right!=NULL)
{
Node *temp ;
temp=root->right ;
free(root) ;
return temp ;
}
else
{
int temp ;
temp=FindMin_tree(root->right) ;
root->data=temp ;
root->right=deleteNode(root->right,temp) ;
}
}
if(root==NULL)
return root ;
root->height=max_of(height_tree(root->left),height_tree(root->right))+1 ;
int balence ;
balence=balence_factor(root) ;
if(balence>1&&balence_factor(root->left)>=0)
{
return RightRotation_tree(root) ;
}
if(balence<-1&&balence_factor(root->right)<=0)
{
return LeftRotation_tree(root) ;
}
if(balence>1&&balence_factor(root->left)<0)
{
root->left=LeftRotation_tree(root->left) ;
return RightRotation_tree(root) ;
}
if(balence<-1&&balence_factor(root->right)>0)
{
root->right=RightRotation_tree(root->right) ;
return LeftRotation_tree(root) ;
}
return root ;
}
--------------------------------------------------------------------------------
Given a root of the tree you need to perform AVL tree deletion operations on it. You need to complete the method delelteNode which takes 2 arguments the first is the root of the tree and the second is the value of the node to be deleted.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. And last line denotes the element to be deleted.
Output:
For each test case output will be 1 if the node was correctly deleted else 0.
Constraints:
1<=T<=100
1<=N<=100
Example(To be used only for expected output):
Input:
2
2
1 2
1
3
1 2 3
2
Output:
1
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*Complete the following function
Node is as follows:
struct Node
{
int data;
Node *left;
Node *right;
int height;
};
*/
int FindMin_tree(Node *root)
{
while(root->left!=NULL)
root=root->left ;
return root->data ;
}
int max_of(int a,int b)
{
return a>b?a:b ;
}
int height_tree(Node *root)
{
if(root==NULL)
return 0 ;
return root->height ;
}
int balence_factor(Node *root)
{
if(root==NULL)
return 0 ;
return height_tree(root->left)-height_tree(root->right) ;
}
Node * RightRotation_tree(Node *one)
{
Node *two=one->left ;
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 ;
}
Node * LeftRotation_tree(Node *one)
{
Node *two=one->right ;
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 ;
}
Node* deleteNode(Node* root, int ele)
{
if(root==NULL)
return NULL ;
if(root->data>ele)
root->left= deleteNode(root->left,ele) ;
else if(root->data<ele)
root->right= deleteNode(root->right,ele) ;
else
{
if(root->left==NULL&&root->right==NULL)
{
Node *temp ;
temp=root ;
free(temp) ;
return NULL ;
}
else if(root->left!=NULL&&root->right==NULL)
{
Node *temp ;
temp=root->left ;
free(root) ;
return temp ;
}
else if(root->left==NULL&&root->right!=NULL)
{
Node *temp ;
temp=root->right ;
free(root) ;
return temp ;
}
else
{
int temp ;
temp=FindMin_tree(root->right) ;
root->data=temp ;
root->right=deleteNode(root->right,temp) ;
}
}
if(root==NULL)
return root ;
root->height=max_of(height_tree(root->left),height_tree(root->right))+1 ;
int balence ;
balence=balence_factor(root) ;
if(balence>1&&balence_factor(root->left)>=0)
{
return RightRotation_tree(root) ;
}
if(balence<-1&&balence_factor(root->right)<=0)
{
return LeftRotation_tree(root) ;
}
if(balence>1&&balence_factor(root->left)<0)
{
root->left=LeftRotation_tree(root->left) ;
return RightRotation_tree(root) ;
}
if(balence<-1&&balence_factor(root->right)>0)
{
root->right=RightRotation_tree(root->right) ;
return LeftRotation_tree(root) ;
}
return root ;
}
--------------------------------------------------------------------------------
Comments
Post a Comment