next up previous index
Next: contains Up: EmptyTree and NodeTree implementations Previous: isEmpty   Index

nrNodes

The nrNodes method is simple for the EmptyTree class. An empty tree has no nodes, so we have
public int nrNodes() {
    return 0;
}
In the NodeTree class, however, we implement the nrNodes method recursively as follows:
public int nrNodes() {
    return left.nrNodes() + right.nrNodes() + 1;
}
In other words, the number of nodes in a NodeTree is one more than the sum of the numbers of nodes in its left and right subtrees.



Peter Williams 2005-06-07