AVL树插入 - 实现

时间:2013-12-11 07:28:07

标签: c++ insertion avl-tree

感谢你们多年来给我的所有帮助。这是另一个问题,像往常一样,任何解释,建议或更正都受到欢迎和赞赏!

我正在使用C ++为AVL树进行分配,目前我正在进行插入,但也需要实现删除,所以尽管这篇文章对插入代码很重要,但任何指针都是如此。如何实现删除最简单的方法来理解我的代码会很棒!

好的,所以我有以下功能来帮助我平衡/插入:

 int AVL :: returnBalance(Node* nodeme)
{
   return (whatisHeight(nodeme->leftbaby) - whatisHeight(nodeme->rightbaby));
}

int AVL :: whatisHeight(Node* localroot)
{
if(localroot == NULL)
{
    return 0;
}else
    return localroot->getHeight();
}

void AVL :: adjustHeight(Node* localroot)
{
if(localroot != NULL)
{
    localroot->height = 1 + max(whatisHeight(localroot->leftbaby), whatisHeight(localroot->rightbaby));
}
}

bool AVL :: contains(Node* nodeme ,int data)
{
if(!nodeme)
{
    return false;
}else
    if(data == nodeme->value)
    {
        return true;
    }
if(data < nodeme->value)
{
    return contains(nodeme->leftbaby,data);
}else
{
    return contains(nodeme->rightbaby,data);
}
}

// Rotation
void AVL :: rotateLeft(Node* localroot)
{
Node * temp = localroot->rightbaby;
localroot->rightbaby = temp->leftbaby;
temp->leftbaby = localroot;
localroot = temp;

adjustHeight(localroot);
adjustHeight(localroot->rightbaby);
adjustHeight(temp);

}

void AVL :: rotateRight(Node* localroot)
{
Node * temp = localroot->leftbaby;
localroot->leftbaby = temp->rightbaby;
temp->rightbaby = localroot;

adjustHeight(localroot);
adjustHeight(localroot->leftbaby);
adjustHeight(temp);

localroot = temp;


}

我的插入和插入递归函数编写如下

bool AVL :: add(int data)
{
if(!contains(root, data))
{
    insertRecursive(root,data);
    return true;
}else
{
    return false;
}
}


Node * AVL :: insertRecursive(Node * nodeme, int data)
{

// getting a number ready to print
int num1 = data;
string out1;
   ostringstream convert1;
  convert1 << num1;
  out1 = convert1.str();

 cout << "running recursive insert -- value: " << out1 << endl;

if(root == NULL)
{
    cout << "adding root node" << endl;

    Node * temporary = new Node(data);
    temporary->height = 0;
    root = temporary;
    return root;

}else
    if(nodeme == NULL)
    {
        cout << "adding at local" << endl;
        nodeme = new Node(data);
        nodeme->height = 0;
        return nodeme;
    }else
        if(data < nodeme->value)
        {
            nodeme->leftbaby = insertRecursive(nodeme->leftbaby,data);

            // balancing and stuff
            adjustHeight(nodeme);
            if ((returnBalance(nodeme) == -2 )&& (returnBalance(nodeme->leftbaby) == -1))
            {
                cout << "rotating right" << endl;
                rotateRight(nodeme);
            }else
            if((returnBalance(nodeme) == -2) && (returnBalance(nodeme->leftbaby) == 1))
            {
                cout << "rotating left ---- 1" << endl;
                rotateLeft(nodeme->leftbaby);
                cout << "rotating right" << endl;
                rotateRight(nodeme);
            }else
                if((returnBalance(nodeme) == 2) && (returnBalance(nodeme->leftbaby) == 1))
                {
                    cout << "rotating left" << endl;
                    rotateLeft(nodeme);
                }
            else
                if((returnBalance(nodeme) == 2) && (returnBalance(nodeme->leftbaby) == -1))
                {
                    cout << "rotating right --- 2" << endl;
                    rotateRight(nodeme->leftbaby);
                    cout << "rotating left" << endl;
                    rotateLeft(nodeme);
                }
            adjustHeight(nodeme);

            //
            return nodeme;
        }
        else
        {

            nodeme->rightbaby = insertRecursive(nodeme->rightbaby,data);


            // balancing and stuff
            adjustHeight(nodeme);
            if ((returnBalance(nodeme) == -2 )&& (returnBalance(nodeme->leftbaby) == -1))
            {
                cout << "rotating right" << endl;
                rotateRight(nodeme);
            }else
                if((returnBalance(nodeme) == -2) && (returnBalance(nodeme->leftbaby) == 1))
                {
                    cout << "rotating left --- 3" << endl;
                    rotateLeft(nodeme->leftbaby);
                    cout << "rotating right" << endl;
                    rotateRight(nodeme);
                }else
                    if((returnBalance(nodeme) == 2) && (returnBalance(nodeme->leftbaby) == 1))
                    {
                        cout << "rotating left" << endl;
                        rotateLeft(nodeme);
                    }
                    else
                        if((returnBalance(nodeme) == 2) && (returnBalance(nodeme-    >leftbaby) == -1))
                        {
                            cout << "rotating right --- 4" << endl;
                            rotateRight(nodeme->leftbaby);
                            cout << "rotating left" << endl;
                            rotateLeft(nodeme);
                        }
            cout << "adjust height" << endl;
            adjustHeight(nodeme);

            //


            return nodeme;
        }
}

cout s仅用于测试,我的类使用给定的测试驱动程序,因为事实证明我的树在添加0,1,2,3到9时不匹配。它在添加时失败2.注意此树中不允许重复的值,因此包含函数。

现在,在我的脑海里,我正在添加一棵树,更新高度,检查平衡,然后在我继续之前旋转,所以在我的脑海里,我保持树的平衡。显然这没有正确实现,所以我需要一些方向!

预期的树是:

0 1 0 0 1 2

我要回来了

0 0 1 1 11 2

我为此通过如此多的代码道歉,我确信没有多少人真的想要阅读它,但只是看了一眼你可能想要呕吐并提供一个很好的解释。

非常感谢! -MJ

1 个答案:

答案 0 :(得分:0)

轮流:

localroot = temp;

您应该通过引用传递参数。如果没有,则无法更改实际参数,您将获得内存泄漏。


AVL :: insertRecursive中的nodeme也是如此。