3

Using Java, is it possible to write a recursive method to find an element in a binary search tree? I say no because of the nature of recursive re-tracing back unless I implemented incorrectly? I have been searching the internet and all i can find is an iterative version. Here is my method:

public boolean findValueRecursively(BSTNode node, int value){
   boolean isFound = false;
   BSTNode currentNode = node;

   if (value == currentNode.getData()){
      isFound = true;
      return isFound;
   } else if (value < currentNode.getData()){
      findValueRecursively(currentNode.getLeftNode(), value);
   } else{
      findValueRecursively(currentNode.getRightNode(), value);
   }

  return isFound;
}

// Node data structure
public class BSTNode
{
    private BSTNode leftNode;
    private BSTNode rightNode;
    private int data;
    public BSTNode(int value, BSTNode left, BSTNode right){
       this.leftNode = left;
       this.rightNode = right;
       this.data = value;
    }
}



public static void main(String[] args){
    BST bst = new BST();
    // initialize the root node
    BSTNode bstNode = new BSTNode(4, null, null);
    bst.insert(bstNode, 2);
    bst.insert(bstNode, 5);
    bst.insert(bstNode, 6);
    bst.insert(bstNode, 1);
    bst.insert(bstNode, 3); 
    bst.insert(bstNode, 7);
    if (bst.findValueRecursively(bstNode, 7)){
        System.out.println("element is found! ");
    } else{
        System.out.println("element is not found!");
    }
 }

I get the print as "element is not found".

Any help/tips or suggestions, more than welcome.

Thanks in advance!

Allen
  • 181
  • 1
  • 4
  • 16

4 Answers4

9

A recursive version:

public boolean findValueRecursively(Node node, int value){
        if(node == null) return false;
        return 
                node.data == value ||
                findValueRecursively(leftNode, value) ||
                findValueRecursively(rightNode, value);
    }
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Is it efficient when we use a Huge Tree with thousands of node ? – Kamel BOUYACOUB Feb 24 '18 at 16:26
  • @KamelBOUYACOUB since you touch any node once at most, I'd say that yes - it is as efficient as it gets. Pay attention that basically this is a DFS scanning of the tree starting from the root node and going down. – Nir Alfasi Feb 24 '18 at 18:39
3

A recursive version that returns a reference to the node found:

public BinaryNode find(BinaryNode node, int value) {
    // Finds the node that contains the value and returns a reference to the node.
    // Returns null if value does not exist in the tree.                
    if (node == null) return null;
    if (node.data == value) {
        return node;
    } else {
        BinaryNode left = find(node.leftChild, value);
        BinaryNode right = find(node.rightChild, value);
        if (left != null) {
            return left;
        }else {
            return right;
        }   
    }
}
hguochen
  • 168
  • 6
0

I believe your isFound = false; is what is always getting returned.

It should be like this:

isFound= findValueRecursively(currentNode.getLeftNode(), value);
DavidPostill
  • 7,734
  • 9
  • 41
  • 60
0
    public TreeNode<E> binarySearchTree(TreeNode<E> node, E data){
    if(node != null) {
        int side = node.getData().compareTo(data);
        if(side == 0) return node;
        else if(side < 0) return binarySearchTree(node.getRightChild(), data);
        else if(side > 0 ) return binarySearchTree(node.getLeftChild(), data);
    }

    return null;
}

That will return a reference to the node, which is a little more useful IRL. You can change it to return a boolean though.