Finding a Key in aBinary Search Tree
To recursively search for a target in a subtree (starting at root):
- If the subtree is empty, return failure (NULL)
- Otherwise:
- If the target is < the key of the subtree root, recursively search the left subtree for the target and return the result value
- Otherwise, if the target is > the key of the subtree root, recursively search the right subtree for the target and return the result value
- Otherwise, the target must be == to the key of the subtree root; return the pointer to the subtree root
The only recursion is tail recursion; an iterative solution is:
- While subtree is not empty and the subtree key is != target
- If target < subtree key, set subtree to left child
- Otherwise, set subtree to right child
- Return final subtree pointer