title: Binary Search Tree: lost pointer in insertion function
body: <p>Doing a project with BSTs, I have a logic fault somewhere in my insertion function, just can't seem to find it.</p>
  
  <p>Insertion function:</p>
  
  <pre><code>int bst_insert (bst_t *tree, bst_key_t key)
  {
    bst_node_t *node, *temp_node;
    if( tree == NULL ) {
      printf("Invalid tree pointer.\n");
      return;
    }
    if( tree-&gt;root == NULL ) {
      node = (bst_node_t *)malloc(sizeof(bst_node_t));
      node-&gt;left = node-&gt;right = NULL;
      node-&gt;key = key;
      tree-&gt;root = node;
      return 1;
    }
    temp_node = tree-&gt;root;
    while(1) {
      if( temp_node == NULL ) {
        node = (bst_node_t *)malloc(sizeof(bst_node_t));
        node-&gt;left = node-&gt;right = NULL;
        node-&gt;key = key;
        temp_node = node;
        return 1;
      }
      if( temp_node-&gt;key == key ) {
        temp_node-&gt;data_ptr = data;
        return 0;
      } else if( key &lt; temp_node-&gt;key ) {
        temp_node = temp_node-&gt;left;
      } else if( temp_node-&gt;key &lt; key ) {
        temp_node = temp_node-&gt;right;
      }
    }  
  }
  </code></pre>
  
  <p>In using this function, inserting one node works just fine (probably because the tree->root is null, so it exits inside that if statement), but when I try and insert a second and leave this function, the tree only has the first node in it. </p>
  
  <p>Let me know if I forgot to give any pertinent information.</p>