华清作业day52

代码:

#include <stdlib.h>
#include <stdio.h>
typedef struct Node
{
	char data;
	struct Node *lchild;
	struct Node *rchild;
}*Tree;
//申请空间
Tree create_space()
{
	Tree t = (Tree)malloc(sizeof(struct Node));
	if(NULL == t)
	{
		return NULL;
	}

	t->data = 0;
	t->lchild = t->rchild = NULL;
	return t;
}
//创建二叉树
Tree create_tree()
{
	printf("please write character:\n");
	char ch;
	scanf(" %c", &ch);
	if('#' == ch)
		return NULL;

	Tree t = create_space();
	t->data = ch;

	t->lchild = create_tree();
	t->rchild = create_tree();

	return t;
}
//销毁
void destroy_tree(Tree t)
{
	if(NULL == t)
		return;
	destroy_tree(t->lchild);
	destroy_tree(t->rchild);
	free(t);
	t = NULL;
}
//先序遍历
void first_output(Tree t)
{
	if(NULL == t)
		return;
	printf("%c", t->data);
	first_output(t->lchild);
	first_output(t->rchild);
}
//中序遍历
void mid_output(Tree t)
{
	if(NULL == t)
		return;
	first_output(t->lchild);
	printf("%c", t->data);
	first_output(t->rchild);
}
//后序遍历
void last_output(Tree t)
{
	if(NULL == t)
		return;
	first_output(t->lchild);
	first_output(t->rchild);
	printf("%c", t->data);
}
int main(int argc, const char *argv[])
{
	Tree t = create_tree();

	printf("先序遍历:\t");
	first_output(t);
	puts("");

	printf("后序遍历:\t");
	mid_output(t);
	puts("");

	printf("中序遍历:\t");
	last_output(t);
	puts("");
	
	destroy_tree(t);
	return 0;
}

效果图:

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值