Can anyone point me to a code example (java preferably) or psuedocode that uses recursion to return a subtree that contains all nodes with keys between fromKey and toKey.
So if I was to call Tree.subtree(5,10) it should return all nodes in the BST that have keys between 5 and 10 inclusive - but I can't use loops or helper methods...only recursive calls to the subtree method, which takes fromKey and toKey as parameters.
Thanks!
Update:
I've try the following which does not work:
public Tree subtree(int from, int to) {
left.subtree(from, to);
right.subtree(from, to);
if (this.key < from || this.key > to) {
this.delete(this.key);
}
return this;
}
I think the problem with this is that it returns too early. What I'm trying to do is traverse every node on the tree, and delete any that don't fall within the range. Am I on the right track?