Binary Tree

[code]
/*binary tree*/

#includev
#include
#define MAX 100

struct node
{
int data;
struct node *left,*right;
}*b,*root=NULL,*x,*t;

struct stack
{
int top;
**arr[MAX];
}s;

void setright (struct node *,struct node *);
void setleft (struct node *,struct node *);

int count=0;
void main()
{
int i,n,fin;
char u;
clrscr();
do
{
printf (“\nEnter operation to be carried out :”);
printf (“\nADD \ DISPLAY \ EXIT :”);
scanf (“%d”,&n);
switch (n)
{
case 1:{
count++;
if (count==1)
{
printf (“\nEnter root :”);
scanf (“%d”,&i);
root=(struct node *)malloc(sizeof (struct node));
root->left=NULL;
root->right=NULL;
root->data=i;
t=root;
}
else
{
printf (“\nEnter subtree :”);
scanf (“%d”,&i);
b=(struct node *)malloc(sizeof (struct node));
b->left=NULL;
b->right=NULL;
b->data=i;
if (b->data>=t->data)
setright(b,t);
if (b->datadata)
setleft(b,t);
}
break;
}
case 2:{
if (count==0)
printf (“\nTree is empty.”);
else
{
s.top=0;
x=root;
fin=0;
do
{
while (x!=NULL)
{
s.top++;
s.arr[s.top]=x;
x=x->left;
}
if (s.top!=0)
{
x=s.arr[s.top];
s.top–;
printf (” %d “,x->data);
x=x->right;
}
else
fin=1;
}while (fin==0);
}
break;
}
case 3:{
exit (0);
break;
}
default:{
printf (“\nInvalid Input.”);
break;
}
}
printf (“\nDo u want to continue? :”);
u=getche();
}while (u==’y’ || u==’Y’);
getch();
}

void setleft (struct node *b,struct node *t)
{
while (b->datadata && t->left!=NULL)
t=t->left;
if (b->datadata && t->left==NULL)
t->left=b;
}

void setright (struct node *b,struct node *t)
{
while (b->data>=t->data && t->right!=NULL)
t=t->right;
if (b->data>=t->data && t->right==NULL)
t->right=b;
}
[/code]

OUTPUT:

Enter operation to be carried out :
ADD DISPLAY EXIT :1

Enter root :5

Do u want to continue? :y
Enter operation to be carried out :
ADD DISPLAY EXIT :1

Enter subtree :3

Do u want to continue? :y
Enter operation to be carried out :
ADD DISPLAY EXIT :1

Enter subtree :7

Do u want to continue? :y
Enter operation to be carried out :
ADD DISPLAY EXIT :1

Enter subtree :1

Do u want to continue? :y
Enter operation to be carried out :
ADD DISPLAY EXIT :2
1 3 5 7
Do u want to continue? :y
Enter operation to be carried out :
ADD DISPLAY EXIT :1

Enter subtree :8

Do u want to continue? :y
Enter operation to be carried out :
ADD DISPLAY EXIT :2
1 3 5 7 8
Do u want to continue? :n

Binary Tree C Code
Title: Binary Tree C Code (256 clicks)
Caption:
Filename: binarytree.zip
Size: 4 KB

Binary Tree C Code
Title: Binary Tree C Code (256 clicks)
Caption:
Filename: binarytree.zip
Size: 4 KB