AVLTree(loading and balancing a binary tree)
<pre><code>#include &lt;iostream&gt;
#include &lt;cstring&gt;
#include &lt;fstream&gt;

using namespace std;

//header file 
template &lt;class V&gt;
struct Node{
   int height;
   V key;
   string value;
   Node&lt;V&gt; *Parent;
   Node&lt;V&gt; *Left;
   Node&lt;V&gt; *Right;

   Node (V Key,string Val)
   {
        this-&gt;key=Key;
        this-&gt;value=Val;
        this-&gt;Parent=NULL;
        this-&gt;Left=NULL;
        this-&gt;Right=NULL;
   }
};

template &lt;class V&gt;
class AVLTree
{
  private:
          Node&lt;V&gt; *Root;
  public:
         AVLTree();
         void deleteTree(Node&lt;V&gt; *node);
         ~AVLTree();
         int height(Node&lt;V&gt; *node);
         int TreeHeight();
         Node&lt;V&gt; *tallGrandChild(Node&lt;V&gt; *node);
         void rotate(Node&lt;V&gt; *node);
         bool isBalanced(Node&lt;V&gt; *node);
         void balance(Node&lt;V&gt; *node);
         void balanceTree();
         void insert(V key,string value);
         void loadTree(const char *file);
         string Search(V key);     
};            

//constructor
template &lt;class T&gt;
AVLTree&lt;T&gt;::AVLTree() 
{
   Root=NULL;
}

template&lt;class V&gt;   
void AVLTree&lt;V&gt;::deleteTree(Node&lt;V&gt; *node)
{
 Node&lt;V&gt; *l=node-&gt;Left;
 Node&lt;V&gt; *r=node-&gt;Right;

 if (l!=NULL)
    deleteTree(l);
 else if (r!=NULL)
      deleteTree(r);

 delete node;                       
}

//destructor
template&lt;class V&gt;
AVLTree&lt;V&gt;::~AVLTree()
{
    deleteTree(Root);
}                  

//calculating height recursively         
template &lt;class V&gt;
int AVLTree&lt;V&gt;::height(Node&lt;V&gt; *node)
{
   int h=0;
    if (node!=NULL)
    {
        h=max(height(node-&gt;Left),height(node-&gt;Right))+1;
    }
    return h;
}

//function to find out height of tree
template &lt;class V&gt;
int AVLTree&lt;V&gt;::TreeHeight()
{
  int h=height(Root);
  return h;
}

//get the grandchild of node
template &lt;class V&gt;
Node&lt;V&gt;* AVLTree&lt;V&gt;::tallGrandChild(Node&lt;V&gt; *node)
{
    cout&lt;&lt;"GC before"&lt;&lt;endl;           

    Node&lt;V&gt; *l=node-&gt;Left;
    Node&lt;V&gt; *r=node-&gt;Right;
    cout&lt;&lt;"GC middle"&lt;&lt;endl;
    if (l!=NULL &amp;&amp; r!=NULL)
    {
      if (height(l)&gt;=height(r))
      {
         if (l-&gt;Left!=NULL &amp;&amp; l-&gt;Right!=NULL)
         {                       
           if (height(l-&gt;Left)&gt;=height(l-&gt;Right))
               return l-&gt;Left;
           else
               return l-&gt;Right;
         }
      }
     }
     else
     {
        if (r-&gt;Left!=NULL &amp;&amp; r-&gt;Right!=NULL)
        {
          if (height(r-&gt;Left)&gt;=height(r-&gt;Right))
              return r-&gt;Left;
          else
              return r-&gt;Right;
        }
     }
  }
cout&lt;&lt;"GC end"&lt;&lt;endl;
return NULL;
}                             


//check node if it is balanced or not                 
template &lt;class V&gt;
bool AVLTree&lt;V&gt;::isBalanced(Node&lt;V&gt; *node)
{
 int l,r;
 if (node-&gt;Left==NULL)
     l=0;

 if (node-&gt;Right==NULL)
     r=0;

 if (node-&gt;Left!=NULL)        
    l=height(node-&gt;Left);

 if (node-&gt;Right!=NULL)
    r=height(node-&gt;Right);

 cout&lt;&lt;"isbalanced checking"&lt;&lt;endl;
 if ((l-r)&gt;1 || (l-r)&lt;-1)
 {
      return false;
 }
 else 
 {
     return true;
 }
}

// rotate the node that is unbalanced,its parent and grandparent
template &lt;class V&gt;
void AVLTree&lt;V&gt;::rotate(Node&lt;V&gt; *node)
{
 if (Root==NULL)
 {
    return;
 }
 if (Root-&gt;Left==NULL &amp;&amp; Root-&gt;Right==NULL)
 {
    return;   
 }
 Node&lt;V&gt; *parent,*grandparent;
 parent=node-&gt;Parent;
 grandparent=parent-&gt;Parent;
 if (grandparent==NULL)
    return;

 V min=node-&gt;key;
 Node&lt;V&gt; *A,*B,*C;
 A=node;

 if (parent-&gt;key&lt;min)
 {
     min=parent-&gt;key;
     A=parent;
 }        
 if (grandparent-&gt;key&lt;min)
 {
      min=grandparent-&gt;key;
      A=grandparent;
 }

 if (parent==A)
 {
     if (grandparent-&gt;key&lt;node-&gt;key)
     {
         B=grandparent;
         C=node;
     }
     else
     {
         B=node;
         C=grandparent;
     }
 }

 if (grandparent==A)
 {
     if (parent-&gt;key&lt;node-&gt;key)
     {
         B=parent;
         C=node;
     }
     else
     {
         B=node;
         C=parent;
     }
 }

 if (node==A)
 {
     if (grandparent-&gt;key&lt;parent-&gt;key)
     {
             B=grandparent;
             C=parent;
     }
     else
     {
             B=parent;
             C=grandparent;
     }
 }

 if (grandparent-&gt;Parent!=NULL)
 {
    B-&gt;Parent=grandparent-&gt;Parent;
    B-&gt;Left=A;
    B-&gt;Right=C;
    A-&gt;Parent=B;
    C-&gt;Parent=B;
 } 
 else
 {
    B-&gt;Left=A;
    B-&gt;Right=C;
    A-&gt;Parent=B;
    C-&gt;Parent=B;
 }       
}     
//balance the node and all nodes following it.this is a recursive function that i //
 //came up with
template &lt;class V&gt;
void AVLTree&lt;V&gt;::balance(Node&lt;V&gt; *node)
{
   if (node-&gt;Left!=NULL) 
       balance(node-&gt;Left); 
    if (node-&gt;Right!=NULL)
       balance(node-&gt;Right);

   Node&lt;V&gt; *temp=node;


    if (temp-&gt;Parent!=NULL &amp;&amp; (temp-&gt;Parent)-&gt;Parent!=NULL)
    {
       temp=temp-&gt;Parent;
       temp=temp-&gt;Parent;     
       if (isBalanced(temp)==0)
       {
           cout&lt;&lt;"balance func check 3"&lt;&lt;endl;                    
           Node&lt;V&gt; *temp1=tallGrandChild(temp);
           if (temp1!=NULL)
              rotate(temp1);
       }      
}
}
//function to balance whole tree
template &lt;class V&gt;
void AVLTree&lt;V&gt;::balanceTree()
{
   balance(Root);
}   

//insert a new key and value
template &lt;class V&gt;
void AVLTree&lt;V&gt;::insert(V key,string value)
{
   Node&lt;V&gt; *temp,*temp1;
   if (Root==NULL)
   {
      Root=new Node&lt;V&gt;(key,value);
      return;
   }          
 temp=Root;

 if (Root!=NULL)
 {
    while (temp!=NULL)
    {
       temp1=temp;
       if (key&gt;temp-&gt;key)
       {
            temp=temp-&gt;Right;
       }
       else if (key&lt;temp-&gt;key)
       {
            temp=temp-&gt;Left;
       }

    }

    if (temp1!=NULL)
    {
        temp=new Node&lt;V&gt;(key,value);
        temp-&gt;Parent=temp1;
        if (temp1-&gt;key&gt;key)
        {
            temp1-&gt;Left=temp;
        }
        else if (temp1-&gt;key&lt;key)
        {
            temp1-&gt;Right=temp;
        }
    }
 }               
}                                  

//reading data from a file and storing it in AVLTree using insert function
template &lt;class V&gt;               
void AVLTree&lt;V&gt;::loadTree(const char *file)
{
 ifstream fin;
 fin.open(file);

 if(fin.fail())
 cout&lt;&lt;"Error in opening file!"&lt;&lt;endl;

 while (!fin.eof())
 {
       string buffer;
       V buff;
       getline(fin,buffer,'~');
       fin&gt;&gt;buff;
       if (!buff)
          continue;
       insert(buff,buffer);

 }


 fin.close();
}
</code></pre>

<p>I am making an AVL Tree as an assignment.I am having problems when balancing a<br>
    tree.The function of balance is a recursive one.When i run the code,after 2 to 3 
    seconds, i get a segmentation fault.when i debug it,the problem is in balance function 
    in the if condition:</p>

<pre><code>if (temp-&gt;Parent!=NULL &amp;&amp; (temp-&gt;Parent)-&gt;Parent!=NULL)
       temp=temp-&gt;Parent;
       temp=temp-&gt;Parent;     
</code></pre>

<p>but i cant figure out what can be wrong as the if condition can only be entered when 
 the node's parent and grandparent are not NULL. </p>
