【问题标题】:Inorder , Preorder and Postorder traversals中序、前序和后序遍历
【发布时间】:2016-07-14 14:03:17
【问题描述】:

我编写了一个 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

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

【问题讨论】:

  • 代码请缩进,否则我就不看了。
  • 没有看代码,但关于第一个警告:在 C 中,您必须在使用 therm 之前声明函数。事实上,你必须在使用它之前声明everything。至于其他的,new_node变量在使用前有没有初始化?

标签: c inorder preorder postorder


【解决方案1】:

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

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

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

void insert(struct tnode ** , int );

也只是尝试在 C 中搜索二叉搜索树。 有许多网站可以准确显示您正在寻找的答案,也有许多网站提供解释所有相关内容的教程。

PS 如果你不想在 main 函数之前声明函数,你可以把 main 函数上面的准备好的函数放在最后。

【讨论】:

    【解决方案2】:
    • 您必须在使用它们之前声明或定义要使用的函数
    • 您对insert() 的使用是错误的。
    • 使用具有自动存储持续时间的未初始化变量的值是不确定的,会调用未定义的行为。在这种情况下,您不必使用结构来读取新节点的数据。
    • 您意想不到的内容可能会被读入ans。在格式说明符%c 之前添加一个空格,让scanf() 在读取字符之前跳过空白字符。
    • 您应该使用main() 的标准签名之一。在 C 中,int main()int main(void) have different meanings
    • 您应该正确地格式化您的代码。
    • 他们说you shouldn't cast the result of 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);
        }
    }
    

    【讨论】:

    • 感谢您的帮助.. 但在输出中只有第一个元素被打印为所有 3 次遍历
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2013-01-22
    • 1970-01-01
    • 2011-03-01
    相关资源
    最近更新 更多