当前位置:网站首页>Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal
Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal
2022-08-02 15:32:00 【Yang Laotou Soft Worker】
一、引言
A common method of traversing a binary tree is preorder traversal、中序遍历、Post-order traversal and hierarchical traversal, etc,本文给出了CNon-recursive and recursive algorithms for postorder traversal of binary trees in the language version.
Postorder traversal is not as simple as preorder traversal,It is the most complicated traversal method.The order in which nodes are visited is :“左—>右—>根”,That is, the left subtree is visited first,Then visit the right subtree,最后访问树根.对于左、右子树而言,The order of its visits remains the same“左—>右—>根”.也就是说,for each subtree,Both are the last to visit the root of the tree.
It can be seen from the above description that the traversal process is actually a recursive process,So it can be implemented using recursive algorithm,But the same can also be achieved using non-recursive methods.
二、The post-order traversal of a binary tree is a detailed demonstration of the process
1、假设二叉树(left and right subtrees)如下图所示:
Then the post-order traversal process is :左子树b—>右子树c—>树根a
2、假设二叉树(没有右子树)如下图所示:

Then the post-order traversal process is :左子树b—>树根a
3、假设二叉树(没有左子树)如下图所示:
Then the post-order traversal process is :右子树c—>树根a
4、For a slightly more complicated binary tree,如下图所示:
The post-order traversal process is demonstrated as follows(“左—>右—>根”)
Step 1. First visit the node d
Step 2. 访问结点 g
Step 3. 访问结点 e

Step 4. 访问结点b
Step 5. 访问结点h
Step 6. 访问结点f
Step 7. 访问结点c
Step 8. 访问树根a
At this point, the in-order traversal of the binary tree ends,遍历结果为:d g e b h f c a
5、Repeat access flag
during this traversal,It will be found that the root of the tree and the root of the subtree will be visited twice,为了避免这个问题,Don't visit when you first encounter it,And visit again the second time you meet,Hence an access flag was introduced.
三、Source code for postorder traversal of a binary tree:
1、Node structure and conditional compilation
typedef struct node
{
datatype data;
struct node *Lchild;
struct node *Rchild;
int flag;
}BiTree;
#ifdef CHAR
typedef char datatype;
#else
typedef int datatype;
#endif
2、递归算法
void PostorderSearch_Recu( BiTree *T)
{
if (T!=NULL)
{
PostorderSearch_Recu(T->Lchild) ;
PostorderSearch_Recu(T->Rchild) ;
VisitNode(T->data) ;
}
}
3、非递归算法
void PostorderSearch( BiTree *T )
{
BiTree *p, *stack[ MAX_NODE ];
int top = 0;//栈顶位置下标
if( T == NULL )
{
return;
}
p = T;
while( 1 )
{
if( p != NULL )//p非空,则入栈,之后p向左走
{
stack[ top++ ] = p;
p = p->Lchild;
}
else//p为空,则出栈
{
p = stack[ --top ];
//右为空,且flag为真,则访问,之后p置空
if( p->Rchild == NULL || p->flag == 1 )
{
VisitNode( p->data );
p = NULL;
}
else//右非空,则p重新入栈,Repeated push flagflag置为真,之后p向右走
{
stack[ top++ ] = p;
p->flag = 1;
p = p->Rchild;
}
}
if( top == 0 )//栈为空,则结束遍历
{
break;
}
}
}
4、VisitNode函数如下:
void VisitNode( datatype data )
{
#ifdef CHAR
printf( "%5c", data );
#else
printf( "%5d", data );
#endif
}
补充:Combine the algorithm for creating a binary tree from the previous article,The binary tree creation and post-order traversal of the binary tree can be completely implemented.The algorithm created is not repeated here.
边栏推荐
- [STM32 Learning 1] Basic knowledge and concepts are clear
- pytorch模型转libtorch和onnx格式的通用代码
- FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图
- MATLAB绘制平面填充图入门详解
- Mysql的锁
- 2021-10-14
- 单端K总线收发器DP9637兼容L9637
- Win10安装了固态硬盘还是有明显卡顿怎么办?
- What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
- 使用npx -p @storybook/cli sb init安装失败,手把手搭建专属的storybook
猜你喜欢
随机推荐
FP5207电池升压 5V9V12V24V36V42V大功率方案
Article pygame drag the implementation of the method
Win11怎么在右键菜单添加一键关机选项
General syntax and usage instructions of SQL (picture and text)
word方框怎么打勾?
Detailed explanation of Golang garbage collection mechanism
win10无法直接用照片查看器打开图片怎么办
Publish module to NPM should be how to operate?Solutions to problems and mistake
golang之GMP调度模型
刷卡芯片CI520可直接PIN对PIN替换CV520支持SPI通讯接口
[STM32 Learning 1] Basic knowledge and concepts are clear
Win11系统找不到dll文件怎么修复
What should I do if I install a solid-state drive in Win10 and still have obvious lags?
项目:数据库表的梳理
STM32LL库使用——SPI通信
Win7怎么干净启动?如何只加载基本服务启动Win7系统
Mysql之MVCC
FP7195大功率零压差全程无频闪调光DC-DC恒流芯片(兼容调光器:PWM调光,无极调光,0/1-10V调光)
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
Win11电脑一段时间不操作就断网怎么解决








![[STM32 Learning 1] Basic knowledge and concepts are clear](/img/1c/7c4fd2d835e15ca13517c6d97e9b3a.png)
