我正在构建一个AVL树程序。我陷入了一个相当容易的境地,但很难理解什么是错的。我认为这是该程序的错误,而不是我的原因是因为我有相同的类功能之前,它与“左”和“右”交换,它工作得很好...成员函数返回变量的前一个值
正如你所看到的,该函数返回temproot
指针,该指针等于temp2
,如果root==temp
。有趣的是,虽然当我测试打印temproot
JUST之前,它的价值(在我的例子中是15),返回的实际值是STILL 20(以前的值temproot
)。我三重检查了一切。它似乎没有返回新获得的价值......可能是什么问题?
更具体,确切的代码是这样的:
//structure
struct avlnode
{
int data;
avlnode * left;
avlnode * right;
}* root;
//class function
avlnode * Tree::RL_rotation (avlnode * temp)
{
avlnode * temproot = temp;
avlnode * temp1= new avlnode;
temp1=temp->right;
avlnode * temp2= new avlnode;
temp2=temp1->left;
temp1->left=temp2->right;
temp2->right=temp1;
temp->right=temp2;
temp->right=temp2->left;
temp2->left=temp;
if (root==temp)
{
root=temp2;
temproot=temp2;
}
cout << "temproot= " << temproot->data << endl;
return temproot;
}
如果你说的是真的,它可能是缓冲区溢出(内存损坏)问题。 – SergeyA
你如何存储函数的返回值? – NathanOliver
我不相信你。你是如何“检查”这些东西的? 'temproot = temp2;'真的被执行了吗? –