I am trying to create a binary tree with the array as defined in my function.
I then then want to print its elements in inorder fashion (root, left, right)
The following code compiles but gives no input (NULL)
Please tell me where am I going wrong
struct BST{
int data;
struct BST *left;
struct BST *right;
};
struct BST *in(int data, struct BST *p1, struct BST *p2);
struct BST *create_BST(int a[], int i, int size);
void inorder(struct BST *tree);
int main(void){
struct BST *root;
int a[]={7, 10, 14, 5, 9, 11, 4, 13, 6};
root=create_BST(a,0,9);
printf("\nPrinting Tree:\t");
inorder(root);
return 0;
}
void inorder(struct BST *root){
if(root!=NULL){
inorder(root->left);
printf("%d",root->data);
inorder(root->right);
}
}
struct BST *in(int data, struct BST *p1, struct BST *p2){
struct BST *t;
t=(struct BST*)malloc(sizeof(struct BST));
t->data=data;
t->left=p1;
t->left=p2;
return t;
}
struct BST *create_BST(int a[], int i, int size){
if(i>=size){
return NULL;
}else{
return(in(a[i],create_BST(a,2*i + 1,size),
create_BST(a,2*i + 2,size)));
}
}
printf("%c", root->data);but this prints characters; small integers print as funky characters. You probably mean%dto print the numbers. Recommend adding a space before as well:printf(" %d", root->data);Printing Tree: 4147in output, which is wrong2*i + 1and2*i + 2doing?{1,2}, {3,4}, {7,8}?? Possible Duplicate Inserting elements in a BSTa[2*i+1] and a[2*i +2]become left and right child ofa[i]