does not stop the recursion from attempting to read a null pointer in root.value
private int min(Node root){
// if(root == null)
// will get a nullpointer exception without this base case?
if(root.leftNode == null && root.rightNode == null)
return root.value;
int left = min(root.leftNode);
int right = min(root.rightNode);
return Math.min(Math.min(left, right), root.value);
}
Hi, It looks like there is a bug. I realized it now on lesson 11. There we have the same base case which works just fine with the given example, but if we choose another tree that has no the same level order (left/right) we will get the same exception. It works fine with this new check you added.
Yes, I can. In our company, we decided to use them always because in this way we will avoid bugs and actually, we have just few ifs with just one line of code inside.
If the tree is empty (root is null) then there is no meaning for finding the min so it should throw an exception in my opinion. If you return any value it is impossible to distinguish between that value actually being the minimum and the empty case.
so this happens when your tree is not complete i.e
something like this ,
what would happen is that when the recursion reaches node with the value(12) it would not take into account that the right leaf is empty because of statement .
// Method to find minimum value
public int findMin() {
if (rootNode == null)
throw new IllegalStateException();
return findMin(rootNode);
}
// Private recursive method
private int findMin(Node root) {
//If a node is a leaf node, i.e it doesn't have either a left or right child
if (isLeaf(root))
return root.value;
//If a node does not have left child but has right child
if (root.leftChild == null)
return Math.min(root.rightChild.value, root.value);
//If a node does not have right child but has left child
if (root.rightChild == null)
return Math.min(root.leftChild.value, root.value);
//If a node is a parent node, i.e it has both left and right children
return Math.min(root.value,
Math.min(findMin(root.leftChild),
findMin(root.rightChild)));
}
// Method to check if a node is a leaf node
private boolean isLeaf(Node root) {
return root.leftChild == null && root.rightChild == null;
}
The Binary Tree base case in the code you provided is incorrect and you are correct that it will result in a null pointer exception. The correct base case should be:
if(root == null)
return Integer.MAX_VALUE;
This will ensure that the function returns Integer.MAX_VALUE if the root node is null, thus avoiding the null pointer exception.
In your case, this 2 if statements returning child values instead of keep diving till the leaf:
//If a node does not have left child but has right child
if (root.leftChild == null)
return Math.min(root.rightChild.value, root.value);
//If a node does not have right child but has left child
if (root.rightChild == null)
return Math.min(root.leftChild.value, root.value);
You may have node with only right child, which not necessary will be a leaf.
here is an approach I used to solve this issue:
private int min(Node root) {
int right = 0;
int left = 0;
if (isLeaf(root)) {
return root.value;
}
else if (root.leftChild == null)
right = left = min(root.rightChild);
else if (root.rightChild == null)
right = left = min(root.leftChild);
else {
left = min(root.leftChild);
right = min(root.rightChild);
}
return Math.min(Math.min(left,right), root.value);
}