I have created the following functionS for an AVL tree. There seems to be a problem with the recursion.
Code:
template <class T>
int AVLtree<T>::insertData (const T &data)
{
AVLnode<T> *newNode;
bool taller;
if (!(newNode = new AVLnode<T>))
return false;
newNode -> right = NULL;
newNode -> left = NULL;
newNode -> balance = EH;
newNode -> info = data;
tree = insertHelper (tree, newNode, taller);
count++;
return true;
}
Code:
template <class T>
AVLnode<T>* AVLtree<T>::insertHelper(AVLnode<T>*root,AVLnode<T>*node,bool&higher)
{
if (!root)
{
root = node;
higher = true;
return root;
}
if (node->info.ID < root->info.ID)
{
root->left = insertHelper (root, node, higher); //<-- HERE
if (higher)
{
switch (root->balance)
{
case LH: root = leftBalance (root, higher);
break;
case EH: root->balance = LH;
break;
case RH: root->balance = EH;
higher = false;
break;
}//switch
}//if
}//if
else
{
root->right = insertHelper (root, node, higher); //<-- HERE
if (higher)
{
switch ( root->balance)
{
case LH: root->balance = EH;
higher = false;
break;
case EH: root->balance = RH;
break;
case RH: root = rightBalance (root, higher);
break;
}//switch
}//if
}//else
return root;
}
The recursion seems to end up in an endless loop and I cant see the problem. Can anyone help?
Thanx
Sophie