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

关于二叉树的存储和遍历主要有一下几个知识点: 遍历的四种方法,建树的几种方法;

话不多说直接上代码:

 因为是节省时间和节省空间我把他们合在了一起,此代码仅供参考,使用时要注意代码的正确性,也可以抓取想要的那部分的代码,如有错误还请各位多多包涵

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
    struct node *l, *r;
    char data;
};
char str[60];
int flag, sum;
struct node *creat() //根据先序(带有空格)建立二叉树
{
    struct node *root;
    flag++;
    if(str[flag] == ',')
    {
        root = NULL;
    }
    else
    {
        root = (struct node *)malloc(sizeof(struct node));
        root->data = str[flag];
        root->l = creat();
        root->r = creat();
    }
    return root;
};
void headcreat(struct node *root) //前序遍历二叉树
{
    if(root)
    {
        printf("%c",root->data);
        headcreat(root->l);
        headcreat(root->r);
    }
}
void midcreat(struct node *root) //中序遍历二叉树
{
    if(root)
    {
        midcreat(root->l);
        printf("%c",root->data);
        midcreat(root->r);
    }
}
void lastcreat(struct node *root) //后序遍历二叉树
{
    if(root)
    {
        lastcreat(root->l);
        lastcreat(root->r);
        printf("%c",root->data);
    }
}
void ccreat(struct node *root)  //层序遍历
{
    int head, end;
    struct node *s[60];//s数组要定义成指针的形式
    head = end = 0; //层序遍历的时候要用到队列的知识,都指为0;
    s[end] = root;
        end++;    //注意应该先赋值++
    while(head < end)
    {
        if(s[head])
        {
           printf("%c",s[head]->data);
           s[end] = s[head]->l;
           end++;
           s[end] = s[head]->r;
            end++;
        }
        head++;
    }
}
void leaf(struct node *root) //计算叶子节点数
{
    if(root)
    {
        if(root->l == NULL&&root->r == NULL)
         sum++;
        leaf(root->l);
        leaf(root->r);
    }
}
int deep(struct node *root)  //计算树的深度
{
    int max, lt, rt;
    if(root)
    {
        lt = deep(root->l);
        rt = deep(root->r);
        if(lt > rt)
            max = lt;
        else max = rt;
        return (max+1);
    }
    else return 0;
}
int main()
{
   struct node *root;
    while(~scanf("%s",str))
    {
        sum = 0;
        flag = -1;
        root = creat();
        headcreat(root);//前序遍历二叉树
        printf("\n");
        midcreat(root);//中序遍历二叉树
        printf("\n");
        lastcreat(root);//后序遍历二叉树
        printf("\n");
        ccreat(root);//层序遍历
        printf("\n");
        leaf(root);  //计算叶子节点数
        printf("%d\n",sum);
        printf("%d\n",deep(root)); //计算树的深度
    }
    return 0;
}

 

数据结构之二叉树的建立方法之二:

1、先序+中序

2、 后序+中序

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
    char data;
    struct node *l,*r;
};
struct node *creat(int n,char a[], char b[])
{
    struct node *root;
    int i;
    if(n == 0)
        return NULL;
    root = (struct node *)malloc(sizeof(struct node));
    root->data = a[0];
    for(i = 0;b[i];i++)
    {
        if(b[i] == a[0])
            break;
    }
    root->l = creat(i,a+1,b);
    root->r = creat(n-i-1,a+i+1,b+i+1);
    return root;
};
void lastcreat(struct node *root)
{
    if(root)
    {
        lastcreat(root->l);
        lastcreat(root->r);
        printf("%c",root->data);
    }
}
int main()
{
    struct node *root;
    char str1[60], str2[60];
    int len;
    scanf("%s %s",str1,str2);
    len = strlen(str1);
        root = creat(len,str1,str2);
        lastcreat(root);
        printf("\n");
    return 0;
}

关于先序+中序,在一场个人赛中有所体现

Accepted 代码如下:

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

 

后序+中序算法: 注意要和先序+中序的算法对记忆

 

 

​​​​​​​​​​​​​​​​​​​​​​​#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
    char data;
    struct node *l, *r;
};
struct node *creat(int n, char a[], char b[]) //a是后序,b是中序
{
    struct node *root;
    int i;
    if(n == 0)
    return NULL;
    root = (struct node *)malloc(sizeof(struct node));
    root->data = a[n-1];
    for(i = 0; a[i];i++)
    {
        if(b[i] == a[n-1])
            break;
    }
    root->l = creat(i,a,b); //首先找左子树长度是i,因为是从后面向前的无法表示所以用a
    root->r = creat(n-i-1,a+i,b+i+1); //n-i-1的意思是总长度减去左子树和根节点等于右子树的长度
    return root;
};
void headcreat(struct node *root)
{
    if(root)
    {
        printf("%c",root->data);
        headcreat(root->l);
        headcreat(root->r);
    }
}
int main()
{
    struct node *root;
    int T, len;
    char str1[60], str2[60];
    while(~scanf("%d",&T))
    {
        while(T--)
        {
            getchar();
            scanf("%s %s",str1,str2);
            len = strlen(str1);
            root = creat(len,str2,str1); //注意写法
            headcreat(root);
            printf("\n");
        }
    }
    return 0;
}

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值