A Program To Delete A Node From Binary Search Tree
A Program To Delete A Node From Binary Search Tree
tree
#include<alloc.h>
#define TRUE 1
#define FALSE 0
Struct btree node
{
Struct btreenode *leftchild;
Int data;
Struct btreenode *rightchild;
};
main();
{
Struct btreenode *bt;
Int req,i=0,numa[] = {11,9,13,8,10,12,14,15,7};
Bt= NULL, /”empty tree”/
Clrscr()
While(i<=8)
{
Insert(&bt,a[i]);
i++;
}
Clrscr();
Printf(“binary tree before deletion:”);
Inorder(bt);
Delete(&bt,10);
Printf(“/n Binary tree after deletion:”);
Inorder(bt);
Delete(&bt,14);
Printf(“/n Binary tree after deletion:”);
Inorder(bt);
Delete(&bt,8);
Printf(/n”Binary tree after deletion:”);
Inorder(bt);
Delete(&bt,13);
Printf(“/Binary tree after deletion:”);
Inorder(bt);
}
/*inserts a node in a binary search tree*/
Insert(struct btreenode **sr,int num)
{
{
If(*sr==NULL)
{
*sr=malloc(sizeof(struct btreenode));
(*sr)-> leftchild =NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
Return;
}
else /*search the node to which new node will be attached */
{
/*if new data is less, traverse to left*/
If(num<(*sr)->data)
Insert(&((*sr)->leftchild),num);
Else
/* else traverse to right*/
Insert(&((*sr)->rightchild),num);
}
Return;
}
/* deletes a node from the binary search tree */
Delete(struct btreenode **root,int num)
{
Int found;
Struct btreenode *parent,*x,*xsucc;
/*if tree is empty*/
If(*root==NULL)
{
Printf(“/n Tree is empty”);
Return;
}
Parent = x= NULL;
/* call to search function to find the node to be deleted */
Search(root,num, &parent, &x, &found);
/*if the node to deleted is not found*/
If (found==FALSE)
{
Printf(“Data to be deleted, not found”);
Return;
}
/* if the node to be deleted has two children */
If(x->leftchild!=NULL&& x->rightchild!=NULL)
{
Parent=x;
Xsucc=x->rightchild;
While(xsucc->leftchild!=NULL)
{
Parent = xsucc;
Xsucc = xsucc->leftchild;
}
X->data =xsucc->data;
X = xsucc;
}
/* if the node to be deleted has no child */
If(x->leftchild==NULL&& x->rightchild==NULL)
{
If (parent->rightchild==x)
Parent ->rightchild=NULL;
Else
Parent->leftchild=NULL;
Free(x);
Return;
}
/* if the node to be deleted has only rightchild*/
If(x->leftchild==NULL && x->rightchild!=NULL)
{
If(parent->leftchild==x)
Parent->leftchild=x->rightchild;
Else
Parent->rightchild=x->rightchild;
Free(x);
Return;
}
/* if the node to be deleted has only left child */
If(x->leftchild!=NULL && x->rightchild == NULL)
{
If parent->leftchild==x)
parent->leftchild=x->leftchild;
Else
Parent->rightchild=x-> leftchild;
free(x);
Return;
}
}
/*returns the address of the node to be deleted, address of its parent and whether the node is
found or not */
Serch (struct btreenode **root, int num, struct btreenode **par, struct btreenode **x,int *
found)
{
Struct btreenode *q;
Q=*root;
*found = FALSE;
*par = NULL;
While (q!=NULL)
{
/* if the node to be deleted is found */
If (q-> data==num)
{
*found = TRUE;
*x=q;
Return;
}
If(q->data>num)
{
*par=q;
q=q->leftchild;
}
Else
{
*par=q;
q=q->rightcild;
}
}
}
/* traverse a binary search tree in a LDR(LEFT-DATA-RIGHT) fashion */
Inorder(struct btreenode *sr)
{
If(sr!=NULL)
{
Inorder(sr->leftchild);
/* print the data of the node whose left child is NULL or the path has already been traversed */