CJCoding With Joseph
15free / daySign inCreate account

Search a BST for a Value

Complete contains(int value) so it returns true when value is stored in the tree and false when it is not.

Reminder: why a BST is fast to search. A BST starts at its root, and each node holds a value and up to two children, left and right. Smaller values always sit in a node's left subtree and larger values in its right subtree. So each comparison tells you which ONE side the value could be on, and you never have to look at the other side.

        50
       /  \
     30    70
    /  \     \
  20    40    80

To look for 40: 40 is smaller than 50, so go left to 30. 40 is larger than 30, so go right, and there it is. To look for 35: left to 30, right to 40, then left to an empty spot, so 35 is not in the tree. Each search follows one path from the top down.

Requirements: 1. Start at root and use a loop that moves one node down per step. 2. Return true as soon as the current node holds value. 3. Move to the left child when value is smaller than the current node's value, and to the right child when it is larger. 4. Return false when you step past the bottom of the tree (the current node becomes null). 5. Do not change the tree.

The Node class is provided - do not rename it and do not add public to it, because a Java source file may contain only one public class.

Do NOT write a main method.

Expected Output:

true
true
true
false
false

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsBinary Search Trees
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.