我写了一个C程序来输入二叉搜索树的元素并显示它的InOrder,PostOrder和PreOrder遍历。
#include<stdio.h>
#include<stdlib.h>
struct tnode
{
int data;
struct tnode *leftc;
struct tnode *rightc;
};
int main()
{
char ans='N';
struct tnode *new_node,*root;
// struct tnode *get_node();
root=NULL;
do{
// new_node=get_node();
printf("\nEnter the Element");
scanf("%d",&new_node->data);
if(root==NULL)
root=new_node;
else
insert(root,new_node);
printf("\nDo you want to enter a new element?(y/n)");
scanf("%c",&ans);
}while(ans == 'y');
printf("Inorder traversal:the elements in the tree are");
inorder(root);
printf("\nPreorder traversal:the elements in the tree are");
preorder(root);
printf("Postorder traversal:the elements in the tree are");
postorder(root);
return 0;
}
void insert(struct tnode ** tree,int num)
{
struct tnode *temp = NULL;
if(!(*tree))
{
temp=(struct tnode *)malloc(sizeof (struct tnode));
temp->leftc=temp->rightc=NULL;
temp->data=num;
*tree=temp;
return;
}
if(num < (*tree)->data)
{
insert(&(*tree)->leftc,num);
}
else if(num > (*tree)->data)
{
insert(&(*tree)->rightc,num);
}
}
void preorder(struct tnode * s)
{
if(s)
{
printf("%d\n",s->data);
preorder(s->leftc);
preorder(s->rightc);
}
}
void inorder(struct tnode * s)
{
if(s)
{
inorder(s->leftc);
printf("%d\n",s->data);
inorder(s->rightc);
}
}
void postorder(struct tnode * s)
{
if(s)
{
postorder(s->leftc);
postorder(s->rightc);
printf("%d\n",s->data);
}
}
我收到这些警告信息:
warning: implicit declaration of functionS,
conflicting types OF FUNCTIONS,
new_node’ may be used uninitialized in this function
我无法理解错误。你能帮我解决这些问题吗?
答案 0 :(得分:3)
在C中为了使用函数你需要在main函数之前声明em,就像在你的情况下你应该写:
void insert(struct tnode ** tree,int num);
//all declarations of other functions here .
//顺便说一下你可以声明em而没有像这样的变量名称:
void insert(struct tnode ** , int );
也只是尝试在C中使用谷歌二进制搜索树。 有很多网站可以准确显示您正在寻找的答案,还有很多网站都提供了解释其周围所有内容的教程。
P.S 如果你不想在main函数之前声明函数,你可以把你在main函数和main函数上面的ready函数放在最后的底部。
答案 1 :(得分:2)
insert()
的使用是错误的。ans
。在格式说明符%c
之前添加一个空格,让scanf()
在读取字符之前跳过空白字符。main()
的标准签名之一。在C中,int main()
和int main(void)
have different meanings。malloc()
in C。试试这个:
#include<stdio.h>
#include<stdlib.h>
struct tnode
{
int data;
struct tnode *leftc;
struct tnode *rightc;
};
/* declare functions */
void insert(struct tnode ** tree,int num);
void preorder(struct tnode * s);
void inorder(struct tnode * s);
void postorder(struct tnode * s);
/* use one of the standard forms of main() */
int main(void)
{
char ans='N';
struct tnode *root;
int new_node_data;
// struct tnode *get_node();
root=NULL;
do{
// new_node=get_node();
printf("\nEnter the Element");
scanf("%d",&new_node_data); /* do not dereference indeterminate pointer */
insert(&root,new_node_data); /* pass correct data */
printf("\nDo you want to enter a new element?(y/n)");
scanf(" %c",&ans); /* add a space before %c to have scanf() skip whitespace characters */
}while(ans == 'y');
printf("Inorder traversal:the elements in the tree are");
inorder(root);
printf("\nPreorder traversal:the elements in the tree are");
preorder(root);
printf("Postorder traversal:the elements in the tree are");
postorder(root);
return 0;
}
void insert(struct tnode ** tree,int num)
{
struct tnode *temp = NULL;
if(!(*tree))
{
temp=malloc(sizeof (struct tnode));
temp->leftc=temp->rightc=NULL;
temp->data=num;
*tree=temp;
return;
}
if(num < (*tree)->data)
{
insert(&(*tree)->leftc,num);
}
else if(num > (*tree)->data)
{
insert(&(*tree)->rightc,num);
}
}
void preorder(struct tnode * s)
{
if(s)
{
printf("%d\n",s->data);
preorder(s->leftc);
preorder(s->rightc);
}
}
void inorder(struct tnode * s)
{
if(s)
{
inorder(s->leftc);
printf("%d\n",s->data);
inorder(s->rightc);
}
}
void postorder(struct tnode * s)
{
if(s)
{
postorder(s->leftc);
postorder(s->rightc);
printf("%d\n",s->data);
}
}