I'm writing a function that finds out the total number of items in a AVL tree by range. For example, the arguments that passed in is "ab" and "au", then I need to find out how many items they are in an AVL tree is in that range.
Currently my way of doing this is to traverse the tree every time when the client calls it. But because the number of items in my AVL tree is vary large, it takes forever if the client calls this function too many times. Is there a faster way to do that?
My range function:
void range(AvlTree T, char* k1, char* k2) {
if ( T == NULL )
return;
if ( strcmp(k1, T->Element) < 0 )
range(T->Left, k1, k2);
if ( strcmp(k1, T->Element) <= 0 && strcmp(k2, T->Element) >= 0 )
total++;
if ( strcmp(k2, T->Element) > 0 )
range(T->Right, k1, k2);
}