#include<bits/stdc++.h>
using namespace std;
typedef char BElemType;
typedef struct Node
{
BElemType data;
struct Node *LChild;
struct Node *RChild;
} BinNode,*BinTree;
typedef BinTree SElemType;
typedef struct LNode
{
SElemType data;
struct LNode *next;
} LNode,*LinkList;
typedef BinTree QElemType;
typedef struct QNode
{
QElemType data;
struct QNode *next;
} Qnode,*Queueptr;
typedef struct
{
Queueptr Front;
Queueptr Rear;
} LinkQueue;
void InitQueue(LinkQueue &Q)
{
Q.Front=Q.Rear=(Queueptr)malloc(sizeof(QNode));
if(!Q.Front) return ;
Q.Front->next=NULL;
}
int isEmptyQueue(LinkQueue Q)
{
if(Q.Front==Q.Rear) return 1;
return 0;
}
void EnQueue(LinkQueue &Q,QElemType e)
{
Queueptr s=(Queueptr)malloc(sizeof(QNode));
s->data=e;
s->next=NULL;
Q.Rear->next=s;
Q.Rear=s;
}
void DeQueue(LinkQueue &Q,QElemType &e)
{
Queueptr s=(Queueptr)malloc(sizeof(QNode));
if(Q.Front==Q.Rear) return ;
s=Q.Front->next;
Q.Front->next=s->next;
e=s->data;
if(Q.Rear==s) Q.Rear=Q.Front;
free(s);
}
void INitStack(LinkList &L)
{
L=NULL;
}
bool isEmptyStack(LinkList L)
{
if(L==NULL) return 1;
return 0;
}
void PushStack(LinkList &L,SElemType e)
{
LNode* s;
s=(LinkList)malloc(sizeof(LNode));
s->data=e;
s->next=L;
L=s;
}
void PopStack(LinkList &L,SElemType &e)
{
LNode* s;
if(L==NULL) return ;
e=L->data;
s=L;
L=L->next;
free(s);
}
BinTree PreCreateTree(BinTree bt)
{
char ch;
scanf("%c",&ch);
if(ch=='#') return NULL;
else
{
bt=(BinNode*)malloc(sizeof(BinNode));
bt->data=ch;
bt->LChild=PreCreateTree(bt->LChild);
bt->RChild=PreCreateTree(bt->RChild);
return bt;
}
}
BinTree InCreateTree(BinTree bt)
{
char ch;
scanf("%c",&ch);
if(ch=='#') return NULL;
else
{
bt=(BinNode*)malloc(sizeof(BinNode));
bt->LChild=InCreateTree(bt->LChild);
bt->data=ch;
bt->RChild=InCreateTree(bt->RChild);
return bt;
}
}
BinTree PostCreateTree(BinTree bt)
{
char ch;
scanf("%c",&ch);
if(ch=='#') return NULL;
else
{
bt=(BinNode*)malloc(sizeof(BinNode));
bt->LChild=PostCreateTree(bt->LChild);
bt->RChild=PostCreateTree(bt->RChild);
bt->data=ch;
return bt;
}
}
void PreOrder(BinTree bt)
{
if(bt)
{
printf("%c",bt->data);
PreOrder(bt->LChild);
PreOrder(bt->RChild);
}
}
void InOrder(BinTree bt)
{
if(bt)
{
InOrder(bt->LChild);
printf("%c",bt->data);
InOrder(bt->RChild);
}
}
void PostOrder(BinTree bt)
{
if(bt)
{
PostOrder(bt->LChild);
PostOrder(bt->RChild);
printf("%c",bt->data);
}
}
int CountCnt(BinTree bt)
{
if(bt==NULL) return 0;
else return CountCnt(bt->LChild)+CountCnt(bt->RChild)+1;
}
int LeafCnt(BinTree bt)
{
if(bt==NULL) return 0;
else if(bt->LChild==NULL&&bt->RChild==NULL) return 1;
else return LeafCnt(bt->LChild)+LeafCnt(bt->RChild);
}
int BintreeDep(BinTree bt)
{
int depl,depr;
if(bt==NULL) return 0;
else
{
return max(BintreeDep(bt->LChild),BintreeDep(bt->RChild))+1;
}
}
int LevelOrderTraverse(BinTree bt)
{
LinkQueue Q;
int sum=0;
BinTree p;
if(bt)
{
InitQueue(Q);
EnQueue(Q,bt);
while(!isEmptyQueue(Q))
{
DeQueue(Q,p);
printf("%c",p->data);
if(p->LChild) EnQueue(Q,p->LChild);
if(p->RChild) EnQueue(Q,p->RChild);
}
}
}
bool isCheckCBT(BinTree bt)
{
if(bt==NULL) return 1;
bool flag=0;
LinkQueue Q;
InitQueue(Q);
EnQueue(Q,bt);
while(!isEmptyQueue(Q))
{
BinTree p;
DeQueue(Q,p);
if(p!=NULL)
{
if(flag) return 0;
EnQueue(Q,p->LChild);
EnQueue(Q,p->RChild);
}
else flag=1;
}
return 1;
}
void InOrderTraverse(BinTree bt)
{
if(bt==NULL) return ;
BinTree p=bt;
LNode* Q;
INitStack(Q);
while(!isEmptyStack(Q)||p)
{
while(p)
{
PushStack(Q,p);
p=p->LChild;
}
if(!isEmptyStack(Q))
{
PopStack(Q,p);
printf("%c",p->data);
p=p->RChild;
}
}
}
void PreOrderTraverse(BinTree bt)
{
if(bt==NULL) return ;
BinTree p=bt;
LNode* Q;
INitStack(Q);
while(!isEmptyStack(Q)||p)
{
while(p)
{
printf("%c",p->data);
PushStack(Q,p);
p=p->LChild;
}
if(!isEmptyStack(Q))
{
PopStack(Q,p);
p=p->RChild;
}
}
}
int main()
{
BinTree bt;
printf("一、先序输入结点元素(#表示空)如:ABD###CE##F##\n");
bt=PreCreateTree(bt);
printf("二、先序遍历二叉树:\n");
PreOrder(bt);
printf("\n");
printf("三、中序遍历二叉树:\n");
InOrder(bt);
printf("\n");
printf("四、后序遍历二叉树:\n");
PostOrder(bt);
printf("\n");
printf("五、层次遍历二叉树\n");
LevelOrderTraverse(bt);
printf("\n");
printf("六、二叉树结点数: %d\n",CountCnt(bt));
printf("七、叶子节点的个数:%d \n",LeafCnt(bt));
printf("八、二叉树的深度:%d \n",BintreeDep(bt));
printf("九:判断一个二叉树是否为完全二叉树\n");
if(isCheckCBT(bt)) puts("is a CBT");
else puts("is not a CBT");
printf("十:求二叉树的中序序列(非递归)\n");
InOrderTraverse(bt);
puts("");
printf("十一:求二叉树的先序序列(非递归)\n");
PreOrderTraverse(bt);
puts("");
return 0;
}