【发布时间】:2016-07-14 21:50:42
【问题描述】:
我已经创建了我的二叉搜索树并将指向要删除的节点的指针指向我的*p。
delete 方法应该是删除*p 指向的节点,并且应该将addtree 的子树添加到我的根目录。 *pBaum 是指向我的根的指针。
但是,每次我声明时,我都会在 addtree 上收到一条名为“冲突类型”的错误消息
Baum = addtree(Baum, p->right);
我还收到一条警告“赋值使指针从整数不进行强制转换”
我的结构包含指向子树的左右指针和指向内容的指针。
struct tnode
{
int content;
struct tnode *left; /* linker Teilbaum */
struct tnode *right; /* rechter Teilbaum */
};
// Deletes the node where *p is pointing at
struct tnode *deletenode(struct tnode *p, struct tnode *pBaum)
{
struct tnode *Baum = pBaum;
if ((p->left == NULL) && (p->right == NULL))
{
free(p);
}
if ((p->left == NULL) && (p->right != NULL))
{
Baum = addtree(Baum, p->right);
free(p);
}
if ((p->right == NULL) && (p->left !=NULL))
{
Baum = addtree(Baum, p->left);
free(p);
}
if ((p->left != NULL) && (p->right !=NULL))
{
Baum = addtree(Baum, p->right);
Baum = addtree(Baum, p->left);
free(p);
}
return Baum;
}
// Adds the Subtrees to my root
struct tnode *addtree(struct tnode *top, struct tnode *p)
{
if (p == NULL)
return top;
else
return addtree(addtree(addelement(top, p->content),p-> right), p->left);
// Adds a node to my Tree
struct tnode *addelement(struct tnode *p, int i)
{
int cond;
if (p == NULL)
{
p = talloc(); /* make a new node */ p->content = i;
p->left =p->right =NULL;
}
else if (p->content == i)
{
return p;
}
else if (i < p->content) /* goes into left subtree */ p->left =addelement(p->left, i);
else /* goes into right subtree */ p->right = addelement(p->right, i);
return p;
}
// Looks for the node which is supposed to get deleted and returns a pointer to it
struct tnode *searchnode(struct tnode *p, int nodtodelete)
{
if (p == NULL)
{
printf("Baum ist leer oder Element nicht vorhanden \n");
return NULL;
}
if ( p -> content == nodtodelete)
{
return p;
}
if (p->content < nodtodelete)
{
return searchnode (p->right, nodtodelete);
}
if (p->content > nodtodelete)
{
return searchnode(p->left, nodtodelete);
}
}
}
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);
Baum = deletenode(tmpPos, Baum);
}
【问题讨论】:
-
实际的错误信息是什么?
-
错误:'addtree' 的类型冲突
-
不,完整的错误信息,包括文件名和行号以及错误的其余部分。
-
阅读How to Ask,整理你的代码,更具体地解决问题
-
@RobinSchmidt “不想发布整个代码。如果你愿意,我可以” 不,准备一个 minimal reproducible example 代替,显示行为/错误问题。
标签: c pointers binary-tree binary-search-tree