C 二叉搜索树指针问题
我正在尝试实现一个应该在leaf中添加值的函数,这里是我的代码 节点结构: 节点包含两个元素:左元素和右元素,以及它的dadi(前元素)和值C 二叉搜索树指针问题,c,algorithm,pointers,recursion,binary-search-tree,C,Algorithm,Pointers,Recursion,Binary Search Tree,我正在尝试实现一个应该在leaf中添加值的函数,这里是我的代码 节点结构: 节点包含两个元素:左元素和右元素,以及它的dadi(前元素)和值 struct arbre { int val ; struct arbre * D; struct arbre * G ; struct arbre * pere ; }; 以下是函数: 该函数始终在叶子上添加值 void ajout_feuille( struct arbre** arbre , int value ) { struct arb
struct arbre {
int val ;
struct arbre * D;
struct arbre * G ;
struct arbre * pere ;
};
以下是函数:
该函数始终在叶子上添加值
void ajout_feuille( struct arbre** arbre , int value )
{
struct arbre* q ,*p ;
if ( *arbre == NULL)
{
q=malloc(sizeof(struct arbre ));
q->D=NULL ;
q->G=NULL;
q->pere=NULL;
q->val=value;
*arbre=q ;
}
else
{
p=*arbre;
while(p!=NULL)
{
if (p->val<value) //if the value is > we go on the right shild;
{
if (p->D==NULL) // if we are on the leaf
{
q=malloc(sizeof(struct arbre ));
q->val=value;
q->D=NULL;
q->G=NULL;
q->pere=p;
p->D=q;
}else
{
p=p->D;
}
}else
{
if (p->val>value)
{
if(p->G==NULL)
{
q=malloc(sizeof(struct arbre ));
q->val=value;
q->D=NULL;
q->G=NULL;
q->pere=p;
p->G=q;
}
}else { p=p->G; }
}
}
}
}
以下是主要方法:
int main()
{
struct arbre * A=NULL ;
ajout_feuille(&A,5);
ajout_feuille(&A,4);
affichage(A);
return 0;
}
问题是:显示方法没有显示任何内容。我认为指针中存在问题。更多的指针和变量并不能使代码更易于阅读。不打印的原因是枚举循环从未终止 您的代码有两个代码无限循环问题。请记住,在这段代码中没有
break
语句,因此唯一释放while循环的是当p
变为NULL时
- 左侧结束的遍历永远不会退出循环
- 重复条目永远不会退出循环
p
设置为NULL。考虑右侧遍历和左侧遍历之间的差异。
右侧遍历执行以下操作:
if (p->val<value) //if the value is >, we go on the right child;
{
if (p->D==NULL) // if we are on the leaf
{
q=malloc(sizeof(struct arbre ));
q->val=value;
q->D=NULL;
q->G=NULL;
q->pere=p;
p->D=q;
}
else
{
p=p->D;
}
}
注意else条件现在挂起的位置。首先,这是错误的,其次,if和else确实不应该出现在第一位,除非您特别尝试构建一个集合(即,没有重复的键),如果是这样,您仍然需要一个exit子句来打破本来没完没了的while(p!=NULL)
状态
下面列出了(a)不允许重复的代码的完整功能版本(您似乎将其作为目标),以及(b)允许重复的代码的完整功能版本。然后,我强烈建议您重新考虑另一种选择
修复#1-无重复项
void ajout_feuille0( struct arbre** arbre , int value )
{
struct arbre* q ,*p ;
if ( *arbre == NULL)
{
q=malloc(sizeof(struct arbre ));
q->D=NULL ;
q->G=NULL;
q->pere=NULL;
q->val=value;
*arbre=q ;
}
else
{
p = *arbre;
while (p!=NULL)
{
if (p->val<value) //if the value is > we go on the right shild;
{
if (p->D==NULL) // if we are on the leaf
{
q=malloc(sizeof(struct arbre ));
q->val=value;
q->D=NULL;
q->G=NULL;
q->pere=p;
p->D=q;
}
else
{
p=p->D;
}
}
else if (value < p->val) // else if less we go down the left side.
{
if(p->G==NULL)
{
q=malloc(sizeof(struct arbre ));
q->val=value;
q->D=NULL;
q->G=NULL;
q->pere=p;
p->G=q;
}
else
{
p=p->G;
}
}
else // else the value is already in the tree.
{
break;
}
}
}
}
void ajout_feuille( struct arbre** tree , int value )
{
struct arbre *pere = NULL;
while (*tree)
{
pere = *tree;
// left side ?
if (value < (*tree)->val)
tree = &(*tree)->G;
// right side ?
else if ((*tree)->val < value)
tree = &(*tree)->D;
else // duplicate found
break;
}
if (!*tree) // null means we have a place to hang a node.
{
*tree = malloc(sizeof **tree);
if (!*tree)
{
perror("Failed to allocate new tree node");
exit(EXIT_FAILURE);
}
(*tree)->val = value;
(*tree)->pere = pere;
(*tree)->G = NULL;
(*tree)->D = NULL;
}
}
void ajout_feuille( struct arbre** tree , int value )
{
struct arbre *pere = NULL;
while (*tree)
{
pere = *tree;
// left side ?
if (value < (*tree)->val)
tree = &(*tree)->G;
// els ejust move to right
else tree = &(*tree)->D;
}
if (!*tree) // null means we have a place to hang a node.
{
*tree = malloc(sizeof **tree);
if (!*tree)
{
perror("Failed to allocate new tree node");
exit(EXIT_FAILURE);
}
(*tree)->val = value;
(*tree)->pere = pere;
(*tree)->G = NULL;
(*tree)->D = NULL;
}
}
允许重复项
void ajout_feuille0( struct arbre** arbre , int value )
{
struct arbre* q ,*p ;
if ( *arbre == NULL)
{
q=malloc(sizeof(struct arbre ));
q->D=NULL ;
q->G=NULL;
q->pere=NULL;
q->val=value;
*arbre=q ;
}
else
{
p = *arbre;
while (p!=NULL)
{
if (p->val<value) //if the value is > we go on the right shild;
{
if (p->D==NULL) // if we are on the leaf
{
q=malloc(sizeof(struct arbre ));
q->val=value;
q->D=NULL;
q->G=NULL;
q->pere=p;
p->D=q;
}
else
{
p=p->D;
}
}
else if (value < p->val) // else if less we go down the left side.
{
if(p->G==NULL)
{
q=malloc(sizeof(struct arbre ));
q->val=value;
q->D=NULL;
q->G=NULL;
q->pere=p;
p->G=q;
}
else
{
p=p->G;
}
}
else // else the value is already in the tree.
{
break;
}
}
}
}
void ajout_feuille( struct arbre** tree , int value )
{
struct arbre *pere = NULL;
while (*tree)
{
pere = *tree;
// left side ?
if (value < (*tree)->val)
tree = &(*tree)->G;
// right side ?
else if ((*tree)->val < value)
tree = &(*tree)->D;
else // duplicate found
break;
}
if (!*tree) // null means we have a place to hang a node.
{
*tree = malloc(sizeof **tree);
if (!*tree)
{
perror("Failed to allocate new tree node");
exit(EXIT_FAILURE);
}
(*tree)->val = value;
(*tree)->pere = pere;
(*tree)->G = NULL;
(*tree)->D = NULL;
}
}
void ajout_feuille( struct arbre** tree , int value )
{
struct arbre *pere = NULL;
while (*tree)
{
pere = *tree;
// left side ?
if (value < (*tree)->val)
tree = &(*tree)->G;
// els ejust move to right
else tree = &(*tree)->D;
}
if (!*tree) // null means we have a place to hang a node.
{
*tree = malloc(sizeof **tree);
if (!*tree)
{
perror("Failed to allocate new tree node");
exit(EXIT_FAILURE);
}
(*tree)->val = value;
(*tree)->pere = pere;
(*tree)->G = NULL;
(*tree)->D = NULL;
}
}
void ajout\u feuille(结构arbre**树,int值)
{
结构arbre*pere=NULL;
while(*树)
{
pere=*树;
//左边?
if(值<(*树)->val)
tree=&(*tree)->G;
//请向右移动
else-tree=&(*tree)->D;
}
if(!*tree)//null表示我们有地方挂起节点。
{
*树=malloc(大小**树);
如果(!*树)
{
perror(“分配新树节点失败”);
退出(退出失败);
}
(*树)->val=值;
(*树)->pere=pere;
(*tree)->G=NULL;
(*tree)->D=NULL;
}
}
在这两种情况下,代码都要干净得多,并且避免了通过外部while的额外循环
希望能有所帮助。您实际上还没有问任何问题,而且
ajout\u feuille
的代码不可读。欢迎使用StackOverflow。看见在您发布MRE代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您指定的问题。由于您没有输出,也没有指定任何问题,因此这不是一个有效的SO问题。display函数没有显示任何内容,我认为ajout_feuille函数中的问题是树是递归数据结构。使用递归函数来处理它们比循环简单得多。仅供参考,看在上帝的份上,记录该结构,或者最好仍然使用有意义的成员名称。还记得你的指导老师告诉你“让你的代码易于阅读,因为你不会一直是阅读它的人”吗?好了,就是这样。我们不应该破译你的密码left
、right
和parent
都是很好的成员名称,阅读本文的人都会非常清楚。只是说说而已。