title: Inserting node in binary tree in C
body: <p>I am learning C and found a basic tree implementation in my C book:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct tree_node {
    int data;
    struct tree_node *left_p, *right_p;
  };
  
  struct tree_node *t_search(struct tree_node *root, int v) {
  
    while (root) {
      printf("Looking for %d, looking at %d\n", v, root-&gt;data);
      if (root-&gt;data == v)
        return root;
      if (root-&gt;data &gt; v)
        root = root-&gt;left_p;
      else
        root = root-&gt;right_p;
    }
  
    return 0;
  }
  
  int t_insert(struct tree_node **root, int v) {
  
    while (*root) {
      if ((*root)-&gt;data == v)
        return 1;
      if ((*root)-&gt;data &gt; v)
        root = &amp;((*root)-&gt;left_p);
      else
        root = &amp;((*root)-&gt;right_p);
    }
  
    if ((*root = (struct tree_node *) malloc(sizeof(struct tree_node))) == 0)
      return 2;
  
    (*root)-&gt;data = v;
    (*root)-&gt;left_p = 0;
    (*root)-&gt;right_p = 0;
  
    return 0;
  }
  
  int main(void) {
  
    struct tree_node *tp, *root_p = 0;
    int i;
  
    t_insert(&amp;root_p, 4);
    t_insert(&amp;root_p, 2);
    t_insert(&amp;root_p, 6);
    t_insert(&amp;root_p, 1);
    t_insert(&amp;root_p, 3);
    t_insert(&amp;root_p, 4);
    t_insert(&amp;root_p, 7);
  
    for (i = 0; i &lt; 9; i++) {
      tp = t_search(root_p, i);
      if (tp)
        printf("%d found\n", i);
      else
        printf("%d not found\n", i);
    }
  
    return 0;
  }
  </code></pre>
  
  <p>While the code seems to be straight forward, I am having a hard time to understand the <code>t_insert</code> function. Why does it take in a <code>struct tree_node **root</code> instead of <code>struct tree_node *root</code>? <code>t_serach</code> is virtually the same code but uses only a pointer and not a pointer pointer. Maybe another example would explain the issue in a better way.</p>
  
  <p>For what it's worth: I am from a Java background.</p>
  
  <p>Note: Yes, I know the tree is not rebalanced upon insertion.</p>