当前位置:网站首页>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 .
边栏推荐
- Latex learning insertion number - list of filled dots, bars, numbers
- Container cloud notes
- Lavel document reading notes -how to use @auth and @guest directives in lavel
- Ruby time format conversion strftime MS matching format
- Static comprehensive experiment ---hcip1
- System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
- What is devsecops? Definitions, processes, frameworks and best practices for 2022
- Write a program to judge whether the two arrays are equal, and then write a similar program to compare the two vectors.
- Recursion and divide and conquer strategy
- 对于程序员来说,伤害力度最大的话。。。
猜你喜欢
RHCE day 3
Idea SSH channel configuration
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
Knapsack problem and 0-1 knapsack problem
Two way process republication + routing policy
Rhcsa - day 13
Some summaries of the third anniversary of joining Ping An in China
The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
Summary of several job scheduling problems
leetcode842. Split the array into Fibonacci sequences
随机推荐
入职中国平安三周年的一些总结
2020-03-28
OSPF summary
对于程序员来说,伤害力度最大的话。。。
If you don't know these four caching modes, dare you say you understand caching?
leetcode842. Split the array into Fibonacci sequences
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
DDL statement of MySQL Foundation
Latex insert picture, insert formula
Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
Network disk installation
Write a program to judge whether the two arrays are equal, and then write a similar program to compare the two vectors.
MPLS: multi protocol label switching
Introduction to extensible system architecture
Batch distribution of SSH keys and batch execution of ansible
Uniapp--- initial use of websocket (long link implementation)
Evolution from monomer architecture to microservice architecture
Talk about scalability
Es entry series - 6 document relevance and sorting
leetcode729. My schedule 1