当前位置:网站首页>Binary tree traversal
Binary tree traversal
2022-06-12 13:46:00 【Disobey the law】
Four traversal methods
( Always remember to write , But I can't find , It was in the draft box , Let's send it out )
In the sequence traversal
void InorderTraversal(BinTree BT)
{
if (BT)
{
InorderTraversal(BT->Left);
printf(" %c", BT->Data);
InorderTraversal(BT->Right);
}
}
The first sequence traversal
Recursive writing :
void PreorderTraversal(BinTree BT)
{
if (BT)
{
printf(" %c", BT->Data);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
}
Non recursive writing :
void PreorderPrintLeaves(BinTree BT)
{
BinTree T = BT;
while (T || !IsEmpty(S)) {
while (T) {
Push(S, T);
printf("%5d", T->Data);
T = T->Left;
}
if (!IsEmpty(S)) {
T = Pop(S);
T = T->Right;
}
}
}
After the sequence traversal
void PostorderTraversal(BinTree BT)
{
if (BT)
{
PostorderTraversal(BT->Left);
PostorderTraversal(BT->Right);
printf(" %c", BT->Data);
}
}
Sequence traversal
struct queue {
BinTree Data;
struct queue* Next;
};
void Pop(BinTree data);// The team
BinTree Push();// Out of the team
struct queue* head, * tail;
void LevelorderTraversal(BinTree BT)
{
if (!BT) return;// The empty tree returns directly
head = tail = NULL;
Pop(BT);
while (tail != NULL&&head!=NULL) {
BinTree T= Push();
if(T)printf(" %c", T->Data);
if (T->Left) Pop(T->Left);
if (T->Right) Pop(T->Right);
}
}
// The team
// Simple queue
void Pop(BinTree data)
{
struct queue* temp = (struct queue*)malloc(sizeof(struct queue));
temp->Data = data;
temp->Next = NULL;
if (head == NULL) head = temp;
else tail->Next = temp;
tail = temp;
}
// Out of the team
BinTree Push() {
BinTree point = head->Data;
head = head->Next;
return point;
}
边栏推荐
- Paw advanced user guide
- Redis message queue repeated consumption
- 播放器屏幕方向方案
- Codeforces 1638 A. reverse - simple thinking
- Multi source BFS problem template (with questions)
- 开发中使用的语言技巧
- 将字符串转为16进制字符串并显示出来
- 高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法
- jsp跳转问题,不能显示数据库数据,并且不能跳转
- Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging
猜你喜欢

Implementing singleton mode of database under QT multithreading

【mysql进阶】mysql索引数据结构的演变(四)

Record some settings for visual studio 2019

Go zero micro Service Practice Series (II. Service splitting)

Implementing tensorflow deep learning framework similarflow with numpy

简述CGI与FASTCGI区别

How to solve the problem of data table query error when SQLite writes the registration function?

Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem

imagemagick:a gentle introduction to magick++

Automatic Generation of Visual-Textual Presentation Layout
随机推荐
Tinyxml Usage Summary
view的子视图的递归
Getting started with NVIDIA Jetson nano Developer Kit
Tensorrt, onnx to tensorrt in mmclas
Simple implementation of gpuimage chain texture
Player screen orientation scheme
Codeforces 1637 E. best pair - Thinking
Codeforces 1629 D. pecuriar movie preferences - simple thinking, palindrome strings
上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
C language structure
【SemiDrive源码分析】【X9芯片启动流程】25 - MailBox 核间通信机制介绍(代码分析篇)之 RPMSG-IPCC RTOS & QNX篇
Innovation training (x) advanced interface beautification
1003: align output
聊聊MySQL的10大经典错误
Resume NFT platform trustrecruit joined Octopus network as a candidate application chain
Transmission and response of events and use cases
How to solve the problem of data table query error when SQLite writes the registration function?
Innovation training (XI) summary of some bugs in the development process
JVM runtime parameters
Cocoapods的相关知识点