简体   繁体   中英

Finding a node in a binary search tree in java?

I am trying to write a method that returns the depth of a target int in a binary search tree. Right now it works for some smaller trees, but not always. Does anyone see where I may be going wrong?

  static int count=1;
            public static int findDepth(TreeNode<Integer> root, int target)
            // pre: 0 or more elements in the tree, integer to search for
            // post: return depth of target if found, -1 otherwise
            {
                count++;
                if (root!=null)
                {
                    if (root.getValue()<target)
                    {
                        if (root.getValue()==target)
                        {System.out.println(count); 
                        return count;}
                        else

                        {findDepth(root.getLeft(), target);
                        count--;
                        findDepth(root.getRight(), target);
                        }
                    }
                    else if (root.getValue()>target)
                    {
                        if (root.getValue()==target)
                        {System.out.println(count); 
                        return count;}
                        else

                        {findDepth(root.getLeft(), target);
                        count--;
                        findDepth(root.getRight(), target);
                        }
                    }
                    else if (root.getValue()==target)
                        return 1;
                    else
                        return -1;
                }
                return count;
            }

There's a few things wrong.

It will never get into this case:

if (root.getValue()<target)
{
   if (root.getValue()==target)
       {System.out.println(count); 
       return count;}

Also it will never get into this case:

else if (root.getValue()>target)
{
    if (root.getValue()==target)
        {System.out.println(count); 
        return count;}

The biggest issue is that you're keeping a global static count, and recursively going through both left and right paths.

First of all, for a BST, there is no need to go both left and right.

Second, it's better to pass the count through a parameter rather than keeping a global.

Rather than fix your code, I'll post this working example for you to use as a reference:

public class Main
{
  public static void main(String[] args)
  {
    BinaryTree tree = new BinaryTree();

    tree.add(20);
    tree.add(10);
    tree.add(30);
    tree.add(15);
    tree.add(25);
    tree.add(5);
    tree.add(35);
    tree.add(1);
    tree.add(6);
    tree.add(14);
    tree.add(16);
    tree.add(24);
    tree.add(26);
    tree.add(34);
    tree.add(36);


    int level = tree.getLevel(6);

    System.out.println(level);

  }
}

public class TreeNode
{
  int data;
  TreeNode left;
  TreeNode right;

  public TreeNode(int d){
    data = d;
    left = null;
    right = null;
  }

}

public class BinaryTree
{
  TreeNode root;

  public BinaryTree(){
    root = null;
  }

  public int getLevel(int val) {
    if (root == null) return 0;
    return getLevelHelper(root, val, 0);

  }

  public int getLevelHelper(TreeNode node, int val, int level){


    int retVal = -1;

    if (node.data == val){
      return level;
    }
    if (val < node.data && node.left != null){
      retVal = getLevelHelper(node.left, val, level + 1);
    }
    else if (val > node.data && node.right != null){
      retVal = getLevelHelper(node.right, val, level + 1);
    }

    return retVal;
  }




  public boolean add(int newData){
    if (root == null){
      root = new TreeNode(newData);
      return true;
    }
    else{
      TreeNode curr = root;
      while (true){
        if (curr.data == newData){
          return false;
        }
        else if (curr.data > newData){
          if (curr.left == null){
            curr.left = new TreeNode(newData);
            return true;
          }
          else{
            curr = curr.left;
          }


        }
        else{
          if (curr.right == null){
            curr.right = new TreeNode(newData);
            return true;
          }
          else{
            curr = curr.right;
          }
        }
      }
    }

  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM