In your insertion function you are not setting leftChild
and rightChild
to NULL.
void BST::insert(int value) {
Node* temp = new Node();
temp->data = value;
temp->leftChild = NULL;
temp->rightChild = NULL;
if(root == NULL) {
root = temp;
return;
}
Also, I cannot be sure (as you have not posted the constructor for BST
) but you may not have set root to NULL in the BST
constructor. Try with these modifications.
Seems like you do not have a constructor in BST from what you have posted:
struct BST {
Node* root;
void insert(int value);
BST(): root(NULL) { } // add a constructor to initialize root to NULL
};