Friday, April 17, 2009

B TREE

#include
#include
#include
#include
struct node
{
int a;
struct node *left;
struct node *right;
};
struct tree *create(arr,l,h)
{
//int *arr,l,h;
struct tree *t;
int m,a;
m=(l+h)/2;
if(arr==NULL)
{
printf("\n Tree does not exist");
return(t);
}
//t= malloc(sizeof(struct tree));
//struct node *left,*right;
t->a=arr[m];
if(l>=h)
{
t->left=NULL;
t->right=NULL;
return(t);
}
if(l<=m-1)
t->left=create(arr,l,m-1);
else
t->left=NULL;
if(m+1<=h)
t->right=create(arr,m+1,h);
else
t->right=NULL;
return(t);
}
void tridis(btree,level)
{
struct tree *btree;
int level,i;
if(btree!=NULL)
{
tridis(btree->right,level+1);
printf("\n");
for(i=0;iprintf(" ");
printf("\t %d",btree->a);
tridis(btree->left,level+1);
}
}
void diadis(btree,level)
{
struct tree *btree;
int level,i;
if(btree)
{
printf("\n");
for(i=0;iprintf(" ");
printf("\t %d",btree->a);
diadis(btree->left,level+1);
diadis(btree->right,level+1);
}
}
void main()
{
int a[20],i,n,ch;
struct tree *x;
clrscr();
printf("enter no of elements in a list");
scanf("%d",&n);
printf("\n enter %d elements :",n);
for(i=0;iscanf("%d",&a[i]);
x=(struct tree *)malloc(sizeof(struct tree));
x=create(a,0,n-1);
clrscr();
printf("\n Binary tree\n");
printf("\nTriangular display\n");
tridis(x,1);
printf("\n Diagonal Display\n");
diadis(x,1);
getch();
}

No comments: