当前位置:网站首页>C language converts arrays into binary trees
C language converts arrays into binary trees
2022-07-16 06:34:00 【Sometimes crazy options】
2021/12/9 Today is my first blog , Because on the way of learning programming , With the guidance of big bloggers, you can eliminate all kinds of problems step by step , Report an attitude of gratitude , Continue their will . secondly , I want to provide a little reference for those beginners . Most important , By the way, strengthen your understanding of knowledge points . I hope I can keep on , Be able to help people who are eager to make progress . If there are some problems in the content, please correct . ——HDU Tu Yisheng (aka Kuang Kuang )
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int a[21];// The data is in 21 Within a
int DataCount = 0;
typedef struct BiNode{
int data;
struct BiNode *lchild;
struct BiNode *rchild;
}*BiTree,BiTNode;
BiTree T = NULL;
// Create a random dataset
void create_data(int MaxData){
int i,j;
srand(time(NULL));
for(i=0;i<DataCount;i++){
a[i] = rand()%MaxData;
for(j=0;j<i;j++){
if(a[j]==a[i]){
a[i] = rand()%MaxData;
j=-1;
}
}
}
}
// Create a binary tree
void create(BiTree Troot,int cnt){
BiTree Nodep;
if(cnt==DataCount){
return ;
}
Nodep = (BiTree)malloc(sizeof(BiTNode));
Nodep->data = a[cnt];
Nodep->lchild = NULL;
Nodep->rchild = NULL;
if(Troot == NULL){
T = Nodep;
create(T,++cnt);
}
else if(Nodep->data<=Troot->data){
if(Troot->lchild==NULL){
Troot->lchild = Nodep;
create(T,++cnt);
}
else{
create(Troot->lchild,cnt);
}
}
else if(Nodep->data>Troot->data){
if(Troot->rchild == NULL){
Troot->rchild = Nodep;
create(T,++cnt);
}
else{
create(Troot->rchild);
}
}
}
// In the sequence traversal
void inOrder(BiTree T){
if(T){
inOrder(T->lchild);
printf("%d ",T->data);
inOrder(T->rchild);
}
}
int main(){
int i;
int MaxData;
printf("DataCount:");
scanf("%d",&DataCount);
printf("MaxData:");
scanf("%d",&MaxData);
create_data(MaxData);
printf(" Raw data :");
for(i=0;i<DataCount;i++){
printf("%d ",a[i]);
}
printf("\n");
create(T,0);
inOrder(T);
return 0;
}
First , Anyone who can search this blog should know the definition of binary tree , I won't go into details here . Build a binary tree , First of all, there must be a structure variable to store data and the addresses of the left child and the right child .
typedef struct BiNode{
int data;
struct BiNode *lchild;
struct BiNode *rchild;
}*BiTree,BiTNode; // Self defined data types ,BiTree Is a pointer ,BiTNode Is the data structure type .
after , We are going to generate a containing n An unequal array of elements .
// Create a random dataset
void create_data(int MaxData,int arr[],int DataCount){
int i,j;
srand(time(NULL)); // Random seeds
for(i=0;i<DataCount;i++){
arr[i] = rand()%MaxData; arr[i] -> (0,MaxData)
for(j=0;j<i;j++){
if(arr[j]==arr[i]){
arr[i] = rand()%MaxData;
j=-1;
}
}
}
}
With the initial array , We can build a binary tree .
// Create a binary tree
void create(BiTree Troot,int cnt){ // Don't return the pointer as a binary tree, there will be problems .
BiTree Nodep;
if(cnt==DataCount){
return ;
}
Nodep = (BiTree)malloc(sizeof(BiTNode));
Nodep->data = a[cnt];
Nodep->lchild = NULL;
Nodep->rchild = NULL;
if(Troot == NULL){
T = Nodep; // initialization
create(T,++cnt);
}
else if(Nodep->data<=Troot->data){
if(Troot->lchild==NULL){
Troot->lchild = Nodep;
create(T,++cnt);
}
else{
create(Troot->lchild,cnt);
}
}
else if(Nodep->data>Troot->data){
if(Troot->rchild == NULL){
Troot->rchild = Nodep;
create(T,++cnt);
}
else{
create(Troot->rchild,cnt);
}
}
}
Finally, we will output the result of the middle order traversal .
// In the sequence traversal
void inOrder(BiTree T){
if(T){
inOrder(T->lchild);
printf("%d ",T->data);
inOrder(T->rchild);
}
}
边栏推荐
猜你喜欢

常见的存储器介绍

【PCB】关于电赛——硬件设计和PCB绘制的一些心得(持续更新)

RT_thread信号量的使用

【PCB】關於電賽——硬件設計和PCB繪制的一些心得(持續更新)

Transformer——注意力模型分析及应用

嵌入式软件开发 STM32F407 蜂鸣器 标准库版

表格图像提取-基于传统交点方法和Tesseract-OCR

Blue Bridge Cup embedded Hal library new project

Target detection (1) -- data preprocessing and data set segmentation

Embedded software development stm32f407 key input standard library version
随机推荐
stm32f429+LAN4720A+lwip 问题记录及解决
嵌入式软件开发 STM32F407 跑马灯 HAL库版
论文阅读笔记——Crop yield prediction using deep neural networks
Machine learning arrangement (introduction of several learning methods)
RT_thread 线程优先级的翻转
Embedded software development stm32f407 buzzer register version
【 pcb】 quelques expériences sur la conception du matériel et le dessin des PCB dans le jeu électrique (mise à jour continue)
Blue Bridge Cup embedded Hal library Tim_ BASE
HDU 2586 How far away ? (lca倍增法)
[Multisim] problems and solutions of Multisim Simulation "op amp integrator"
【CVPR2022】MPViT : Multi-Path Vision Transformer for Dense Prediction
DHT11和DHT22(AM2302)比较及使用方法
OPENGL 射线拾取法
二叉树的各种操作(叶子节点、双亲节点、搜索二叉树、删除二叉树中的节点、二叉树的深度)
3.6格式化数字和字符串
EasyCVR国标协议接入设备,设备在线、通道却不在线的原因是什么?
HDU 3585 maximum shortest distance (二分+最大团)
stride for plane for YUV
Transformer——注意力模型分析及应用
GAN:Generative Adversarial Nets——论文分析及其背后的数学概念