当前位置:网站首页>二叉树的三种遍历方式
二叉树的三种遍历方式
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
边栏推荐
- 深入理解 happens-before 原则
- i. Construction of mx6ull C language environment
- 【On nacos】快速上手 Nacos
- . Net6 access skywalking link tracking complete process
- 【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(二)
- 【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元
- 消息隊列的使用
- Topic38——56. Consolidation interval
- Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
- 行业洞察 | 新零售业态下,品牌电商应如何重塑增长?
猜你喜欢

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

Online bidding of Oracle project management system

AI for Science:科研范式、开源平台和产业形态

MySQL高阶语句(一)

57. The core principle of flutter - layout process

Interviewer: with the for loop, why do you need foreach?

pull request

怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段

如何修改 node_modules 裏的文件

56. Core principle of flutter - flutter startup process and rendering pipeline
随机推荐
Dynamic programming [III] (interval DP) stone merging
MapReduce原理剖析(深入源码)
Daily leetcode force deduction (21~25)
57. The core principle of flutter - layout process
行业洞察 | 新零售业态下,品牌电商应如何重塑增长?
私藏干货分享:关于企业架构中如何进行平台化
Uniapp drop-down layer selection box effect demo (sorting)
Custom multithreading base class threading Event
uniapp下拉弹层选择框效果demo(整理)
自学ADT和OOP
Secyun won the "2022 AI analysis · it operation and maintenance vendor panorama report" as the representative vendor of intelligent operation and maintenance aiops Market
Jianmu continuous integration platform v2.5.0 release
想学好C语言,操作符也很重要
动态规划【四】(计数类dp)例题:整数划分
面试突击60:什么情况会导致 MySQL 索引失效?
What is the TCP 3-time handshake process?
Two usages of enumeration classes
如何修改 node_modules 里的文件
Interview shock 60: what will cause MySQL index invalidation?
Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
