数据结构之 二叉树的储存和遍历总结

知道前序(包括空结点 下面代码用’,’代替)建立一个二叉树,前序 中序 后序 层序输出 如何求叶子结点数, 如何求二叉树深度。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
    char data;
    struct node *lt, *rt;
}ST;
char str[52];
int flag, num;
ST *creat()
{
    ST *root;
    if(str[++flag] == ',')
    {
        root = NULL;
    }
    else
    {
        root = (ST *)malloc(sizeof(ST));
        root->data = str[flag];
        root->lt = creat();
        root->rt = creat();
    }
    return root;
}
void midsort(ST *root)
{
    if(root)
    {
        midsort(root->lt);
        printf("%c", root->data);
        midsort(root->rt);
    }
}
void lastsort(ST *root)
{
    if(root)
    {
        lastsort(root->lt);
        lastsort(root->rt);
        printf("%c", root->data);
    }
}
void headsort(ST *root)
{
    if(root)
    {
        printf("%c", root->data);
        headsort(root->lt);
        headsort(root->rt);
    }
}
void input(ST *root)
{
    ST *team[100];
    int head, last;
    head = 0; last = 0;
    team[last++] = root;
    while(head < last)
    {
        if(team[head]){
        printf("%c", team[head]->data);
        team[last++] = team[head]->lt;
        team[last++] = team[head]->rt;
        }
        head++;
    }
}
void leaf(ST *root)
{
    if(root)
    {
        if(!root->lt && !root->rt)
        num++;
        leaf(root->lt);
        leaf(root->rt);
    }
}
int deep(ST *root)
{
    int max, l, r;
    if(root)
    {
        l = deep(root->lt);
        r = deep(root->rt);
        if(l > r) max = l;
        else max = r;
        return max + 1;
    }
    else return 0;
}
int main()
{
    ST *root;
    while(~scanf("%s", str))
    {
    num = 0;
        flag = -1;
        root = creat();
        headsort(root);/*前序输出*/
        printf("\n");
        midsort(root);/*中序输出*/
        printf("\n");
        lastsort(root);/*后序输出*/
        printf("\n");
        input(root);/*层序输出*/
        printf("\n");
        leaf(root);
        printf("%d\n", num);/*输出叶子结点数*/
        printf("%d\n", deep(root));/*二叉树深度的求法*/
    }
    return 0;
}

知道前序和中序建立二叉树过程

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
    char data;
    struct node *lt, *rt;
}ST;
ST *creat(int n, char a[], char b[])
{
    ST *root;
    int i;
    if(n == 0)
    return NULL;
    root = (ST *)malloc(sizeof(ST));
    root->data = a[0];
    for(i = 0; b[i]; i++)
    if(b[i] == a[0]) break;
    root->lt = creat(i, a + 1, b);
    root->rt = creat(n - i - 1, a + i + 1, b + i + 1);
    return root;
}
void lastsort(ST *root)
{
    if(root)
    {
        lastsort(root->lt);
        lastsort(root->rt);
        printf("%c", root->data);
    }
}
int main()
{
    ST *root;
    int len;
    char a[100], b[100];
    while(~scanf("%s %s", a, b))
    {
        len = strlen(a);
        root = creat(len, a, b);
        lastsort(root);
        printf("\n");
    }
    return 0;
}

知道中序和后序建二叉树过程

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
    char data;
    struct node *lt, *rt;
}ST;
ST *creat(int n, char a[], char b[])//后序,中序
{
    ST *root;
    int i;
    if(n == 0)
    return NULL;
    root = (ST *)malloc(sizeof(ST));
    root->data = a[n-1];
    for(i = 0; a[i]; i++)
    if(b[i] == a[n-1]) break;
    root->lt = creat(i, a, b);
    root->rt = creat(n - i - 1, a + i, b + i + 1);
    return root;
}
void headsort(ST *root)
{
    if(root){
    printf("%c", root->data);
    headsort(root->lt);
    headsort(root->rt);
    }
}
int main()
{
    ST *root;
    int t, len, i, j;
    char a[55], b[55], t1;
    while(~scanf("%d", &t))
    {
        while(t--)
        {
            memset(a, 0, sizeof(a));
            memset(b, 0, sizeof(b));
            scanf("%s %s", b, a);
            len = strlen(b);
            root = creat(len, a, b);
            headsort(root);
            printf("\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值