Insert an object in BST in C++

I want to insert an object of created type (Fruit) in BST, but at the point of code to be inserted when (fruit==null) i think there is a problem i cant solve, because after inserting objects the root still null(no any children) .

Fruit* insert(Fruit* fruit,Fruit f){
    if(fruit == NULL){
        Fruit ftemp = *new Fruit() ;
        ftemp.setWeight(f.getWeight()) ;
        ftemp.setName(f.getName()) ;
        return &ftemp  ;
    if(f.getWeight() < fruit->getWeight() ){
        fruit->left = insert(fruit->left,f);
        fruit->right = insert(fruit->right,f);
    return fruit ;


Read more here:

Content Attribution

This content was originally published by Omar Ashraf at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: