二叉树(二)——遍历、深度统计、叶子结点统计、结点统计

1.二叉树的相关算法的实现(链表)。
#include <stdio.h>
#include <malloc.h>

#define NULL	0

typedef struct tree
{
	int data;
	struct tree *left, *right;
}ElemBT;

void create_btree(ElemBT *root, int list[], int n) /*n表示list数组中元素的个数*/
{
	int i;
	ElemBT *current, *parent, *p;

	for(i = 1; i < n; i++)
	{
		p = (ElemBT *)malloc(sizeof(ElemBT));
		p->left = p->right = NULL;
		p->data = list[i];
		current = root;
		while(current != NULL)
		{
			parent = current;
			if(current->data > p->data)
				current = current->left;
			else
				current = current->right;
		}
		if(parent->data > p->data)
			parent->left = p;
		else
			parent->right = p;
	}
}

void pre_order(ElemBT *root) /*前序遍历*/
{
	if(root) /*root != NULL*/
	{
		printf("%4d", root->data); /*根结点打印*/
		pre_order(root->left); /*左子树遍历*/
		pre_order(root->right); /*右子树遍历*/
	}
	else
	{
		return;
	}
}

void in_order(ElemBT *root) /*中序遍历*/
{
	if(root) /*root != NULL*/
	{
		in_order(root->left); /*左子树遍历*/
		printf("%4d", root->data); /*根结点打印*/
		in_order(root->right); /*右子树遍历*/
	}
	else
	{
		return;
	}
}

void post_order(ElemBT *root) /*后序遍历*/
{
	if(root) /*root != NULL*/
	{
		post_order(root->left); /*左子树遍历*/
		post_order(root->right); /*右子树遍历*/
		printf("%4d", root->data); /*根结点打印*/
	}
	else
	{
		return;
	}
}

int node_count(ElemBT *root) /*二叉树结点个数统计*/
{
	int cnt = 0;
	
	if(root)
	{
		cnt += node_count(root->left);
		cnt += node_count(root->right);
		cnt ++; /*根节点*/
	}

	return cnt;
}

/*
int node_count(ElemBT *root) //结点个数统计
{
	if(root == NULL)
		return 0;
	else
		return (node_count(root->right)+node_count(root->left)+1);
}
*/

int btree_depth(ElemBT *root) /*二叉树的深度*/
{
	int d1 = 0, d2 = 0;

	if(root == NULL)
		return 0;
	else
	{
		d1 = btree_depth(root->left);
		d2 = btree_depth(root->right);
		return (d1>d2 ? d1+1 : d2+1);
	}
}

int leaf_count(ElemBT *root) /*统计叶子结点个数*/
{
	if(!root)
		return 0;

	if(!root->left && !root->right)
		return 1;
	else
		return (leaf_count(root->left)+leaf_count(root->right));
}

int main()
{
	int list[7] = {30, 18, 16, 25, 34, 7, 31};
	ElemBT *root;

	root = (ElemBT *)malloc(sizeof(ElemBT));
	root->data = list[0];
	root->left = root->right = NULL;
	create_btree(root, list, 7);

	printf("pre_order:\n");
	pre_order(root);
	printf("\n");

	printf("in_order:\n");
	in_order(root);
	printf("\n");

	printf("post_order:\n");
	post_order(root);
	printf("\n");

	printf("Node number is: %d\n", node_count(root));
	printf("Btree depth is: %d\n", btree_depth(root));
	printf("Leaf node number is: %d\n", leaf_count(root));
	return 0;
}

程序运行截图:


2.结论
(1)通过一棵二叉树的前序、中序、后序中任意两个的访问顺序可以唯一的确定一棵二叉树。
假设有二叉树的中序和后序访问顺序分别为:
中序:c, b, d, e, a, g, i, h, j, f
后序:c, e, d, b, i, j, h, g, f, a
分析:由后序遍历最末一个是a可以知道a为该树的根,再结合中序可知道c, b, d, e为其左子树,g, i, h, j, f为其右子树。又结合后序访问可以知道b, f分别为为a的左子女和右子女,再结合中序遍历可以知道b的左子女个数为1,右子女个数为2,f的左子女个数为4,右子女个数为0。依次类推可以得到如下的二叉树:
                              a
                         /          \
                     b                  f
                 /     \           /       
              c          d       g
                           \        \
                            e          h
                                       /   \           
                                     i       j
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值