当前位置:网站首页>On binary tree (C language)
On binary tree (C language)
2022-07-04 10:27:00 【Lol only plays Timo】
Binary tree is a very classical non-linear discontinuous data structure in tree data structure , It is widely used because of its excellent time complexity , In this article, we will talk about the generation and traversal of binary trees .
For example, how can we solve the storage problem of binary trees in the figure below ?
Here we can control the input format when inputting , That is, we can input the left subtree first, set an end flag after the input of the left subtree of a branch, and then continue to input the right subtree , In the figure above, we can input according to the following format :
ABC##DE###FG###
Now let's look at the code directly , There are some recursive ideas in the code , If you don't understand anything, you can track variables , It will also be clearer to see in this way .
void creatB_TREE(B_TREE **root) {
char ch;
scanf("%c",&ch);
if (ch == '#') {
*root = NULL;
} else {
*root = (B_TREE *) calloc(sizeof(B_TREE), 1);
(*root)->data = ch;
creatB_TREE(&((*root)->leftChild));
creatB_TREE(&((*root)->rightChild));
}
}
After writing this function, let's take a look at the traversal of binary tree , The traversal of binary trees is divided into three types, namely, the first root order , Middle root order , Posterior root order . It looks like three types, but their code is only a little different , We use “ First root order ” For example, let's talk about traversal of binary tree .
The so-called first root order is to access the root node first, take out the data in it, and then treat the left subtree of the root node as the root node, continue to access the data in it, and then access the right subtree after the left subtree is accessed . There are also some recursive ideas , It is suggested that you carry out variable tracking for easy understanding .
void preRootOrder(B_TREE *root) {
if (root) {
printf("[%c] ",root->data);
preRootOrder(root->leftChild);
preRootOrder(root->rightChild);
}
}
After understanding the above code, we will directly follow the code of middle root order and post root order , The difference between them is very small , There is only a small difference in when to fetch the data in the root node .
void posteriorRootOrder(B_TREE *root){
if (root) {
posteriorRootOrder(root->leftChild);
posteriorRootOrder(root->rightChild);
printf("[%c] ",root->data);
}
}
void middleRootOrder(B_TREE *root) {
if (root) {
middleRootOrder(root->leftChild);
printf("[%c] ",root->data);
middleRootOrder(root->rightChild);
}
}
Finally, let's take a look at the complete code and its operation :
#include <stdio.h>
#include <malloc.h>
typedef struct B_TREE {
char data;
struct B_TREE *leftChild;
struct B_TREE *rightChild;
}B_TREE;
void creatB_TREE(B_TREE **root);
void preRootOrder(B_TREE *root);
void middleRootOrder(B_TREE *root);
void posteriorRootOrder(B_TREE *root);
void posteriorRootOrder(B_TREE *root){
if (root) {
posteriorRootOrder(root->leftChild);
posteriorRootOrder(root->rightChild);
printf("[%c] ",root->data);
}
}
void middleRootOrder(B_TREE *root) {
if (root) {
middleRootOrder(root->leftChild);
printf("[%c] ",root->data);
middleRootOrder(root->rightChild);
}
}
void preRootOrder(B_TREE *root) {
if (root) {
printf("[%c] ",root->data);
preRootOrder(root->leftChild);
preRootOrder(root->rightChild);
}
}
void creatB_TREE(B_TREE **root) {
char ch;
scanf("%c",&ch);
if (ch == '#') {
*root = NULL;
} else {
*root = (B_TREE *) calloc(sizeof(B_TREE), 1);
(*root)->data = ch;
creatB_TREE(&((*root)->leftChild));
creatB_TREE(&((*root)->rightChild));
}
}
int main() {
B_TREE *root = NULL;
printf("please input your data,'#' is symbol of over:");
creatB_TREE(&root);
printf("preRootOrder:");
preRootOrder(root);
printf("\n");
printf("middleRootOrder:");
middleRootOrder(root);
printf("\n");
printf("posteriorRootOrder:");
posteriorRootOrder(root);
printf("\n");
return 0;
}

If you have any questions, please leave a message below , Thank you for watching .
边栏推荐
- 使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
- Es entry series - 6 document relevance and sorting
- 【Day2】 convolutional-neural-networks
- Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing
- uniapp---初步使用websocket(长链接实现)
- Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
- IPv6 comprehensive experiment
- Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 2
- Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
- Write a program to define an array with 10 int elements, and take its position in the array as the initial value of each element.
猜你喜欢

入职中国平安三周年的一些总结

Static comprehensive experiment ---hcip1
![[FAQ] summary of common causes and solutions of Huawei account service error 907135701](/img/73/c4ee842475f05e2e67297fcac68779.png)
[FAQ] summary of common causes and solutions of Huawei account service error 907135701

Servlet基本原理与常见API方法的应用

Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment

Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address

2. Data type

基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1

If the uniapp is less than 1000, it will be displayed according to the original number. If the number exceeds 1000, it will be converted into 10w+ 1.3k+ display

Custom type: structure, enumeration, union
随机推荐
Es advanced series - 1 JVM memory allocation
A little feeling
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
Service developers publish services based on EDAs
Application of safety monitoring in zhizhilu Denggan reservoir area
Delayed message center design
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
leetcode1229. Schedule the meeting
/*The rewriter outputs the contents of the IA array. It is required that the type defined by typedef cannot be used in the outer loop*/
Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
Kotlin set operation summary
Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
Three schemes of ZK double machine room
Rhcsa learning practice
Check 15 developer tools of Alibaba
BGP advanced experiment
Leetcode48. Rotate image
AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers