当前位置:网站首页>Three traversal methods of binary tree
Three traversal methods of binary tree
2022-07-05 17:06:00 【Daily study of bald girls】
Traversal of binary tree
I've been following B Watch the data structure again in the video of the station , I want to write down what I combed , Easy to review .
The traversal of binary tree has The first sequence traversal 、 In the sequence traversal 、 After the sequence traversal .
The following is an example , Summarize the three traversal methods of the binary tree .
The first sequence traversal
The principle of preorder traversal is : First root 、 To the left 、 To the right .
namely :ABCDEFGH
#define _CRT_SECURE_NO_WARNINGS
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
// Define a binary tree node
struct BinaryNode
{
char ch; // For letters
struct BinaryNode *lChild; // Left the child
struct BinaryNode *rChild; // The right child
};
void recursion(struct BinaryNode *root)
{
if(root==NULL)
{
return;
}
// The first sequence traversal
// First root
printf("%c\n",root->ch);
// To the left
recursion(root->lChild);
// To the right
recursion(root->rChild);
}
void test01()
{
// Create nodes
struct BinaryNode nodeA = {'A',NULL,NULL};
struct BinaryNode nodeB = {'B',NULL,NULL};
struct BinaryNode nodeC = {'C',NULL,NULL};
struct BinaryNode nodeD = {'D',NULL,NULL};
struct BinaryNode nodeE = {'E',NULL,NULL};
struct BinaryNode nodeF = {'F',NULL,NULL};
struct BinaryNode nodeG = {'G',NULL,NULL};
struct BinaryNode nodeH = {'H',NULL,NULL};
// Establish the relationship between nodes
nodeA.lChild=&nodeB;
nodeA.rChild=&nodeF;
nodeB.rChild=&nodeC;
nodeC.lChild=&nodeD;
nodeC.rChild=&nodeE;
nodeF.rChild=&nodeG;
nodeG.lChild=&nodeH;
// recursive Realize preorder traversal
recursion(&nodeA);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
The operation results are as follows :
In the sequence traversal
The principle of medium order traversal is : First left 、 Regrowth 、 To the right .
namely :BDCEAFHG
#define _CRT_SECURE_NO_WARNINGS
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
// Binary tree node
struct BinaryNode
{
char ch; // For letters
struct BinaryNode *lChild; // Left the child
struct BinaryNode *rChild; // The right child
};
void recursion(struct BinaryNode *root)
{
if(root==NULL)
{
return;
}
// In the sequence traversal
// First left
recursion(root->lChild);
// Regrowth
printf("%c\n",root->ch);
// To the right
recursion(root->rChild);
}
void test01()
{
// Create nodes
struct BinaryNode nodeA = {
'A',NULL,NULL};
struct BinaryNode nodeB = {
'B',NULL,NULL};
struct BinaryNode nodeC = {
'C',NULL,NULL};
struct BinaryNode nodeD = {
'D',NULL,NULL};
struct BinaryNode nodeE = {
'E',NULL,NULL};
struct BinaryNode nodeF = {
'F',NULL,NULL};
struct BinaryNode nodeG = {
'G',NULL,NULL};
struct BinaryNode nodeH = {
'H',NULL,NULL};
// Establish the relationship between nodes
nodeA.lChild=&nodeB;
nodeA.rChild=&nodeF;
nodeB.rChild=&nodeC;
nodeC.lChild=&nodeD;
nodeC.rChild=&nodeE;
nodeF.rChild=&nodeG;
nodeG.lChild=&nodeH;
// recursive Implement middle order traversal
recursion(&nodeA);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
The operation results are as follows :
After the sequence traversal
The principle of post order traversal is : First left 、 To the right 、 Regrowth .
namely :DECBHGFA
#define _CRT_SECURE_NO_WARNINGS
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
// Binary tree node
struct BinaryNode
{
char ch; // Show letters
struct BinaryNode *lChild; // Left the child
struct BinaryNode *rChild; // The right child
};
void recursion(struct BinaryNode *root)
{
if(root==NULL)
{
return;
}
// After the sequence traversal
// First left
recursion(root->lChild);
// To the right
recursion(root->rChild);
// Regrowth
printf("%c\n",root->ch);
}
void test01()
{
// Create nodes
struct BinaryNode nodeA = {
'A',NULL,NULL};
struct BinaryNode nodeB = {
'B',NULL,NULL};
struct BinaryNode nodeC = {
'C',NULL,NULL};
struct BinaryNode nodeD = {
'D',NULL,NULL};
struct BinaryNode nodeE = {
'E',NULL,NULL};
struct BinaryNode nodeF = {
'F',NULL,NULL};
struct BinaryNode nodeG = {
'G',NULL,NULL};
struct BinaryNode nodeH = {
'H',NULL,NULL};
// Establish the relationship between nodes
nodeA.lChild=&nodeB;
nodeA.rChild=&nodeF;
nodeB.rChild=&nodeC;
nodeC.lChild=&nodeD;
nodeC.rChild=&nodeE;
nodeF.rChild=&nodeG;
nodeG.lChild=&nodeH;
// recursive Implement postorder traversal
recursion(&nodeA);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
The operation results are as follows :
If there are mistakes, you are welcome to criticize and correct ~
边栏推荐
- If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
- 【剑指 Offer】63. 股票的最大利润
- Error in composer installation: no composer lock file present.
- 国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
- [team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
- Jarvis OJ Flag
- ECU introduction
- High number | summary of calculation methods of volume of rotating body, double integral calculation of volume of rotating body
- Keras crash Guide
- Jarvis OJ 远程登录协议
猜你喜欢
项目引入jar从私服Nexus 拉去遇到的一个问题
Learnopongl notes (I)
[Jianzhi offer] 63 Maximum profit of stock
【剑指 Offer】63. 股票的最大利润
Detailed explanation of use scenarios and functions of polar coordinate sector diagram
深耕5G,芯讯通持续推动5G应用百花齐放
浏览器渲染原理以及重排与重绘
7.Scala类
The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
干货!半监督预训练对话模型 SPACE
随机推荐
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
Is it safe for qiniu business school to open a stock account? Is it reliable?
Keras crash Guide
麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
Jarvis OJ Flag
阈值同态加密在隐私计算中的应用:解读
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Jarvis OJ shell流量分析
拷贝方式之DMA
【性能测试】全链路压测
【性能测试】jmeter+Grafana+influxdb部署实战
thinkphp模板的使用
Sentinel-流量防卫兵
C# TCP如何设置心跳数据包,才显得优雅呢?
【729. 我的日程安排錶 I】
NPM installation
Timestamp strtotime the day before or after the date
Wechat official account web page authorization login is so simple
The first lesson of EasyX learning
[first lecture on robot coordinate system]