I am trying to convert an integer array into a binary tree using C.
For example for an array a[10]={5,2,1,6,7,3,4} , the Binary tree should look like
5
/ \
2 1
/\ /\
6 7 3 4
I tried to convert using the following code
typedef struct btree
{
int value;
struct btree *left;
struct btree *right;
}Btree;
void insert(Btree *t,int *a,int index,int n)
{
t=(Btree *)malloc(sizeof(Btree));
if(index<n)
{
t->value=a[index];
insert(t->left,a,2*index,n);
insert(t->right,a,2*index+1,n);
}
}
int main(void) {
int a[100],i;
Btree *t;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
insert(t,a,0,i+1);
return 0;
}
Can someone help me out with this problem?