1
votes

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

1
If this is null, how can it have testable values for children (left and right)?Alex Reynolds

1 Answers

0
votes

In a "binary tree", an external node is a node that does not have any children, neither left or right

No. "External" here is used as an adjective. It means a node that is not part of the tree yet.

external nodes are always null

Er, a node can't "be" null. A node pointer can be null, but that just means it's not pointing to a node.

Nodes can have null pointers instead of pointers to child nodes. When it has no children (both pointers null), it's called a Leaf node.