简体   繁体   中英

Binary Search Tree in C++

I'm learning C++ language and I'm trying to write BST, but something goes wrong. I try to add element to empty tree, root is NULL, but after adding element root is still NULL despite of the fact that addiing was successful (I saw it in debug mode, node is set as tmp). I have no idea why it happens.

struct Node
{
    int data;
    Node* left;
    Node* right;
};

struct Tree
{
    Node* root;
};

Tree createTree()
{
    Tree tmp;
    tmp.root = NULL;
    return tmp;
}

void addToNode(Node* node, int value)
{
    Node* tmp = new Node;
    tmp->data = value;
    tmp->left = NULL;
    tmp->right = NULL;
    if(node == NULL)
        node = tmp;
    else if(value >= node->data)
        addToNode(node->right, value);
    else
        addToNode(node->left, value);
}

void add(Tree* tree, int value)
{
    addToNode(tree->root, value);
}

int _tmain(int argc, _TCHAR* argv[])
{
    Tree tree = createTree();
    add(&tree, 10);
    printf("%d", tree.root->data);
    scanf("%*s");
    return 0;
}

In the function addToNode when you assign to node , that assignment is not visible in the function calling addToNode because node is a local variable.

You should pass it as a reference instead:

void addToNode(Node*& node, int value)
{
    ...
}

When you are passing your pointer into the function, you create a local version of the pointer. This local variable ( node ) does indeed point into the same memory that the outer pointer you were passing. However, any attempt to change this variable ( not the memory it points to, but the pointer variable itself) will only change the local variable.

So your node points to the same memory location as your tree , but the node variable itself isn't equal to the tree variable, so your changes are not visible from the outer function.

It sounds complicated, sorry, but it's exacly the same thing as in this:

void foo( int a )
{
    a++;
}
int main()
{
    int var = 5;
    foo( var );
    std::cout << var;
}

Of course in this case the var will not change, it's the a that is being changed inside the function.

To fix the issue, pass a reference to the pointer instead of the pointer itself:

void addToNode(Node*& node, int value)

Joachim already beat me to the answer, but I'll add this observation in anyway.

Your code leaks memory.

void addToNode(Node* node, int value)
{
    Node* tmp = new Node;
    tmp->data = value;
    tmp->left = NULL;
    tmp->right = NULL;
    if(node == NULL)
        node = tmp;
    else if(value >= node->data)
        addToNode(node->right, value);
    else
        addToNode(node->left, value);
}

Every call to addToNode creates a new Node instance in tmp , but if the parameter Node* node is not NULL , this new Node is not deleted and does not become accessible by the rest of the application.

There are a number of ways to avoid this. The simplest would be to check if node is NULL before creating a new instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM