当前位置:网站首页>Establishment and traversal of binary tree (implemented in C language)
Establishment and traversal of binary tree (implemented in C language)
2022-07-28 14:56:00 【Three waters of ndream】
Binary tree
Definition of binary tree structure
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
The establishment of binary tree
Expand the binary tree input according to the previous order
void CreatBiTree(BiTree* T)
{
TElemType ch;
scanf("%c",&ch);
if(ch == '#')
{
*T = NULL; // If the input is # Set the address of the node to NULL
}
else
{
*T = (BiTree) malloc(sizeof(BiTNode));
if(!T)
{
exit(OVERFLOW);
}
(*T)->data = ch;
CreatBiTree(&(*T)->lchild);
CreatBiTree(&(*T)->rchild);
}
}
When typing this code , I have always had a doubt , Is to ask why it needs to pass a double pointer in this place , So when I wrote it, I changed it to use pointer variables , Instead of double pointers , After writing , An error occurred while outputting , The found value is not passed into the binary tree , Then it was changed to the original version , Use double pointers , Just make mistakes , This should be the problem of formal parameters and arguments , A little confused , I still need to read more books ……
Another problem is that when you create input, you can't input characters one by one , Instead, you can only lose all at once , He will jump out of the cycle , Now I don't know the reason ……
Traversal of binary tree ( Recursive implementation )
The former sequence traversal
void PreOrderTraverse(BiTree T)
{
if(!T)
{
return;
}
printf("%c",T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
In the sequence traversal
void InOrderTraverse(BiTree T)
{
if(!T)
{
return;
}
InOrderTraverse(T->lchild);
printf("%c",T->data);
InOrderTraverse(T->rchild);
}
After the sequence traversal
void PostOrderTraverse(BiTree T)
{
if(!T)
{
return;
}
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
printf("%c",T->data);
}
experimental result 
Complete code
/* Binary tree */
#include <stdio.h>
#include <stdlib.h>
#define OVERFLOW 0
typedef char TElemType;
// Define the structure of the nodes of the tree ,BiTree Pointer variable of node
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
void CreatBiTree(BiTree* T)
{
TElemType ch;
scanf("%c",&ch);
if(ch == '#')
{
*T = NULL; // If the input is # Set the address of the node to NULL
}
else
{
*T = (BiTree) malloc(sizeof(BiTNode));
if(!T)
{
exit(OVERFLOW);
}
(*T)->data = ch;
CreatBiTree(&(*T)->lchild);
CreatBiTree(&(*T)->rchild);
}
}
// Recursive implementation of preorder traversal
void PreOrderTraverse(BiTree T)
{
if(!T)
{
return;
}
printf("%c",T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
// Recursive implementation of middle order traversal
void InOrderTraverse(BiTree T)
{
if(!T)
{
return;
}
InOrderTraverse(T->lchild);
printf("%c",T->data);
InOrderTraverse(T->rchild);
}
// Recursive implementation of subsequent traversal
void PostOrderTraverse(BiTree T)
{
if(!T)
{
return;
}
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
printf("%c",T->data);
}
int main()
{
printf(" Please expand the binary tree in the previous order to enter :");
BiTree t;
CreatBiTree(&t);
printf(" The binary tree was established successfully !!!\n");
printf(" The former sequence traversal :");
PreOrderTraverse(t);
printf("\n");
printf(" In the sequence traversal :");
InOrderTraverse(t);
printf("\n");
printf(" After the sequence traversal :");
PostOrderTraverse(t);
printf("\n");
return 0;
}
边栏推荐
- 看了就会的 Rainbond 入门教程
- VTK annotation class widget vtkborderwidget
- String转为long 类型报错原因:要转为long必须是int、double、float型[通俗易懂]
- 我正在使用中的博客创作工具
- Redis-配置文件讲解
- Crawler: from entry to imprisonment (II) -- Web collector
- C language related programming exercises
- Error reason for converting string to long type: to convert to long type, it must be int, double, float type [easy to understand]
- 为 @CloudStorage 添加了类 @Published 的能力
- VTK vtkcontourwidget extracts regions of interest
猜你喜欢
随机推荐
MITK creates plug-ins and generates plug-ins
linux安装mysql
Crawler: from entry to imprisonment (II) -- Web collector
How many ways can multithread run in sequence?
Added the ability of class @published for @cloudstorage
如何在 Core Data 中进行批量操作
多商户商城系统功能拆解17讲-平台端订单列表
(function(global,factory){
When Xcode writes swiftui code, it is a small trap that compiles successfully but causes the preview to crash
6、 C language circular statement
八、picker用法 下拉框选择效果
文件批量重命名工具Bulk Rename Utility
7、 Detailed explanation of C language function definition
BGP experiment
&0xffffffff(0x08)
Store and guarantee rancher data based on Minio objects
Analysis of thrift serialization protocol
基础架构之日志管理平台及钉钉&邮件告警通知
Switch the cloud synchronization status of core data in real time
Realization of chat room function








