当前位置:网站首页>Trees and binary trees: traversal of binary trees
Trees and binary trees: traversal of binary trees
2022-06-13 09:33:00 【Ritian juvenile wzh】
Trees and binary trees : Traversal of binary tree
The concept of binary tree traversal
Traversal of binary tree It refers to accessing all nodes in the tree in a certain order , also Each node is accessed only once The process of
Traversal is the most basic operation of binary tree , It is the basis of other operations in binary tree
Composition of binary tree :

1. Preorder traversal process
The first sequence traversal NLR The process of binary tree is :
- Access the root node ;
- The order traverses the left subtree ;
- The order traverses the right subtree ;
Binary tree preorder traversal demonstration 
First, we traverse the sequence :
A B D G C E F ( Traversal complete )
The first node of the preorder sequence is the root node
2. Middle order traversal process
In the sequence traversal LNR The process of binary tree is :
- The middle order traverses the left subtree ;
- Access the root node ;
- The middle order traverses the right subtree ;
Binary tree in order to traverse the demonstration 
Middle order traversal sequence :
D G B A E C F ( Traversal complete )
To the left of the root node of the middle order sequence is the node of the left subtree , On the right is the node of the right subtree
3. Post order traversal process
After the sequence traversal LRN The process of binary tree is :
- The latter traverses the left subtree ;
- The latter traverses the right subtree ;
- Access the root node ;
Binary tree post order traversal demonstration 
Post traversal sequence :
G D B E F C A ( Traversal complete )
The last node in the subsequent sequence is the root node
Example : If a binary tree's pre order sequence and post order sequence are just opposite . What is the form of the binary tree ?
Binary tree traversal recursive algorithm
From the three traversal processes of binary tree, we get 3 A recursive algorithm
Recursive algorithm of preorder traversal :
void PreOrder(BTNode *b) {
if(b!=NULL) {
printf("%c",b->data); // Access the root node
PreOrder(b->lchild);
PreOrder(b->rchild);
}
}
appeal visit Is the direct output node value . actually , The access node can perform various operations on the node , Such as counting , Delete nodes, etc
Recursive algorithm of middle order traversal :
void InOrder(BTNode *b) {
if(b!=NULL) {
InOrder(b->lchild);
printf("%c",b->data); // Access the root node
InOrder(b->rchild);
}
}
Recursive algorithm for post order traversal :
void PostOrder(BTNode *b) {
if(b!=NULL) {
PostOrder(b->lchild);
PostOrder(b->rchild);
printf("%c",b->data); // Access the root node
}
}
Hierarchical traversal algorithm
Hierarchical traversal process :
For a binary tree , Start at the root node , Press from top to bottom , Access each node from left to right , Each node is accessed only once
Algorithm design ideas : Use a queue
- Queue the root node ;
- The team does not have a space-time cycle : Dequeue a node from the queue *p, Visit it ;
(1) If it has a left child node , Join the left child node in the team
(2) If it has a right child node , Join the right child node in the team
The corresponding algorithm is as follows :
void LevelOrder(BTNode *b) {
BTNode *p;
BTNode *qu[MaxSize]; // Define ring queue , Store the node pointer
int front,rear; // Define the head and tail pointers
front=rear=0; // Leave the queue empty
rear++;
qu[rear]=b; // The root node pointer enters the queue
while(front!=rear) {
// The queue is not empty loop
front=(front+1)%MaxSize;
p=qu[front]; // Team leader out of line
printf("%c",p->data); // Access to the node
if(p->lchild!=NULL) {
// When you have a left child, put it in the team
rear=(rear+1)%MaxSize;
qu[rear]=p->lchild;
}
if(p->rchild!=NULL) {
// When you have a right child, put it in the team
rear=(rear+1)%MaxSize;
qu[rear]=p->rchild;
}
}
}
The time complexity of the algorithm is O(n)
边栏推荐
- Jenkins integrates LDAP. The problem of login failure of Jenkins users caused by LDAP configuration error is solved
- Classes and objects -- Inheritance
- C language: sanziqi
- 计算两个时间相差的天数(支持跨月、跨年)
- 虚拟化和云计算文章大合集
- LeetCode 201. 数字范围按位与
- Batch read all voice files under the folder
- Delete soft link
- Routing - static routing
- I have summarized the knowledge points of JS [intermediate and advanced] for you
猜你喜欢

(state compression dp+ binary) acwing 91 Shortest Hamilton path

C language: deep understanding of character functions and string functions (2)

Final principle

(dp+ memory) acwing 901 skiing

C/s model and P2P model

Jenkins接入Openldap用户认证

Figure introduction to database neo4j

Online debugging tool Arthas advanced

acwing 788. Number of pairs in reverse order

Alibaba senior experts analyze the standard design of protocol
随机推荐
Yolov5 face video stream
LeetCode 1143. 最长公共子序列
Figure introduction to database neo4j
LeetCode 202. Happy number
SQL ROW_ The number() function uses
I have summarized the knowledge points of JS [intermediate and advanced] for you
Trees and binary trees: the concept of binary trees
批量讀取文件夾下的全部語音文件
LeetCode 202. 快乐数
C language: callback function
Exercise 8-3 rotate the array to the right (20 points)
全新BMW i3的操控,能符合对3系之名产品的期待吗?
Lecture par lots de tous les fichiers vocaux sous le dossier
How to build an aby framework and run an instance
Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
Dynamic display of analog clock using digital clock in turtle Library
SpEL表达式 简单使用
@Value不生效及extend/implement其他类无法注入bean的手动注入
C language: dynamic memory management
LeetCode 6098. 统计得分小于 K 的子数组数目(前缀和+二分查找)