数据结构[树形结构和森林]二叉树、

Author:JaneOnly300
Date:2021:12.6
Categories: 数据结构(专升本)
本章参考王卓数据结构与算法基础

树结构和森林

一、树的定义

树形结构(非线性结构),结点之间有分支,具有层次关系。
![image.png](https://img-blog.csdnimg.cn/img_convert/0b04ef0cc7ca33c3562321da3cd02a6e.png#clientId=u750c423a-8f30-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=174&id=u18704364&margin=[object Object]&name=image.png&originHeight=674&originWidth=1280&originalType=binary&ratio=1&rotation=0&showTitle=false&size=215641&status=done&style=none&taskId=u67ab8239-5097-42b1-9d2a-d9078ca8602&title=&width=330)

树的定义

树是有n个结点的有限集合

  • 若n=0为空树
  • 若n>0,则他满足如下的两个条件
    1. 有且仅有一个特定的根结点
    2. 其余的结点可以分为m个不互相叫的有限集,每个集合本身有事一颗树,并且称为根的子树(su Tree)。

![image.png](https://img-blog.csdnimg.cn/img_convert/2a81d1a6c4c7268256ed2e96271c3ab9.png#clientId=u750c423a-8f30-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=182&id=u78fd6cd7&margin=[object Object]&name=image.png&originHeight=648&originWidth=1072&originalType=binary&ratio=1&rotation=0&showTitle=false&size=210293&status=done&style=none&taskId=ud1dd2fb6-cd34-436d-9fea-00a7699db53&title=&width=301)
![image.png](https://img-blog.csdnimg.cn/img_convert/31b749a0f674c094afd5d0d8b998c85c.png#clientId=u750c423a-8f30-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=230&id=u307c49f7&margin=[object Object]&name=image.png&originHeight=460&originWidth=460&originalType=binary&ratio=1&rotation=0&showTitle=false&size=91552&status=done&style=none&taskId=u58d1e849-3f5c-4b26-9088-47638075a44&title=&width=230)

它是一个递归嵌套的定义.

树的基本术语

  • 根节点(无前驱结点)
  • 结点的度
    • 就是结点拥有的子树个数
  • 树的度
    • 就是所有节点度的最大值
  • 叶子
    • 就是度=0
  • **分支节点 **
    • 度!=0

![image.png](https://img-blog.csdnimg.cn/img_convert/71bda182c83a2e88cbdd35ef57393266.png#clientId=u750c423a-8f30-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=698&id=uc0aa853d&margin=[object Object]&name=image.png&originHeight=1396&originWidth=2880&originalType=binary&ratio=1&rotation=0&showTitle=false&size=2646490&status=done&style=none&taskId=udc435d45-bacd-4223-86a8-fae09c21316&title=&width=1440)

有序树.子树有次序和无次序

森林

m(>=0)的互不相交的树的集合,一颗树也可以看成一个特殊森林
![image.png](https://img-blog.csdnimg.cn/img_convert/7033a7d64801dc03fca3a7ad2d5de42a.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=699&id=ub402f5f5&margin=[object Object]&name=image.png&originHeight=1398&originWidth=2386&originalType=binary&ratio=1&rotation=0&showTitle=false&size=1395012&status=done&style=none&taskId=ua23a55f3-8433-4399-a3ea-e62b8f8c153&title=&width=1193)

二、二叉树的定义

  • 二叉树的结构性强,规律性强
  • 所有的树都能转换为对应的二叉树,不失去一般性

普通树(多叉树)若不转换为二叉树,那么就会导致运算能难…二叉树的结构性强,规律性强,每个节点最多能有2个分支节点或者叶子,所有的树都可以转换为二叉树,这样就能解决很多树的存储结构和运算的存储性

二叉树的特点

  • 每个节点最多能有两个孩子
  • 子树有左右区分,次序不能颠倒
  • 二叉树可以是空集合

注意: 二叉树不是树的特殊情况,他们是两种概念。
二叉树节点的子树一定需要区分左右子树!!
![image.png](https://img-blog.csdnimg.cn/img_convert/8dadf9abebfa92c7437e908c0fce42de.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=375&id=uc7fe6f0d&margin=[object Object]&name=image.png&originHeight=750&originWidth=1154&originalType=binary&ratio=1&rotation=0&showTitle=false&size=291867&status=done&style=none&taskId=u9b29a2b5-9ad4-4af1-9e31-d6591e30dfa&title=&width=577)

二叉树的5中形态

![image.png](https://img-blog.csdnimg.cn/img_convert/e78a7b820b875e66f0bb87d3ff8c96d8.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=123&id=u7f1eaf5d&margin=[object Object]&name=image.png&originHeight=246&originWidth=1370&originalType=binary&ratio=1&rotation=0&showTitle=false&size=161072&status=done&style=none&taskId=u316b0f8b-71c5-433b-94fb-65e00ed29d3&title=&width=685)

虽然二叉树和树的概念不同,但树的基本术语和二叉树通用

三、树和二叉树的抽象数据类型定义

重要操作:

  • 前中后序遍历

![image.png](https://img-blog.csdnimg.cn/img_convert/38be17e15300fba54677c2b0036061ec.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=332&id=u2f2abd9a&margin=[object Object]&name=image.png&originHeight=664&originWidth=1208&originalType=binary&ratio=1&rotation=0&showTitle=false&size=594419&status=done&style=none&taskId=udab01059-214b-47ee-8c9f-16cda4f7e86&title=&width=604)

四、二叉树的性质和存储结构

1. 性质1

  • 第i层当中最多2^i-1个节点
  • 第i层当中最少有1个结点

![image.png](https://img-blog.csdnimg.cn/img_convert/a08b94dd673fcaa5756768c7935a21d1.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=243&id=u3ce069e2&margin=[object Object]&name=image.png&originHeight=486&originWidth=1092&originalType=binary&ratio=1&rotation=0&showTitle=false&size=188787&status=done&style=none&taskId=u7be9bf51-b375-4ace-9f6e-7bb1c132096&title=&width=546)

2. 性质二

  • 深度为K的二叉树当中最多有2^k-1个结点

![image.png](https://img-blog.csdnimg.cn/img_convert/64b08278866f79ce557d751843528b9e.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=317&id=uc82b3485&margin=[object Object]&name=image.png&originHeight=634&originWidth=1076&originalType=binary&ratio=1&rotation=0&showTitle=false&size=157221&status=done&style=none&taskId=u87b37d17-7001-40ae-862b-4c408354e59&title=&width=538)

3. 性质三

![image.png](https://img-blog.csdnimg.cn/img_convert/fd0dcf5657d6dc4b41f4131effd69197.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=278&id=ub0a92abf&margin=[object Object]&name=image.png&originHeight=768&originWidth=1350&originalType=binary&ratio=1&rotation=0&showTitle=false&size=491526&status=done&style=none&taskId=u1e04ee3a-7156-41ea-9931-71fe0fd1ddd&title=&width=489)

特殊性质的二叉树

二叉树有两种特殊形式,满二叉树完全二叉树

满二叉树

定义
  • 一个深度为k的二叉树有2^k-1个结点称之为满二叉树(也就是结点==最大结点)

![image.png](https://img-blog.csdnimg.cn/img_convert/9432ba90c37d69f801061b716535b9c1.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=156&id=u20d9648b&margin=[object Object]&name=image.png&originHeight=434&originWidth=722&originalType=binary&ratio=1&rotation=0&showTitle=false&size=189055&status=done&style=none&taskId=uac037eeb-86f1-49af-a173-6e14e99d154&title=&width=260)

特点
  1. 每一层上的结点数,都是最大结点树
  2. 叶子结点全部都在最底层
  3. 二叉树的编号规则
    1. 从根节点开始,层层编号

在同样深度的二叉树结点个数最多
在同样深度的二叉树叶子结点个数最多

完全二叉树

深度为k的具有n个结点的二叉树,每一个结点与深度为k的满二叉树中编号一一对应的就是完全二叉树。

判断是否为完全二叉树
  • 给二叉树编号,和满二叉树的编号作对比
    • 若完全一样,则是完全二叉树
    • 若有不一样,则不是完全二叉树
  • 方法二

![image.png](https://img-blog.csdnimg.cn/img_convert/eb09e5e1bd809c7d06a9774e7a8549c1.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=117&id=u7028a14c&margin=[object Object]&name=image.png&originHeight=234&originWidth=698&originalType=binary&ratio=1&rotation=0&showTitle=false&size=87351&status=done&style=none&taskId=ubb14b731-f27e-4816-8516-4dd4e05a733&title=&width=349)
![image.png](https://img-blog.csdnimg.cn/img_convert/e0bd991c35db491c542efc65b81a13c2.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=176&id=uccd909f2&margin=[object Object]&name=image.png&originHeight=352&originWidth=1380&originalType=binary&ratio=1&rotation=0&showTitle=false&size=254750&status=done&style=none&taskId=ub4766332-afed-456c-be73-d21d63ecf78&title=&width=690)

特点

  1. 叶子结点只可能分配在层此最大的两层之上
  2. 对于任意的结点,如果右边子树最大层次为i,name左子树的最大层此必须为i或者i+1

性质1

  • 表明了完全二叉树结点n树与完全二叉树深度K之间的关系

![image.png](https://img-blog.csdnimg.cn/img_convert/d895673acd7b98f99e7026f218d65547.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=355&id=ucf0d7991&margin=[object Object]&name=image.png&originHeight=710&originWidth=1220&originalType=binary&ratio=1&rotation=0&showTitle=false&size=250114&status=done&style=none&taskId=uf6c1e4ce-fd7c-4680-9dca-808a0fb59d3&title=&width=610)

性质2

  • 求完全二叉树双亲结点和孩子结点编号之间的关系

![image.png](https://img-blog.csdnimg.cn/img_convert/9f45c6b70c7bf0a7579325ffa341b069.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=207&id=u0aec2183&margin=[object Object]&name=image.png&originHeight=414&originWidth=894&originalType=binary&ratio=1&rotation=0&showTitle=false&size=284874&status=done&style=none&taskId=u70c7d328-05a3-488e-b375-351db5b106d&title=&width=447)

五、二叉树的存储结构

1. 二叉树的顺序存储

按照满二叉树的结点层此编号,一次存放二叉树当中的元素

![image.png](https://img-blog.csdnimg.cn/img_convert/4752a9951291d4ef94528e61f716d956.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=270&id=u1b9baf5e&margin=[object Object]&name=image.png&originHeight=540&originWidth=904&originalType=binary&ratio=1&rotation=0&showTitle=false&size=267327&status=done&style=none&taskId=u640c5635-38b0-45d1-818b-c9a36ae8aa8&title=&width=452)

  • 二叉树的顺序存储表示方式
#define MAXSIZE 100
Typedef TElemType sqlBiTree[MAXSIZE]
SqBiTree bt; 

小练习

  • 将下面用完全二叉树表述出来

![image.png](https://img-blog.csdnimg.cn/img_convert/581b680b4f3f2b15a7856d339bc9bdf9.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=104&id=u2dc35351&margin=[object Object]&name=image.png&originHeight=208&originWidth=1180&originalType=binary&ratio=1&rotation=0&showTitle=false&size=60923&status=done&style=none&taskId=u30c2ec00-7f8e-496f-aa2a-08179841e5f&title=&width=590)


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Mq7rgewY-1639276286536)(%E9%98%BF%E6%96%AF%E9%A1%BF#pic_center)]

3. 顺序存储的特点

  • 二叉树顺序存储缺点,神队为K的只有k个结点的树,却需要2^k-1的二维数组,造成空间浪费
  • 只能适用于满二叉树和完全二叉树当中
  • 结点关系在存储位置当中

![image.png](https://img-blog.csdnimg.cn/img_convert/1fbcc889b8638529abca6227a8fcb677.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=172&id=u5697767a&margin=[object Object]&name=image.png&originHeight=444&originWidth=592&originalType=binary&ratio=1&rotation=0&showTitle=false&size=105346&status=done&style=none&taskId=u5afe4f1d-45fe-4edf-b761-460442bfdf7&title=&width=229)

2. 二叉树的链式存储结构(常用)

每个节点有两个指针,分别指向左孩子和右孩子。
![image.png](https://img-blog.csdnimg.cn/img_convert/070d9c51517b6a71c89bc14053c2f6c5.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=81&id=ub7ac537a&margin=[object Object]&name=image.png&originHeight=162&originWidth=526&originalType=binary&ratio=1&rotation=0&showTitle=false&size=49300&status=done&style=none&taskId=uf95268c7-98f6-4c3b-bfc0-8bd62cfc845&title=&width=263)

代码表示
typedef struct BiNode{
	TElemType data;
    struct BiNode *lchild,*rchild;
}BiNode,*BiTree;

![image.png](https://img-blog.csdnimg.cn/img_convert/b4ad098e8d8bbc972cc64be3f6a2c6ba.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=270&id=u31b9b22a&margin=[object Object]&name=image.png&originHeight=540&originWidth=766&originalType=binary&ratio=1&rotation=0&showTitle=false&size=157298&status=done&style=none&taskId=u521f3ebd-d8b9-4a46-9c4e-0b1a662ca20&title=&width=383)
结论:
在n个结点当中,一共有n+1个空指针域

三叉链表(略…)

![image.png](https://img-blog.csdnimg.cn/img_convert/408e5a59db39e313d4bab9732651caed.png#clientId=u86547499-290f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=72&id=u8d99cd36&margin=[object Object]&name=image.png&originHeight=144&originWidth=688&originalType=binary&ratio=1&rotation=0&showTitle=false&size=55692&status=done&style=none&taskId=u4732a049-470c-416f-83cd-91e54c5344b&title=&width=344)

六、 二叉树的遍历(核心)

遍历就是通过某一条搜索路径访问二叉树的结点,使得每个结点都被访问一次
为了得到树的线性排列

1. 遍历方法

若是规定先左后右边,则有三种情况

  • DLR:先序遍历

  • LDR:中序遍历

  • LRD:后序遍历

    ![image.png](https://img-blog.csdnimg.cn/img_convert/3cbd8092ffe5ad15ff56e35f94dd4036.png#clientId=u6a2ebc61-e068-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=190&id=u30f21d0f&margin=[object Object]&name=image.png&originHeight=444&originWidth=1300&originalType=binary&ratio=1&rotation=0&showTitle=false&size=504936&status=done&style=none&taskId=uba16e86c-3bdd-42d8-aedf-deb98a7f1a6&title=&width=555)

1. 先序遍历

  • 若二叉树为空,则空操作,否则
    • 访问根节点
    • 遍历左子树
    • 遍历右子树

2. 中序遍历

  • 若二叉树为空,则空操作,否则
    • 中序遍历左子树
    • 访问根节点
    • 中序遍历右子树

3. 后序遍历

  • 若二叉树为空,则空操作,否则
    • 后序遍历左子树
    • 后序遍历右子树
    • 最后访问根节点

练习1:
![image.png](https://img-blog.csdnimg.cn/img_convert/d97a4fa620578a36d25e476a0da8ff3c.png#clientId=u6a2ebc61-e068-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=236&id=HBRj7&margin=[object Object]&name=image.png&originHeight=472&originWidth=608&originalType=binary&ratio=1&rotation=0&showTitle=false&size=165854&status=done&style=none&taskId=u633de56a-698e-4c46-9904-24d9a299d42&title=&width=304)

练习2
![image.png](https://img-blog.csdnimg.cn/img_convert/e26ec96a516feda37c2e572211159653.png#clientId=u6a2ebc61-e068-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=282&id=uc7413d48&margin=[object Object]&name=image.png&originHeight=564&originWidth=684&originalType=binary&ratio=1&rotation=0&showTitle=false&size=141951&status=done&style=none&taskId=ud929eaab-ae1f-4d61-9b83-f145a579a34&title=&width=342)

2. 遍历算法的实现

递归实现

算法时间复杂度为On

  • 先序遍历递归遍历的实现
void preOrder(Btree *T)
{
    if(T != NULL) {
        printf("%d ", T->element);  //访问根节点
        preOrder(T->lchild);    //先根序遍历左子树
        preOrder(T->rchild);    //先根序遍历右子树
    }
}
  • 中序遍历
void inOrder(Btree *T)
{
    if(T != NULL) {
        inOrder(T->lchild);     //中根序遍历左子树
        printf("%d ", T->element);  //访问根节点 
        inOrder(T->rchild); //中根序遍历右子树
    }
}
  • 后序遍历
void postOrder(Btree *T)
{
    if(T != NULL) {
        postOrder(T->lchild);   //后根序遍历左子树
        postOrder(T->rchild);   //后根序遍历右子树
        printf("%d ", T->element);  //访问根节点
    }
}

非递归的算法

  • 中序遍历非递归算法
  1. 建立一个;
  2. 结点进栈,遍历左子树,
  3. 根结点出栈,输出根结点,遍历右子树。

![image.png](https://img-blog.csdnimg.cn/img_convert/428860f4e8da8cd7b19d9a00a04d27fa.png#clientId=ua065e676-5e1c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=384&id=ubc3d86ae&margin=[object Object]&name=image.png&originHeight=768&originWidth=1122&originalType=binary&ratio=1&rotation=0&showTitle=false&size=373582&status=done&style=none&taskId=u174e281a-60bb-4779-af8e-a8089f7ebe2&title=&width=561)

层此遍历算法

  • 根据深度一层一层访问,从上到下,从左到右,每个结点只访问一次
  1. 将根结点入队;
  2. 队不为空循环: 从队列出列一个节点*p,访问它;
    1. 若有左孩子结点,将左孩子结点进队。
    2. 若有右孩子结点,将右孩子结点进队。
//创建一个循环队列
typedef struct{
    BTNode data[MaxSize]; //存放队中的元素
    int front,rear; //队头和尾
}SqQueue;

//层侧遍历算法
void LevelOrder(BTNode *b){
 	BTNode *p; SqQueue *qu;
    initQueue(qu);
    enQueue(qu,b);
	while(!QueueEmpty(qu)){ //如果队列不为空
        DeQueue(qu,p);
        printf("%c",p->data);//访问数
        if(p->Lchild!=null) enQueue(qu,p->LChild);
       	if(p->rChild!=null) enQueue(qu,p->rChild);
    }
}

3. 遍历算法的应用

1. 二叉树的算法建立

![image.png](https://img-blog.csdnimg.cn/img_convert/31b0fdbf1783ba0bbfd9c840ce7c0b86.png#clientId=ua065e676-5e1c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=391&id=u28c42268&margin=[object Object]&name=image.png&originHeight=782&originWidth=1002&originalType=binary&ratio=1&rotation=0&showTitle=false&size=408454&status=done&style=none&taskId=u8c2f0235-81ae-4ea2-a85f-7abb7f51d08&title=&width=501)

2. 二叉树的复制

算法的实现: (先序遍历)

  • 如果是空树,递归结束
  • 否则,申请新的结点空间,复制根结点
  • 递归复制左子树
  • 递归复制右 子树
int Copy(BiTree T,BiTree &NewT){
 	if(T == null ){ //如果是空树,返回
    	NewT = NULL ; return 0;
    }else{
     	NewT = new BiTNode;
        NewT->data = T->Data;
        //
        Copy(T->LChild,New->Lchild();
        Copy(T->RChild,New->Rchild();
    }
    
}

计算二叉树的深度

  • 若果是为空树,则深度为0
  • 否则
    • 计算左子树的深度,
    • 计算右子树的深度
    • 求出来后+1
int Depth(BiTree T){
 	if(T == null) return 0; //空树返回0
    else{
      m = Depath(T->lChild); //计算左子树的深度
      n = Depath(T->RChild); //计算右子树的深度
      if(m>n) return (m+1);
      else{
    	return (n+1)        
      }
    }
}

计算二叉树的节点

  • 如果为空树,结点个数==0
  • 否则节点的个数加上左子树的结点个数+右子树的结点个数+1
int CountNode(BiTree b){
 	if(b == null){
     	return 0;   
    }else{ //如果不等于null
     	  //计算左子树+右边子树的的节点+根节点
        return CountNode(b->Lchild)+CountNode(B->Rchild)+1;
    }
}

计算二叉树的叶子结点

  • 如果是空树,则叶子结点的个数为0
  • 否则,判断
    • 左右都没有孩子(叶子结点)返回1
    • 否则
      • 左子树叶子结点+右子树叶子结点
int LeadCount(BiTree T){
	if(T == null){
        return 0;
    }
    if(T->LChild == null && T->Rchild == null){
    	return 1; //找出了一个叶子结点
    }else{
    	return LeadCount(T->LChild)+LeadCount(T->Rchild)
    }
}

4. 根据遍历序列确定一个二叉树

若果二叉树的各个节点都不一样,则而二叉树的先序遍历中序遍历和后序遍历都是唯一的
由二叉树的先序和中序,或者由二叉树的后序和中序则可以确定一个唯一的二叉树

例子

先序与中序判断
  • 先序: ABCDEFGHIJ
  • 中序: CDBFEAIHGJ

后序和中序
  • 后序: BDCEAFHG
  • DECBHGFA

5. 线索二叉树

使用二叉链表作为二叉树存储结构时,可以方便找到某个结点左右孩子; 但一般情况, ** 无法直接找到该结点在某种遍历序列中的前驱和后继结点。**

  • 如果寻找特定遍历序列中二叉树结点的前驱和后继??

了利用空着的指针域

1. 概念

利用二叉链表的空指针域
如果某个结点的左孩子为空,则将左孩子指针域改为指向前驱,如果右孩子为空,则指向后继。

这种改变指向的指针称之为线索
对于二叉树按照某种遍历次使得其变为线索二叉树的过程叫做线索化

加上了线索的二叉树就是_线索二叉树 _
![image.png](https://img-blog.csdnimg.cn/img_convert/add64773e399a1587e1e62c220b6afc8.png#clientId=u652de503-a3c7-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=179&id=u7046dfb2&margin=[object Object]&name=image.png&originHeight=357&originWidth=492&originalType=binary&ratio=1&rotation=0&showTitle=false&size=114360&status=done&style=none&taskId=u7a80a6d5-ba29-4e0c-9b09-a19e6a5bcac&title=&width=246)
为了区分二叉链表的中Lchild和Rchild是指向左右孩子还是前驱后继的,我们来添加tag来加以区分

ltag = 0  //指向左孩子
rtag = 0 // 指向右孩子
    
ltag = 1; //指向前驱
rtag = 1; //指向后继

//Code
typedef struct BiThrNode{
	int data;
    int ltag,rtag ; //表示符号
    struct BiThrNode *lchild,rchild;
}BiThrNode.*BiThrTree;

![image.png](https://img-blog.csdnimg.cn/img_convert/2ecc2a6df1a1247330095583849a7412.png#clientId=u652de503-a3c7-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=32&id=ua1a01f6f&margin=[object Object]&name=image.png&originHeight=63&originWidth=568&originalType=binary&ratio=1&rotation=0&showTitle=false&size=20846&status=done&style=none&taskId=uabb9945c-06c7-4bb3-9a58-5155df7d9bf&title=&width=284)

1.1. 关于线索二叉树线索为空的状态?

![image.png](https://img-blog.csdnimg.cn/img_convert/d57e5219d3414fef1155c146aa01db68.png#clientId=u652de503-a3c7-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=228&id=uf10dfb77&margin=[object Object]&name=image.png&originHeight=455&originWidth=858&originalType=binary&ratio=1&rotation=0&showTitle=false&size=320714&status=done&style=none&taskId=u85855ead-b5bf-4899-87c2-f5d0b5f7ab0&title=&width=429)

2. 线索二叉树练习

练习1

先序序列: A B C D E
![image.png](https://img-blog.csdnimg.cn/img_convert/3ff8567300dfb6fbde34c46d9daae571.png#clientId=u652de503-a3c7-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=85&id=u51f7d319&margin=[object Object]&name=image.png&originHeight=170&originWidth=166&originalType=binary&ratio=1&rotation=0&showTitle=false&size=13017&status=done&style=none&taskId=u20b39987-5a64-4e6a-a8cb-fc0b37507d4&title=&width=83)
请根据以上的 二叉树,将其按先序、中序、后续、进行线索化;

练习2

![image.png](https://img-blog.csdnimg.cn/img_convert/48011540418c80d0da54feb92b4e9784.png#clientId=u652de503-a3c7-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=252&id=u7a56ba5c&margin=[object Object]&name=image.png&originHeight=504&originWidth=749&originalType=binary&ratio=1&rotation=0&showTitle=false&size=96753&status=done&style=none&taskId=u8471c03e-f24b-4bae-bb15-f638050b803&title=&width=374.5)

七、树和森林

review 树的定义

1. 树的存储

1. 双亲表示法

  • 定义: 定义结构数组存放树的结点,每个结点含有两个域
  • 数据域: 存放结点本身的信息
  • 双亲域: 指示本结点 的双亲结点在数组当中的位置
  • 数组索引表示位置

![image.png](https://img-blog.csdnimg.cn/img_convert/cf57469fe109bcaa83a2da77809ae30f.png#clientId=u652de503-a3c7-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=180&id=u339442c7&margin=[object Object]&name=image.png&originHeight=359&originWidth=178&originalType=binary&ratio=1&rotation=0&showTitle=false&size=43268&status=done&style=none&taskId=u3a16bcb4-6d15-4bdd-a29f-b0e35359add&title=&width=89)

c语言表示
typedf struct PtNode{
	TElemType data;
    int parent ; //双亲位置域
}PTNode;
#define MAX_TREE_SIZE
//树
type struct{
	PTNode nodes[MAX_TREE_SIZE];
    int r,n ; //根节点的位置,和结点个数
}PTree;

找到双亲很容易,找孩子难。

2. 孩子链表

把每个结点的孩子结点排列起来,看成一个线性表,用单链表进行存储,则n个结点就有n个孩子链表,(叶子结点的链表为空),而n个头指针又组成一个线性表,用顺序表进行存储。
![image.png](https://img-blog.csdnimg.cn/img_convert/982918f70fe3e0217f4818e10d3fd33a.png#clientId=u652de503-a3c7-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=369&id=uedfa04a6&margin=[object Object]&name=image.png&originHeight=738&originWidth=1835&originalType=binary&ratio=1&rotation=0&showTitle=false&size=538648&status=done&style=none&taskId=u8d0ed401-0fff-446a-8681-6a633ce0f79&title=&width=917.5)

c语言表示
//孩子结点
typedef struct CTNode{
	int child ; //
    struct CTNode *next;
}*ChildPtr;
//双亲结点
typedef struct{
	TElemType data;//数据
    ChildPtr fistchild; //孩子链表的头结点
}CTBox;
//树
typedef struct{
	CTBox Nodes[MAXSIZE];
    int n,r;//头结点位置和根节点位置
}

找孩子容易,找双亲难

3. 孩子兄弟表示法(二叉链表表示法)

实现: 用儿茶链表作树的存储结构,链表中每一个结点的两个指针域,
分别指向第一个孩子结点和下一个兄弟结点

c语言表示

typedef struct CSNodeP{
	ElemType data;
    struct CSNode *firstChild,*nextsibling;
}CSnode,*CSTree; 

![image.png](https://img-blog.csdnimg.cn/img_convert/dd98ce3bb7d069c23607aaa1ef1adc2d.png#clientId=u652de503-a3c7-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=265&id=ubc58e80a&margin=[object Object]&name=image.png&originHeight=529&originWidth=950&originalType=binary&ratio=1&rotation=0&showTitle=false&size=152085&status=done&style=none&taskId=u613b64d9-57d5-48b3-938e-8dc06825c81&title=&width=475)

2. 树和二叉树的转换

目的

将树转换为二叉树进行处理,利用二叉树算法实现对树的操作。
二叉树,都是可以使用二叉链表作为存储结构,则二叉链表作为媒介可以导出树与二叉树之间的对应关系。

  • 给定一颗树,我们就能将其转换为二叉树

    ![image.png](https://img-blog.csdnimg.cn/img_convert/c855fcf0a3c2294374045571a8d45770.png#clientId=u80458de3-478f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=241&id=u7777c156&margin=[object Object]&name=image.png&originHeight=481&originWidth=927&originalType=binary&ratio=1&rotation=0&showTitle=false&size=201506&status=done&style=none&taskId=u682f8eff-21a6-45f9-baf1-780670f673f&title=&width=463.5)

树转换为二叉树
  1. 加线: 在兄弟之间加一个线
  2. 摸线: 对每个结点,除了左孩子,去除其余孩子的关系
  3. 旋转,以树的根节点为轴心。整个树旋转45°

![image.png](https://img-blog.csdnimg.cn/img_convert/e4d52b1802e2028af84a989dc2baf976.png#clientId=u80458de3-478f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=170&id=u30a4e832&margin=[object Object]&name=image.png&originHeight=340&originWidth=562&originalType=binary&ratio=1&rotation=0&showTitle=false&size=100818&status=done&style=none&taskId=uaeba8192-8cdb-41f8-b805-54df6cabd58&title=&width=281)

兄弟相连接留长子

二叉树转换为树
  • 左孩右右连双亲、
  • 去掉原来右孩线

![image.png](https://img-blog.csdnimg.cn/img_convert/a00b56768ff9cb97704503ef21867eb2.png#clientId=u80458de3-478f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=114&id=u176b24c6&margin=[object Object]&name=image.png&originHeight=227&originWidth=201&originalType=binary&ratio=1&rotation=0&showTitle=false&size=20489&status=done&style=none&taskId=u32e99c6d-6028-4188-b990-f23ef106ada&title=&width=100.5)

3. 森林和二叉树的转换

1. 森林转换成为二叉树

  • 树变二叉,根相连

![image.png](https://img-blog.csdnimg.cn/img_convert/f79ce3c4cb2d2595523833cb463cfd3b.png#clientId=u80458de3-478f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=66&id=ud846b3f6&margin=[object Object]&name=image.png&originHeight=131&originWidth=328&originalType=binary&ratio=1&rotation=0&showTitle=false&size=18570&status=done&style=none&taskId=u92a1a853-2dba-461e-be16-ee78590328d&title=&width=164)

2. 二叉树转换为森林

  • 去掉全部右孩子线,孤立二叉再还原

![image.png](https://img-blog.csdnimg.cn/img_convert/abf16d07303fbaecdf68b6552611ee20.png#clientId=u80458de3-478f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=125&id=ua19b3273&margin=[object Object]&name=image.png&originHeight=250&originWidth=231&originalType=binary&ratio=1&rotation=0&showTitle=false&size=17564&status=done&style=none&taskId=u187fee8d-c464-477c-9e2e-8414de0fd1e&title=&width=115.5)

4. 树和森林的遍历

1. 树的遍历

先根遍历
  • 若树不为空,则先访问根节点,然后依次遍历各个子树
后根遍历
  • 若树不空,则先依次后根遍历各个子树,然后访问根结点
按照层次遍历
  • 若树不为空,则自上而下从左到右访问每个结点
练习:
  • 写出下列树的先后根和层次遍历的序列

![image.png](https://img-blog.csdnimg.cn/img_convert/a7a52ddc354610f2f057bf8fd44b426e.png#clientId=u80458de3-478f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=172&id=uf6cb87a7&margin=[object Object]&name=image.png&originHeight=344&originWidth=426&originalType=binary&ratio=1&rotation=0&showTitle=false&size=90043&status=done&style=none&taskId=u261430f3-a38c-4514-b66b-3600f42bb08&title=&width=213)

2. 森林的遍历

我们可以将森林看做三个部分

  • 第一棵树的根节点
  • 第一棵树的子树森林
  • 其他的树构成的森林

![image.png](https://img-blog.csdnimg.cn/img_convert/7d628616b86f27e15b5ca4a3c093f725.png#clientId=u80458de3-478f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=303&id=u50033773&margin=[object Object]&name=image.png&originHeight=606&originWidth=772&originalType=binary&ratio=1&rotation=0&showTitle=false&size=420452&status=done&style=none&taskId=ud52b098d-dfee-46db-91cb-672bad897b0&title=&width=386)

先序遍历
  • 若森林不为空
  1. 先访问根
  2. 然后访问子树森林

依次进行先序遍历

中序遍历
  • 若森林不为空
  1. 中序遍历森领当中第一棵树的子森林
  2. 访问森林中第一棵树的根结点
  3. 中序遍历森林中其余树构成森林

![image.png](https://img-blog.csdnimg.cn/img_convert/07aef7a1212b7e6e9108bb3e4bf53ebd.png#clientId=u80458de3-478f-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=163&id=ua9edf39f&margin=[object Object]&name=image.png&originHeight=326&originWidth=832&originalType=binary&ratio=1&rotation=0&showTitle=false&size=157307&status=done&style=none&taskId=ucf5271a2-b534-4636-959c-2529bb6f1a2&title=&width=416)

依次从左到右对森林每一个树进行后根遍历

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值