
We must use the correct links to traverse the nodes in the BST.


Node curr_node; // Help variable to perform the tree traversal curr_node = root; // Start at the root node 
Result:
(Note: I abbreviated the name of the auxiliary variable in all the figures to save space)

curr_node = curr_node.left; 
will change the current node to the

Example:

curr_node = curr_node.right; 
will change the current node to the

Example:

/* =========================================================== findNode(x): return the reference to a Node that contain the value x (if x is found in the BST) returns "null" if x is not found =========================================================== */ Node findNode( double x ) { Node curr_node; // Help variable curr_node = root; // start searching at the root node while ( curr_node has not reached the "end" of the tree ) { if ( x is equal to curr_node.value ) { return curr_node; // Found ! } else if ( x < curr_node.value ) { search further in the left tree } else // This must be true: ( x > curr_node.value ) { search further in the right tree } } /* ============================================ When program reaches here, x is NOT in BST =========================================== */ return null; // Return not found } 
public Node findNode(double x) { Node curr_node; // Help variable /*  Find the node with search value == "x" in the BST  */ curr_node = root; // Always start at the root node while ( curr_node != null ) { if ( x == curr_node.value ) { // Found search value in BST return curr_node; } else if ( x < curr_node.value ) { curr_node = curr_node.left; // Continue search in left subtree } else // This must be true: ( x > curr_node.value ) { curr_node = curr_node.right; // Continue search in right subtree } } /* ====================================== When we reach here, x is NOT in BST ====================================== */ return null; // Return not found } 


Here is an augmented version of the findNode() method that records the previous node of the current node:
public Node myParent; // Used by findNode() to save the previous node public Node findNode(double x) { Node curr_node; // Points to the current node Node prev_node; // Points to the previous node /*  Find the node with search value == "x" in the BST  */ curr_node = root; // Always start at the root node prev_node = root; // Remember the previous node for insertion while ( curr_node != null ) { if ( x == curr_node.value ) { // Found search value in BST myParent = prev_node; // Save in myParent return curr_node; } else if ( x < curr_node.value ) { prev_node = curr_node; // Remember prev. node curr_node = curr_node.left; // Continue search in left subtree } else // This must be true: ( x > curr_node.value ) { prev_node = curr_node; // Remember prev. node curr_node = curr_node.right; // Continue search in right subtree } } /* ====================================== When we reach here, x is NOT in BST ====================================== */ myParent = prev_node; // Save in myParent return null; // Return not found } 

Example: find the node with the value 7:


Example: find the node with the value 10:
