数据结构——树(转载)

(9条消息) 数据结构---二叉树的详解_William-CSDN博客_数据结构二叉树

 

 

/*********************************************
@File name : BiTree.c
@Author : StudyCcYa
@Version : 1.0
@Date : 2020-11-19
@Description : 用c语言实现数据结构——二叉树
********************************************/

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

typedef char Type;

/*树的结点的定义*/
typedef struct BiNode
{
	Type data;//数据
	struct BiNode *LChild;//左孩子
	struct BiNode *RChild;//右孩子
}BiNode,*BiTree;



/*函数声明*/
BiNode *initBiNode(Type elem);//初始化二叉树的结点
BiTree creatBiTree();//完全先序创建一个二叉树
int preOrder(BiTree tree);//先序遍历二叉树
int inOrder(BiTree tree);//中序遍历二叉树
int postOrder(BiTree tree);//后序遍历二叉树
int getNum(BiTree tree);//统计结点个数
int getHeight(BiTree tree);//统计二叉树的高
int getLeafNum(BiTree tree);//统计叶子结点的数量

/*
@Function name : initBiNode
@Description   : 初始化二叉树的结点
@Parameter     : 
                 @elem : 传入的数据
@return        : 返回一个初始化结束的结点,否则失败
*/
BiNode *initBiNode(Type elem)
{
	BiNode *temp = (BiNode *)malloc(sizeof(BiNode));//申请空间
	assert(temp);//断言
	temp->data = elem;//赋值
	temp->LChild = temp->RChild = NULL;//左孩子右孩子指向空
	return temp;//返回到函数
}


/*
@Function name : creatBiTree
@Description   : 完全先序创建一个二叉树
@Parameter     : 
                 无
@return        : 通过递归最后返回树的第一个结点,也就是根结点。否则失败
*/
BiTree creatBiTree()
{
	Type elem;
	BiTree tree;
	scanf("%c",&elem);
	getchar();

	if (elem == '#')//#代表这个结点的某一个孩子结点为空
		tree=NULL;
	else
	{
		tree = initBiNode(elem);//根
		tree->LChild = creatBiTree();//左孩子
		tree->RChild = creatBiTree();//右孩子
	}
	return tree;
}

/*
@Function name : preOrder
@Description   : 先序遍历(根左右)
@Parameter     : 
                 @tree : 传入要遍历的二叉树
*/
int preOrder(BiTree tree)
{
	if (tree == NULL)
		return 0;
	printf("%c ", tree->data);//根
	preOrder(tree->LChild);//左
	preOrder(tree->RChild);//右
}

/*
@Function name : inOrder
@Description   : 中序遍历(左根右)
@Parameter     : 
                 @tree : 传入要遍历的二叉树
*/
int inOrder(BiTree tree)
{
	if (tree == NULL)//递归出口
		return 0;
	inOrder(tree->LChild);//左
	printf("%c ",tree->data);//根
	inOrder(tree->RChild);//右
}

/*
@Function name : postOrder
@Description   : 后序遍历(左右根)
@Parameter     : 
                 @tree : 传入要遍历的二叉树
*/
int postOrder(BiTree tree)
{
	if (tree == NULL)//递归出口
		return 0;
	postOrder(tree->LChild);//左
	postOrder(tree->RChild);//右
	printf("%c ", tree->data);//根
}

/*
@Function name : getNum
@Description   : 统计结点个数
@Parameter     : 
                 @tree : 传入要统计结点的二叉树
*/
int getNum(BiTree tree)
{
	if (tree == NULL)//递归结束条件
		return 0;
	return getNum(tree->LChild) + getNum(tree->RChild) + 1;//左子树的结点数+右子树的结点数+根
}

/*
@Function name : getHeight
@Description   : 统计二叉树的高
@Parameter     : 
                 @tree : 传入要统计高的二叉树
*/
int getHeight(BiTree tree)
{
	if (tree == NULL)
		return 0;//空树
	int left = getHeight(tree->LChild);
	int right = getHeight(tree->RChild);
	return (left > right ? left : right) + 1;
}
/*
@Function name : getHeight
@Description   : 统计叶子结点的数量
@Parameter     : 
                 @tree : 传入要统叶子结点的二叉树
*/
int getLeafNum(BiTree tree)//统计叶子结点的数量
{
	if (tree == NULL)
		return 0;//空树
	if (tree->LChild == NULL&&tree->RChild == NULL)
		return 1;//找到叶子结点
	return getLeafNum(tree->LChild) + getLeafNum(tree->RChild);

}

int main()
{
	BiTree tree = creatBiTree();
	printf("前序遍历:");
	preOrder(tree);
	printf("\n");

	printf("中序遍历:");
	inOrder(tree);
	printf("\n");

	printf("后序遍历:");
	postOrder(tree);
	printf("\n");

	printf("这个二叉树的结点个数为:%d\n", getNum(tree));
	printf("这个二叉树的高度为:%d\n",getHeight(tree));
	printf("这个二叉树的叶子结点数为:%d\n",getLeafNum(tree));
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值