当前位置:网站首页>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.
边栏推荐
- LORA芯片ASR6601支持M4内核的远距离传输芯片
- 开心一下,9/28名场面合集
- A clean start Windows 7?How to load only the basic service start Windows 7 system
- 5. Use RecyclerView to elegantly achieve waterfall effect
- pygame image rotate continuously
- Win10上帝模式干嘛的?Win10怎么开启上帝模式?
- What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
- Win11声卡驱动如何更新?Win11声卡驱动更新方法
- What should I do if I install a solid-state drive in Win10 and still have obvious lags?
- 倍增和稀疏表
猜你喜欢
GMP scheduling model of golang
CS4398音频解码替代芯片DP4398完全兼容DAC解码
cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
如何用硬币模拟1/3的概率,以及任意概率?
推开机电的大门《电路》(二):功率计算与判断
镜像法求解接地导体空腔电势分布问题
Win11 computer off for a period of time without operating network how to solve
Spark及相关生态组件安装配置——快速回忆
Yolov5 official code reading - prior to transmission
pygame draw arc
随机推荐
2022TI杯D题混沌信号产生实验装置
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
镜像法求解接地导体空腔电势分布问题
推开机电的大门《电路》(二):功率计算与判断
2.4G无线小模块CI24R1超低成本
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
Flink + sklearn - use JPMML implement flink deployment on machine learning model
MATLAB绘制平面填充图入门详解
Use tencent cloud builds a personal blog
DP1332E内置c8051的mcu内核NFC刷卡芯片国产兼容NXP
Win10 computer can't read U disk?Don't recognize U disk how to solve?
LORA芯片ASR6601支持M4内核的远距离传输芯片
The SSE instructions into ARM NEON
GMP scheduling model of golang
如何用硬币模拟1/3的概率,以及任意概率?
flink+sklearn——使用jpmml实现flink上的机器学习模型部署
MATLAB绘图函数ezplot入门详解
MATLAB绘图函数plot详解
FP7195降压恒流PWM转模拟调光零压差大功率驱动方案原理图
pygame图像连续旋转