#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#define MaxSize 100
using namespace std;
typedef struct node
{
char data;
struct node * lchild;
struct node * rchild;
}BTNode;
typedef struct
{
BTNode *data[MaxSize];
int front,rear;
}SqQueue;
typedef struct
{
BTNode *data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void InitQueue(SqQueue *&q)
{ q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,BTNode * e)
{
if (s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool QueueEmpty(SqQueue *q)
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,BTNode * e)
{ if ((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,BTNode *&e)
{ if (q->front==q->rear)
return false;
q->front=(q->front+1)%MaxSize;
e=q->data[q->front];
return true;
}
bool Pop(SqStack *&s,BTNode * &e)
{
if (s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
bool GetTop(SqStack *s,BTNode *&e)
{
if (s->top==-1)
return false;
e=s->data[s->top];
return true;
}
void CreateBTree(BTNode *&b,char *str)
{
BTNode *St[MaxSize],*p;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case'(':
top++;
St[top]=p;
k=1;
break;
case')':
top--;
break;
case',':
k=2;
break;
default:
p=(BTNode *)malloc(sizeof(BTNode));
p->data = ch;
p->lchild = p->rchild = NULL;
if(b==NULL)
{
b=p;
}
else
{
switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
void PreOrderFX(BTNode *b)
{
BTNode *p;
SqStack *st;
InitStack(st);
if(b!=NULL)
{
Push(st,b);
while(!StackEmpty(st))
{
Pop(st,p);
printf("%c",p->data);
if(p->rchild !=NULL)
{
Push(st,p->rchild);
}
if(p->lchild !=NULL)
{
Push(st,p->lchild);
}
}
printf("\n");
}
DestroyStack(st);
}
void InOrderFX(BTNode *b)
{
BTNode *p;
SqStack *st;
InitStack(st);
p=b;
while(!StackEmpty(st) || p!=NULL)
{
while(p!=NULL)
{
Push(st,p);
p=p->lchild;
}
if(!StackEmpty(st))
{
Pop(st,p);
printf("%c",p->data);
p=p->rchild;
}
}
printf("\n");
DestroyStack(st);
}
void PostOrderFX(BTNode *b)
{
BTNode *p,*r;
bool flag;
SqStack *st;
InitStack(st);
p=b;
do
{
while(p!=NULL)
{
Push(st,p);
p=p->lchild;
}
r=NULL;
flag=true;
while(!StackEmpty(st)&& flag)
{
GetTop(st,p);
if(p->rchild==r)
{
printf("%c",p->data);
Pop(st,p);
r=p;
}
else
{
p=p->rchild;
flag=false;
}
}
}
while(!StackEmpty(st));
printf("\n");
DestroyStack(st);
}
void PreOrder(BTNode *b)
{
if(b!=NULL)
{
printf("%c",b->data);
PreOrder(b->lchild);
PreOrder(b->rchild);
}
}
void InOrder(BTNode *b)
{
if(b!=NULL)
{
InOrder(b->lchild);
printf("%c",b->data);
InOrder(b->rchild);
}
}
void PostOrder(BTNode *b)
{
if(b!=NULL)
{
PostOrder(b->lchild);
PostOrder(b->rchild);
printf("%c",b->data);
}
}
void LevelOrder(BTNode *b)
{
BTNode *p;
SqQueue *qu;
InitQueue (qu);
enQueue(qu,b);
while(!QueueEmpty(qu))
{
deQueue(qu,p);
printf("%c",p->data);
if(p->lchild!=NULL)
{
enQueue(qu,p->lchild);
}
if(p->rchild!=NULL)
{
enQueue(qu,p->rchild);
}
}
}
void DestroyBTree(BTNode *& b)
{
if(b!=NULL)
{
DestroyBTree(b->lchild);
DestroyBTree(b->rchild);
free(b);
}
}
bool Check(BTNode *a,BTNode *b)
{
if(!a && !b)
return true;
if(!a || !b)
return false;
if(a && b)
{
if(a->data != b->data)
return false;
bool l=Check(a->lchild,b->lchild);
bool r=Check(a->rchild,b->rchild);
return l& r;
}
}
int main()
{ BTNode *b,*p;
CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
CreateBTree(p,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
if(Check(b,p)==true)
{
printf("p是b的子树");
}
if(Check(b,p)==false)
{
printf("p不是b的子树");
}
}