当前位置:网站首页>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 .
边栏推荐
- Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
- Latex error: missing delimiter (. Inserted) {\xi \left( {p,{p_q}} \right)} \right|}}
- 183 sets of free resume templates to help everyone find a good job
- Basic principle of servlet and application of common API methods
- 今日睡眠质量记录78分
- MongoDB数据日期显示相差8小时 原因和解决方案
- uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
- system design
- Latex insert picture, insert formula
- Service developers publish services based on EDAs
猜你喜欢
If you don't know these four caching modes, dare you say you understand caching?
Evolution from monomer architecture to microservice architecture
【Day1】 deep-learning-basics
【Day2】 convolutional-neural-networks
Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
Servlet基本原理与常见API方法的应用
Dynamic address book
BGP advanced experiment
From programmers to large-scale distributed architects, where are you (I)
Histogram equalization
随机推荐
用数据告诉你高考最难的省份是哪里!
Rhcsa12
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.
BGP ---- border gateway routing protocol ----- basic experiment
Dynamic address book
Crawl Zhejiang industry and trade news page
Exercise 7-4 find out the elements that are not common to two arrays (20 points)
Basic principle of servlet and application of common API methods
Write a program that uses pointers to set all elements of an int array to 4.18: 0.
OSPF comprehensive experiment
VLAN part of switching technology
183 sets of free resume templates to help everyone find a good job
A little feeling
Legion is a network penetration tool
Basic data types of MySQL
转载:等比数列的求和公式,及其推导过程
Seven examples to understand the storage rules of shaped data on each bit
Dynamic memory management
From programmers to large-scale distributed architects, where are you (I)
Exercise 8-7 string sorting (20 points)