当前位置:网站首页>Sequence traversal of binary tree (implemented in C language)
Sequence traversal of binary tree (implemented in C language)
2022-07-26 13:51:00 【Three waters of ndream】
Sequence traversal
Sequence traversal code idea
With a secondary queue .
- First join the root node of the binary tree ,
- And then out of the team , Access the outbound node ,
- If it has a left subtree , The root node of the left subtree will be queued ;
- If it has a right subtree , The root node of the right subtree will be queued .
- And then out of the team ,
- Access the outbound node …… So again and again , Until the queue is empty
void LevelOrder(BiTree T)
{
InitQueue(&Q); // Initialize the secondary queue
BiTree p;
EnQueue(&Q, T); // Put the root node in the team
while(!IsEmpty(Q)) // If the queue is not empty, the loop
{
DeQueue(Q, p); // The head of the team goes out
visit(p);
if (p->lchild != NULL)
{
EnQueue(Q, p->lchild); // Zuozi tree is not empty , Then the root node of the left subtree joins the team
}
if (p->rchild != NULL)
{
EnQueue(Q, p->rchild); // Right subtree is not empty , Then the root node of the right subtree joins the team
}
}
}
Implementation process
According to the above code ideas , You need to implement two data structures , A binary tree , A line , The functions that need to be realized are
- Binary tree 、 Data structure definition of queue
- The creation of binary tree 、 Initialization of the queue
- Sequence traversal of binary tree 、 Out of queue 、 Joining operation
Code implementation

#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef char TElemType;
const int MAXSIZES = 50;
// Binary tree storage structure
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
typedef BiTree QElemType;
// The storage structure of the queue
typedef struct {
QElemType data[MAXSIZES];
int front;
int rear;
}SqQueue;
// Create a binary tree through preorder traversal
void CreateBiTree(BiTree *T)
{
TElemType ch;
scanf("%c", &ch);
if (ch == '#')
{
*T = NULL;
}
else
{
*T = (BiTree) malloc(sizeof(BiTNode));
if (!T)
{
exit(0);
}
(*T)->data = ch;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
// Initialize queue
void InitQueue(SqQueue *Q)
{
Q->front = Q->rear = 0;
}
// Queue entry of auxiliary queue
void EnQueue(SqQueue *Q, QElemType q)
{
if ((Q->rear + 1) % MAXSIZES == Q->front)
return;
Q->data[Q->rear] = q;
Q->rear = (Q->rear + 1) % MAXSIZES;
}
// Outbound operation of auxiliary queue
void DeQueue(SqQueue *Q, QElemType *q)
{
if (Q->front == Q->rear)
return;
*q = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZES;
}
// Middle order traversal of binary trees
void InOrderTraverse(BiTree T)
{
if (!T)
{
return;
}
InOrderTraverse(T->lchild);
printf("%c", T->data);
InOrderTraverse(T->rchild);
}
// Sequence traversal of binary tree
void LevelOrder(BiTree T)
{
SqQueue Q;
InitQueue(&Q); // Initialize the secondary queue
BiTree p;
EnQueue(&Q, T); // Put the root node in the team
while(Q.front != Q.rear) // If the queue is not empty, the loop
{
DeQueue(&Q, &p); // The head of the team goes out
printf("%c", p->data);
if (p->lchild != NULL)
{
EnQueue(&Q, p->lchild); // Zuozi tree is not empty , Then the root node of the left subtree joins the team
}
if (p->rchild != NULL)
{
EnQueue(&Q, p->rchild); // Right subtree is not empty , Then the root node of the right subtree joins the team
}
}
}
int main()
{
BiTree t;
printf(" Please expand the binary tree in the previous order to enter :(# Represents an empty node )\n");
CreateBiTree(&t);
printf(" In the sequence traversal : ");
InOrderTraverse(t);
printf("\n Sequence traversal : ");
LevelOrder(t);
return 0;
}
Reference resources :
Sequence traversal idea 《 Wang Dao data structure 》
边栏推荐
- 2022-07-26日报:Alphafold DB数据库建立一周年,官推盘点亮点研究
- [oauth2] v. oauth2loginauthenticationfilter
- Unicode file parsing methods and existing problems
- [oauth2] VIII. Configuration logic of oauth2 login -oauth2loginconfigurer and oauth2clientconfigurer
- Explain four interesting NPM usages with charts
- Comparator (interface between comparable and comparator)
- JUC summary
- Detailed relation extraction model casrel
- Re bet overseas: Alibaba, jd.com and SF again fight for "internal power"
- Official announcement! Edweisen group and Baidu xirang reached a deep co creation cooperation
猜你喜欢

IDEA(warning)No artifacts configured

Unicode file parsing methods and existing problems
![[shaders realize overlay to re cover cross dressing effect _shader effect Chapter 9]](/img/f3/48ca9e1e8889afc0993084d6416575.png)
[shaders realize overlay to re cover cross dressing effect _shader effect Chapter 9]

循环队列(c语言实现)

Solve the problem that JUnit of idea console cannot be input with scanner

大小端模式

Time complexity and space complexity

LCL three-phase PWM rectifier (inverter)

JS get the current time, time and timestamp conversion
Control the probability of random winning [C | random]
随机推荐
421. 数组中两个数的最大异或值
(Reprint) creation methods of various points in ArcGIS Engine
Comparison between SIGMOD function and softmax function
MySql的DDL和DML和DQL的基本语法
周伟:寻找非共识性投资机会,陪伴延迟满足的创始团队
JSON data transfer parameters & date type parameter transfer
SuperMap iclient for leaflet loads Gauss Kruger projection three-dimensional zonation CGCS2000 geodetic coordinate system WMTs service
【黑马早报】字节旗下多款APP下架;三只松鼠脱氧剂泄露致孕妇误食;CBA公司向B站索赔4.06亿;马斯克否认与谷歌创始人妻子有婚外情...
.net6 encounter with the League of heroes - create a game assistant according to the official LCU API
Time complexity analysis of bubble sorting
IDEA(warning)No artifacts configured
JS submit the form to determine whether the user name and password are empty
Pytoch learning notes (III) use, modification, training (cpu/gpu) and verification of the model
.NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI
大脑带来的启发:深度神经网络优化中突触整合原理介绍
云智技术论坛工业专场 明天见!
Ros2 learning (1) introduction to ros2
Force deduction ----- the number of words in the string
"Intermediate and advanced test questions": what is the implementation principle of mvcc?
Pytorch学习笔记(三)模型的使用、修改、训练(CPU/GPU)及验证