Why is the parent->left not persistent
<p>Bellow is the code for inserting in a binary tree.The issue is that after inserting the head , for the second input the function insert is called recursively with input as parent->left or parent->right ,and then it is set to item .After retuening from the insert function the value of parent->left or parent->right(whichever be the case) remains NULL and no node is assigned to left or right of head.</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;string&gt;

using namespace std;

typedef struct Tnode{
    string word;
    int count;
    Tnode* left;
    Tnode* right;
}; 

static  Tnode *head=NULL ;

Tnode* getNode(string const&amp; iData)
{
    Tnode* pNewNode = new Tnode();
    pNewNode-&gt;word = iData;
    pNewNode-&gt;count = 0;
    pNewNode-&gt;left = NULL;
    pNewNode-&gt;right = NULL;
    return pNewNode;
}

void insert_in_tree(Tnode* parent,Tnode *item)
{
    item-&gt;count++;
    if ( head== NULL )
    {
        head=item;
        cout&lt;&lt;"Inserting head "&lt;&lt;head-&gt;word&lt;&lt;endl;
        return;
    }
    else
    {
        if (item-&gt;count==1) parent=head;
        if( parent == NULL )
        { 
            parent=item;
            cout&lt;&lt;"Inserting "&lt;&lt;parent-&gt;word&lt;&lt;"count is "&lt;&lt;parent-&gt;count&lt;&lt;endl; 
            return;
        }
        else
        {
            if(item-&gt;word &lt; parent-&gt;word ) 
            {
                insert_in_tree(parent-&gt;left,item);
                cout&lt;&lt;"inserted in left of "&lt;&lt;parent-&gt;word&lt;&lt;endl;
            }
            else 
            {
                insert_in_tree(parent-&gt;right,item);
                cout&lt;&lt;"inserted in right of "&lt;&lt;parent-&gt;word&lt;&lt;endl;
            }
        }
   }
}

void print_elements(Tnode *tree)
{
    if(tree!=NULL)
    {
        print_elements(tree-&gt;left);
        cout&lt;&lt;tree-&gt;word&lt;&lt;endl;
        print_elements(tree-&gt;right);
    }
}

int main()
{
    string x;

    while(cin&gt;&gt;x) 
    {
        Tnode * node=getNode(x);
        insert_in_tree(NULL,node);
    }
    if(!cin.eof()) cout&lt;&lt;"Error"&lt;&lt;endl;
    else print_elements(head);

    return 0;
}
</code></pre>
