数据结构 二叉树的先序 中序 后序遍历(linux 下c语言实现)
来源:互联网 发布:知党史,学党史,跟党走 编辑:程序博客网 时间:2024/05/03 01:10
测试序列:AB#CD###E#FGH##K###
一.头文件btree.h实现
#ifndef __BTREE_H__
#define __BTREE_H__
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef char datatype_bt;
typedef struct btreenode
{
datatype_bt data;
struct btreenode *lchild,*rchild;
}btree_node,*btree_pnode;
extern void create_btree(btree_pnode *T);
extern void pre_order(btree_pnode t);
extern void mid_order(btree_pnode t);
extern void last_order(btree_pnode t);
#endif
二.函数实现btree.c
#include"btree.h"
void create_btree(btree_pnode *T)
{
datatype_bt ch;
scanf("%c",&ch);
if('#' == ch)
*T = NULL;
else
{
*T = (btree_pnode)malloc(sizeof(btree_node));
if(NULL == *T)
{
perror("malloc");
exit(1);
}
(*T)->data = ch;
create_btree(&(*T)->lchild);
create_btree(&(*T)->rchild);
}
}
//先序遍历
void pre_order(btree_pnode t)
{
if(t != NULL)
{
//访问根结点
printf("%c",t->data);
//先序遍历左子树
pre_order(t->lchild);
//先序遍历右子树
pre_order(t->rchild);
}
}
//中序遍历
void mid_order(btree_pnode t)
{
if(t != NULL)
{
//先序遍历左子树
mid_order(t->lchild);
printf("%c",t->data);
//先序遍历右子树
mid_order(t->rchild);
}
}
//后序遍历
void last_order(btree_pnode t)
{
if(t != NULL)
{
//先序遍历左子树
last_order(t->lchild);
printf("%c",t->data);
//先序遍历右子树
last_order(t->rchild);
}
}
三.包含主函数的test.c文件
#include"btree.h"
int main()
{
btree_pnode t;
create_btree(&t);
printf("先序遍历序列\n");
pre_order(t);
puts("");
printf("中序遍历序列\n");
mid_order(t);
puts("");
printf("后序遍历序列\n");
last_order(t);
puts("");
return 0;
}
四.makefile文件实现
CC = gcc
CFLAGS = -Wall -g -O0
SRC = btree.c test.c
OBJS = test
$(OBJS):$(SRC)
$(CC) $(CFLAGS) -o $@ $^
clean:
$(RM) $(OBJS) .*.sw?
- 数据结构 二叉树的先序 中序 后序遍历(linux 下c语言实现)
- c语言实现二叉树的先序遍历,中序遍历,后序遍历
- c语言原生实现二叉树的 非递归 中序 先序 后序 遍历
- C实现二叉树的先序遍历,中序遍历,后序遍历
- 【数据结构与算法】(六) c 语言实现简单的二叉树静态创建及先序、中序、后序遍历
- 数据结构 二叉树的递归算法、前序、中序、后序遍历(c语言实现)
- 二叉树的先序、中序和后序的非递归遍历(C语言实现)
- C语言非递归实现二叉树的先序、中序、后序、层序遍历
- 二叉树的先序,中序,后序遍历 c语言
- c语言实现二叉树(BiNodeTree)的建立与先序遍历,中序遍历
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- 二叉树的遍历(先、中、后、层序)C实现
- 用Java语言实现二叉树的三种遍历(先序、中序、后序遍历)
- 二叉树T 的先序遍历、中序遍历、后序遍历(递归实现)
- java实现二叉树的先序遍历,中序遍历,后序遍历
- 实现二叉树的先序遍历、中序遍历、后序遍历
- 数据结构_二叉树的先序建立与先序,中序,后序(递归)遍历方式_C语言源代码
- 数据结构与算法系列-树-二叉树的遍历(先序、中序、后序)
- mint-ui —— tabbar示例
- File类递归(统计文件夹大小)
- VMware vSphere Web Services SDK编程指南(四)- vSphere API 编程模型(一)
- 算法面试题之句子反转
- GamePlay
- 数据结构 二叉树的先序 中序 后序遍历(linux 下c语言实现)
- C语言中typedef和define一些理解
- vue的修饰符
- MySQL创建视图的语法格式
- CodeChef ROBOTDAG Robots in a DAG
- lua-2 逻辑控制和函数
- 自定义linux命令
- C语言如何模拟C++实现继承和多态
- B1006