简体   繁体   中英

Pointer pass by a pointer doesn't work

//binary_tree.h file
typedef struct node node;

struct node
{   node():left(0), right(0), value(-1){};
    ~node(){if(left) delete left; if(right) delete right;};
    node *left;
    node *right;
    int value;
};

inline void insert_node(node **root, node *new_node)
{
    assert(new_node != NULL);
    if(*root == NULL)
    {
        *root = new_node;
    }
    else
    {
        node *itr = *root;
        while(1)
        {
            if(itr->value > new_node->value)
                itr = itr->left;
            else
                itr = itr->right;
            if(!itr)
            {
                itr = new_node;
                break;
            }
        }
    }
}

inline void inorder_print(node *root)
{
    if(!root) return;
    inorder_print(root->left);
    printf("%d\n", root->value);
    inorder_print(root->right);
}

//main.cpp file
#include "binary_tree.h"

int main()
{
    node *node1 = new node();
    node *node2 = new node();
    node *node3 = new node();
    node *node4 = new node();
    node *node5 = new node();

    node1->value = 5;
    node2->value = 10;
    node3->value = 3;
    node4->value = 1;
    node5->value = 4;   

    node *binary_tree = NULL;

    insert_node(&binary_tree, node1);
    insert_node(&binary_tree, node2);
    insert_node(&binary_tree, node3);
    insert_node(&binary_tree, node4);
    insert_node(&binary_tree, node5);

    assert(binary_tree != NULL);
    inorder_print(binary_tree);

    return 0;
}

I have a very simple program and I want to create a binary tree and print the tree. Hoever the code segment shown below doesn't change the tree structure.

        node *itr = *root;
        while(1)
        {
            if(itr->value > new_node->value)
                itr = itr->left;
            else
                itr = itr->right;
            if(!itr)
            {
                itr = new_node;
                break;
            }
        }

inorder_print function always prints '5'

The problem is to use 'itr' variable. I don't really see how I could do this without using a local variable nor changing the pointer to the root.

Your insertion routine will only insert a node into the root.

        if(!itr)
        {
            itr = new_node;
            break;
        }

Since itr is a local variable, new_node did not actually get inserted. You can correct this by making itr a pointer to a pointer like root.

    node **itr = root;
    while(1)
    {
        if((*itr)->value > new_node->value)
            itr = &(*itr)->left;
        else
            itr = &(*itr)->right;
        if(!*itr)
        {
            *itr = new_node;
            break;
        }
    }

Use std::set. Your code seems to be old C code. Be C++:

#include <set>
#include <iostream>

int main()
{
    std::set<int> binary_tree;
    binary_tree.insert(5);
    binary_tree.insert(10);
    binary_tree.insert(3);
    binary_tree.insert(1);
    binary_tree.insert(4);   

    //inorder_print(binary_tree);
    for (std::set<int> i = binary_tree.begin(); i != binary_tree.end(); ++i)
        std::cout << *i  << std::endl;
}

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