0% found this document useful (0 votes)
32 views8 pages

Tree Traversal

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views8 pages

Tree Traversal

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

WAP for the Creation, Traversal (In order, Preorder, Postorder) in a Binary Search Tree

(BST).

Objective: To understand basic tree operations.

Prequisite: To have basic understanding of operations on tree as a data structure.

#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *lchild;
struct node *rchild;
}*root;
void postorder(struct node *);
void preorder(struct node *);
void inorder(struct node *);
void insert(int);
void del(int);
void display(struct node *,int);
void case_a(struct node *,struct node *);
void case_b(struct node *,struct node *);
void case_c(struct node *,struct node *);

void main()
{
int ch,num;
char z;
clrscr();
while(1)
{
clrscr();
printf("\n\t **********MENU************ ");
printf("\n\t 1.Insert.");
printf("\n\t 2.Delete.");
printf("\n\t 3.Inorder Traversal.");
printf("\n\t 4.Preorder Traversal.");
printf("\n\t 5.Postorder Traversal.");
printf("\n\t 6.Display.");
printf("\n\t 7.Exit.");
printf("\n\t Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\t Enter the number to be inserted:");
scanf("%d",&num);
insert(num);
break;

case 2:
printf("\n\t Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;

case 3:
inorder(root);
break;

case 4:
preorder(root);
break;

case 5:
postorder(root);
break;

case 6:
display(root,1);
break;

case 7:
exit(0);

default:
printf("\n\t Wrong choice\n");
}
printf("\n\t Do u want to continue:");
fflush(stdin);
scanf("%c",&z);
}
getch();
}

void find(int item,struct node **par,struct node **loc)


{
struct node *ptr,*ptrsave;

if(root==NULL) /*tree empty*/


{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->info) /*item is at root*/
{
*loc=root;
*par=NULL;
return;
}
/*Initialize ptr and ptrsave*/
if(item<root->info)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;

while(ptr!=NULL)
{
if(item==ptr->info)
{ *loc=ptr;
*par=ptrsave;
//return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}/*End of while */
*loc=NULL; /*item not found*/
*par=ptrsave;
}/*End of find()*/

void insert(int item)


{ struct node *tmp,*parent,*location;
find(item,&parent,&location);
if(location!=NULL)
{
printf("\n\t Item already present");
return;
}

tmp=(struct node *)malloc(sizeof(struct node));


tmp->info=item;
tmp->lchild=NULL;
tmp->rchild=NULL;

if(parent==NULL)
root=tmp;
else
if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}/*End of insert()*/
void del(int item)
{
struct node *parent,*location;
if(root==NULL)
{
printf("\n\t Tree empty");
return;
}

find(item,&parent,&location);
if(location==NULL)
{
printf("\n\t Item not present in tree");
return;
}

if(location->lchild==NULL && location->rchild==NULL)


case_a(parent,location);
if(location->lchild!=NULL && location->rchild==NULL)
case_b(parent,location);
if(location->lchild==NULL && location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL && location->rchild!=NULL)
case_c(parent,location);
free(location);
}/*End of del()*/

void case_a(struct node *par,struct node *loc )


{
if(par==NULL) /*item to be deleted is root node*/
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;
}/*End of case_a()*/

void case_b(struct node *par,struct node *loc)


{
struct node *child;

/*Initialize child*/
if(loc->lchild!=NULL) /*item to be deleted has lchild */
child=loc->lchild;
else /*item to be deleted has rchild */
child=loc->rchild;

if(par==NULL ) /*Item to be deleted is root node*/


root=child;
else
if( loc==par->lchild) /*item is lchild of its parent*/
par->lchild=child;
else /*item is rchild of its parent*/
par->rchild=child;
}/*End of case_b()*/

void case_c(struct node *par,struct node *loc)


{
struct node *ptr,*ptrsave,*suc,*parsuc;

/*Find inorder successor and its parent*/


ptrsave=loc;
ptr=loc->rchild;
while(ptr->lchild!=NULL)
{
ptrsave=ptr;
ptr=ptr->lchild;
}
suc=ptr;
parsuc=ptrsave;

if(suc->lchild==NULL && suc->rchild==NULL)


case_a(parsuc,suc);
else
case_b(parsuc,suc);

if(par==NULL) /*if item to be deleted is root node */


root=suc;
else
if(loc==par->lchild)
par->lchild=suc;
else
par->rchild=suc;

suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}/*End of case_c()*/

void preorder(struct node *ptr)


{
if(root==NULL)
{
printf("\n\t Tree is empty");
return;
}
if(ptr!=NULL)
{
printf("\n\t %d ",ptr->info);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}/*End of preorder()*/

void inorder(struct node *ptr)


{
if(root==NULL)
{
printf("\n\t Tree is empty");
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("\n\t %d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/

void postorder(struct node *ptr)


{
if(root==NULL)
{
printf("\n\t Tree is empty");
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("\n\t %d ",ptr->info);
}
}/*End of postorder()*/

void display(struct node *ptr,int level)


{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}
}

You might also like