好友
阅读权限10
听众
最后登录1970-1-1
|
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 5
typedef int Status;
typedef char Selemtype;
int n=0;
typedef struct{
Selemtype *base;
Selemtype *top;
int stacksize;
}Sqstack;
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
Status Initstack (Sqstack &s){
s.base=(Selemtype *)malloc(STACK_INIT_SIZE * sizeof(Selemtype));
if(!s.base) exit(OVERFLOW);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Pop(Sqstack &s,Selemtype &e){
if (s.top ==s.base) return ERROR;
e=*--s.top ;
return OK;
}
Status Push(Sqstack &s,Selemtype e){
if(s.top -s.base >=s.stacksize ){
s.base=(Selemtype *)realloc (s.base,(s.stacksize +STACKINCREMENT)* sizeof(Selemtype));
if(!s.base) exit (OVERFLOW);
s.top=s.base +s.stacksize;
s.stacksize +=STACKINCREMENT;
}
*s.top ++=e;
return OK;
}
Status StackEmpty(Sqstack s){
if(s.base ==s.top ) return TRUE;
else return FALSE;
}
Status CreateBiTree (BiTree &T){
char ch;
ch=getchar();
if(ch==' ') T=NULL;
else{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
T->data =ch;
CreateBiTree(T->lchild );
CreateBiTree(T->rchild );
}
return OK;
}
Status perorder (BiTree &T){
Sqstack s;
BiTree p;
Initstack(s);
Push(s,T);
while(!StackEmpty(s)){
Pop(s,p);
while(p){
if(!p->lchild&&!p->rchild) n++;
Push(s,p->rchild);
p=p->lchild;
}
}
return OK;
}
int main()
{
BiTree T;
printf("请输入二叉树:");
CreateBiTree(T);
perorder(T);
printf("该二叉树的叶子结点数为:%d",n);
return OK;
} |
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|