Thursday, 30 October 2014

Tree

int u=0;
int n=1;
tree(int a[],int length,int nodes)
{
printf("Enter the left subnode for root %d\n",a[u]);
scanf("%d",&a[(2*u)+1]);
if(a[(2*u)+1]!=-1){n++;}
printf("Enter the right subnode for root %d\n",a[u]);
scanf("%d",&a[(2*u)+2]);
if(a[(2*u)+2]!=-1){n++;}
u++;
if(n<nodes){tree(a,length,nodes);}
}
main()
{
int n;
printf("Enter the number of nodes\n");
scanf("%d",&n);
int l=(2*n)+1;
int a[l];
int k=0;
while(k<l)
{
a[k]=-1;
k++;
}
printf("Enter the root\n");
scanf("%d",&a[0]);
tree(a,l,n);
k=0;
while(k<l)
{
printf("%d",a[k]);
k++;
}


}

No comments:

Post a Comment