当前位置:网站首页>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.
边栏推荐
- DP1332E内置c8051的mcu内核NFC刷卡芯片国产兼容NXP
- win10任务栏不合并图标如何设置
- MATLAB绘图函数fplot详解
- Win10无法连接打印机怎么办?不能使用打印机的解决方法
- 发布模块到npm应该怎么操作?及错误问题解决方案
- 使用 腾讯云搭建一个个人博客
- TypeScript 快速进阶
- 使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
- Win7怎么干净启动?如何只加载基本服务启动Win7系统
- Flink + sklearn - use JPMML implement flink deployment on machine learning model
猜你喜欢
Win10 computer can't read U disk?Don't recognize U disk how to solve?
FP6293电池升压5V-12V大电流2APWM模式升压方案
Win7遇到错误无法正常开机进桌面怎么解决?
13.56MHZ刷卡芯片CI521兼容cv520/ci520支持A卡B卡MIFARE协议
为vscode配置clangd
win10无法直接用照片查看器打开图片怎么办
Mysql之MVCC
How to simulate 1/3 probability with coins, and arbitrary probability?
轻量化AlphaPose
FP7122降压恒流内置MOS耐压100V共正极阳极PWM调光方案原理图
随机推荐
使用 腾讯云搭建一个个人博客
jest测试,组件测试
倍增和稀疏表
将SSE指令转换为ARM NEON指令
LORA芯片ASR6505无线远距离传输8位MCU
pygame图像连续旋转
轻量化AlphaPose
FP7195降压恒流PWM转模拟调光零压差大功率驱动方案原理图
Detailed explanation of Golang garbage collection mechanism
专硕与学硕
In-depth understanding of Golang's Map
Publish module to NPM should be how to operate?Solutions to problems and mistake
KiCad Common Shortcuts
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
Win11没有本地用户和组怎么解决
Win10电脑不能读取U盘怎么办?不识别U盘怎么解决?
win10无法直接用照片查看器打开图片怎么办
DP4056电源保护芯片锂电池pin对pinTP4056
2.4G无线小模块CI24R1超低成本
用U盘怎么重装Win7系统?如何使用u盘重装系统win7?