I have a binary tree and I am having problems with one function. It is my add function where I add another node to my tree. I use recursion to call it from within the function. I believe I am not using the correct syntax to call the node from within but I cannot understand why this is not working. I think I may be getting confused with how I am calling this.

Here is the function in question.

bool BinarySearchTree::addNode(METADATA **current_node, METADATA *new_node)
{
    if(*current_node == NULL)
    {
        *current_node = new_node;
        size++;
        return true;
    }
    else
    {
        if(strcmp(new_node->key, (*current_node)->key) < 0)
        {
            return addNode(&((current_node->left), new_node);
        }
        else if(strcmp(new_node->key, (*current_node)->key) > 0)
        {
            return addNode(&((*current_node->right), new_node);
        }
        else
        {
            delete new_node;
            return false;
        }
    }
}

Both of the recursion calls are getting the following errors

error C2227: left of '->left' must point to class/struct/union/generic type
error C2143: syntax error : missing ')' before ';'
Error 7 error C2660: 'BinarySearchTree::addNode' : function does not take 1 arguments

It seams like it does not like the left but this is defined in my structure

typedef struct METADATA
{
    struct METADATA(char *key, char *value)
    {
        strcpy(this->key, key);
        strcpy(this->value, value);
        left = NULL;
        right = NULL;
    }
    char key[SIZE_KEY];
    char value[SIZE_VALUE];
    struct METADATA* left;
    struct METADATA* right;
}METADATA;

Any help in understanding why my recursion call is not working is appreciated.

Recommended Answers

All 2 Replies

Your bracing and indirection are off. To get the address of the links, you need to first dereference current_node. Then you need to do a member access using the arrow operator to get the link, then you need to take the address of that result:

&( *current_node )->left

Or with full parens:

&( ( *current_node )->left )

Thanks, i figured it was something with my bracing but could not get it to work.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.