Binary search tree




#include<iostream.h>
#include<process.h>
#include<malloc.h>
#include<conio.h>
#include<stdio.h>

void getnew();
struct tree
{
int data;
struct tree *llink,*rlink;
}*newnode,*ptr,*pptr,*stack=NULL,*start=NULL;

void getnew()
{
newnode=(tree*)malloc(sizeof(tree));
newnode->rlink=stack;
stack=newnode;
newnode->llink=ptr;
stack->data=1;
}//function to add new node to stack


int main(void)
{
int ch,data,total=0,l,m,n,i,digit=0,pdigit,yy=39;
float x,y,ub,lb;
textcolor(2);
textbackground(4);
_setcursortype(_NOCURSOR);
start=stack=ptr=NULL;
while(1)
{
ptr=start;
p:
ch=1;
clrscr();
if(start==NULL)
{
gotoxy(35,10);
cout<<"Empty Tree";
goto o;
}
ub=80;lb=0;
x=39;y=4;
l=13;m=13;
n=13;

getnew();
while(stack!=NULL)
{
if(stack->data==1)//node visited 1st time
{
gotoxy(x,y);
digit=0;
for(ch=ptr->data;ch>0;ch=ch/10)
{
digit++;
}
if(y<=20)
{//giving graphical view to the tree
cout<<"É";
for(ch=0;ch<digit;ch++)
{
cout<<"Í";
}
cout<<"»";
gotoxy(x,y+1);
cout<<"º"<<ptr->data<<"º";
gotoxy(x,y+2);
cout<<"È";
for(ch=0;ch<digit;ch++)
{
cout<<"Í";
}
cout<<"¼";//box end
}
else
{
gotoxy(30,24);
cout<<"can't show beyond this..";
gotoxy(27,26);
cout<<"use 'View from Node' to view more..";
}
gotoxy(1,30);
cout<<"In-order:";
gotoxy(l,30);
cout<<ptr->data<<",";
l=wherex();
stack->data++;
if(ptr->llink!=NULL)
{
x=(x+lb)/2;
y=y+4;
ub=(x-lb)*2+lb;
ptr=ptr->llink;
getnew();
pdigit=digit;
digit=0;
for(ch=ptr->data;ch>0;ch=ch/10)
{

digit++;
}
if(y<=20)
{
for(i=x+digit/2+1;i<=ub+pdigit/2;i++)
{
gotoxy(i,y-1);
cout<<"Í";
}
gotoxy(ub+pdigit/2,y-1);
cout<<"¼";
gotoxy(x+1+digit/2,y-1);
cout<<"É";
}
}
}//end of if stack->data==1

else if(stack->data==2)//node visited 2nd time
{
gotoxy(1,31);
cout<<"Pre-order:";
gotoxy(m,31);
cout<<ptr->data<<",";
m=wherex();
stack->data++;
if(ptr->rlink!=NULL)
{
x=(x+ub)/2;
y=y+4;
lb=x-(ub-x);
ptr=ptr->rlink;
getnew();
pdigit=digit;
digit=0;
for(ch=ptr->data;ch>0;ch=ch/10)
{
digit++;
}
if(y<=20)
{
for(i=lb+pdigit/2+2;i<=x+digit/2;i++)
{
gotoxy(i,y-1);
cout<<"Í";
}
gotoxy(lb+pdigit/2+1,y-1);
cout<<"È";
gotoxy(x+digit/2+1,y-1);
cout<<"»";
}
}
}//end of if stack->data==2

else//node visied 3rd time
{
gotoxy(1,32);
cout<<"Post-order:";
gotoxy(n,32);
cout<<ptr->data<<",";
n=wherex();
pptr=stack;
stack=stack->rlink;
ptr=stack->llink;
free(pptr);
if(stack->data==2)
{
x=ub;
ub=(ub-lb)+ub;
}
if(stack->data==3)
{
x=lb;
lb=x-(ub-x);
}
y=y-4;
digit=pdigit;
}//end of if stack->data==3
}//end of while stack!=NULL


gotoxy(33,37);
cout<<"Total "<<total<<" nodes";
//end of viewing the tree

o:
while(ch!=13)
{
gotoxy(1,35);
cout<<"\n\t MENU\n_____________________\n\n";
cout<<"     Insert Node\t\n     Delete Node\t\n     View from Node\t\n     EXIT/<esc>\t\t\n";
gotoxy(4,yy);
cout<<char(3);
gotoxy(21,yy);
cout<<char(3);
ch=getch();
switch(ch)
{
case 13://enter
break;

case 72://up
if(yy>39)
yy--;
break;

case 80://down
if(yy<42)
yy++;
break;

case 27:
return 0;
}//end switch
}//end while
switch(yy)
{

case 39://insert node
total++;
newnode=(tree*)malloc(sizeof(tree));
gotoxy(1,44);
cout<<"Enter the data:";
cin>>data;
newnode->data=data;
pptr=ptr=start;
if(start==NULL)
{
start=newnode;
newnode->llink=newnode->rlink=NULL;
}
else
{
while(ptr!=NULL)
{
pptr=ptr;
if(data<ptr->data)
{
ptr=ptr->llink;
}
else
{
ptr=ptr->rlink;
}
}
if(data<pptr->data)
{
pptr->llink=newnode;
}
else
{
pptr->rlink=newnode;
}
newnode->llink=newnode->rlink=NULL;
}
fflush(stdin);
break;//end of case 1



case 40://delete node
gotoxy(1,44);
cout<<"Node to be Deleted:";
cin>>data;
pptr=ptr=start;
while(1)
{
if(ptr==NULL)
{
cout<<"Node is not in the tree";
break;
}

if(data==ptr->data)
{
if(ptr->llink!=NULL||ptr->rlink!=NULL)
{
cout<<"Node is not empty..\n";
}
cout<<"Delete (Y/N) :";
ch=getche();
if(ch==110)//no
{
cout<<"\nNode is not Deleted";
break;

}
else if(ch==121)//yes
{
cout<<"\nNode deleted";
if(ptr==start)
{
total=0;
start=NULL;
free(ptr);
}
if(data<pptr->data)
{
pptr->llink=NULL;
}
else
{
pptr->rlink=NULL;
}
q:

if(start==NULL&&pdigit==10)
{
exit(0);
}
//freeing all the Nodes below
getnew();
while(stack!=NULL)
{
if(stack->data==1)
{
stack->data++;
if(ptr->llink!=NULL)
{
ptr=ptr->llink;
getnew();
}
}//end of stack->data==1

else if(stack->data==2)
{
stack->data++;
if(ptr->rlink!=NULL)
{
ptr=ptr->rlink;
getnew();
}
}//end of if stack->data==2

else
{
total--;
pptr=stack;
stack=stack->rlink;
free(ptr);
ptr=stack->llink;
free(pptr);
}//end of if stack->data==3
}//end of while stack!=NULL

if(pdigit==10)
{
exit(0);
}
}//end of delete if yes

else
cout<<"\nWrong choice..";
break;

}//end of if data found

pptr=ptr;
if(data<ptr->data)
{
ptr=ptr->llink;
}
else
{
ptr=ptr->rlink;
}
}//end of while

cout<<"\npress any key to continue..";
getch();
break;//end of case 2 deletion


case 41://view from
gotoxy(1,44);
cout<<"view from Node:";
cin>>data;
ptr=start;
while(1)
{
if(ptr==NULL)
{
cout<<"\nNode is not in the tree";
ptr=start;
cout<<"\n\npress any key to continue..";
getch();
goto p;
}

if(data==ptr->data)
{
break;
}//end of if data found
if(data<ptr->data)
{
ptr=ptr->llink;
}
else
{
ptr=ptr->rlink;
}

}//end of while
goto p;


case 42://exit
ptr=start;
pdigit=10;
goto q;//freeing all nodes before exit

}//end of switch
}//end of while
}//end of main

Comments

Popular Posts