当前位置:网站首页>Three traversal methods of binary tree
Three traversal methods of binary tree
2022-06-27 12:33:00 【Yun Xiaoyi】
Catalog
1. The structure of a binary tree :
2. Preorder traversal of two tree :
3. Middle order traversal of binary trees :
4. Postorder traversal of binary trees :
5. Before the binary tree 、 in 、 Code implementation of post order :
Middle order ergodic function :
After the sequence traversal :
Screenshot of code running result :
When learning data structure , Many of us don't know where to start , I don't know how to consolidate my knowledge , Today, I would like to recommend a website that I think is particularly excellent : Cattle from , There are a lot of algorithm questions and interview questions ,
link : Cattle from YYDS
1. The structure of a binary tree :
Each binary tree can be divided into three parts :1. The root node 2. The left subtree 3. Right subtree .
Like the one above A The left and right subtrees of are B、C, and E The left and right subtrees of are NULL、NULL. Usually we put NULL Omit .
2. Preorder traversal of two tree :
Also called root first traversal , The traversal order is the root node 、 The left subtree 、 Right subtree .
Preorder traversal in the figure above :A B D NULL NULL E NULL NULL C NULL NULL
Omit NULL after :A B D E C
3. Middle order traversal of binary trees :
traversal order : The left subtree 、 The root node 、 Right subtree
The middle order traversal in the figure above :NULL D NULL B NULL E NULL A NULL C NULL
Omit NULL after :D B E A C
4. Postorder traversal of binary trees :
traversal order : The left subtree 、 Right subtree 、 The root node
The middle order traversal in the figure above :NULL NULL D NULL NULL E B NULL NULL C A
Omit NULL after :D E B C A
5. Before the binary tree 、 in 、 Code implementation of post order :
Preorder traversal function :
void prev_order(BTNode* root)// The former sequence traversal
{
if (root == NULL)
{
printf("NULL");
return;
}
printf("%c ", root->data);
prev_order(root->left);
prev_order(root->right);
}
Middle order ergodic function :
void on_order(BTNode* root)// In the sequence traversal
{
if (root == NULL)
{
printf("NULL\n");
return;
}
prev_order(root->left);
printf("%c ", root->data);
prev_order(root->right);
}

After the sequence traversal :
void post_order(BTNode* root)// After the sequence traversal
{
if (root == NULL)
{
printf(" NULL \n");
return;
}
prev_order(root->left);
prev_order(root->right);
printf("%c ", root->data);
}
Complete code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType data;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}BTNode;
BTNode* BuyNode(BTDataType x)// Open up space to store variables
{
BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
newnode->data = x;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
void prev_order(BTNode* root)// The former sequence traversal
{
if (root == NULL)
{
printf("NULL");
return;
}
printf("%c ", root->data);
prev_order(root->left);
prev_order(root->right);
}
void on_order(BTNode* root)// In the sequence traversal
{
if (root == NULL)
{
printf("NULL\n");
return;
}
prev_order(root->left);
printf("%c ", root->data);
prev_order(root->right);
}
void post_order(BTNode* root)// After the sequence traversal
{
if (root == NULL)
{
printf(" NULL \n");
return;
}
prev_order(root->left);
prev_order(root->right);
printf("%c ", root->data);
}
int main(void)
{
BTNode* n1 = BuyNode('A');
BTNode* n2 = BuyNode('B');
BTNode* n3 = BuyNode('C');
BTNode* n4 = BuyNode('D');
BTNode* n5 = BuyNode('E');
n1->left = n2;
n1->right = n3;
n2->left = n4;
n2->right = n5;
prev_order(n1);
printf("\n");
on_order(n1);
printf("\n");
post_order(n1);
printf("\n");
return 0;
}Screenshot of code running result :

When learning data structure , Many of us don't know where to start , I don't know how to consolidate my knowledge , Today, I would like to recommend a website that I think is particularly excellent : Cattle from , There are a lot of algorithm questions and interview questions ,
link : Cattle from YYDS
边栏推荐
- word文本框换页
- In 2021, the global professional liability insurance revenue was about USD 44740million, and it is expected to reach USD 55980million in 2028. From 2022 to 2028, the CAGR was 3.5%
- JMETER连接DM8
- 带你认识图数据库性能和场景测试利器LDBC SNB
- Deep understanding of happens before principle
- 关闭windows defender安全中心的方法
- Industry insight - how should brand e-commerce reshape growth under the new retail format?
- [high frequency interview questions] difficulty 1.5/5, LCS template questions
- Word text box page feed
- 57. The core principle of flutter - layout process
猜你喜欢

MySQL高阶语句(一)

二叉树的三种遍历方式

Minimum editing distance (linear DP writing method)

ssh工作流程及原理

推荐系统的下一步?阿里时空聚合GNN,效果吊打LightGCN!

picocli-入门

In 2021, the global carbon graphite brush revenue is about US $2366million, and it is expected to reach US $2701.8 million in 2028

Operators are also important if you want to learn the C language well

最短编辑距离(线性dp写法)

JMeter connection DM8
随机推荐
Take stock of some easy-to-use and niche markdown editors
Thymeleaf的相关知识
C # WPF realizes undo redo function
How histrix works
Uni app sends request instructions using the escook / request miniprogram plug-in
AI for Science:科研范式、开源平台和产业形态
Master formula
记一次 .NET 某物管后台服务 卡死分析
In 2021, the global enhanced oil production surfactant revenue was about USD 202.3 million, and it is expected to reach USD 297.1 million in 2028
Interview shock 60: what will cause MySQL index invalidation?
在 Golang 中构建 CRUD 应用程序
mybaitis生成器详解
Browser cookie to selenium cookie login
面试突击60:什么情况会导致 MySQL 索引失效?
pull request
自学ADT和OOP
mysql学习1:安装mysql
master公式
聊聊 Go 语言与云原生技术
Mit6.031 software construction7 reading notesdesigning specifications
