二叉树中叶子结点个数算法
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}*BitTree;
int w = 0;
typedef struct stack{
int top;
BitTree MaxSize[MAXSIZE];
}*Stack;
void creattree(BitTree *T)
{
char ch;
scanf("%c",&ch);
if(ch == '.')
*T = NULL;
printf("请先序输入二叉树(‘.’代表空子树):\n");
creattree(&T);
printf("先序输出为:");
PrintTree1(T);
printf("\n");
printf("%d",w);
printf("\n");
w = leaf(T);
printf("%d",w);
printf("\n");
}
int leaf(BitTree T)
{
if(!T)
return 0; //空树,无叶子
else if(!(T)->lchild && !(T)->rchild)
return 1;
else
return (leaf(T->lchild) + leaf(T->rchild));
}
main()
{
BitTree T;
else{
(*T) = (struct BitNode *)malloc(sizeof(struct BitNode));
(*T)->data = ch;
creattree(&((*T)->lchild));
creattree(&((*T)->rchild));
}
}
void PrintTree1(BitTree Boot)
//两种算法实现计算二叉树叶子节点的个数
//二叉树叶子节点个数算法之非递归算法和递归算法
#include"stdio.h"
#includeБайду номын сангаасstdlib.h"
#define MAXSIZE 12500
typedef struct BitNode{
char data;
struct BitNode *lchild,*rchild;
S->top++;
Boot = Boot->lchild;
}
else
{
Boot = S->MaxSize[S->top-1];
S->top--;
if(Boot->rchild == NULL && Boot->lchild == NULL)
{
w ++;
}
Boot = Boot->rchild;
}
}
}
{
Stack S;
S = (Stack)malloc(sizeof(struct stack));
S->top = 0;
while(Boot != NULL||S->top != 0)
{
if(Boot != NULL)
{
printf("%2c",Boot->data);
S->MaxSize[S->top] = Boot;
int w = 0;
typedef struct stack{
int top;
BitTree MaxSize[MAXSIZE];
}*Stack;
void creattree(BitTree *T)
{
char ch;
scanf("%c",&ch);
if(ch == '.')
*T = NULL;
printf("请先序输入二叉树(‘.’代表空子树):\n");
creattree(&T);
printf("先序输出为:");
PrintTree1(T);
printf("\n");
printf("%d",w);
printf("\n");
w = leaf(T);
printf("%d",w);
printf("\n");
}
int leaf(BitTree T)
{
if(!T)
return 0; //空树,无叶子
else if(!(T)->lchild && !(T)->rchild)
return 1;
else
return (leaf(T->lchild) + leaf(T->rchild));
}
main()
{
BitTree T;
else{
(*T) = (struct BitNode *)malloc(sizeof(struct BitNode));
(*T)->data = ch;
creattree(&((*T)->lchild));
creattree(&((*T)->rchild));
}
}
void PrintTree1(BitTree Boot)
//两种算法实现计算二叉树叶子节点的个数
//二叉树叶子节点个数算法之非递归算法和递归算法
#include"stdio.h"
#includeБайду номын сангаасstdlib.h"
#define MAXSIZE 12500
typedef struct BitNode{
char data;
struct BitNode *lchild,*rchild;
S->top++;
Boot = Boot->lchild;
}
else
{
Boot = S->MaxSize[S->top-1];
S->top--;
if(Boot->rchild == NULL && Boot->lchild == NULL)
{
w ++;
}
Boot = Boot->rchild;
}
}
}
{
Stack S;
S = (Stack)malloc(sizeof(struct stack));
S->top = 0;
while(Boot != NULL||S->top != 0)
{
if(Boot != NULL)
{
printf("%2c",Boot->data);
S->MaxSize[S->top] = Boot;