二叉树的应用

二叉树的应用
算法描述:

#include "stdio.h"
#include "malloc.h"
typedef  char TelernType; 

typedef struct BTreeNode 
{
	TelernType data ;      //树的数据域为字符型
	struct BTreeNode *LChild ;         //*左孩子指针
	struct BTreeNode *RChild ;         //*右孩子指针
}BTree ; 

BTree *CreatBTree()
{
	BTree *T;
	char x;
	scanf("%c",&x);getchar();
	if (x=='0') T=NULL;
	else 
	{	T=new BTree;
		T->data=x;
		printf(" 请输入 %c 结点的左孩子:",T->data);
		T->LChild=CreatBTree();
		printf(" 请输入 %c 结点的右孩子:",T->data);
		T->RChild=CreatBTree();
	}
	return T;
}
int TreeHeight(BTree *T)
{
	int lh,rh;
	if (T==NULL) return 0;
	else
	{
		lh=TreeHeight(T->LChild );
		rh=TreeHeight(T->RChild );
		if (lh>rh) return lh+1;
		else return rh+1;
	}
}

int count=0;
void leafnum(BTree *T)
{
	if(T)        //树不空
	{	if (T->LChild==NULL && T->RChild ==NULL)
		count++;
		leafnum(T->LChild );
		leafnum(T->RChild ) ;
	}
}

void main()
{
	BTree *T;
	int k;	
	do
    {  
	printf("\n\n\n\n");
    printf("\t\t\t  二叉树的遍历\n");
	printf("\t\t******************************\n");
	printf("\t\t*        1----建二叉树    *\n");
	printf("\t\t*        2----求树高度      *\n");
	printf("\t\t*        3----叶子个数     *\n");
	printf("\t\t*        0----返  回    *\n");
	printf("\t\t******************************\n");
	printf("\t\t 请选择菜单项(0-3):");
	scanf("%d",&k);getchar();
		if (k == 1)
		{
			printf("\n 请输入此树的根结点: ");
			//建立二叉树
		}
		else if (k == 2)
		{
			printf("\n   此二叉树的深度: ");
		}
		else if (k == 3)
		{
			printf("\n   此二叉树的叶子结点的个数:");

		}
	} while (k != 0);
}

实验结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值