I am trying to build a a binary search tree. But I am not getting the correct output when performing the different traversals.
typedef struct binary_search_tree{
struct binary_search_tree *lchild;
int data;
struct binary_search_tree *rchild;
}bst_t;
#define ALLOCATE (bst_t*)malloc(sizeof(bst_t))
Here is the insert function:
void insert(bst_t *ptr,int data){
if( ptr->data < data){
if ( ptr->lchild == NULL ){
ptr->lchild = ALLOCATE;
ptr->lchild->data = data;
return;
}else
insert(ptr->lchild,data);
}else{
if ( ptr->rchild == NULL ){
ptr->rchild = ALLOCATE;
ptr->rchild->data = data;
return;
}else
insert(ptr->rchild,data);
}
}
Is this function correct? I am sending the address of root while calling that function.