#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) { LDR(T->lc); //cout<<T->x<<' '; xx[xi++]=T->x; LDR(T->rc); } } //后序遍历 void LRD(BiTree T) { if(T) { LRD(T->lc); LRD(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;cout<<b[3]<<endl; while(a[i]!=0) { j=0;r=i; while(a[r]==b[j]) { if(a[r]==b[j])state=1; else state=-1; if(b[j]==0)break; j++;r++; } i++; } 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); f1=match(b,a); if(f1==0)return 0; cout<<"--------------------------------------"<<endl; //LDR f2 //for(int i=0;i<20;i++) { cout<<a[i]<<","<<b[i]<<endl;} xi=0; LDR(T);copy(xx,a); xi=0; LDR(Tb);copy(xx,b); // for(int i=0;i<20;i++) { cout<<a[i]<<","<<b[i]<<endl;} f2=match(b,a); if(f2==0) return f2; //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; // f4 /******************** 1 1 1 1 5 5 6 6 前序中序后序都匹配后 还有一个特殊情况不是 TC->LC->x=TB->LC->x TC->RC->X=TB->RC->X *******************/ } 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; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。