S
sophia.agnes
Hi all,
this is a simple binary tree program done by me for learning node
deletions in a binary tree, how good is the deletion method ???? can
anyone suggest corrections/improvements ????
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct node
{
struct node *left;
struct node *right;
int val;
}sn;
void insert(sn **,int);
void inorder(sn*);
void search(sn**,int,sn**,sn**,int*);
void delete(sn**,int);
sn* allocate(int);
int main(void)
{
sn *bt;
int req,i=1,num,dv;
bt = NULL;
printf("\n Enter the no: of nodes to be inserted:: ");
scanf("%d",&req);
assert(req > 0);
while(i++ <= req)
{
printf("Enter the value of the node you want to insert:: ");
scanf("%d",&num);
insert(&bt,num);
}
printf("Enter the value of the node you want to delete:: ");
scanf("%d",&dv);
assert(dv > 0);
assert(bt != NULL);
delete(&bt,dv);
if(bt == NULL)
{puts("Tree empty");exit(0);}
puts(".............................");
printf("IN ORDER TRAVERSAL\n");
puts(".............................\n");
inorder(bt);
puts("");
return(EXIT_SUCCESS);
}
void insert(sn** sr,int num)
{
if(*sr == NULL)
{
*sr = allocate(num);
}
else
{
if( num < (*sr) -> val )
insert( &((*sr)->left),num);
else
insert( &((*sr)->right),num);
}
}
sn* allocate(int n)
{
sn* node;
node = malloc(sizeof(sn));
assert(node != NULL);
node -> left = NULL;
node -> right = NULL;
node -> val = n;
return node;
}
void inorder(sn *root)
{
if(root != NULL)
{
inorder(root -> left);
printf("%d\t",root ->val);
inorder(root -> right);
}
}
void delete(sn **root, int num)
{
int found;
sn *parent,*x,*xsucc;
sn *temp,*temp1;
if( (*root)->val == num && (*root)->left == NULL && (*root)-> right
== NULL)
{
*root = NULL;
return;
}
parent = x = NULL;
search(root,num,&parent,&x,&found);
if(found == 0)
{
puts("Data to be deleted not found");
return;
}
/*if the node to be deleted has no child.*/
if(x ->left == NULL && x -> right == NULL)
{
if(parent -> left == x)
parent -> left = NULL;
else
parent -> right = NULL;
free(x);
return;
}
/*if the node to be deleted has only right child*/
if(x->left == NULL && x-> right != NULL)
{
if(parent == NULL)
{
*root = x -> right;
return;
}
if(parent -> left == x)
parent -> left = x -> right;
else
parent -> right = x -> right;
free(x);
return;
}
/*if the node to be deleted has only left child*/
if(x->left != NULL && x-> right == NULL)
{
if(parent == NULL)
{
*root = x -> left;
return;
}
if(parent -> left == x)
parent -> left = x -> left;
else
parent -> right = x -> left;
free(x);
return;
}
/*if the node to be deleted have both child*/
if(x->left != NULL && x-> right != NULL)
{
if(parent == NULL)
{
temp1 = temp = (*root)-> left;
while(temp ->right != NULL)
temp = temp -> right;
temp->right = (*root)-> right;
*root = temp1;
return;
}
temp1 = temp = x -> left;
while(temp ->right != NULL)
temp = temp -> right;
temp->right = x -> right;
if(parent -> left == x)
parent-> left = temp1;
else
parent-> right = temp1;
x->left = x-> right = NULL;
free(x);
}
}
void search(sn** root,int num,sn** par, sn** x,int *found)
{
sn *q;
q = *root;
*par = NULL;
*found = 0;
while(q != NULL)
{
if(q -> val == num)
{
*x = q;
*found = 1;
return;
}
*par = q;
if(q -> val < num )
q = q -> right;
else
q= q -> left;
}
}
this is a simple binary tree program done by me for learning node
deletions in a binary tree, how good is the deletion method ???? can
anyone suggest corrections/improvements ????
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct node
{
struct node *left;
struct node *right;
int val;
}sn;
void insert(sn **,int);
void inorder(sn*);
void search(sn**,int,sn**,sn**,int*);
void delete(sn**,int);
sn* allocate(int);
int main(void)
{
sn *bt;
int req,i=1,num,dv;
bt = NULL;
printf("\n Enter the no: of nodes to be inserted:: ");
scanf("%d",&req);
assert(req > 0);
while(i++ <= req)
{
printf("Enter the value of the node you want to insert:: ");
scanf("%d",&num);
insert(&bt,num);
}
printf("Enter the value of the node you want to delete:: ");
scanf("%d",&dv);
assert(dv > 0);
assert(bt != NULL);
delete(&bt,dv);
if(bt == NULL)
{puts("Tree empty");exit(0);}
puts(".............................");
printf("IN ORDER TRAVERSAL\n");
puts(".............................\n");
inorder(bt);
puts("");
return(EXIT_SUCCESS);
}
void insert(sn** sr,int num)
{
if(*sr == NULL)
{
*sr = allocate(num);
}
else
{
if( num < (*sr) -> val )
insert( &((*sr)->left),num);
else
insert( &((*sr)->right),num);
}
}
sn* allocate(int n)
{
sn* node;
node = malloc(sizeof(sn));
assert(node != NULL);
node -> left = NULL;
node -> right = NULL;
node -> val = n;
return node;
}
void inorder(sn *root)
{
if(root != NULL)
{
inorder(root -> left);
printf("%d\t",root ->val);
inorder(root -> right);
}
}
void delete(sn **root, int num)
{
int found;
sn *parent,*x,*xsucc;
sn *temp,*temp1;
if( (*root)->val == num && (*root)->left == NULL && (*root)-> right
== NULL)
{
*root = NULL;
return;
}
parent = x = NULL;
search(root,num,&parent,&x,&found);
if(found == 0)
{
puts("Data to be deleted not found");
return;
}
/*if the node to be deleted has no child.*/
if(x ->left == NULL && x -> right == NULL)
{
if(parent -> left == x)
parent -> left = NULL;
else
parent -> right = NULL;
free(x);
return;
}
/*if the node to be deleted has only right child*/
if(x->left == NULL && x-> right != NULL)
{
if(parent == NULL)
{
*root = x -> right;
return;
}
if(parent -> left == x)
parent -> left = x -> right;
else
parent -> right = x -> right;
free(x);
return;
}
/*if the node to be deleted has only left child*/
if(x->left != NULL && x-> right == NULL)
{
if(parent == NULL)
{
*root = x -> left;
return;
}
if(parent -> left == x)
parent -> left = x -> left;
else
parent -> right = x -> left;
free(x);
return;
}
/*if the node to be deleted have both child*/
if(x->left != NULL && x-> right != NULL)
{
if(parent == NULL)
{
temp1 = temp = (*root)-> left;
while(temp ->right != NULL)
temp = temp -> right;
temp->right = (*root)-> right;
*root = temp1;
return;
}
temp1 = temp = x -> left;
while(temp ->right != NULL)
temp = temp -> right;
temp->right = x -> right;
if(parent -> left == x)
parent-> left = temp1;
else
parent-> right = temp1;
x->left = x-> right = NULL;
free(x);
}
}
void search(sn** root,int num,sn** par, sn** x,int *found)
{
sn *q;
q = *root;
*par = NULL;
*found = 0;
while(q != NULL)
{
if(q -> val == num)
{
*x = q;
*found = 1;
return;
}
*par = q;
if(q -> val < num )
q = q -> right;
else
q= q -> left;
}
}