当前位置:网站首页>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.
边栏推荐
- What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
- Win10 cannot directly use photo viewer to open the picture
- Redis常见面试题
- 关于c语言的调试技巧
- Win11没有本地用户和组怎么解决
- What should I do if I install a solid-state drive in Win10 and still have obvious lags?
- 专硕与学硕
- 模板系列-二分
- Golang 垃圾回收机制详解
- 镜像法求解接地导体空腔电势分布问题
猜你喜欢
随机推荐
How to add a one-key shutdown option to the right-click menu in Windows 11
PHY6222蓝牙5.2支持MESH组网M0内核超低功耗
项目:数据库表的梳理
2022TI杯D题混沌信号产生实验装置
一篇文章彻底理解Redis的持久化:RDB、AOF
LORA芯片ASR6601支持M4内核的远距离传输芯片
使用 腾讯云搭建一个个人博客
深入理解Golang之Map
推开机电的大门《电路》(一):电压,电流,参考方向
Mysql lock
使用npx -p @storybook/cli sb init安装失败,手把手搭建专属的storybook
总结计算机网络超全面试题
Win11怎么在右键菜单添加一键关机选项
IPV4和IPV6是什么?
Please make sure you have the correct access rights and the repository exists. Problem solved
Use tencent cloud builds a personal blog
模板系列-并查集
Yolov5 official code reading - prior to transmission
13.56MHZ刷卡芯片CI521兼容cv520/ci520支持A卡B卡MIFARE协议
KiCad Common Shortcuts









