当前位置:网站首页>二叉树的三种遍历方式
二叉树的三种遍历方式
2022-06-27 12:05:00 【云小逸】
目录
在学数据结构的时候,我们很多人不知道在从何学起,也不知道怎么巩固所学的知识,今天在这里向大家推荐一个我认为特别优秀的一个刷题网站:牛客网,其中含有大量的算法题和面试题,
链接:牛客网YYDS
1.二叉树的结构:
每一个二叉树均可以分为三部分:1.根节点 2.左子树 3.右子树。
比如上图中的A的左右子树分别是B、C,而E的左右子树为NULL、NULL。通常我们把NULL省略。
2.二叉树的前序遍历:
又叫先根遍历,遍历顺序是根节点、左子树、右子树。
上图的前序遍历:A B D NULL NULL E NULL NULL C NULL NULL
省略NULL后:A B D E C
3.二叉树的中序遍历:
遍历顺序:左子树、根节点、右子树
上图的中序遍历:NULL D NULL B NULL E NULL A NULL C NULL
省略NULL后:D B E A C
4.二叉树的后序遍历:
遍历顺序:左子树、右子树、根节点
上图的中序遍历:NULL NULL D NULL NULL E B NULL NULL C A
省略NULL后:D E B C A
5.二叉树前、中、后序的代码实现:
前序遍历函数:
void prev_order(BTNode* root)//前序遍历
{
if (root == NULL)
{
printf("NULL");
return;
}
printf("%c ", root->data);
prev_order(root->left);
prev_order(root->right);
}
中序遍历函数:
void on_order(BTNode* root)//中序遍历
{
if (root == NULL)
{
printf("NULL\n");
return;
}
prev_order(root->left);
printf("%c ", root->data);
prev_order(root->right);
}

后序遍历:
void post_order(BTNode* root)//后序遍历
{
if (root == NULL)
{
printf(" NULL \n");
return;
}
prev_order(root->left);
prev_order(root->right);
printf("%c ", root->data);
}
完整代码:
#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)//开辟空间存储变量
{
BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
newnode->data = x;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
void prev_order(BTNode* root)//前序遍历
{
if (root == NULL)
{
printf("NULL");
return;
}
printf("%c ", root->data);
prev_order(root->left);
prev_order(root->right);
}
void on_order(BTNode* root)//中序遍历
{
if (root == NULL)
{
printf("NULL\n");
return;
}
prev_order(root->left);
printf("%c ", root->data);
prev_order(root->right);
}
void post_order(BTNode* root)//后序遍历
{
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;
}代码运行结果截图:

在学数据结构的时候,我们很多人不知道在从何学起,也不知道怎么巩固所学的知识,今天在这里向大家推荐一个我认为特别优秀的一个刷题网站:牛客网,其中含有大量的算法题和面试题,
链接:牛客网YYDS
边栏推荐
- JMeter connection DM8
- namespace ‘rlang’ 0.2.0 is being loaded, but &gt;= 0.3.0 is required
- [tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (II)
- Tidb 6.0: making Tso more efficient tidb Book rush
- picocli-入门
- 号称史上最难618,淘宝数据盘点你做对了吗?
- Getting started with go web programming: validators
- MySQL high level statements (I)
- MapReduce practical cases (customized sorting, secondary sorting, grouping, zoning)
- Neo4j:入门基础(一)之安装与使用
猜你喜欢

dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article

亚马逊测评掉评、留不上评是怎么回事呢?要如何应对?

How histrix works

pull request

56. Core principle of flutter - flutter startup process and rendering pipeline

Picocli getting started

解除百度文库VIP、语雀、知乎付费限制,原来这么简单

$15.8 billion! 2021 the world's top15 most profitable hedge fund giant

【On nacos】快速上手 Nacos

怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段
随机推荐
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
On ticheck
怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段
Comment modifier Node Fichiers dans les modules
Shell script learning notes
聊聊 Go 语言与云原生技术
树莓派 3b+ 学习
picocli-入门
1. Mx6ull startup mode
Basic usage and principle of fork/join framework
MapReduce实战小案例(自定义排序、二次排序、分组、分区)
uni-app开发微信小程序动态渲染页面,动态改变页面组件模块顺序
Neo4j:入门基础(一)之安装与使用
深入理解 happens-before 原则
dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
pull request
Thymeleaf的相关知识
Thymeleaf的配置
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
Talk about go language and cloud native technology
