c ++中的AVL TREE

Zia*_*man 5 c++ avl-tree data-structures

这个非常简单的代码块有问题.请给我你的建议. (我的这个问题已经解决了,在解决这个问题时,有id stakx的人确实对我有帮助,唯一的问题是我正在使用stack <treeNode>,当我仔细看到堆栈的推送方法时,有一个复制过程当我写head-> object = number时,最后我做了一堆指针,就像这个堆栈<treeNode*>它真的解决了问题,我现在没有问题,我非常非常感谢stakx.)

在代码之前你需要支持以下树的

替代文本http://img44.imageshack.us/img44/7016/avlimage06.jpg
,你可以在图片中看到root是8,堆栈有两个节点,即6和4.我将此堆栈和根节点传递给以下代码

void Avltree::attachwithtree(treeNode* tree, Stack<treeNode>&s)
{
if(!s.isempty())
{
treeNode *stacknode;
stacknode=s.pop();
cout<<"\ninside the attachwithtree function, stack node is "<<stacknode->data;
stacknode->right=tree;//attaching the passed node to the right of popped node
root=stacknode;//setting the root to stack node which is the private data member of class
updatebalance(root);//this function is ok, it does not create problem
while(!s.isempty())
{
cout<<"\nstack is still not empty";
stacknode=s.pop();
cout<<"\nright side of "<<root->data<<" is "<<(root->right)->data;
//the below three lines causing the problem i don't know why,
root=stacknode;
treeNode* temp;
temp=root->right;
cout<<"\n\n\nthe right side of "<<temp->data<<" is now "<<(temp->right)->data;
updatebalance(root);
}
Run Code Online (Sandbox Code Playgroud)

这个函数的输出由
替代文本http://img704.imageshack.us/img704/5976/avlimage07.jpg给出


这是我正在使用的堆栈的pop方法的代码

template <class t>
t * Stack<t>::pop()
{
if(topelement!=NULL)
{
t* num;
current=topelement;
num=&(current->object);
topelement=topelement->preptr;
current=topelement;
return(num);
}
else
{
head=NULL;
}
}
Run Code Online (Sandbox Code Playgroud)


这是堆栈的推送方法的代码

template <class t>
void Stack<t>::push(t &number)
{
Node<t>* newNode=new Node<t>;
if(head==NULL)
{
head=newNode;
topelement=newNode;
current=newNode;
head->object=number;
head->preptr=NULL;
}
else
{
topelement=newNode;
newNode->preptr=current;
current=topelement;
newNode->object=number;
}
}
Run Code Online (Sandbox Code Playgroud)

sta*_*ica 4

原答案:

难道4堆栈上的节点6的右侧有一个与您正在处理的7节点6(右侧有节点的节点)不同的节点吗?8您可以比较它们的地址,以确保您没有两个不同的节点副本6

对上述论点的阐述:

让我们看看你的方法的签名:

void Avltree::attachwithtree(treeNode* tree, Stack<treeNode>&s)
Run Code Online (Sandbox Code Playgroud)

s被定义为对 a 的引用Stack<treeNode>

难道它应该是一个Stack<treeNode*>

根据您的treeNode类,当您压入该堆栈时,您实际上可能X会得到一个副本X而不是X其本身。同样,当您从堆栈中弹出时,您可能实际上并没有获得您推送的项目,而是获得了它的外观相同的副本!?

这意味着,当您将节点压6入堆栈时,它的右子节点是节点7。但是您已将一个新的、相同的节点推送到堆栈上。即使您从堆栈中弹出该元素并更改它,您也只更改了一个副本并保留原始树节点与之前一样。

因此,您将在节点的不同副本上进行操作6。首先,从堆栈中弹出它的副本,并附加一棵树作为其右子节点。检查这一点将给出正确的结果。

4然后,从堆栈中弹出节点的副本。6正如预期的那样,它的右子节点是 node ,不是您刚刚修改的节点,而是原始节点!因此,您位于7节点的右侧6

演示按值传递和按引用传递之间的区别:

好的,这是使用指针或引用时需要了解的一些内容。它基本上显示了按值传递参数(将创建副本)或按引用传递参数(不会创建副本)之间的区别。

仔细研究它,然后看看它如何应用于您的问题。

#include <iostream>

class someObject
{
private:
    int _value;
public:
    someObject(int value) : _value(value) { }

    int getValue()
    {
        return _value;
    }
};

void someFunction(someObject objCopy, someObject* objPtr)
{
    std::cout << "objCopy.getValue() -> " << objCopy.getValue() << std::endl;
    std::cout << "objPtr->getValue() -> " << objPtr->getValue() << std::endl;
    if ( &objCopy != objPtr )
    {
        std::cout << "objCopy is not actually *objPtr but a copy of it." << std::endl;
    }
    else
    {
        std::cout << "objCopy and *objPtr are one and the same object." << std::endl;
    }
}


int main()
{
    someObject X(17);
    someFunction(X, &X);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

提示:是的,在您的pop方法中,您使用指针,但最有可能使用指向最初压入堆栈的对象副本的指针。