Threaded binary tree

#include<stdio.h>
#include<conio.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
int i=0,a[20],c=0;
struct tree *s,*t,*header;
struct tree *create();
void inorder(struct tree *);
void inorder1(struct tree *);
void header_node(struct tree *p);
void display(struct tree *p);
struct tree *create()
{
struct tree *p,*root;
int m,x;
char s;
root=(struct tree *)malloc(sizeof(struct tree));
printf("\nenter the value of the main root");
scanf("%d",&m);
root->data=m;
root->left=NULL;
root->right=NULL;
printf("\nenter n to discontinue creation of the binary search tree");
fflush(stdin);
scanf("%c",&s);
while(s!='n')
{
p=root;
printf("\nenter the value of the newnode");
fflush(stdin);
scanf("%d",&x);
while(1)
{
if(x<p->data)
{
if(p->left==NULL)
{
p->left=(struct tree *)malloc(sizeof(struct tree));
p=p->left;
p->data=x;
p->right=NULL;
p->left=NULL;
break;
}
else
p=p->left;
}
else
{
if(p->right==NULL)
{
p->right=(struct tree *)malloc(sizeof(struct tree));
p=p->right;
p->data=x;
p->right=NULL;
p->left=NULL;
break;
}
else
p=p->right;
}
}
printf("\nwant to continue");
fflush(stdin);
scanf("%c",&s);
}
return(root);
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
a[i]=p->data;
i++;
inorder(p->right);
}
}
void inorder1(struct tree *p)
{
if(p!=NULL)
{
inorder1(p->left);
if(a[c]==p->data)
s=p;
else if(a[c+1]==p->data)
{
t=p;
if(s->right==NULL)
s->right=t;
else if(t->left==NULL)
t->left=s;
s=t;
c++;
}
inorder1(p->right);
}
}
void header_node(struct tree *p)
{
struct tree *top;
int m;
top=p;
header=(struct tree *)malloc(sizeof(struct tree));
printf("\nenter the value of the header node");
scanf("%d",&m);
header->data=m;
while(p->right!=NULL)
p=p->right;
p->right=header;
p=top;
while(p->left!=NULL)
p=p->left;
p->left=header;
header->left=top;
header->right=top;
}
void display(struct tree *p)
{
char a;
while(p->right!=header)
p=p->right;
printf("\npress n to quit display");
scanf("%c",&a);
while(a!='n')
{
while(p->left!=header)
{
printf("\t%d",p->data);
p=p->left;
}
printf("\t%d\t",header->data);
while(p->right!=header)
{
printf("\t%d",p->data);
p=p->right;
}
printf("\npress n to discontinue");
scanf("%c",&a);
}
}
void main()
{
int h;
struct tree *root;
clrscr();
while(1)
{
printf("\nenter 1. for creation of the binary search tree");
printf("\nenter 2. for creation of a binary threaded tree");
printf("\nenter 3. for display");
printf("\nenter 4. for exit");
printf("\nenter your choice");
scanf("%d",&h);
switch(h)
{
case 1:
root=create();
break;
case 2:
inorder(root);
inorder1(root);
header_node(root);
break;
case 3:
display(root);
break;
case 4:
exit(0);
default:
printf("\nentered a wrong choice");
}
}
}

ABC of Electronics

Everything on Electronics

Other Links:

SACHIN TENDULKAR                                 20 20 CRICKET                                     RICKY PONTING

Google