In a "binary tree", an external node is a node that does not have any children, neither left or right, -correct me if I'm wrong-, and in a "binary search tree", an external nodes are always null, because according to my lecture notes, an internal nodes always have 2 children even if not created yet we assume children of that internal node are null. So how can I access an external node if it's null?
I wrote this code as part of BST Node class :
/*
* Checks if this node is an internal node.
* Returns true if it is internal node, false otherwise.
*/
protected boolean isInternal(){
// TODO Put your code here
if(this!=null)
return true;
else
return false;
}
/*
* Checks if this node is an external node.
* Returns true if it is external node, false otherwise.
*/
protected boolean isExternal(){
if(this==null && this.left==null && this.right==null)
return true;
else return false;
}
Last method give me nullPointerException
this
isnull
, how can it have testable values for children (left
andright
)? – Alex Reynolds