title: Tree building function from precalculated tree
body: <p>I am building a binary tree. The binary tree is pre-built in a file and I need to construct it. Due to the way it is structured, I read the tree into an array. Each tree nodes look something like this.</p>
  
  <pre><code>struct Tree_Node
  {
      float normalX;
      float normalY;
      float normalZ;
      float splitdistance;
      long region;
      long left, right; //array index
      Tree_Node* left_node; // pointer to left node
      Tree_Node* right_node; // pointer to right node
  } typedef Tree_Node;
  </code></pre>
  
  <p>I have tried a number of ways to write some code that will build the tree. Let me give you some pseudocode so you understand what I am trying to do.</p>
  
  <ul>
  <li>Read in head node. Node is number one in the array.</li>
  <li>If the node has a right and left array index, create new nodes and
  insert the information from the array
  indicies into that tree node.</li>
  <li>If the node does not have a right and left index, it is a leaf node.</li>
  </ul>
  
  <p>Here is my building function:</p>
  
  <pre><code>void WLD::treeInsert(BSP_Node *tree_root, int node_number)
  {
      /// Add the item to the binary sort tree to which the parameter
      // "root" refers.  Note that root is passed by reference since
      // its value can change in the case where the tree is empty.
      if ( tree_root == NULL ) 
      {
          // The tree is empty.  Set root to point to a new node containing
          // the new item.  This becomes the only node in the tree.
          tree_root = new BSP_Node();
  
          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;
          tree_root-&gt;right = bsp_array[node_number].right;
          tree_root-&gt;left_node[node_number];
          tree_root-&gt;right_node[node_number];
  
          errorLog.OutputSuccess("Inserting new root node: %i", node_number);
                      // NOTE:  The left and right subtrees of root
                      // are automatically set to NULL by the constructor.
                      // This is important...
      }
  
      if ( tree_root-&gt;left != 0 ) 
      {
          errorLog.OutputSuccess("Inserting left node number: %i!", tree_root-&gt;left);
          treeInsert( tree_root-&gt;left_node, tree_root-&gt;left );
      }
      else if (  tree_root-&gt;right != 0 )
      {
          errorLog.OutputSuccess("Inserting right node: %i!", tree_root-&gt;right);
          treeInsert( tree_root-&gt;right_node, tree_root-&gt;right );
      }
      else if ( tree_root-&gt;right == 0 &amp;&amp; tree_root-&gt;left == 0)
      {
          errorLog.OutputSuccess("Reached a leaf node!");
          return;
      }
      else
      {
      errorLog.OutputError("Unknown BSP tree error!");
      }
  
  }
  </code></pre>
  
  <p>My debug shows that the function tries to insert node 2 until the program crashes.</p>
  
  <p>Can someone help me with this?</p>