using namespace std;
string CH = "ABC##DE#G##F###";
int w[7]={1,2,3,4,5,6,7},w1=0;
int CBT = 0;
int path[10];
typedef struct BiTNode{
TElemType data;
int weight;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
bool InitBiTree(BiTree &T){
T = new BiTNode;
T->lchild = T->rchild = NULL;
return true;
}
void CreateBiTree(BiTree &T){
char ch = CH[CBT++];
if(ch=='#') T = NULL;
else{
T = new BiTNode;
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void PostOrderTraverse2(BiTree T){
stack< BiTree > s;
BiTNode *p = T;
BiTNode *r = NULL;
while(p||!s.empty()){
if(p){
s.push(p);
p = p->lchild;
}else{
p = s.top();
if(p->rchild&&p->rchild!=r)
p = p->rchild;
else{
s.pop();
cout<<p->data;
r = p;
p = NULL;
}
}
}
}
void change(BiTree &T){
if(T){
change(T->lchild);
change(T->rchild);
if(T->rchild!=NULL&&T->lchild!=NULL){
BiTNode *c;
c = T->lchild;
T->lchild = T->rchild;
T->rchild = c;
}
}
}
int leaf(BiTree T){
if(!T) return 0;
int l,r;
l = leaf(T->lchild);
r = leaf(T->rchild);
if(l>0||r>0)
return l+r;
else
return 1;
}
bool isTree(BiTree T1,BiTree T2){
if(T1==NULL&&T2==NULL)
return true;
else if(T1==NULL||T2==NULL)
return false;
else if(T1->data != T2->data)
return false;
return (isTree(T1->lchild,T2->lchild)&&isTree(T1->rchild,T2->rchild));
}
void du(BiTree T){
if(T){
cout<<T->data<<endl;
du(T->lchild);
cout<<T->data<<endl;
du(T->rchild);
}
}
int with(BiTree T){
if(T==NULL) return 0;
BiTNode* p;
queue <BiTree> q1,q2;
int count1,count2,sum = 0;
q1.push(T);
while((!q1.empty())||(!q2.empty())){
count1 = 0;
while(!q1.empty()){
p = q1.front();
if(p->lchild!=NULL)
q2.push(p->lchild);
if(p->rchild!=NULL)
q2.push(p->rchild);
q1.pop();
count1++;
}
if(sum<count1)
sum =count1;
count2 = 0;
while(!q2.empty()){
p = q2.front();
if(p->lchild!=NULL)
q1.push(p->lchild);
if(p->rchild!=NULL)
q1.push(p->rchild);
q2.pop();
count2++;
}
if(sum<count2)
sum = count2;
}
return sum;
}
int with2(BiTree T){
if(T==NULL) return 0;
queue <BiTree> q;
BiTNode* p;
int front = 1;
int rear = 1;
int last = 1;
int temp = 0,maxw = 0;
q.push(T);
while(front<=last){
p = q.front();
front++;
q.pop();
temp++;
if(p->lchild){
q.push(p->lchild);
rear++;
}
if(p->rchild){
q.push(p->rchild);
rear++;
}
if(front>last){
last = rear;
if(temp>maxw) maxw = temp;
temp = 0;
}
}
return maxw;
}
int du1(BiTree T){
if(T==NULL) return 0;
BiTNode *p;
queue <BiTree> q;
q.push(T);
int count = 0;
while(!q.empty()){
p = q.front();
q.pop();
if(p->lchild){
q.push(p->lchild);
if(p->rchild==NULL){
count++;
continue;
}
}
if(p->rchild){
q.push(p->rchild);
if(p->lchild==NULL)
count++;
}
}
return count;
}
void longPath(BiTree T){
BiTree p=T,l[10],s[10];
int top = 0;
int longest = 0;
int tag[10];
while(p||top>0){
while(p){
s[++top] = p;
tag[top] = 0;
p = p->lchild;
}
if(tag[top]==1)
{
if(!s[top]->lchild && !s[top]->rchild)
if(top>longest){
for(int i=1;i<=top;i++) l[i] = s[i];
longest = top;
}
top--;
}
else if(top>0){
tag[top] = 1;
p = s[top]->rchild;
}
}
for(int i=1;i<=longest;i++)
cout<<l[i]->data;
}
void longPath2(BiTree T){
BiTree p = T,s[10],l[10];
int top = 0,longest = 0;
int tag[10];
while(p||top>0){
while(p){
s[++top] = p;
tag[top] = 0;
p = p->lchild;
}
if(tag[top]==1){
if(!s[top]->lchild && !s[top]->rchild){
if(top>longest){
for(int i=1;i<=top;i++) l[i] = s[i];
longest = top;
}
}
top--;
}
else if(top>0){
tag[top] = 1;
p = s[top]->rchild;
}
}
for(int i=1;i<=longest;i++)
cout<<l[i]->data;
}
void pathc(BiTree T,char *path,int len){
if(T){
if(!T->lchild && !T->rchild){
cout<<"叶子结点"<<T->data<<"到根的路径为 ";
for(int i=len-1;i>=0;i--)
cout<<path[i];
cout<<endl;
}
else
{
path[len] = T->data;
pathc(T->lchild,path,len+1);
pathc(T->rchild,path,len+1);
}
}
}
int WPL(BiTree T,int len){
if(T){
if(!T->lchild && !T->rchild)
return len*T->weight;
else
return WPL(T->lchild,len+1)+WPL(T->rchild,len+1);
}
else
return 0;
}
int main(){
BiTree T,T2;
char path[10];
CreateBiTree(T2);
CBT = 0;
CreateBiTree(T);
cout<<isTree(T,T2);
}