二叉树的创建,遍历(前序,中序,后序,层次),查找(C语言)

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

typedef struct BiTree {

	char data;
	struct BiTree* lchild;
	struct BiTree* rchild;
}BiTree;

typedef struct BiQueue {
	
	struct BiTree** pBase;//二级指针创建一个指针数组
	int front;
	int rear;
}BQueue;//创建一个存放节点的队列

BiTree* CreateBiTree() {

	BiTree* T;
	char a;
	scanf_s("%c", &a,sizeof(a));

	if (a == '#') {
		T = NULL;
	}
	else {
		T = (BiTree*)malloc(sizeof(BiTree));
		T->data = a;
		T->lchild = CreateBiTree();
		T->rchild = CreateBiTree();
	}
	return T;
}

BQueue* createBQueue() {

	BQueue* bQ = (BQueue*)malloc(sizeof(BQueue));
	bQ->pBase = (struct BiTree**)malloc(10 * sizeof(struct BiTree*));
	for (int i = 1; i < 10; i++) {
		bQ->pBase[i] = NULL;
	}
	bQ->front = 0;
	bQ->rear = 0;
	return bQ;
}

void PreTraverse(BiTree* T) {

	if (T != NULL) {

		printf("%c ", T->data);
		PreTraverse(T->lchild);
		PreTraverse(T->rchild);
	}
}

void MidTraverse(BiTree* T) {

	if (T != NULL) {

		MidTraverse(T->lchild);
		printf("%c ", T->data);
		MidTraverse(T->rchild);
	}
}

void LastTraverse(BiTree* T) {

	if (T != NULL) {

		LastTraverse(T->lchild);
		LastTraverse(T->rchild);
		printf("%c ", T->data);
	}
}

void LayerTraverse(BiTree* T, BQueue* bQ) {

	if (T != NULL) {
		bQ->pBase[bQ->rear] = T;
		bQ->rear++;	
		while (bQ->pBase[bQ->front] != NULL) {//每次出队一个节点		
			//如果左孩子不为空,入队
			if (bQ->pBase[bQ->front]->lchild != NULL) {
				bQ->pBase[bQ->rear] = bQ->pBase[bQ->front]->lchild;
				bQ->rear++;
			}
			//如果右孩子不为空,入队
			if (bQ->pBase[bQ->front]->rchild != NULL) {
				bQ->pBase[bQ->rear] = bQ->pBase[bQ->front]->rchild;
				bQ->rear++;
			}
			printf("%c ", bQ->pBase[bQ->front]->data);
			bQ->front++;
		}
	}
	else {
		printf("树为空!\n");
		return;
	}
}

void Search(BiTree* T, int key) {

	if(T != NULL) {

		if (key == T->data) {
			printf("查找成功!");
			return;
		}
		else if (key < T->data) {
			Search(T->lchild, key);
		}
		else if (key > T->data) {
			Search(T->rchild, key);
		}
	}
	else if (T == NULL) {
		printf("查找失败!");
	}
	return;
}

BiTree* AddData(BiTree* T, char data) {

	if (T == NULL) {
		T = (BiTree*)malloc(sizeof(BiTree));
		T->data = data;
		T->lchild = NULL;
		T->rchild = NULL;

		return T;
	}
	if (data > T->data) {
		T->rchild = AddData(T->rchild, data);
	}
	else if (data > T->data) {
		T->lchild = AddData(T->lchild, data);
	}
	else if (data == T->data) {
		printf("不能插入相同元素!");
		exit(0);
	}
	return T;
}

int main() {
	
	BiTree* T;
	T = CreateBiTree();
	BQueue* bQ = createBQueue();
	
	printf("先序遍历:\n");
	PreTraverse(T);
	printf("\n");

	printf("中序遍历:\n");
	MidTraverse(T);
	printf("\n");

	printf("后序遍历:\n");
	LastTraverse(T);
	printf("\n");

	printf("层次遍历:\n");
	LayerTraverse(T, bQ);
	printf("\n");
	
	//T = AddData(T, '5');//注意字符类型需要加''符号
	//printf("先序遍历:\n");
	//PreTraverse(T);



	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值