#include "linkqueue.h"
/**
* @description: 二叉树的创建
* @param {*}
* @return {r-二叉树根节点的地址}
*/
bittree *tree_create()
{
datatype value;
bittree *r;
scanf("%c", &value);
if(value == '#')
return NULL;
r = (bittree *)malloc(sizeof(bittree));
if(r == NULL)
{
#if DEBUG
printf("bittree create error!\n");
#endif
return NULL;
}
r->data = value;
r->left_tree = tree_create();
r->right_tree = tree_create();
return r;
}
/**
* @description: 前序遍历法
* @param {bittree} *r-二叉树根节点的地址
* @return {0-没有子结点时,1-函数结点}
*/
int preorder(bittree *r)
{
if(r == NULL)
return 0;
printf("%c ", r->data);
preorder(r->left_tree);
preorder(r->right_tree);
return 1;
}
/**
* @description: 中序遍历
* @param {bittree} *r-二叉树根节点的地址
* @return {0-没有子结点时,1-函数结点}
*/
int inorder(bittree *r)
{
if(r == NULL)
return 0;
inorder(r->left_tree);
printf("%c ", r->data);
inorder(r->right_tree);
return 1;
}
/**
* @description: 后序遍历
* @param {bittree} *r-二叉树根节点的地址
* @return {0-没有子结点时,1-函数结点}
*/
int backorder(bittree *r)
{
if(r == NULL)
return 0;
backorder(r->left_tree);
backorder(r->right_tree);
printf("%c ", r->data);
return 1;
}
/**
* @description: 层次遍历
* @param {bittree} *r-二叉树根节点的地址
* @return {0-函数失败,1-函数成功}
*/
int levelorder(bittree *r)
{
linkqueue lq;
if(r == NULL)
{
#if DEBUG
printf("r is NULL\n");
#endif
return 0;
}
if((lq = linkqueue_create()) == NULL)
return 0;
linkqueue_enter(lq, r);
while(!linkqueue_empty(lq))
{
r = linkqueue_out(lq);
printf("%c ",r->data);
if(r->left_tree != NULL)
linkqueue_enter(lq, r->left_tree);
if(r->right_tree != NULL)
linkqueue_enter(lq, r->right_tree);
}
puts("");
return 1;
}
没有合适的资源?快使用搜索试试~ 我知道了~
【数据结构与算法】二叉树的实现C语言代码
共6个文件
c:3个
h:2个
test:1个
需积分: 0 5 下载量 188 浏览量
2022-03-12
16:51:46
上传
评论
收藏 7KB ZIP 举报
温馨提示
包含二叉树的创建、遍历、程序,可在test.c文件中测试功能
资源详情
资源评论
资源推荐
收起资源包目录
bittree.zip (6个子文件)
bittree
linkqueue.h 955B
bittree.h 734B
linkqueue.c 2KB
test 13KB
test.c 529B
bittree.c 2KB
共 6 条
- 1
修成真
- 粉丝: 2w+
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0