previous | start | next

The sizeAtHeight method

The sizeAtHeight method seems similar to the sizeAtDepth method, but the problem is we can't calculate the height of each node as we go down the tree, only on the way back up. Why?

We could call the recursive private height(Node t) method at every Node, but this would be very inefficient and add to the recursive calls of the private sizeAtHeight.

For the size and remove methods whose condition involves the height, an efficient implementation can be achieved by using an extra member of the Node to store its height. This avoids the recursive calls to height(Node t).



previous | start | next