c - 序、前序和后序遍历



我编写了一个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("%dn",s->data);
        preorder(s->leftc);
        preorder(s->rightc);
    }
}
void inorder(struct tnode * s)
{
    if(s)
    {
        inorder(s->leftc);
        printf("%dn",s->data);
        inorder(s->rightc);
    }
}
void postorder(struct tnode * s)
{
    if(s)
    {
        postorder(s->leftc);
        postorder(s->rightc);
        printf("%dn",s->data);
    }
}

我收到以下警告消息:

warning: implicit declaration of functionS,
conflicting types OF FUNCTIONS,
new_node’ may be used uninitialized in this function

我无法理解错误。你能帮我解决这些问题吗?

在 C 中,为了使用函数,您需要在 main 函数之前声明 em,就像在您的情况下,您应该编写:

void insert(struct tnode ** tree,int num);
//all declarations of other functions here . 
/

/顺便说一句,你可以在没有变量名称的情况下声明 em,如下所示:

void insert(struct tnode ** , int );

也只是尝试谷歌二叉搜索树在C.有许多网站可以准确显示您正在寻找的答案,并且许多网站提供了解释其周围一切的教程。

附言如果你不想在 main 函数之前声明函数,你可以把你拥有的 main 和 main 函数的上部的 ready 函数放在末尾的底部。

  • 在使用函数之前,必须声明或定义要使用的函数。
  • 你对insert()的使用是错误的。
  • 使用具有自动存储持续时间(不确定)的未初始化变量的值调用未定义的行为。在这种情况下,不必使用结构来读取新节点的数据。
  • 你不期望的东西可能会读进ans。在格式说明符%c之前添加一个空格,以便scanf()在读取字符之前跳过空格标记。
  • 您应该使用 main() 的标准签名之一。在C中,int main()int main(void)具有不同的含义。
  • 您应该正确格式化代码。
  • 他们说你不应该用 C 来投malloc()的结果。

试试这个:

#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("%dn",s->data);
        preorder(s->leftc);
        preorder(s->rightc);
    }
}
void inorder(struct tnode * s)
{
    if(s)
    {
        inorder(s->leftc);
        printf("%dn",s->data);
        inorder(s->rightc);
    }
}
void postorder(struct tnode * s)
{
    if(s)
    {
        postorder(s->leftc);
        postorder(s->rightc);
        printf("%dn",s->data);
    }
}

最新更新