title: Inserting nodes in a binary search tree (C)
body: <p>I'm trying to write a function to insert a node to a binary search tree, and I have the following:</p>
  
  <pre><code>typedef struct Node {
      int key;
      struct Node *left;
      struct Node *right;
  } Node;
  
  Node *createNode(int key)
  {
      Node *newNode = (Node *)malloc(sizeof(Node));
      newNode-&gt;key = key;
      newNode-&gt;left = NULL;
      newNode-&gt;right = NULL;
  
      return newNode;
  }
  
  Node *insert(Node *node, int key)
  {
      if (node==NULL)
      {
          node = createNode(key);
      }
      else
      {
          if (node-&gt;key &gt; key)
          {
              node-&gt;left = insert(node-&gt;left, key);
          }
          else
          {
              node-&gt;right = insert(node-&gt;right, key);
          }
      }
      return node;
  }
  
  int main(int argc, char* argv[])
  {
      Node *root = NULL;
      root = insert(root, 10);
  
      return 0;
  }
  </code></pre>
  
  <p>I know this works, and if I want to insert 5 to the tree with root node <code>root</code>, I can write <code>root = insert(root, 5);</code>. My question is, how can I write another version of <code>insert</code> that can achieve the same thing with simply <code>insert(root, 5);</code>? I have tried the following but with no avail.</p>
  
  <pre><code>void insert(Node *node, int key)
  {
      if (node==NULL)
      {
          node = createNode(key);
      }
      else
      {
          if (node-&gt;key &gt; key)
          {
              insert(node-&gt;left, key);
          }
          else
          {
              insert(node-&gt;right, key);
          }
      }
  }
  </code></pre>
  
  <p>What is wrong with this and why doesn't this work?. Any pointers (no pun intended) would be greatly appreciated!</p>