数据结构基础篇-------7.2 非完全二叉树

/*
 * 非完全二叉树
 *  2018.10.23
 *  @L.F
 *
 */

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

//定义结点结构体
typedef struct node{
	char id; //编号
	struct node *lchild; //保存左子树地址的指针
	struct node *rchild; //保存右子树地址的指针
}bitree_t;

//创建一个非完全二叉树
bitree_t *bitree_create() 
{
	char c;
	scanf("%c", &c);

	if(c == '#')
	{
		return NULL;
	}
	
	bitree_t *root = (bitree_t *)malloc(sizeof(bitree_t));
	root->id = c;

	root->lchild = bitree_create();
	root->rchild = bitree_create();   
	
	return root;
}

//先序遍历:根 左 右
void bitree_order_before(bitree_t *root)
{
	//先打印根结点
	printf("%c ", root->id);

	//判断是否存在左右子树,如果存在,则将其当作根继续往下遍历
	if(root->lchild != NULL)
	{
		bitree_order_before(root->lchild);
	}

	if(root->rchild != NULL)
	{
		bitree_order_before(root->rchild);	
	}

	return ;
}

//中序遍历:左 根 右
void bitree_order_in(bitree_t *root)
{
	if(root->lchild != NULL)
	{
		bitree_order_in(root->lchild);
	}

	printf("%c ", root->id);

	if(root->rchild != NULL)
	{
		bitree_order_in(root->rchild);	
	}

	return ;
}

//后序遍历:左 右 根
void bitree_order_after(bitree_t *root)
{
	if(root->lchild != NULL)
	{
		bitree_order_after(root->lchild);
	}

	if(root->rchild != NULL)
	{
		bitree_order_after(root->rchild);	
	}

	printf("%c ", root->id);

	return ;
}

int main(int argc, const char *argv[])
{
	bitree_t *root = bitree_create();

	bitree_order_before(root);
	putchar(10);

	bitree_order_in(root);
	putchar(10);

	bitree_order_after(root);
	putchar(10);
	
	return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值