I don't understand why I get double pointers with C binary tree insertion/deletion parameters

Asked 1 years ago, Updated 1 years ago, 130 views

I think it's enough to receive a single pointer with the C binary tree insertion/deletion parameter It's hard to understand why a book or a few blogs receive double pointers as parameters.

The following is the insertion code, but the deletion is long, so I will just upload the insertion.

typedef struct Treenode {
    int key; // Value to enter node in tree
    structure Treenode *left, *right; // pointer to point to two child nodes of that node
} } Treenode;

// Change parameters from bottom to Treenode* root, //
void insert_treenode(Treenode **root,  int key) 
{
    Treenode
    *p = NULL, //parent node, NULL initialization
    *t = *root, // change to t = root, //
    *n; //New node

    //Check if the key value you want to insert already exists in the existing tree
    while (t != NULL)
    {
        If (key == t->key) return; // exit function if it already exists
        p = t;
        if (key < p->key)
            t = p->left;
        else
            t = p->right;
    } //If you've come this far, there's no duplicate key value, so you can insert it

    n = (Treenode *)malloc(sizeof(Treenode))); //Dynamic assignment of new nodes

    if (n == NULL) return;
    //Copy key value
    n->key = key;
    n->left = n->right = NULL;

    //Connect to parent node
    if (p != NULL)
    {
        if (key < p->key)
            p->left = n;
        else
            p->right = n;
    }   
    else
        *root = n; // Can't I change it to root = n? //
}

//Set tree node value
void setTreenode(Treenode *ThisNode, int data, Treenode *L, Treenode *R)
{
        ThisNode->key = data;
        ThisNode->left = L;
        ThisNode->right = R;
}

//main function
main()
{
    Treenode
        *n0 = (Treenode*)malloc(sizeof(Treenode)),
        *n1 = (Treenode*)malloc(sizeof(Treenode)),
        *n2 = (Treenode*)malloc(sizeof(Treenode)),
        *n3 = (Treenode*)malloc(sizeof(Treenode)),
        *n4 = (Treenode*)malloc(sizeof(Treenode)),
        *n5 = (Treenode*)malloc(sizeof(Treenode)),
        *n6 = (Treenode*)malloc(sizeof(Treenode));

    setTreenode(n0, 30, n1, n2);
    setTreenode(n1, 20, n3, n4);
    setTreenode(n2, 40, n5, n6);
    setTreenode(n3, 13, NULL, NULL);
    setTreenode(n4, 25, NULL, NULL);
    setTreenode(n5, 35, NULL, NULL);
    setTreenode(n6, 50, NULL, NULL);

    insert_treenode(&n0, 4);
}     

If you look closely, in the above situation, t will eventually point directly to the root node at the peak of the current tree. If you change the code slightly to reduce the pointer range from step 2 to step 1, as if it were on an annotation, Again, t points to the root node at that time.

The reason for using double pointers in books is that in insertion,

"The root node pointer needs to be changed..."

In the deletion, "The root node may be deleted and changed..." It comes out like this.

There seems to be no problem even without the double pointer.

When you turn to VS2017, the results are the same when the parameter is a single pointer and a double pointer.

In fact, some blogs use only a single pointer, so why?

(FYI, the main function was dynamically assigned to malloc after declaring the pointer of the Treenode structure type.)

pointer tree parameter

2022-09-22 18:45

1 Answers

There is no problem with the code you uploaded For example, if the Root Node is replaced, The Root node in the main function needs to be replaced You must replace the Root node in the Insert function It does not affect the root node pointer of the main function You may need to hand over the double pointer as a parameter.

void changeRoot(Node* root)
{
    root = new Node();
}

int main()
{
    Node* root = new Node();
    changeRoot(root);
}

It's a code that I omitted a lot and wrote simply, so if you take it into consideration, The root is probably not changed in the main.

void changeRoot(Node** root)
{
    *root = new Node();
}

int main()
{
    Node* root = new Node();
    changeRoot(&root);
}

If you change it like this, the root of the main will be changed. Is it necessary to delete the root?

If you understand why you have to use a double pointer, you don't have to memorize it like a formula.


2022-09-22 18:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.