Recursion with AVL trees, function minimization

Viewed 72

I have made these two functions that find if any AVL tree contains a string that starts with 'a':

int check2( int x, AVLNode T )
{
    if(x == 1)
        return 0;

    if( T != NULL )
    {
        x += check2( x, T->Right );
        if(T->Word[0] == 'a')
            x++;
        x += check2( x, T->Left );
    }
    return x;
}


int check( AVLNode T )
{
    return check2( 0, T );
}

where AVL is here

typedef struct node* AVLNode;

    struct node
    {
        char Word[MAX_WORD_SIZE];
        AVLNode Left;
        AVLNode Right;
    };

Everything works perfectly fine, but I'm using check(T); only to call check2(0, T); and pass 0 to it, is there any way to use check() only, instead of calling a function that calls another function only to pass 0 initially?

1 Answers

Because C dont have default arguments like in C++

int check2(AVLNode T, int x = 0)
{
//...
}

And not function overloading

int check2(int x, AVLNode T)
{
//...
}
int check2(AVLNode T)
{
  return check2(0, T)
}

You have two options: use C++ or use ugly define

Ugly define:

#define check2(n) check2(0, n)
int main() {
    AVLNode node;
    check2(node); // Expands to check2(0, node)
}
Related