当前位置:网站首页>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;
}
边栏推荐
- Return value of WaitForSingleObject
- Informatics Olympiad all in one 1000: introductory test questions
- Factory mode of "object creation" mode
- Go language functions as parameters of functions
- C#DBHelper_ FactoryDB_ GetConn
- [wustctf2020] selfie score query -1
- C language array and pointer
- Resume NFT platform trustrecruit joined Octopus network as a candidate application chain
- 2062: [example 1.3] movie tickets
- Cocoapods的相关知识点
猜你喜欢

Web3.0,「激发创造」的时代

一种快速创建测试窗口的方法

torch_ About the geometric Mini batch

编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构

Qt5 plug-in production

Data type conversion and conditional control statements

Behind the unsealing of Shanghai, this group of developers "cloud gathering" built an AI anti epidemic robot

简历 NFT 平台 TrustRecruit 加入章鱼网络成为候选应用链

List of common ACM knowledge points (to be continued)

Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging
随机推荐
Multi source BFS problem template (with questions)
C#DBHelper_ FactoryDB_ GetConn
Transmission and response of events and use cases
Successfully rated Tencent t3-2, 10000 word parsing
import torch_ Geometric loads some common datasets
C language structure
事件的传递和响应以及使用实例
1002: output the second integer
Dameng database DM8 Windows environment installation
Scyther工具形式化分析Woo-Lam协议
Relevant knowledge points of cocoapods
【mysql进阶】查询优化原理与方案(六)
2062: [example 1.3] movie tickets
Teach you how to create SSM project structure in idea
2021-05-28
章鱼网络进展月报 | 2022.5.1-5.31
聊聊MySQL的10大经典错误
Time processing in C language (conversion between string and timestamp)
Top 10 tips for visual studio code on Google
[WUSTCTF2020]颜值成绩查询-1