I have a solution here, but I'm not too fond of it because I feel like it can be done better.
template <typename Comparable>
bool AugmentedBinarySearchTree<Comparable>::Rank(const Comparable& x, BinaryNode* t, int* nodesVisited) const {
if (t == NULL) {
return false;
}
if (Rank(x, t->m_left, nodesVisited)) {
return true;
}
*nodesVisited += 1;
if (t->m_element == x) {
return true;
}
return Rank(x, t->m_right, nodesVisited);
}
My biggest stumbling block was finding way to both confirm the node actually existed and also returning its rank.
I started with this method signature
int BinarySearchTree<Comparable>::Rank(const Comparable& x, BinaryNode* t) const {
but in the end I had to make the method return a bool and add an extra parameter to keep track to where the program was in the traversal.
Is there any way to do this without the nodesVisited parameter?
KeyError
exception. \$\endgroup\$