title: Binary Search Tree Insertion C without recursion
body: <p>I am new to the page and I am really stuck at my university's homework to recreate a function that inserts nodes to the tree without Recursion. I have been given the Recursive method and I need to convert it to Iterative. This is the given Recursive Code:</p>
  
  <pre><code>TreeNode *InsertTree(TreeNode *root, TreeNode *newnode)
  {
     if (!root)
     {
        root = newnode;
        root-&gt;left = root-&gt;right=NULL;
     }
     else if (newnode-&gt;entry &lt; root-&gt;entry)
     {  
        root-&gt;left = InsertTree(root-&gt;left, newnode);
     }
     else
     {
        root-&gt;right = InsertTree(root-&gt;right, newnode);
     }
     return root;
  }
  </code></pre>
  
  <p>and I made this one:</p>
  
  <pre><code>TreeNode *InsertTree(TreeNode *root, TreeNode *newnode)
  {
     if (!root)
     {
        root = newnode;
        root-&gt;left = root-&gt;right=NULL;
     }  
     else 
     {
        TreeNode * temp = root, *prev = NULL;
  
        while(temp)
        {
          if (temp-&gt;entry &lt; newnode-&gt;entry)
            temp = temp-&gt;right;
          else
            temp = temp-&gt;left;
        }
        newnode;
        temp-&gt;left = temp-&gt;right = NULL;
     }
     return root;
  }
  </code></pre>
  
  <p>it works for the first elements but it doesn't save the rest elements.
  Any ideas? 
  Thanks in advance</p>