0

I am attempting to implement a findNode method as part of a binary search tree in java.

public Node findNode(int findkey){
    Node tempNode5 = root;  

    while(findkey != tempNode5.key){
        if(tempNode5 != null){ // if the tempNode5 is not null
                               // and the node to be found hasn't been found yet
                               // then we will go to the leftChild or rightChild
                               // depending on the key
            if(findkey < tempNode5.key){
                tempNode5 = tempNode5.leftChild;
            }
            else tempNode5 = tempNode5.rightChild;
        }
        else return null; // this is the line that Eclipse marks "dead code"
                          // if the tempNode5 reaches null, it means the node to be found
                          // was not found in the BST, in which case I want to return null
    }
    return tempNode5; // if the while loop has exited and reached this line it means that 
                      // the node *has* been found, so I want to return it

}

Eclipse marks the line "else return null;" as dead code. I need to deal with the case that the node is not found in the tree, otherwise the while loop will run forever, so I need something similar that will return null when the node is not found.

Any help is appreciated :)

Zubin Mukerjee
  • 166
  • 2
  • 11

1 Answers1

3

Have a look at these lines:

while(findkey != tempNode5.key){
        if(tempNode5 != null){

If tempNode5 is null, while will throw a NullPointerException and hence, any subsequent line won't be executed. So, control will only enter the while loop if tempNode5 is not null, making if..else redundant.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Thank you. So, the entire findNode method will return a `NullPointerException` if `tempNode5` is null? Could I get around this by changing the condition of the while loop to `while((findkey != tempNode5.key) && (tempNode5 != null))` ? Then the `return tempNode5;` would either return the node after it was found, or `null` . – Zubin Mukerjee May 07 '17 at 21:07
  • 1
    @ZubinMukerjee It needs to be the other away around: `while((tempNode5 != null) && (findkey != tempNode5.key))` – Michael May 07 '17 at 21:08
  • @Michael Is that because there would be a `NullPointerException`? Why wouldn't there be one if you switch them? – Zubin Mukerjee May 07 '17 at 21:10
  • 1
    @ZubinMukerjee If you try to dereference the node, which happens when you do anything with the dot operator i.e. `tempNode5.key` you will get a null pointer exception if the reference is null. Therefore there is no reason to check it second because if it *is* in fact null, any exception will have already been thrown. You need to check it first. – Michael May 07 '17 at 21:12
  • For an && statement, don't both conditions need to be checked regardless? And so won't the null pointer exception happen even if the order is the right way? Edit: I just tried it both ways, and you are definitely right, there is no null pointer exception when it is switched ... I still don't really understand why, though :( – Zubin Mukerjee May 07 '17 at 21:18
  • 1
    @ZubinMukerjee `&&` is short circuit operator, meaning second condition won't be executed if first is `false`. If you use single `&`, both conditions will be executed. – Darshan Mehta May 07 '17 at 21:20
  • @DarshanMehta I didn't know that! Thank you both :) – Zubin Mukerjee May 07 '17 at 21:20