Sam12
Sam12

Reputation: 1805

Recursion reaches a non-void, binary search tree

I get the warning that it reaches an end of non-void function. It's supposed to search in the binary search tree. And return SUCCESS if it found the desirable. What can I do to fix it?

TREEResult FindInBST(Node<T>* node,int key, void** value,int treeSize)
    {
        if(node!=NULL && node->key==key)
        {
            *value=node->data;
            return SUCCESS;
        }
        if(treeSize==0)
        {
            return FAILURE;
        }
        if(node->key<key)
        {   treeSize--;
            FindBST(node->rightSon,key,value,treeSize);
        }
        else
        {   treeSize--;
            FindBST(node->LeftSon,key,value,treeSize);
        }

    }

Upvotes: 0

Views: 70

Answers (1)

drescherjm
drescherjm

Reputation: 10837

You promise to return a TREEResult but don't in the two recursive calls. Thankfully the compiler is warning about this because it is Undefined Behavior to break this rule.

To fix this you need to change:

FindBST(node->rightSon,key,value,treeSize);

to

return FindBST(node->rightSon,key,value,treeSize);

and also

FindBST(node->LeftSon,key,value,treeSize);

to

return FindBST(node->LeftSon,key,value,treeSize);

As @molbdnilo mentioned in the comments there is no "magic" in recursive calls versus non recursive ones. If you're supposed to return something, you need to do that.

failure to return a value is Undefined Behavior.

The value that these recursive calls return will propagate from one of the SUCCESS or FAILURE results returned from your base case depending on if the search found or did not find the item.

Upvotes: 1

Related Questions