c语言 将数组转化成二叉树

2021/12/9 今天是我第一次写博客,因为在学习编程的道路上,有大佬博主的指点自己才能一步步排除各种问题,报着感恩的态度,接续他们的意志。其次,想为那些初学者提供一点点的参考。最主要的,顺便加强自己对于知识点的理解。希望我能坚持下去,能够对渴望上进的人助一点力。如果内容存在一些问题欢迎指正。                                                        ——HDU 屠奕生 (aka匡匡)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int a[21];//数据在21个以内
int DataCount = 0;

typedef struct BiNode{
	int data;
	struct BiNode *lchild;
	struct BiNode *rchild;
}*BiTree,BiTNode;

BiTree T = NULL;

//创建随机数据集
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;
			}
		}
	}
}

// 创建一棵二叉树
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);
        }
    }
}

//中序遍历
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("原始数据:");
	for(i=0;i<DataCount;i++){
		printf("%d ",a[i]);
	}
	printf("\n");
	create(T,0);
    inOrder(T);
	return 0;
}

首先,能搜索到这篇博文的应该都了解二叉树的定义,在此就不多加赘述了。建立二叉树,首先得有一个结构体变量存储数据和左孩子及右孩子的地址。

typedef struct BiNode{
        int data;
        struct BiNode *lchild;
        struct BiNode *rchild;
}*BiTree,BiTNode;  //自己定义的数据类型,BiTree是指针,BiTNode是该数据结构类型。

 之后,我们要生成一个含有n个元素的不相等的数组。

//创建随机数据集
void create_data(int MaxData,int arr[],int DataCount){
        int i,j;
        srand(time(NULL));  //随机种子
        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;
                        }
                }
        }
}

 有了初始的数组,我们就可以建立二叉树了。

// 创建一棵二叉树
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,cnt);
                }
        }
}
 

 最后我们中序遍历输出一下结果。

//中序遍历
void inOrder(BiTree T){
    if(T){    
        inOrder(T->lchild);
        printf("%d ",T->data);
        inOrder(T->rchild);
    }
}

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时而癫狂的匡匡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值