1个回答
展开全部
//求节点数
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define TRUE 1
#define FLASE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
Status CreateBiTree(BiTree &T)
{
TElemType e;
scanf("%d",&e);
if(e==0) T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)
exit(OVERFLOW);
T->data =e;
CreateBiTree(T->lchild );
CreateBiTree(T->rchild );
}
return OK;
}
int BTNodeCount (BiTree T)
{
int m=0,n=0;
if(T==NULL)
return 0;
else
{
m=BTNodeCount(T->lchild );
n=BTNodeCount(T->rchild );
return (m+n+1);
}
}
void main()
{
BiTree T;
printf("请输入二叉树中节点的值(int型),0表示空树:\n");
CreateBiTree(T);
printf("该树的节点数是%d\n",BTNodeCount(T));
}
//求叶子数
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define TRUE 1
#define FLASE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
Status CreateBiTree(BiTree &T)
{
TElemType e;
scanf("%d",&e);
if(e==0) T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)
exit(OVERFLOW);
T->data =e;
CreateBiTree(T->lchild );
CreateBiTree(T->rchild );
}
return OK;
}
int LeftBiTree(BiTree T)
{
int m,n;
if(!T)
return 0;
else if(!T->lchild &&!T->rchild )
return 1;
else
{
m=LeftBiTree(T->lchild );
n=LeftBiTree(T->rchild );
return (m+n);
}
}
void main()
{
BiTree T;
printf("请输入树中节点的值(int型),0表示空树:\n");
CreateBiTree(T);
printf("该二叉树的树叶是%d\n",LeftBiTree(T));
}
如果需要两个程序合并,自己修改一下就可以了
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define TRUE 1
#define FLASE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
Status CreateBiTree(BiTree &T)
{
TElemType e;
scanf("%d",&e);
if(e==0) T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)
exit(OVERFLOW);
T->data =e;
CreateBiTree(T->lchild );
CreateBiTree(T->rchild );
}
return OK;
}
int BTNodeCount (BiTree T)
{
int m=0,n=0;
if(T==NULL)
return 0;
else
{
m=BTNodeCount(T->lchild );
n=BTNodeCount(T->rchild );
return (m+n+1);
}
}
void main()
{
BiTree T;
printf("请输入二叉树中节点的值(int型),0表示空树:\n");
CreateBiTree(T);
printf("该树的节点数是%d\n",BTNodeCount(T));
}
//求叶子数
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define TRUE 1
#define FLASE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
Status CreateBiTree(BiTree &T)
{
TElemType e;
scanf("%d",&e);
if(e==0) T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)
exit(OVERFLOW);
T->data =e;
CreateBiTree(T->lchild );
CreateBiTree(T->rchild );
}
return OK;
}
int LeftBiTree(BiTree T)
{
int m,n;
if(!T)
return 0;
else if(!T->lchild &&!T->rchild )
return 1;
else
{
m=LeftBiTree(T->lchild );
n=LeftBiTree(T->rchild );
return (m+n);
}
}
void main()
{
BiTree T;
printf("请输入树中节点的值(int型),0表示空树:\n");
CreateBiTree(T);
printf("该二叉树的树叶是%d\n",LeftBiTree(T));
}
如果需要两个程序合并,自己修改一下就可以了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询