#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAXSIZE 100
typedef struct BiTNode{
char data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
typedef struct SQueue
{
BiTree data[MAXSIZE];
int front,rear;
}SQueue,*Queue;
typedef struct{
int top;
int StackSize;
BiTree *T;
}SqStack;
void InitStack(SqStack *S){
S->top=-1;
S->StackSize=MAXSIZE;
S->T=(BiTree*)malloc(sizeof(BiTree)*S->StackSize);
}
int StackEmpty(SqStack S){
return S.top<0?1:0;
}
void Push(SqStack *S,BiTree T){
if(S->top==S->StackSize-1)
exit(1);
else
S->T[++(S->top)]=T;
}
void Pop(SqStack *S,BiTree *T){
*T=S->T[S->top];
S->top--;
}
void InOrder(BiTree T){
SqStack S;
BiTree p=T;
InitStack(&S);
while(p||!(StackEmpty(S))){
if(p){
Push(&S,p);
p=p->lchild;
}
else{
Pop(&S,&p);
printf("%c",p->data);
p=p->rchild;
}
}
}
//
定义栈
//typedef struct {
// BiTree *stack;
// int top;
// int stacksize;
//}SqStack;
//
初始化栈
//void initstack(SqStack *s)
//{
// s->stacksize=MAXSIZE;
// s->top = -1;
// s->stack = (BiTree*)malloc(sizeof(BiTree)*(s->stacksize));
//}
//
判断栈是否为空
//int StackEmpty(SqStack *s)
//{
// if (s->top < 0) return 1;
// return 0;
//}
栈的push操作
//void StackPush(SqStack *s,BiTree T)
//{
// if (s->top == s->stacksize - 1)
// exit(1);
// else{
// s->stack[++(s->top)] = T;
// }
//}
栈的top操作:返回栈顶指针
//BiTree StackTop(SqStack *s)
//{
// if (StackEmpty(s) == 1)
// exit(1);
// else{
// return s->stack[s->top];
// }
//}
栈的pop操作:删除栈顶数据
//void StackPop(SqStack *s)
//{
// if (StackEmpty(s) == 1)
// exit(1);
// else{
// s->top--;
// }
//}
中序遍历
//int inorder(BiTree T){
// SqStack s;
// initstack(&s);
// BiTree p = T;
//
// while (p || !StackEmpty(&s)){
// if (p){
// StackPush(&s, p);
// p = p->lchild;
// }
// else{
// p = StackTop(&s);
// StackPop(&s);
//
// printf("%c",p->data);//打印数据
// p = p->rchild;
// }
// }
// return 1;
//}
//先序创建
void CreateBiTree(BiTree &T){
char ch=getchar();
if(ch=='#'){
T=NULL;
}
else{
T=(BiTree)malloc(sizeof(BiTNode));
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
//后序打印
void PastOrderBiTree(BiTree T){
if(T){
PastOrderBiTree(T->lchild);
PastOrderBiTree(T->rchild);
printf("%c",T->data);
}
}
void InitQueue(Queue Q)
{
Q -> front = Q -> rear = 0;
}
int IsEmptyQueue(Queue Q)
{
return Q -> rear == Q -> front?1:0;
}
void EnQueue(BiTree T,Queue Q)
{
if((Q -> rear+1) % MAXSIZE == Q -> front)
{
printf("Queue is fulled!\n");
return ;
}
Q->rear=(Q->rear+1)%MAXSIZE;
Q->data[Q->rear]=T;
}
BiTree Gethead(Queue Q)
{
if(IsEmptyQueue(Q)) return 0;
BiTree T;
T=Q ->data[(Q ->front+1) % MAXSIZE];
Q ->front=(Q ->front+1) % MAXSIZE;
return T;
}
void LevelOrder(BiTree T)//层次遍历
{
SQueue Q ;
BiTree S ;
if(!T) return ;
InitQueue(&Q);
EnQueue(T,&Q);
while(!IsEmptyQueue(&Q))
{
S=Gethead(&Q);
printf(" %c",S->data);
if(S->lchild!=NULL) EnQueue(S->lchild,&Q);
if(S->rchild!=NULL) EnQueue(S->rchild,&Q);
}
}
//复制树
void copy(BiTree T,BiTree *NewT){
if(T==NULL)
*NewT=NULL;
else{
*NewT=(BiTree)malloc(sizeof(BiTNode));
(*NewT)->data=T->data;
copy(T->lchild,&((*NewT)->lchild));
copy(T->rchild,&((*NewT)->rchild));
}
}
//计算叶子节点深度
int Depth(BiTree T){
int m,n;
if(T==NULL)
return 0;
else{
m=Depth(T->lchild);
n=Depth(T->rchild);
return m>n?m+1:n+1;
}
}
//节点的个数
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 main(){
BiTree T,NewT;
CreateBiTree(T);
PastOrderBiTree(T);
printf("\n");
InOrder(T);
printf("\n");
LevelOrder(T);
copy(T,&NewT);
printf("\n");
LevelOrder(NewT);
printf("\n%d %d %d",Depth(T),NodeCount(T),LeafCount(T));
}