标签:小代码
#include <iostream>
using namespace std;
typedef struct node
{
int x;
node*lc;
node*rc;
node(){}
node(int xx){x=xx;lc=NULL;rc=NULL;}
}*BiTree;
//int ss[]={1,2,3,0,0,4,0,0,5,6,0,0,7,0,0};int si=0;
//int ss[]={1,2,-3,0,0,-4,0,0,5,-6,0,0,7,0,0};int si=0;//sum=7
int ss[]={1,2,3,0,0,4,0,0,5,-6,0,0,7,0,0};int si=0;//sum=16
BiTree Tb;
BiTree Tc;
void Creat(BiTree &T)
{int d=ss[si++];
if(d==0) T=NULL;
else{
T=new node(d);
Creat(T->lc);
Creat(T->rc);
}
}
void print(node *root,int base)
{//nead creat new style setw(x)
if(root)
{
print(root->rc,base+1);
for(int i=0;i<base;i++)cout<<"*";
if(root->x > 0)
cout<<" "<<root->x<<endl;
else
cout<<root->x<<endl;
print(root->lc,base+1);
}
else return;
}
int sum=0;
bool flag=false;
void maxsum(node *root)
{
if(root==NULL)return;
if(root->lc){maxsum(root->lc);root->x += root->lc->x;}
if(root->rc){maxsum(root->rc);root->x += root->rc->x;}
if(flag)
{if(root->x > sum )sum=root->x;}
else
{sum=root->x;flag=true;}
}
int sonTree(node *Tb,node *T)
{
if(Tb)
{
if(Tb==T)return 1;
return sonTree(Tb->lc,T)+sonTree(Tb->rc,T);
}
else
return 0;
}
int a[20]={0};
int b[20]={0};
int xx[20]={0};
int xi=0;
//先序遍历
void DLR(BiTree T)
{
if(T)
{
//cout<<T->x<<‘ ‘;
xx[xi++]=T->x;
DLR(T->lc);
DLR(T->rc);
}
}
//中序遍历
void LDR(BiTree T)
{
if(T)
{
DLR(T->lc);
//cout<<T->x<<‘ ‘;
xx[xi++]=T->x;
DLR(T->rc);
}
}
//后序遍历
void LRD(BiTree T)
{
if(T)
{
DLR(T->lc);
DLR(T->rc);
// cout<<T->x<<‘ ‘;
xx[xi++]=T->x;
}
}
void copy(int *xx,int *ab)
{int i;
for( i=0;i<20;i++)
ab[i]=xx[i];
xx[i]=0;
}
void clear(int *array)
{
for(int i=0;i<20;i++)
array[i]=0;
}
int match(int *a,int *b)
{
int i=0,j=0,r=0;int state=-1;
while(a[i]!=0)
{
j=0;r=i;
while(a[r]==b[j])
{ //cout<<a[r]<<" "<<b[j]<<endl;
if(a[r]==b[j]&&b[j+1]==0)state=1;
else state=-1;
if(b[j]==0)break;
j++;r++;
}
i++;
}
clear(a);
clear(b);
//cout<< state<<endl;
return state;
}
int sonTree2(node *Tb,node *T)
{
int f1,f2,f3;
f1=f2=f3=0;
//DLR f1
DLR(T);copy(xx,a);
xi=0;
DLR(Tb); copy(xx,b);
//for(int i=0;i<20;i++) { cout<<a[i]<<","<<b[i]<<endl;}
f1=match(b,a);
if(f1==0)return 0;
cout<<"--------------------------------------"<<endl;
//LDR f2
xi=0;
LDR(T);copy(xx,a);
xi=0;
LDR(Tb);copy(xx,b);
f2=match(b,a);
if(f2==0)return 0;
//LRD f3
xi=0;
LRD(T);copy(xx,a);
xi=0;
LRD(Tb);copy(xx,b);
f3=match(b,a);
if(f3==0)return 0;
return f1 && f2 && f3;
}
int main()
{
cout<<"-------- test 1---------------"<<endl;
BiTree T;
Creat(T);
print(T,4);
maxsum(T);
cout<<"sum = "<<sum<<endl;
print(T,4);
cout<<"-------- test 2 Tb---------------"<<endl;
si=0;
Creat(Tb);
print(Tb,4);
cout<<"-------- test 2 Tb,T---------------"<<endl;
Tb->rc=T;
print(Tb,4);
cout<<sonTree(Tb,T)<<endl;
cout<<"-------- test 3---------------"<<endl;
si=0;
Creat(Tc);
print(Tc,4);
cout<<"-------- test 3 Tc,T---------------"<<endl;
cout<<sonTree(Tb,Tc)<<endl;
cout<<"-------- test 3 Tb---------------"<<endl;
//Tb->lc=Tc;
//print(Tb,4);
//cout<<sonTree(Tb,Tc)<<endl;
cout<<"-------- test 4 Tb---------------"<<endl;
//cout<<sonTree(Tb,Tc->lc)<<endl; //0
//cout<<sonTree2(Tb,Tc->lc)<<endl;//1
cout<<sonTree2(Tb,Tc->rc)<<endl;//1 must 0;
return 0;
}//BUG
标签:小代码
原文地址:http://wzsts.blog.51cto.com/10251779/1771206