AVL Tree Compiles and Runs But Crashes Instantly
<p>So the program I have here will compile however it will crash instantly if I create a class object. What I mean is, in my main.cpp if I create say "AVLTree obj;" The program crashes....
If I leave that out then everything is fine... Any help would be appreciated. </p>

<p>Thank You. // MAIN below</p>

<pre class="lang-cpp prettyprint-override"><code>using namespace std;

int main()
{
    cout &lt;&lt; "******************************" &lt;&lt; endl;
    cout &lt;&lt; "  Self Balancing AVL Tree    " &lt;&lt; endl;
    cout &lt;&lt; "******************************" &lt;&lt; endl;
   /** AVLtree obj;
    obj.insert(100);
    obj.insert(20);
    obj.insert(25);
    obj.insert(200);
    assert isEmpty();
    obj.preOrderPrint(*root);
    obj.inOrderPint(*root);
    obj.postOrderPrint(*root);
    obj.remove(20);

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

<p>HEADER</p>

<pre class="lang-cpp prettyprint-override"><code>#ifndef AVLTREE_H
#define AVLTREE_H

//Moved this outside of the class trying to get things running
struct TreeNode
{
    int key;
    int data;
    TreeNode *parent;
    TreeNode *right;
    TreeNode *left;

    char factor; //byte
};
//-------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------- s
class AVLtree
{
   private:
   protected:
    //neccessary tree nodes
    TreeNode *root;
    TreeNode *tmp, *node;
    TreeNode *holder1, *holder2, *holder3, *newnode;
    int tmpdata;
    bool h;

    int height(TreeNode * pos) const;
    int max(int a, int b) const;

    //Rotate functions broken up individually and used within the
    //insert function. Was having pointer issues when insert was
    //all one function
    TreeNode * singleRotateLeft(TreeNode *holder2);
    TreeNode * singleRotateRight(TreeNode *holder2);

    TreeNode * doubleRotateLeft(TreeNode *holder2);
    TreeNode * doubleRotateRight(TreeNode *holder2);

    TreeNode * _insert(int key, TreeNode * node);
    TreeNode * _remove(int key, TreeNode * node);

    public:
    AVLtree();
    void insert(int key, int data);
    bool isEmpty();
    void remove(int key);
    int retrieve(int key);
    void preOrderPrint(TreeNode *root)const;
    void inOrderPrint(TreeNode *root)const;
    void postOrderPrint(TreeNode *root)const;
    int size;
};
#endif // AVLTREE_H
</code></pre>

<p>CPP for HEADER</p>

<pre class="lang-cpp prettyprint-override"><code>#include "avltree.h"
#include &lt;cstdio&gt;
#include &lt;iostream&gt;

using namespace std;
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
AVLtree::AVLtree()
{
    size = 0;
    //Initialize values
    root = NULL;
    root-&gt;left = NULL;
    root-&gt;right = NULL;
    root-&gt;parent = NULL;
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
int AVLtree::retrieve(int key)
{
    //height of 0 means the tree must be empty
    if(size == 0)
    {
        return NULL;
    }
    tmp = root;
    //While not empty search both sides of tree for key
    while(tmp != NULL)
    {
        if(key &lt; tmp-&gt;key)
            tmp = tmp-&gt;left;
        else if(key &gt; tmp-&gt;key)
            tmp = tmp-&gt;right;
        else
            return tmp-&gt;data;
    }
    return NULL;
}

//Simple bool determining if the tree is empty via the root
bool AVLtree::isEmpty()
{
    if(root == NULL)
    {
        cout &lt;&lt; "The Tree Is Empty!! " &lt;&lt; endl;
        return true;
    }
    else
    {
        cout &lt;&lt; "The Tree Is NOT Empty" &lt;&lt; endl;
        return false;
    }
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------


int AVLtree::height( TreeNode * pos ) const
{
        if( pos == NULL )
            return -1;
        else
            return pos-&gt;factor;
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
int AVLtree::max( int a, int b ) const
{
    return a &gt; b ? a : b;
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
TreeNode * AVLtree::singleRotateLeft(TreeNode *holder2)
{
    holder1 = holder2-&gt;left;
    holder2-&gt;left = holder1-&gt;right;
    holder1-&gt;right = holder2;

    holder2-&gt;factor = max(height(holder2-&gt;left), height(holder2-&gt;right))+1;
    holder1-&gt;factor = max(height(holder1-&gt;left), holder2-&gt;factor)+1;


    return holder1;  // new root
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
TreeNode * AVLtree::singleRotateRight(TreeNode *holder1)
{
    holder2 = holder1-&gt;right;
    holder1-&gt;right = holder2-&gt;left;
    holder2-&gt;left = holder1;

    holder1-&gt;factor = max(height(holder1-&gt;left), height(holder1-&gt;right))+1;
    holder2-&gt;factor = max(height(holder2-&gt;right), holder1-&gt;factor)+1;


    return holder2;  // new root
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
TreeNode * AVLtree::doubleRotateLeft(TreeNode *holder3)
{
    holder3-&gt;left = singleRotateRight(holder3-&gt;left);

    return singleRotateLeft(holder3);
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
TreeNode * AVLtree::doubleRotateRight(TreeNode *holder1)
{
    holder1-&gt;right = singleRotateLeft(holder1-&gt;right);

    return singleRotateRight(holder1);
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
void AVLtree::insert(int key, int data)
{
    size++;
    tmpdata = data;
    root =_insert(key,root);
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
TreeNode * AVLtree::_insert(int key, TreeNode * node)
{
    //Empty case, create a new tree
    if (node == NULL)
    {
        node = new TreeNode;
        node-&gt;factor = 0;
        node-&gt;key = key;
        node-&gt;data = tmpdata;
        node-&gt;left = NULL;
        node-&gt;right = NULL;
//      if(size==1)
//          root=node;

    }
    //Key is less than, move down the left child
    else if(key &lt; node-&gt;key)
    {
        node-&gt;left= _insert(key,node-&gt;left);
        if(height(node-&gt;left) - height(node-&gt;right) == 2)
        {
            if(key &lt; node-&gt;left-&gt;key)
                node = singleRotateLeft(node);
            else
                node = doubleRotateLeft(node);
        }
    }
    //Key is greater than move down the right child
    else if(key &gt; node-&gt;key)
    {
        node-&gt;right= _insert(key,node-&gt;right);
        if(height(node-&gt;right) - height(node-&gt;left) == 2)
        {
            if(key &gt; node-&gt;right-&gt;key)
                node = singleRotateRight(node);
            else
                node = doubleRotateRight(node);
        }
    }



//  node-&gt;factor=-1;
//  if(node-&gt;left!=NULL)
//      node-&gt;factor=node-&gt;left-&gt;factor;
//  if(node-&gt;right!=NULL)
//      node-&gt;factor=max(node-&gt;factor, node-&gt;right-&gt;factor);

    node-&gt;factor = max(height(node-&gt;left ),height(node-&gt;right))+1;
    return node;

}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
void AVLtree::preOrderPrint(TreeNode *node) const
{
    //Empty node returns out
    if(node == NULL) return;
    //print the contents of the node specified
    cout &lt;&lt; node-&gt;data &lt;&lt; " ";
    //Navigate and display left subtree
    preOrderPrint(node-&gt;left);
    //Followed by the right subtree
    preOrderPrint(node-&gt;right);
}

void AVLtree::inOrderPrint(TreeNode *node) const
{
    if(node == NULL) return;
    inOrderPrint(node-&gt;left);
    // Root middle value is displayed in the middle of the printing
    //operation
    cout &lt;&lt; node-&gt;data &lt;&lt; " ";
    inOrderPrint(node-&gt;right); // Left childeren last to be printed
}

void AVLtree::postOrderPrint(TreeNode *node) const
{
    if(node == NULL) return; // Empty tree returns

    postOrderPrint(node-&gt;left); //Display left side subtree
    postOrderPrint(node-&gt;right); // Followed by right side subtree
    cout &lt;&lt; node-&gt;data &lt;&lt; " "; //Finish with root
}

void AVLtree::remove(int key)
{
    root =_remove(key,root);
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
TreeNode * AVLtree::_remove(int key, TreeNode * node)
{
    //temp bool determining state of removal
    bool done = false;
    //Empty case there is nothing to do, return done immediately
    if (node == NULL)
    {
        h = false;
        done = true;
    }
    else
    //If key data is less than the current node
    if (key &lt; node-&gt;key) //delete from left subtree
    {
        newnode =_remove(key,node-&gt;left);
        node-&gt;left = newnode;
        if(h)
        {
            //Check for height imbalance
            if(height(node-&gt;right) - height(node-&gt;left) == 2)
            {
                if(height(node-&gt;right) &gt; height(node-&gt;left))
                    node = singleRotateLeft(node);
                else
                    node = singleRotateRight(node);
            }

            node-&gt;factor = max(height(node-&gt;left ),height(node-&gt;right))+1;


            if (node-&gt;factor &gt;= 0)
                {
                   node-&gt;factor = root-&gt;factor -1;
                   if (node-&gt;factor == -1)
                        h = false;
                }
                else if (node-&gt;right-&gt;factor == -1)
                    singleRotateRight(node);
                else
                    singleRotateLeft(node);

                done = true;
                return node;
        }


    }
    else if (key == node-&gt;key) //del node
    {
        if (node-&gt;left == NULL || node-&gt;right == NULL)  // one or no children
        {
            if (node-&gt;left == NULL)
                holder1 = node-&gt;right;
            else
                holder1 = node-&gt;left;

            delete node;

            h = true; done = true;

            return(holder1);

        }
        else // both of children
        {
            holder2 = node-&gt;right;
                while (holder2-&gt;left != NULL)
                    holder2 = holder2-&gt;left;

                node-&gt;key = holder2-&gt;key;
                key = node-&gt;key;
        }
    }

    if (!done &amp;&amp; key &gt;= node-&gt;key) // delete from right subtree
    {
            newnode=_remove(key, node-&gt;right);
            node-&gt;right = newnode;
        if (h)
        {
                if(height(node-&gt;right) - height(node-&gt;left) == 2)
            {
                if(height(node-&gt;right) &gt; height(node-&gt;left))
                    node = singleRotateLeft(node);
                else
                    node = singleRotateRight(node);
            }
            node-&gt;factor = max(height(node-&gt;left ),height(node-&gt;right))+1;
                //
/*              if (node-&gt;factor &lt;= 0)
                {
                    node-&gt;factor=node-&gt;factor+1;
                    if (node-&gt;factor ==1)
                        h=false;
                }
                else if (node-&gt;right-&gt;factor==1)
                    singleRotateLeft(node);
                else
                    singleRotateRight(node);*/
                 return node;
            }
    }


}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
</code></pre>
