title: Recursive binary tree function
body: <p>I am trying to insert items from my array into my tree. My function works fine and creates nodes all the way down to the leaf node on the left hand side of my tree. The problem is when it is supposed to return recursively when detecting a leaf node to the higher level, it just stops building the tree completely. Here is the code:</p>
  
  <pre><code>void WLD::treeInsert(BSP_Node *tree_root, int node_number)
  
  {
  
  if ( tree_root == NULL ) 
      {
          tree_root = new BSP_Node();
  
          tree_root-&gt;node_number = node_number;
          tree_root-&gt;normalX = bsp_array[node_number].normal[0];
          tree_root-&gt;normalY = bsp_array[node_number].normal[1];
          tree_root-&gt;normalZ = bsp_array[node_number].normal[2];
          tree_root-&gt;splitdistance = bsp_array[node_number].splitdistance;;
          tree_root-&gt;region = bsp_array[node_number].region;
          tree_root-&gt;left = bsp_array[node_number].left; //because the array starts at index 0
          tree_root-&gt;right = bsp_array[node_number].right; //because the array starts at index 0
          tree_root-&gt;left_node = NULL;
          tree_root-&gt;right_node = NULL;
  
          errorLog.OutputSuccess("Inserting new node: %i", node_number);
          errorLog.OutputSuccess("Left node index: %i", bsp_array[node_number].left);
          errorLog.OutputSuccess("Right node index: %i", bsp_array[node_number].right);
  
          node_number++;
  
          // Check for leaf nodes
          if(tree_root-&gt;region != 0)
          {
              errorLog.OutputSuccess("This is a leaf node! Returning!");
              return;
          }
      }
  
  
      if ( tree_root-&gt;left &gt; 0) 
      {
          //tree_root-&gt;left_node = new BSP_Node();
          errorLog.OutputSuccess("Left node not NULL, inserting it!");
          treeInsert( tree_root-&gt;left_node, tree_root-&gt;left );
      }
      else if (tree_root-&gt;right &gt; 0)
      {
          //tree_root-&gt;right_node = new BSP_Node();
          errorLog.OutputSuccess("Right node not NULL, inserting it!");
          treeInsert( tree_root-&gt;right_node = NULL, tree_root-&gt;right );
      }
  
  }
  </code></pre>
  
  <p>As you can see, when it detects a leaf node, it is supposed to return to the calling function (this function but on a level closer to the node. Does anyone have any suggestions?</p>