当前位置:网站首页>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
边栏推荐
- MIT6.031 软件构造 Reading7阅读笔记Designing Specifications(设计规范)
- mybaitis生成器详解
- Minimum editing distance (linear DP writing method)
- 一个有趣的网络掩码的实验
- Detailed configuration of log4j
- 在 Golang 中构建 CRUD 应用程序
- How to participate in openharmony code contribution
- 秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商
- Write it down once Net analysis of a property management background service stuck
- .NET6接入Skywalking链路追踪完整流程
猜你喜欢
![[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (II)](/img/ce/b58e436e739a96b3ba6d2d33cf8675.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (II)

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

微服务拆分
![Dynamic programming [4] (counting class DP) example: integer partition](/img/06/4b3863bbb85582348c6f4ea7c5511e.png)
Dynamic programming [4] (counting class DP) example: integer partition

面试突击60:什么情况会导致 MySQL 索引失效?

nmcli team bridge 基本配置
![[on Nacos] get started quickly](/img/cc/af4ab640952b880595a89f66688ff5.jpg)
[on Nacos] get started quickly

JMeter connection DM8

It is so simple to remove the payment restrictions on VIP, YuQue and Zhihu in Baidu Library

Histrix工作原理
随机推荐
亚马逊测评掉评、留不上评是怎么回事呢?要如何应对?
Mit6.031 software construction7 reading notesdesigning specifications
script defer async模式
LeetCode_快速幂_递归_中等_50.Pow(x, n)
Write it down once Net analysis of a property management background service stuck
Time management understood after being urged to work at home
自学ADT和OOP
Topic38——56. Consolidation interval
Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
MySQL高阶语句(一)
MapReduce principle analysis (in-depth source code)
[on Nacos] get started quickly
log4j. Detailed configuration of properties
How histrix works
面试突击60:什么情况会导致 MySQL 索引失效?
微服务之配置管理中心
解除百度文库VIP、语雀、知乎付费限制,原来这么简单
Private dry goods sharing: how to implement platform in Enterprise Architecture
Comment modifier Node Fichiers dans les modules
Snipaste, the world's strongest screenshot software
