【发布时间】:2012-11-25 04:27:52
【问题描述】:
我有三个函数来管理二叉树:
static void insertion(Noeud* &top, Noeud *newNoeud)
{
if(top == NULL)
top = newNoeud;
else if(newNoeud->nbr < top->nbr)
insertion(top->left, newNoeud);
else
insertion(top->right, newNoeud);
}
static void affichage(Noeud* &top) //displaying
{
if(top != NULL)
{
affichage(top->left);
affichage(top->right);
cout << "\n" << top->nbr;
}
}
static Noeud* recherche(Noeud* &top, int nbr) //searching
{
while(top != NULL)
{
if(top->nbr == nbr)
return(top);
else if(nbr < top->nbr)
top = top->left;
else
top = top->right;
}
}
但是我一直收到错误消息,说我在尝试读取内存点时违反了访问权限。我猜这与我的指针有关,但我猜不出它是什么。
【问题讨论】:
-
你试过在调试器中运行吗?它究竟在哪里崩溃?如果您使用的是 Linux,您是否尝试过 valgrind?
-
因为
static Noeud* recherche(Noeud* &top, int nbr)表示它在 C++ 中的含义,所以您将通过引用传递top,top = top->left;resp。top = top->right;会毁掉你的树。
标签: c++ pointers recursion tree