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个结点的有限集合
![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)
它是一个递归嵌套的定义.
![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)
![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)
![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)
![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)
![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)
二叉树有两种特殊形式,满二叉树和完全二叉树。
![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)
在同样深度的二叉树结点个数最多
在同样深度的二叉树叶子结点个数最多
深度为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)
![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)
![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)
按照满二叉树的结点层此编号,一次存放二叉树当中的元素
![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)]
![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)
每个节点有两个指针,分别指向左孩子和右孩子。
![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)
遍历就是通过某一条搜索路径访问二叉树的结点,使得每个结点都被访问一次
为了得到树的线性排列
若是规定先左后右边,则有三种情况
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:
![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)
算法时间复杂度为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); //访问根节点
}
}
![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)
//创建一个循环队列
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);
}
}
![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)
算法的实现: (先序遍历)
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();
}
}
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)
}
}
}
int CountNode(BiTree b){
if(b == null){
return 0;
}else{ //如果不等于null
//计算左子树+右边子树的的节点+根节点
return CountNode(b->Lchild)+CountNode(B->Rchild)+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)
}
}
若果二叉树的各个节点都不一样,则而二叉树的先序遍历中序遍历和后序遍历都是唯一的
由二叉树的先序和中序,或者由二叉树的后序和中序则可以确定一个唯一的二叉树
使用二叉链表作为二叉树存储结构时,可以方便找到某个结点左右孩子; 但一般情况, ** 无法直接找到该结点在某种遍历序列中的前驱和后继结点。**
了利用空着的指针域
利用二叉链表的空指针域
如果某个结点的左孩子为空,则将左孩子指针域改为指向前驱,如果右孩子为空,则指向后继。
这种改变指向的指针称之为线索
对于二叉树按照某种遍历次使得其变为线索二叉树的过程叫做线索化
加上了线索的二叉树就是_线索二叉树 _
![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)
![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)
先序序列: 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)
请根据以上的 二叉树,将其按先序、中序、后续、进行线索化;
![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 树的定义
![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)
typedf struct PtNode{
TElemType data;
int parent ; //双亲位置域
}PTNode;
#define MAX_TREE_SIZE
//树
type struct{
PTNode nodes[MAX_TREE_SIZE];
int r,n ; //根节点的位置,和结点个数
}PTree;
找到双亲很容易,找孩子难。
把每个结点的孩子结点排列起来,看成一个线性表,用单链表进行存储,则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)
//孩子结点
typedef struct CTNode{
int child ; //
struct CTNode *next;
}*ChildPtr;
//双亲结点
typedef struct{
TElemType data;//数据
ChildPtr fistchild; //孩子链表的头结点
}CTBox;
//树
typedef struct{
CTBox Nodes[MAXSIZE];
int n,r;//头结点位置和根节点位置
}
找孩子容易,找双亲难
实现: 用儿茶链表作树的存储结构,链表中每一个结点的两个指针域,
分别指向第一个孩子结点和下一个兄弟结点
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)
将树转换为二叉树进行处理,利用二叉树算法实现对树的操作。
二叉树,都是可以使用二叉链表作为存储结构,则二叉链表作为媒介可以导出树与二叉树之间的对应关系。
给定一颗树,我们就能将其转换为二叉树
![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)
![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)
![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)
![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)
![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)
我们可以将森林看做三个部分
![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)
依次进行先序遍历
![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)
依次从左到右对森林每一个树进行后根遍历