二叉树的创建前中后序以及节点数、叶子数和

#define _CRT_SECURE_NO_WARNINGS  //解决scanf会错误
#include<stdint.h>
#include"iostream"  //C++一定要的
#define OK 1
#define ERROR 0
#define MAXLEN 100

#define DataType char

typedef struct Node{
 DataType data;
 struct Node * Lchild;
 struct Node * Rchild;
 }BiTNode, *BiTree;
 
 void Visit(DataType data)
{
 printf("%c", data);
}
//前序
void qianxv(BiTree root)
{
	if (root)
	{
 	 Visit(root->data);
  	qianxv(root->Lchild);
 	 qianxv(root->Rchild);
	 }
 }
 //中序
 void zhongxv(BiTree root){
 	if (root){
 	zhongxv(root->Lchild);
 	Visit(root->data);
 	zhongxv(root->Rchild);
 	}
 }
 //后序
 void houxv(BiTree root)
 {
 	if (root)
 	{
 		qianxv(root->Lchild);
  		qianxv(root->Rchild);
  		Visit(root->data);
  	}
  }
  //创建二叉树
void CreateBiTree(BiTree *T)
{
	DataType ch;
	 scanf("%c", &ch);
 	if (ch == '#')
	 {
	  *T = NULL;
	 }
	  else
	 {
	 *T = (BiTree)malloc(sizeof(BiTNode));
	 (*T)->data = ch;
 	 CreateBiTree(&(*T)->Lchild);
  	CreateBiTree(&(*T)->Rchild);
 	 }
  }
  
  //节点总数
int NodeCount(BiTree T)
{
 if (T == NULL) return 0;
 else return NodeCount(T->Lchild) + NodeCount(T->Rchild) + 1;
}

//叶子数
int leafCount(BiTree T)
{
 if (T == NULL) return 0;
 if (T->Lchild == NULL&&T->Rchild == NULL) return 1;
 else return leafCount(T->Lchild) + leafCount(T->Rchild);
}

//高度
int gethight(BiTree T)
{
 if (T == NULL) return 0;
 int L = gethight(T->Lchild);
 int R = gethight(T->Rchild);
 return ((L > R) ? (L + 1) : (R + 1));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值