华清作业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;
}

效果图:

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-02-06 05:14:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-06 05:14:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-06 05:14:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-06 05:14:01       18 阅读

热门阅读

  1. 蓝桥杯刷题--python-1

    2024-02-06 05:14:01       39 阅读
  2. 初始化整形数组,后从小到大冒泡排序

    2024-02-06 05:14:01       34 阅读
  3. 基于单片机的LED显示系统

    2024-02-06 05:14:01       30 阅读
  4. 蓝桥杯刷题--python-2

    2024-02-06 05:14:01       28 阅读
  5. 实习记录——第十三天

    2024-02-06 05:14:01       33 阅读
  6. 【C/C++ 15】C++11右值引用

    2024-02-06 05:14:01       28 阅读
  7. 机器学习小结

    2024-02-06 05:14:01       33 阅读
  8. 机器学习本科课程 实验5 贝叶斯分类

    2024-02-06 05:14:01       26 阅读
  9. 嵌入式C语言学习——基于Linux与GCC

    2024-02-06 05:14:01       33 阅读