views:

118

answers:

4

Hi again.... I'm either really stupid or my program hates me. Probably both.

Problem: I am searching through a tree to find a value that is passed. Unfortunately, it does not work. I started debugging it with prints and stuff, and what is weird is it actually finds the value... But skips the return statement!!!

    /**
  * Returns the node with the passed value
  */
 private TreeNode searchNodeBeingDeleted(Comparable c, TreeNode node)
 {  
  if(node == null) 
  {
   return null;
  }

  if(c.equals((Comparable)node.getValue()))
  {
   System.out.println("Here");
   return node;
  }
  else
  {
   if(node.getLeft() != null)
   {
    System.out.println("left");
    searchNodeBeingDeleted(c, node.getLeft());
   }
   if(node.getRight() != null)
   {
    System.out.println("right");
    searchNodeBeingDeleted(c, node.getRight());
   }
  }
  return null; //i think this gives me my null pointer at bottom
 }

It prints out the results as follows:

left
left
right
right
Here
right
left
right
left
right
Exception in thread "main" java.lang.NullPointerException
at Program_14.Driver.main(Driver.java:29)

I dont know if this will help, but here is my tree:

     L
   /   \
  D     R
 / \   / \
A   F M   U
 \       / \
  B     T   V

Thanks for your time :)

+1  A: 

I think you must return the value of searchNodeBeingDeleted(c, node.getLeft()) and searchNodeBeingDeleted(c, node.getRight()), not just call those methods.

True Soft
actually you need to return it only if it is not null
Thirler
@Thirler, you're right.
True Soft
so i return both those statements, but i still get the null pointer with the following output: left, left, right, null errorit stops at the right node, but seems to return null when it finds it...
JavaFail
Maybe you call the method twice, which is wrong. You should take the value in a variable like this: `TreeNode n = searchNodeBeingDeleted(c, node.getLeft());` then you test for null: `if (n != null) {return n;}`
True Soft
+1  A: 

You are using recursion in your function. The 'here' you see is the result of a function call that has been created from the same function. So it will return a value to the 'recursing' function, at this point you aren't done yet, even though you have found the answer, you still need to keep propagating it upwards.

Thirler
+2  A: 

Try this:

private TreeNode searchNodeBeingDeleted(Comparable c, TreeNode node)
 {  
  if(node == null) 
  {
   return null;
  }

  if(c.equals((Comparable)node.getValue()))
  {
   System.out.println("Here");
   return node;
  }
  else
  {
   if(node.getLeft() != null)
   {
    System.out.println("left");
    TreeNode n = searchNodeBeingDeleted(c, node.getLeft());
    if (n != null) {
      return n;
    }
   }
   if(node.getRight() != null)
   {
    System.out.println("right");
    TreeNode n = searchNodeBeingDeleted(c, node.getRight());
    if (n != null) {
      return n;
    }
   }
  }
  return null; //i think this gives me my null pointer at bottom
 }
nanda
thanks, this worked... i get what your saying now :)
JavaFail
+1  A: 

Assuming your tree is a binary search tree and not a "regular" binary tree.

You should return your recursive calls and not return null at the end of your method.

Something like this:

private TreeNode searchNodeBeingDeleted(Comparable c, TreeNode node) {
    if(nodle == null) return null;
    int diff = c.compareTo((Comparable)node.getValue());
    if (diff == 0) { // yes, we found a match!
        System.out.println("Here");
        return node;
    }
    else if (diff < 0) { // traverse to the left
        System.out.println("left");
        return searchNodeBeingDeleted(c, node.getLeft());
    }
    else {  // traverse to the right
        System.out.println("right");
        return searchNodeBeingDeleted(c, node.getRight());
    }
}
Bart Kiers
-1: Nowhere does it say that it is a binary search tree.
Moron
@moron, no it does not, but the 10 nodes in the OP's example ARE sorted. Could be a coincidence of course... Also, I see no reason to keep the values being stored in the tree as `Comparable` if the tree is not a BST. Nevertheless, you have a point: I added my assumption to my answer.
Bart Kiers
@Bart: Removed the -1.
Moron