,所以我C编码的二叉搜索树看起来与这个结构:二叉搜索树
struct tnode
{
int content;
struct tnode *left; /* left tree part */
struct tnode *right; /* right tree part */
};
我的主要方法:
int main()
struct tnode *Baum = NULL;
struct tnode *tmpPos = NULL;
Baum = addelement (Baum, 32);
Baum = addelement(Baum, 50);
Baum = addelement(Baum, 60);
tmpPos = searchnode(Baum,50);
所以basicly这造成我3二叉搜索树元素(32,50,60)。我的searchnode方法应该将指针移动到“50”,这样我可以在之后删除它。然而,如果搜索的元素是我的二叉搜索树的根,我的searchnode方法只返回指针。
searchnode:
struct tnode *searchnode(struct tnode *p, int nodtodelete)
{
if (p == NULL)
{
printf("Baum ist leer oder Element nicht vorhanden \n");
}
if (p -> content == nodtodelete)
{
return p;
}
if (p->content > nodtodelete)
{
searchnode (p->right, p->content);
}
if (p->content < nodtodelete)
{
searchnode(p->left, p->content);
}
}
也许你们能帮助我。
难道你没有将左/右子树的决定颠倒过来吗? 'if(p-> content> nodtodelete)'你应该走左路。 –
欢迎来到本站!查看[tour](https://stackoverflow.com/tour)以获得更多关于提问的问题,以吸引高质量的答案。你是否已经确认'addelement()'正常工作? – cxw
'searchnode'不会在4个if中的3个中返回任何结果。 – halex