[英]My binary search tree c program has a problem.I am using inorder traversal.only root node printed why?
it is a binary search tree with inorder traversal.there is an issue while printing elements.它是一个具有中序遍历的二叉搜索树。打印元素时出现问题。 I can't solve the issue.Only the root is gettting printed.it could be a semantic error also.Maybe there is issue in inserting.or some issue in displaying inorderly.我无法解决这个问题。只有根正在打印。它也可能是语义错误。也许插入有问题。或者显示无序有问题。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *llink;
struct node *rlink;
};
typedef struct node bstnode;
bstnode *root=NULL;
void insert(int ele)
{
bstnode *cur,*prev,*temp;
temp=(bstnode *)malloc(sizeof(bstnode));
temp->data=ele;
temp->llink=temp->rlink=NULL;
cur=root;
prev=NULL;
if(root==NULL)
root=temp;
else
{
prev=cur;
while(cur!=NULL)
{
if(ele<cur->data)
cur=cur->llink;
else
cur=cur->rlink;
}
if(ele<prev->data)
prev->llink=temp;
else
prev->rlink=temp;
}
//return root;
}
void inorder()
{
if(root==NULL)
{
return;}
else{
inorder(root->llink);
printf("\n%d",root->data);
inorder(root->llink);
}
}
int main()
{
insert(45);
insert(76);
inorder();
}
In this code snippet在这个代码片段中
else
{
prev=cur;
while(cur!=NULL)
{
if(ele<cur->data)
cur=cur->llink;
else
cur=cur->rlink;
}
if(ele<prev->data)
prev->llink=temp;
else
prev->rlink=temp;
}
there is a logical mistake.有一个逻辑错误。 The statement该声明
prev=cur;
shall be within the following while statement.应在以下 while 语句中。 For example例如
while(cur!=NULL)
{
prev=cur;
if(ele<cur->data)
cur=cur->llink;
else
cur=cur->rlink;
}
And the function inorder
.和函数inorder
。 Also incorrect because it is declared without a parameter同样不正确,因为它是在没有参数的情况下声明的
void inorder()
{
if(root==NULL)
{
return;}
else{
inorder(root->llink);
printf("\n%d",root->data);
inorder(root->llink);
}
}
Also there is used the data member llink
two times.还有两次使用数据成员llink
。
It should be declared and defined like它应该像这样声明和定义
void inorder( const bstnode *node )
{
if ( node )
{
inorder( node->llink );
printf( "\n%d", node->data );
inorder( node->rlink );
}
}
And it is called like它被称为
inorder( root );
Firstly, the part首先,部分
prev=cur;
while(cur!=NULL)
{
if(ele<cur->data)
cur=cur->llink;
else
cur=cur->rlink;
}
is wrong.是错的。
Due to the wrong place of prev=cur;
由于prev=cur;
位置不对prev=cur;
, prev
will be only root
instead of pointing at the node to be the parent of the new node. , prev
将只是root
而不是指向要成为新节点父节点的节点。 The statement should be inside the loop:该语句应该在循环内:
while(cur!=NULL)
{
prev=cur;
if(ele<cur->data)
cur=cur->llink;
else
cur=cur->rlink;
}
Secondly, the function inorder
is wrong.其次,函数inorder
错误。 It is executed unconditionally when root != NULL
.当root != NULL
时无条件执行。
Instaed of ignoring arguments and print root
infinitely, it should take an argument to specify which node should be printed.代替忽略参数并无限打印root
,它应该使用一个参数来指定应该打印哪个节点。
Using llink
twich is also weird.使用llink
也很奇怪。
It can be like this:它可以是这样的:
void inorder(bstnode* node)
{
if(node==NULL)
{
return;
}
else{
inorder(node->llink);
printf("\n%d",node->data);
inorder(node->rlink);
}
}
And inorder();
和inorder();
in the main
function should be inorder(root);
在main
函数中应该是inorder(root);
. .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.