davide
davide

Reputation: 1

Pointer of a Pointer in C: How to use them in RB Trees?

I have seen online many people showing their own implementation of RB trees in C. Most of them, when calling any function, use the parameter struct Node **root referring to a struct node like this:

typedef struct Node {
    int data;
    char color;
    struct Node *left;
    struct Node *right;
    struct Node *parent;
} Node;

Why people do so? Why struct Node *root isn't enough? Can you please give an explanation of the meaning of a pointer of a pointer in this specific case and add some valid examples?

Upvotes: 0

Views: 124

Answers (1)

Tom
Tom

Reputation: 491

If a linked list's first node is struct Node *root type, then now you want to insert a node where the first node before, so you will change the first node pointed, now you need to use struct Node ** root.

See more details at Here. That answer use you mentioned type like: struct Node ** root. (Ignore that answer have 2 vote down.) That answer's code can beautifully run at https://godbolt.org/z/bTdqE8Pcv.

Hope this can help you.

Upvotes: 0

Related Questions