0

I am working with an organized BST in my java code. This function/method is supposed to search the tree for a node with a specific value and let the user know whether or not it exists.

    void search(int item, Node root, int r, int c) {

        //if the integer is found
        if(root.val == item) {
            System.out.println("integer located at row: " + r + " & child: " + c + "\n");
        }

        //if the integer is not found (use the closest value to find it)
        else if(root != null) {
            if(item < root.val) 
                search(item, root.left, r + 1, (c * 2) - 1);
            else
                search(item, root.right, r + 1, c * 2);
        }

        //if the root is a null (it doesn't exist or cannot be found)
        else {
            System.out.println("integer cannot be located\n");
        }
    }

The problem is the else statement at the end. My compiler says that anything in that else statement is dead code, meaning it isn't used. However, I need the code in that else statement in the event that the function does encounter a null and cannot find the node with the assigned value. It goes away if I change the second else statement to else if(root.val != item && root != null) but it leaves me to wonder if there is a point where root won't equal null I know that it should be a possibility. Is the else statement really dead code and if it is, how can I change that?

  • 2
    The `else if (root != null)` is fishy. In the `if`, `root.val` is accessed. If `root` were `null`, a `NullPointerException` would be thrown. Thus if the `else if` is reached, it will always be entered. Thus the final `else` will never be entered. I would assume that the outer `if` should be the first `if` in the outer `else if`, and the outer `else if` should be an `if`. – Turing85 Feb 08 '20 at 22:08
  • If root is null, referencing root.val will throw an exception in the first if. The non-null check in the first else is thus unnecessary. When encountering problems like this the first assumption must never be "the compiler used by millions must be wrong." The problem is always in your code. – Torben Feb 08 '20 at 22:09
  • In short, the compiler is quite smart. – MC Emperor Feb 09 '20 at 00:38

2 Answers2

1

It's dead code because the dereference of root in root.val requires root to be non-null. If it was null you would get a NullPointerException.

In my IDE it is a warning; the code is syntactically correct, but semantically speaking, the final else will never be entered.

To solve this, check for null in the if statement first:

void search(int item, Node root, int r, int c) {
    if (root == null) {
        // if the root is a null (it doesn't exist or cannot be found)

        System.out.println("integer cannot be located\n");
    } else if (root.val == item) {
        // if the integer is found

        System.out.println("integer located at row: " + r + " & child: " + c + "\n");
    } else if (item < root.val) {
        // if the integer is not found (use the closest value to the left to find it)

        search(item, root.left, r + 1, (c * 2) - 1);
    } else {
        // if the integer is not found (use the closest value to the right find it)

        search(item, root.right, r + 1, c * 2);
    }
}

Note that you may be able to change the first two ifs in such a way that they directly return or stop execution of the method. Then the check item < root.val doesn't have to be within an else block. The more shallow your if statements the better (but always use braces for each block!).

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
0

Your first if expression:

if (root.val == item)

will either throw a NullPointerException if root is null or perform the comparison if not. Hence the final else block can never be executed.

You could try re-ordering your code:

void search(int item, Node root, int r, int c) {
    if (root != null) {
       if (root.val == item) {
           System.out.println("integer located at row: " + r + " & child: " + c + "\n");
       } else if (item < root.val) {
            search(item, root.left, r + 1, (c * 2) - 1);
       } else {
            search(item, root.right, r + 1, c * 2);
       }
    } else {
       System.out.println("integer cannot be located\n");
    }
 }
dave
  • 11,641
  • 5
  • 47
  • 65