title: How do I make two consecutive recursive functions execute each level of recursion in alternation?
body: <p>The following is my assignment on a programming course I'm working on:</p>
  <p><a href="https://i.stack.imgur.com/osONq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/osONq.png" alt="enter image description here" /></a></p>
  <p>Below is the code I've written so far. I've adjusted it to contain the integers in an array, without the need to read them in from another file, to be easily usable by you. Below it I will explain why it does not create the binary tree successfully, followed by my question. Here's the code:</p>
  <pre><code>/* A program that should use integers in an array to make binary trees
     using the integers, then prints the integers from the tree inorder.
     By John
  */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct NODE
  {
      int data;
      struct NODE *left;
      struct NODE *right;
  } NODE;
  
  typedef NODE *BTREE;
  
  //allocate memory for the node
  BTREE create_node(BTREE t)
  {
      t = (BTREE)malloc(sizeof(NODE));
      return t;
  }
    
  //initialize the node
  BTREE init_node(int number, BTREE p1, BTREE p2)
  {
      BTREE t = NULL;
      t = create_node(t);
      t-&gt;data = number;
      if (p1-&gt;data &lt;= t-&gt;data)
      {
          t-&gt;left = p1;
      }
      else if (p1-&gt;data &gt; t-&gt;data)
      {
          t-&gt;right = p1;
      }
      if (p2-&gt;data &lt;= t-&gt;data)
      {
          t-&gt;left = p2;
      }
      else if (p2-&gt;data &gt; t-&gt;data)
      {
          t-&gt;right = p2;
      }
      return t;
  }
  
  //create the tree from the integers stored in the array
  BTREE create_tree(int num_array[], int i, int size)
  {
      if (i &gt;= size)
      {
          return NULL;
      }
      else
      {
          return (init_node(num_array[i],
                            create_tree(num_array, 2*i + 1, size),
                            create_tree(num_array, 2*i + 2, size)));
      }
  }
  
  //printing the integers stored in the trees inorder
  void print_inorder(BTREE t)
  {
      if (t != NULL)
      {
          print_inorder(t-&gt;left);
          printf(&quot;%d  :&quot;, t-&gt;data);
          print_inorder(t-&gt;right);
      }
  }
  
  int main(void)
  {
      BTREE t = NULL;
      int data[] = { 9, 11, 4, 5 };   //pointer to integers array
      int i = 0;           //represents indexes of the integers array
      int number = 0;      //number of integers to be read into array
      
      t = create_tree(data, i, number);
      print_inorder(t);
      
      free(t);
          
      return 0;
  }
  </code></pre>
  <p>I get a segmentation fault, because I have a bad (dangling?) pointer, to my understanding (and please correct me if I'm wrong). When <code>init_node</code> which is being returned by the <code>create_tree</code> function is finally called, the <code>i</code> value being passed to the <code>init_node</code> function is == 3 , which refers to the last integer in my <code>num_array</code>. Hence, in the <code>init_node</code> function, <code>if(p1 -&gt; data &lt;= t -&gt; data)</code> is an invalid statement because p1 itself cannot exist, since it would've needed an int that's taken from the <code>num_array</code>, but I had already reached the end of the <code>num_array</code> before attempting to create p1, hence p1 is a bad / dangling pointer.</p>
  <p>My problem is in the return value of the <code>create_tree</code> function:</p>
  <pre><code>BTREE create_tree(int num_array[], int i, int size)
  {
      if (i &gt;= size)
      {
          return NULL;
      }
      else
      {
          return (init_node(num_array[i],
                            create_tree(num_array, 2*i + 1, size),
                            create_tree(num_array, 2*i + 2, size)));
      }
  }
  </code></pre>
  <p>The point of this:</p>
  <pre><code>     return (init_node(num_array[i],
                         create_tree(num_array, 2*i + 1, size),
                         create_tree(num_array, 2*i + 2, size)));
  </code></pre>
  <p>is to create <code>NODE p1</code> and <code>NODE p2</code> to be used by the <code>init_node</code> function to create a tree, that assigns <code>p1</code> to the left pointer and <code>p2</code> to the right pointer. <code>p1</code> should contain one integer, then <code>p2</code> should contain the integer contained at the very next index in the <code>num_array</code>. What ended up happening was that when we get to the execution of <code>create_tree(num_array, 2*i + 1, size)</code>, it recurses, and with the recursions <code>i</code> takes the values <code>0</code>, <code>1</code>, <code>3</code>, then <code>i</code> becomes <code>== 7</code>, hence <code>NULL</code> is returned to the <code>create_tree</code> function, and the <code>i</code> value passed to <code>create_tree(num_array, 2*i + 2, size)</code> is already <code>== 3</code>, so the <code>create_tree</code> function in this case returns <code>NULL</code>, and <em>then</em> the <code>init_node</code> function starts executing.</p>
  <p>What I wanted by the <code>create_tree</code> recursions in the returned <code>init_node</code> function, was to use the integer at index <code>i == 0</code> of the <code>num_array</code> in <code>create_tree(num_array, 2*i + 1, size)</code>, then use the integer at index <code>i == 1</code> in <code>create_tree(num_array, 2*i + 2, size)</code>, and for the <code>init_node</code> function to be called to create the trees, and then for the alternation between the first and second <code>create_tree</code> function calls to continue using <code>i == 2</code> and <code>i == 3</code> <em>consecutively</em>. <strong>How can I fix my code to make it do that?</strong></p>
  <p>(The professor teaching the course actually implemented the code the way I did it, meaning he wrote the <code>create_tree</code> function the way I did, except that he used a char array containing chars <code>a</code>, <code>b</code>, <code>c</code>, <code>d</code>, <code>e</code> instead of an int array containing ints, and somehow his program executed properly, and mines does not, which is also very confusing.)</p>
  <p>Below is a snippet from my course, showing generally the same logic that I used, used by the professor for his <code>init_node</code> and <code>create_tree</code> functions. He then runs the compiled program and it works just fine, and I cannot understand how that worked for him.</p>
  <p><a href="https://i.stack.imgur.com/ugAMj.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ugAMj.jpg" alt="enter image description here" /></a></p>
  <p>Thank you for all your help!</p>