Some of the size methods ask you to count the number of nodes satisfying some property. Some of the remove methods ask you to remove the subtree at nodes satisfying some property. These properties include:
- whether the node contains an odd integer key
- size of the left and right subtrees
- height of the node
- depth of the node
In the case of the height and depth, the public method has an int parameter. E.g.
public int sizeAtDepth(int k)
This should return the number of nodes whose depth is k.
What parameters should the recursive private sizeAtDepth method have? It always needs 1 more, e.g. Node.
The depth of the root is 0 and the depth of a child is 1 more than its parent. So the depth of nodes can be determined as you go down the tree.
Recalling the principles
-
code before the recursive calls correspond to actions as you go down the tree.
-
code after the recursive calls return correspond to actions as you go back up the tree.
It is easy to pass an additional parameter to the private method that gives the depth of the subtree at the Node parameter:
/** * * @param t the subtree * @param k the required depth * @param d the depth of node t * @return the number of nodes in the subtree at t that * have depth k private int sizeAtDepth(Node t, int k, int d)
What does the public method pass to the private method for these three parameters?