树的创建和遍历

#include <stdio.h>
#include <stdlib.h>

struct node{
    char data;

10年积累的成都网站设计、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有扶余免费网站建设让你可以放心的选择与我们合作。

    struct node* left;
    struct node* right;
};

void preorder(struct node* root)        //前序遍历
{
    if(root == NULL)
        return ;
    else {
        printf("%c\t", root->data);
        pre_order(root->left);
        pre_order(root->right);
    }
}
void minorder(struct node* root)        //中序遍历
{
    if(root == NULL)
        return ;
    else {
        min_order(root->left);
        printf("%c\t", root->data);
        min_order(root->right);
    }
}
void postorder(struct node* root)        //后序遍历
{
    if(root == NULL)
        return ;
    else {
        postorder(root->left);
        postorder(root->right);
        printf("%c\t", root->data);
    }
}
struct node* create(struct node* root)    //利用前序创建树,中序和后序不能创建树
{
    char ch = getchar();    
    if(ch == '#')
        return NULL;
    else {
        root = malloc(sizeof(struct node));
        root->data = ch;
        root->left = create(root->left);
        root->right = create(root->right);
        return root;
    }
}

int main()
{
    struct node* root = NULL;

    root = create(root);

    preorder(root);
    printf("\n");
    minorder(root);
    printf("\n");
    postorder(root);
    printf("\n");

    return 0;
}




网页标题:树的创建和遍历
链接URL:https://www.cdcxhl.com/article26/pocejg.html

成都网站建设公司_创新互联,为您提供App设计品牌网站建设外贸网站建设企业建站关键词优化手机网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化