数据结构——二叉树

目录

二叉树基础知识:

二叉树的结论:

二叉树的遍历:

先序:

中序:

后序:

基于二叉树遍历的其他操作:

统计结点个数:

统计叶子个数:

统计度为一节点个数:

统计度为二节点个数:

求二叉树深度:

1:

2:

从下往上打印指定元素的所有祖先:

二叉树的层次遍历:

层次输出第n个结点:

二叉树的建立:

已知空树:

直接返回树版:

c++引用返回树版(提前建立树,然后通过函数来改变树):

先序+中序:

后序+中序:


二叉树基础知识:

二叉树的结论:

二叉树的遍历:

先序:

中序:

后序:

基于二叉树遍历的其他操作:

统计结点个数:
统计叶子个数:
统计度为一节点个数:
统计度为二节点个数:
求二叉树深度:
1:
2:
从下往上打印指定元素的所有祖先:
二叉树的层次遍历:
层次输出第n个结点:

二叉树的建立:

已知空树:

直接返回树版:
cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

typedef char ElementType;
typedef struct BiTNode{
    ElementType data;
    struct BiTNode *lchild;
    struct BiTNode *rchild;
}BiTNode,*BiTree;

BiTree CreatBinTree();
void  preorder( BiTree T );

int main()
{
    BiTree T = CreatBinTree();
    preorder(  T );
    return 0;
}
void  preorder( BiTree T )
{
   if(T)
   {
     printf("%c",T->data);
     preorder(T->lchild);
     preorder(T->rchild);
   }
}
BiTree CreatBinTree()
{
   char ch;BiTree T;
   scanf("%c",&ch);
   if(ch=='#') return NULL;
   T=(BiTree)malloc(sizeof(BiTNode));
   T->data=ch;
   T->lchild=CreatBinTree();
   T->rchild=CreatBinTree();
   return T;
}
c++引用返回树版(提前建立树,然后通过函数来改变树):
cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct BiTNode{
    ElementType data;
    struct BiTNode *lchild;
    struct BiTNode *rchild;
}BiTNode,*BiTree;

void CreatBinTree(BiTree  &T);
void preorder( BiTree T );

int main()
{
    BiTree T;
    CreatBinTree(T);
    preorder(  T );
    return 0;
}
void preorder( BiTree T )
{
    if(T)
    {
        printf("%c",T->data);
        preorder(T->lchild);
        preorder(T->rchild);
    }
}
void CreatBinTree(BiTree &T)
{
    char ch;
    scanf("%c",&ch);
    if(ch=='#')  T=NULL;
    else
    {
        (BiTree)malloc(sizeof(BiTNode));
        T->data=ch;
        CreatBinTree( T->lchild);
        CreatBinTree( T->rchild);
    }
}

c语言指针版(和引用版底层逻辑一样,只不过c语言更呆):

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

typedef char ElementType;
typedef struct BiTNode{
    ElementType data;
    struct BiTNode *lchild;
    struct BiTNode *rchild;
}BiTNode,*BiTree;

void CreatBinTree(BiTree  *T);
void preorder( BiTree T );

int main()
{
     BiTree T;
     CreatBinTree(&T);
     preorder(  T );
     return 0;
}
void preorder( BiTree T )
{
    if(T)
    {
        printf("%c",T->data);
        preorder(T->lchild);
        preorder(T->rchild);
    }
}
void CreatBinTree(BiTree  *T)
{
    char ch;
    scanf("%c",&ch);
    if(ch=='#')  *T=NULL;
    else
    {
       *T=(BiTree)malloc(sizeof(BiTNode));
       (*T)->data=ch;
       CreatBinTree( &(*T)->lchilde);注意一下优先级->的优先级高于*,所以给*加()
       CreatBinTree( &(*T)->rchilde);
    }
}

除了已知空树的情况外,如果我们已知先序+中序 或者后序加中序那么我们也可以唯一确定一棵树。

先序+中序:

先序是跟左右进行遍历,所以我们一定可以确定根节点是什么;

记录当前根节点,然后在中序数组中寻找根节点的位置,并记录这个位置;

因为中序是左根右进行遍历,所以根节点左边的都是左树,右边的都是右树;

递归进行除了左树和右数即可;

注意:1:creat函数的返回值是BiTree类型,最后要返回T;

2:递归不用想太多,只想一层的效果即可,往后每一层都是如此操作,直至最终到边界

(其实递归所传参数就是对应数组的起始位置以及数组长度,寻找位置传参即可,具体看代码后的图片,图片和代码结合着看)

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char ElementType;
typedef struct BiTNode{
    ElementType data;
    struct BiTNode *lchild;
    struct BiTNode *rchild;
}BiTNode,*BiTree;

BiTree CreatBinTree(char *pre,char *in,int n );
void postorder( BiTree T );

int main()
{
    BiTree T;
    char prelist[100];
    char inlist[100];
    int length;
    scanf("%s",prelist);
    scanf("%s",inlist);
    length=strlen(prelist);
    T=CreatBinTree(prelist,inlist, length);
    postorder(  T );
    return 0;
}
void  postorder( BiTree T )//后序输出二叉树
{
    if(T)
    {
        
        postorder(T->lchild);
        postorder(T->rchild);
        printf("%c",T->data);
    }
}
BiTree CreatBinTree(char *pre,char *in,int n)
pre是先序数组,in是中序数组,n是树的节点数
随着递归的深入,先序数组和中序数组会不断缩小,直至节点个数<=0,递归结束
{
    BiTree T;
    int i;
    if(n<=0) return NULL;
    T=(BiTree)malloc(sizeof(BiTNode));
    T->data=pre[0];记录根节点
    for(i=0;in[i]!=pre[0];i++);寻找根节点的位置
    T->lchild=CreatBinTree(pre+1,in,i);//递归左树
    T->rchild=CreatBinTree(pre+i+1,in+1+i,n-i-1);//递归右树
    return T;
}

后序+中序:

思路和先序+中序差不多,都是寻找根节点,然后递归左树和右树;

不同的是后序是左右根遍历,所以根节点在最后面;

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char ElementType;
typedef struct BiTNode{
    ElementType data;
    struct BiTNode *lchild;
    struct BiTNode *rchild;
}BiTNode,*BiTree;

BiTree CreatBinTree(char *post,char*in,int n);
void preorder( BiTree T );

int main()
{
    BiTree T;
    char postlist[100];
    char inlist[100];
    int length;
    scanf("%s",postlist);
    scanf("%s",inlist);
    length=strlen(postlist);
    T=CreatBinTree(postlist,inlist, length);
    preorder(  T );
    return 0;
}
void  preorder( BiTree T )
{
    if(T)
    {
        printf("%c",T->data);
        preorder(T->lchild);
        preorder(T->rchild);
            
    }
}
BiTree CreatBinTree(char *post,char*in,int n )
post是后序数组,in是中序数组,n是树的节点数
随着递归的深入,后序数组和中序数组会不断缩小,直至节点个数<=0,递归结束
{
    BiTree T;
    int i;
    if(n<=0) return NULL;
    T=(BiTree)malloc(sizeof(BiTNode));
    T->data=post[n-1];记录根节点
    for(i=0;in[i]!=post[n-1];i++);寻找根节点的位置
    T->lchild=CreatBinTree(post,in,i);递归左树
    T->rchild=CreatBinTree(post+i,in+1+i,n-i-1);递归右树
    return T;
}

尚未完结。

相关推荐
Yzzz-F2 小时前
Problem - 2205D - Codeforces
算法
智者知已应修善业2 小时前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
Halo_tjn3 小时前
Java Set集合相关知识点
java·开发语言·算法
生成论实验室3 小时前
《事件关系阴阳博弈动力学:识势应势之道》第四篇:降U动力学——认知确定度的自驱演化
人工智能·科技·神经网络·算法·架构
AI科技星3 小时前
全域数学·72分册:场计算机卷【乖乖数学】
算法·机器学习·数学建模·数据挖掘·量子计算
科研前沿4 小时前
镜像孪生VS视频孪生核心技术产品核心优势
大数据·人工智能·算法·重构·空间计算
水蓝烟雨4 小时前
1931. 用三种不同颜色为网格涂色
算法·leetcode
晨曦夜月5 小时前
map与unordered_map区别
算法·哈希算法
qeen875 小时前
【数据结构】建堆的时间复杂度讨论与TOP-K问题
c语言·数据结构·c++·学习·
图码5 小时前
如何用多种方法判断字符串是否为回文?
开发语言·数据结构·c++·算法·阿里云·线性回归·数字雕刻