3

So here is the function that works fine:

void Insert(node ** root, int inpdata){//tree's root should be passed here
   if(*root == NULL){
        *root = createNode(inpdata);
    }
   else if(inpdata < (*root)->data){
        Insert(&(*root)->left,inpdata);
    }
   else{
        Insert(&(*root)->right,inpdata);
    }
}

But I could not figure out why we have to use a double pointer. Why wouldn't the following code work for instance:

void Insert(node * root, int inpdata){//tree's root should be passed here
    if(root == NULL){
        root = createNode(inpdata);
    }
    else if(inpdata < root->data){
        Insert(root->left,inpdata);
    }
    else{
        Insert(root->right,inpdata);
    }
}

Also, in the first function, I could not comprehend the usage of &(*root).Wouldn't that make no sense because *root itself is a pointer. So, the "address of a pointer" is redundant since pointer already stores an adress value. I might be a bit confused so a little help would be much appreciated.
Thanks!

Huzo
  • 1,652
  • 1
  • 21
  • 52

2 Answers2

2

C passes argument by value, if you use then second approach:

void Insert(node * root, int inpdata);

After calling this function, the root in the caller side won't be affected.

I could not comprehend the usage of &(*root)

You are confused by precedence and wrongly parse the expression. &(*root)->left is

&((*root)->left).
llllllllll
  • 16,169
  • 4
  • 31
  • 54
  • I could not understand why `root` in the caller side wouldnt be affected when I do `root = createNode(inpdata);`? – Huzo Mar 18 '18 at 11:56
  • wouldnt it assign root pointer as a new pointer that holds the data? – Huzo Mar 18 '18 at 11:57
  • 1
    @Huzo Because `root` pointer in the function body is a local copy. Think about you pass `Insert(NULL, 11)`, how can `NULL` be changed after calling ? – llllllllll Mar 18 '18 at 11:58
  • Ahh, I see now. It is all about the second function just modifying the local copy instead of the real pointer itself! Thanks. Got it now. – Huzo Mar 18 '18 at 12:02
-1

First of all, thank you for asking this question. As I was also doing the implementation of BT(BINARY TREE ).The answer of your question lies in the full part of the program.

If you declare the root / head pointer externally then you don't need to use double pointer. But iIf you declare the root pointer inside the main function. Then we come up with 2 possibilities:

As you know we declare root as NULL in the starting point inside the main

  1. When you call the insert method and you put the parameters as root and data then you will collect in the insert function as local root pointer variable. Even if you change the links of root local that doesn't create any effect on the (main) root, so that's why you have to declare double pointer in the insert function. Then if you do some compute with root then it will go for (real)main, and if you are using double pointer then by recursion you'll send an address to double pointer so you have to send the address of a pointer though it will effect the actual pointer that you called
  2. Next approach is either you can return the address of root at every calling of the insert method and collect it inside the root and compute everything normal like as in recursive doubly linked list inside insert method

EG:. root=insert(root,data);

Christopher Moore
  • 3,071
  • 4
  • 30
  • 46
  • 2
    Please, take time to proof-read your answer and format it in a legible way. This wall of text is unreadable. – Dan Mašek Sep 23 '18 at 16:05