
創(chuàng)新互聯(lián)專注于達茂旗網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供達茂旗營銷型網(wǎng)站建設(shè),達茂旗網(wǎng)站制作、達茂旗網(wǎng)頁設(shè)計、達茂旗網(wǎng)站官網(wǎng)定制、小程序設(shè)計服務(wù),打造達茂旗網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供達茂旗網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
#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;
}
文章標題:小代碼二叉樹之最大子樹和子樹判斷
瀏覽路徑:http://www.yijiale78.com/article48/ghdjhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站導(dǎo)航、響應(yīng)式網(wǎng)站、品牌網(wǎng)站制作、域名注冊、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)