ǰλ: ҳ > >

2015*ʡǿ

ʱ:

1tǸһöĵݹcount(t):tозǿյ,ӵĽN2;ֻзǿӵĸNL;ֻзǿҶӵĽNRҶӽN0N2NLNRN0ȫڵcount(t)֮ǰΪ0.
typedef struct node
{int data; struct node *lchild,*rchild;}node;
int N2,NL,NR,N0;
void count(node *t)
{if (t->lchild!=NULL) if (1)___ N2++; else NL++;
else if (2)___ NR++; else (3)__ ;
if(t->lchild!=NULL)(4)____; if (t->rchild!=NULL) (5)____;
}
26.ǵݹ㷨
void example(b)
btree *b;
{ btree *stack[20], *p
int top;
if (b!=null)
{ top=1; stack[top]=b;
while (top>0)
{ p=stack[top]; top--;
printf(%d,p->data);
if (p->rchild!=null)
{(1)___; (2)___;
}
if (p->lchild!=null)
(3)___; (4)__;
}}}}

2nׯ֮Ľͨͼׯij֮е·򽫶ijñӣϵWijʾ·ijȣҪnׯѡһׯһҽԺҽԺӦĸׯʹҽԺԶĴׯҽԺ·?һ㷨Ӧø㷨ͼʾʵ20
void Hospital(AdjMatrix w,int n)
//ڽӴȨʾnׯУҽԺںδʹҽԺԶĴׯҽԺ·̡
{for (k=1;k<=n;k++) //·
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if (w[i][k]+w[k][j]<w[i][j]) w[i][j]=w[i][k]+w[k][j];
m=MAXINT; //趨mΪ
for (i=1;i<=n;i++) //·̵һ
{s=0;
for (j=1;j<=n;j++) //ijׯi1<=i<=nׯ·
if (w[i][j]>s) s=w[i][j];
if (s<=m) {m=s; k=i;}//·Уȡ̵һm·kdz±ꡣ
Printf(ҽԺӦ%dׯҽԺΪ%d\n,i,m);
}//for
}//㷨
ʵģĹԡ996799⼸СΪ6ҽԺӦڵׯ,ҽԺԶĴׯҽԺľ6

1ͼ1ʾͨGPrim㷨Сÿѡȡһ߻һͼ

3TһдһTתΪеĵݹ㷨
4 V1V2ÿ㣬ڽӵǷͬһУǣΪǶͼΪˣ12ʾϡһнṹͼзʵĶ㡣
int BPGraph (AdjMatrix g)
//жڽӾʾͼgǷǶͼ
{int s[]; //ԪֵʾǸϣֵ12ʾϣ
int Q[];//QΪУԪΪͼĶ㣬趥ϢǶš
int f=0,r,visited[]; //frֱǶеͷβָ,visited[]Ƿ
for (i=1;i<=n;i++) {visited[i]=0;s[i]=0;} //ʼδȷǸ
Q[1]=1; r=1; s[1]=1;//1뼯S1
while(f<r)
{v=Q[++f]; if (s[v]==1) jh=2; else jh=1;//׼vڽӵļϺ
if (!visited[v])
{visited[v]=1; //ȷÿһ㣬Ҫڽӵ㲻Ӧһ
for (j=1,j<=n;j++)
if (g[v][j]==1){if (!s[j]) {s[j]=jh; Q[++r]=j;} //ڽӵ
else if (s[j]==s[v]) return(0);} //Ƕͼ
}//if (!visited[v])
}//while
return(1); }//Ƕͼ
[㷨] Ŀͨͼͨ㷨Ҫ޸ġ

5 V1V2ÿ㣬ڽӵǷͬһУǣΪǶͼΪˣ12ʾϡһнṹͼзʵĶ㡣
int BPGraph (AdjMatrix g)
//жڽӾʾͼgǷǶͼ
{int s[]; //ԪֵʾǸϣֵ12ʾϣ
int Q[];//QΪУԪΪͼĶ㣬趥ϢǶš
int f=0,r,visited[]; //frֱǶеͷβָ,visited[]Ƿ
for (i=1;i<=n;i++) {visited[i]=0;s[i]=0;} //ʼδȷǸ
Q[1]=1; r=1; s[1]=1;//1뼯S1
while(f<r)
{v=Q[++f]; if (s[v]==1) jh=2; else jh=1;//׼vڽӵļϺ
if (!visited[v])
{visited[v]=1; //ȷÿһ㣬Ҫڽӵ㲻Ӧһ
for (j=1,j<=n;j++)
if (g[v][j]==1){if (!s[j]) {s[j]=jh; Q[++r]=j;} //ڽӵ
else if (s[j]==s[v]) return(0);} //Ƕͼ
}//if (!visited[v])
}//while
return(1); }//Ƕͼ
[㷨] Ŀͨͼͨ㷨Ҫ޸ġ

6K1KnnؼʣԽ
öIJ㷨һöؼʵIJΪK1K2Knʱ㷨һLLINK / RLINK ӱʾĶ

7K1KnnؼʣԽ
öIJ㷨һöؼʵIJΪK1K2Knʱ㷨һLLINK / RLINK ӱʾĶ

8 IJαеĵһǶĸʵϣαеÿ㶼ǡֲȷ󣬵У鵽ý㣬ý㽫Ϊҡ֡УиĺӦĸֻֻڲеĸҲֻĸĸһȫֱָRָдԪء㷨ȴ㣬ŮϢСȻڶвյ£ѭĽ㡣Ԫصݽṹ£
typedef struct
{ int lvl; //ָ룬ָǰ㡱ڲеλ
int l,h; //еϽ
int f; //еǰ㡱˫׽ָ
int lr; // 1˫׵ 2˫׵
}qnode;
BiTree Creat(datatype in[],level[],int n)
//ɶIJlevel[n]in[n]ɶ nǶĽ
{if (n<1) {printf(\n); exit(0);}
qnode s,Q[]; //QԪΪqnode͵ĶУ㹻
init(Q); int R=0; //RDzָ룬ָǰĽ
BiTree p=(BiTree)malloc(sizeof(BiNode)); //ɸ
p->data=level[0]; p->lchild=null; p->rchild=null; //дý
for (i=0; i<n; i++) //вҸ㣬ȻŮϢ
if (in[i]==level[0]) break;
if (i==0) //е1n-1
{p->lchild=null;
s.lvl=++R; s.l=i+1; s.h=n-1; s.f=p; s.lr=2; enqueue(Q,s);
}
else if (i==n-1) //е1n-1
{p->rchild=null;
s.lvl=++R; s.l=1; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);
}
else //
{s.lvl=++R; s.l=0; s.h=i-1; s.f=p; s.lr=1;enqueue(Q,s);//йϢ
s.lvl=++R; s.l=i+1;s.h=n-1;s.f=p; s.lr=2;enqueue(Q,s);//йϢ
}
while (!empty(Q)) //вգѭ
{ s=delqueue(Q); father=s.f;
for (i=s.l; i<=s.h; i++)
if (in[i]==level[s.lvl]) break;
p=(bitreptr)malloc(sizeof(binode)); //ռ
p->data=level[s.lvl]; p->lchild=null; p->rchild=null; //дý
if (s.lr==1) father->lchild=p;
else father->rchild=p //˫׵Ůָָý
if (i==s.l)
{p->lchild=null; //Ů
s.lvl=++R; s.l=i+1; s.f=p; s.lr=2; enqueue(Q,s);
}
else if (i==s.h)
{p->rchild=null; //Ů
s.lvl=++R; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);
}
else{s.lvl=++R; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);//йϢ
s.lvl=++R; s.l=i+1; s.f=p; s.lr=2; enqueue(Q,s); //йϢ
}
}//while (!empty(Q))
return(p);
}//㷨

9һļ¼ؼΪ(131824354750628390)ҷöֲңҪҹؼ62ʱıȽϴҳɹʱ*ҳȡ
10IOֱʾջͳջջij̬̬ΪգջͳջIJпɱʾΪIOɵУƿԲΪϷУΪǷС15֣
1ADǺϷУBC ǷǷС
2豻жIJѴһάAС
int Judge(char A[])
//жַAеǷǺϷСǣtrue򷵻false
{i=0; //iΪ±ꡣ
j=k=0; //jkֱΪIĸOĵĸ
while(A[i]!=\0) //δַβ
{switch(A[i])
{caseI: j++; break; //ջ1
caseO: k++; if(k>j){printf(зǷ\n)exit(0);}
}
i++; //A[i]ǡIOָiơ}
if(j!=k) {printf(зǷ\n)return(false);}
else {printf(кϷ\n)return(true);}
}//㷨

11ԶijϵĽ㣬öнṹαˡ
int LeafKlevel(BiTree bt, int k) //bt ĵk(k>1) Ҷӽ
{if(bt==null || k<1) return(0);
BiTree p=bt,Q[]; //QǶУԪǶָ,㹻
int front=0,rear=1,leaf=0; //front rearǶͷͶβָ, leafҶӽ
int last=1,level=1; Q[1]=p; //lastǶͬҽָ룬level ǶIJ
while(front<=rear)
{p=Q[++front];
if(level==k && !p->lchild && !p->rchild) leaf++; //Ҷӽ
if(p->lchild) Q[++rear]=p->lchild; //Ů
if(p->rchild) Q[++rear]=p->rchild; //Ů
if(front==last) {level++; //ͬҽѴ,1
last=rear; } //lastƵָ²һԪ
if(level>k) return (leaf); //k ˳
}//while }//LeafKLevel

12·ǽŹؼr[1..n]йؼֶַ·ֱ뵽d[1..n]ǰ벿ͺ벿ע:dΪѭԭΪȽr[l]d[1]ٴr[2] ¼ʼֶ·롣дʵֶ·㷨
13·ǽŹؼr[1..n]йؼֶַ·ֱ뵽d[1..n]ǰ벿ͺ벿ע:dΪѭԭΪȽr[l]d[1]ٴr[2] ¼ʼֶ·롣дʵֶ·㷨
14۰ҵĵݹ㷨㷨ʱ临ӶԷ
15l*̨ijȣkָʾ*̨bеʼλã±꣩jסֲ*̨ʼλãiָʾɨb±꣬i0ʼκͺԪرȽϣֲ*̨ȣi-jlʱ޸*̨ijkl=i-jbеʼλãk=jֱblΪ
void Platform (int b[ ], int N)
//NԪصb*̨ijȡ
{l=1;k=0;j=0;i=0;
while(i<n-1)
{while(i<n-1 && b[i]==b[i+1]) i++;
if(i-j+1>l) {l=i-j+1;k=j;} //ֲ*̨
i++; j=i; } //*̨
printf(*̨%dbʼ±Ϊ%dlk)
}// Platform

16ͼGУrGеÿ㶼·ɴ*rΪGĸ㡣дһ㷨йܣ
1ͼGڽӱ洢ṹ
2жͼGǷиУӡиֵ

17֤ɶкͺУҲΨһȷһö
29. ҳĶ
1ͬ 2ͬ
3ͬ 4αͬ

18tǸһöĵݹcount(t):tозǿյ,ӵĽN2;ֻзǿӵĸNL;ֻзǿҶӵĽNRҶӽN0N2NLNRN0ȫڵcount(t)֮ǰΪ0.
typedef struct node
{int data; struct node *lchild,*rchild;}node;
int N2,NL,NR,N0;
void count(node *t)
{if (t->lchild!=NULL) if (1)___ N2++; else NL++;
else if (2)___ NR++; else (3)__ ;
if(t->lchild!=NULL)(4)____; if (t->rchild!=NULL) (5)____;
}
26.ǵݹ㷨
void example(b)
btree *b;
{ btree *stack[20], *p
int top;
if (b!=null)
{ top=1; stack[top]=b;
while (top>0)
{ p=stack[top]; top--;
printf(%d,p->data);
if (p->rchild!=null)
{(1)___; (2)___;
}
if (p->lchild!=null)
(3)___; (4)__;
}}}}

19һּ򵥵㷨count sorting㷨һı(ʾ)򣬲ŵһµıСעǣдĹؼ뻥ͬ㷨Աеÿ¼ɨıһˣͳƱжٸ¼Ĺؼȸü¼ĹؼСijһ¼ͳƳļֵΪcô¼µеĺʵĴλüΪc
(1) (3)ڼݱ;
(2) (7)ʹPascalCԱдʵּ㷨
(3) (4)n¼ıؼȽϴǶ?
(4) (3)ѡȽϣַǷ?Ϊʲô?

204 void LinkList_reverse(Linklist &L)
//ľ͵;Ϊ㷨,2
{
p=L->next;q=p->next;s=q->next;p->next=NULL;
while(s->next)
{
q->next=p;p=q;
q=s;s=s->next; //LԪ±ͷ
}
q->next=p;s->next=q;L->next=s;
}//LinkList_reverse

21ͼbipartite graph G=VEһܽ㼯VΪཻӼV 1V2=V-V1ͼʹãV1еκͼGоڣV2еκνͼGҲڡ
1һΪ5ĶͼͷǶͼӡ
2CPASCALдһBIPARTITEжһͨͼGǷǶͼʱ临ӶȡGöάAʾСΪn*nnΪڳмӱҪע͡бҪֱöջв

22tǸһöĵݹcount(t):tозǿյ,ӵĽN2;ֻзǿӵĸNL;ֻзǿҶӵĽNRҶӽN0N2NLNRN0ȫڵcount(t)֮ǰΪ0.
typedef struct node
{int data; struct node *lchild,*rchild;}node;
int N2,NL,NR,N0;
void count(node *t)
{if (t->lchild!=NULL) if (1)___ N2++; else NL++;
else if (2)___ NR++; else (3)__ ;
if(t->lchild!=NULL)(4)____; if (t->rchild!=NULL) (5)____;
}
26.ǵݹ㷨
void example(b)
btree *b;
{ btree *stack[20], *p
int top;
if (b!=null)
{ top=1; stack[top]=b;
while (top>0)
{ p=stack[top]; top--;
printf(%d,p->data);
if (p->rchild!=null)
{(1)___; (2)___;
}
if (p->lchild!=null)
(3)___; (4)__;
}}}}

23#define maxsize ջռ

void InOutS(int s[maxsize])
//sԪΪջ㷨ջջ
{int top=0; //topΪջָ룬top=0ʱΪջա
for(i=1; i<=n; i++) //n
{scanf(%d,&x); //Ӽ̶С
if(x!=-1) // -1ʱջ
if(top==maxsize-1){printf(ջ\n);exit(0);}
else s[++top]=x; //xջ
else //-1ʱջ
{if(top==0){printf(ջ\n);exit(0);}
else printf(ջԪ%d\n,s[top--])}
}
}//㷨

24֤ɶкͺУҲΨһȷһö
n=1ʱֻһ㣬кͺпȷö
赱n=m-1ʱ۳֤n=mʱ۳
ΪS1S2Sm,P1,P2,PmһԪPmǸпҵPmȵĽ㣨и㻥ͬSi(1im)ãSiǸ㣬S1S2Si-1УSi+1,Si+2,,SmС
i=1S1ǸʱΪգĽm-1,{S2S3Sm}{P1P2Pm-1}ΨһȷӶҲȷ˶
i=m,SmǸʱΪգĽm-1{S1S2Sm-1}{P1P2Pm-1}ΨһȷӶҲȷ˶
󣬵1<i<mʱSiзֳ{S1S2Si-1}{Si+1,Si+2,Sm}ںǡ㡱{P1,P2,,Pi-1}{Pi,Pi+1,Pm-1}ǶĺС{S1S2Si-1}{P1P2Pi-1}
Ψһȷ{Si+1,Si+2,Sm}
{Pi,Pi+1,Pm-1}Ψһȷ

25ԶijϵĽ㣬öнṹαˡ
int LeafKlevel(BiTree bt, int k) //bt ĵk(k>1) Ҷӽ
{if(bt==null || k<1) return(0);
BiTree p=bt,Q[]; //QǶУԪǶָ,㹻
int front=0,rear=1,leaf=0; //front rearǶͷͶβָ, leafҶӽ
int last=1,level=1; Q[1]=p; //lastǶͬҽָ룬level ǶIJ
while(front<=rear)
{p=Q[++front];
if(level==k && !p->lchild && !p->rchild) leaf++; //Ҷӽ
if(p->lchild) Q[++rear]=p->lchild; //Ů
if(p->rchild) Q[++rear]=p->rchild; //Ů
if(front==last) {level++; //ͬҽѴ,1
last=rear; } //lastƵָ²һԪ
if(level>k) return (leaf); //k ˳
}//while }//LeafKLevel

26Ӧʹȱdfs(v)ʱ ʼ˳dfs()ǰѷͼȫ㣨ΪnͼиvΪ㡣n1nţһdfs()̣Ϳȫĸ㡣ͼڽӱ洢ṹǶıԼʱȾΪȫֱͼgڽӱ洢ṹμ2⣬ֻжͼǷи㷨
int num=0 visited[]=0 //numǷʶ,visitedʼ
const n=ûĶ;
AdjList g ; //ڽӱ洢ṹͼg
void dfs(v)
{visited [v]=1; num++; //ʵĶ1
if (num==n) {printf(%dͼĸ\n,v); num=0;}//if
p=g[v].firstarc;
while (p)
{if (visied[p->adjvex]==0) dfs (p->adjvex);
p=p->next;} //while
visited[v]=0; num--; //ָv
}//dfs
void JudgeRoot()
//жͼǷии֮
{static int i ;
for (i=1;i<=n;i++ ) //ÿdfs()һΡ
{num=0; visited[1..n]=0; dfs(i); }
}// JudgeRoot
㷨дӡʱڽӱеţ±꣩ҪϢʹg[i].vertex



27ĿҪԪص*ֵ˳ÿԪظȣ*ֵ밴ÿԪ֮һ˼ӦԪ֮ͣһάУȻѡһ򷽷ԸעʱԪƶ֮ӦиԪҲӦ䶯
void Translationfloat *matrixint n
//㷨nnľmatrixͨб任ʹԪص*ֵС
{int i,j,k,l
float summin //sumݴԪ֮
float *p, *pi, *pk;
for(i=0; i<n; i++)
{sum=0.0; pk=matrix+i*n; //pkָе1Ԫ.
for (j=0; j<n; j++){sum+=*(pk); pk++;} //һԪ֮.
*(p+i)=sum; //һԪ֮ʹһά.
}//for i
for(i=0; i<n-1; i++) //ѡ񷨶p
{min=*(p+i); k=i; //ʼiԪ֮С.
for(j=i+1;j<n;j++) if(p[j]<min) {k=j; min=p[j];} //µСֵк.
if(i!=k) //Свǵǰ,Ҫн(ԪؼԪ֮)
{pk=matrix+n*k; //pkָkе1Ԫ.
pi=matrix+n*i; //piָiе1Ԫ.
for(j=0;j<n;j++) //жӦԪ.
{sum=*(pk+j); *(pk+j)=*(pi+j); *(pi+j)=sum;}
sum=p[i]; p[i]=p[k]; p[k]=sum; //һάԪ֮.
}//if
}//for i
free(p); //ͷp.
}// Translation
[㷨] 㷨ʹѡ,Ƚϴ϶,ݽ(ƶ).򷽷,ɼٱȽϴ,ƶ.㷨ʱ临ӶΪO(n2).

28ǿáȦȨͨͼһСνȦǡȡһȦȥȦȨıߡִһ裬ֱûȦΪֹáȦĴȨͨͼһСϸ㷨óʵ㷨עȦǻ·
29 IJαеĵһǶĸʵϣαеÿ㶼ǡֲȷ󣬵У鵽ý㣬ý㽫Ϊҡ֡УиĺӦĸֻֻڲеĸҲֻĸĸһȫֱָRָдԪء㷨ȴ㣬ŮϢСȻڶвյ£ѭĽ㡣Ԫصݽṹ£
typedef struct
{ int lvl; //ָ룬ָǰ㡱ڲеλ
int l,h; //еϽ
int f; //еǰ㡱˫׽ָ
int lr; // 1˫׵ 2˫׵
}qnode;
BiTree Creat(datatype in[],level[],int n)
//ɶIJlevel[n]in[n]ɶ nǶĽ
{if (n<1) {printf(\n); exit(0);}
qnode s,Q[]; //QԪΪqnode͵ĶУ㹻
init(Q); int R=0; //RDzָ룬ָǰĽ
BiTree p=(BiTree)malloc(sizeof(BiNode)); //ɸ
p->data=level[0]; p->lchild=null; p->rchild=null; //дý
for (i=0; i<n; i++) //вҸ㣬ȻŮϢ
if (in[i]==level[0]) break;
if (i==0) //е1n-1
{p->lchild=null;
s.lvl=++R; s.l=i+1; s.h=n-1; s.f=p; s.lr=2; enqueue(Q,s);
}
else if (i==n-1) //е1n-1
{p->rchild=null;
s.lvl=++R; s.l=1; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);
}
else //
{s.lvl=++R; s.l=0; s.h=i-1; s.f=p; s.lr=1;enqueue(Q,s);//йϢ
s.lvl=++R; s.l=i+1;s.h=n-1;s.f=p; s.lr=2;enqueue(Q,s);//йϢ
}
while (!empty(Q)) //вգѭ
{ s=delqueue(Q); father=s.f;
for (i=s.l; i<=s.h; i++)
if (in[i]==level[s.lvl]) break;
p=(bitreptr)malloc(sizeof(binode)); //ռ
p->data=level[s.lvl]; p->lchild=null; p->rchild=null; //дý
if (s.lr==1) father->lchild=p;
else father->rchild=p //˫׵Ůָָý
if (i==s.l)
{p->lchild=null; //Ů
s.lvl=++R; s.l=i+1; s.f=p; s.lr=2; enqueue(Q,s);
}
else if (i==s.h)
{p->rchild=null; //Ů
s.lvl=++R; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);
}
else{s.lvl=++R; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);//йϢ
s.lvl=++R; s.l=i+1; s.f=p; s.lr=2; enqueue(Q,s); //йϢ
}
}//while (!empty(Q))
return(p);
}//㷨

30һļ¼ؼΪ(131824354750628390)ҷöֲңҪҹؼ62ʱıȽϴҳɹʱ*ҳȡ
31ݶýֵΪʣڱнǰǰֵȽϣɵóۣΪȫָpreֵΪnullȫֱflagֵΪtrueǶflagΪfalse
#define true 1
#define false 0
typedef struct node
{datatype data; struct node *llink,*rlink;} *BTree;
void JudgeBSTBTree t,int flag
// ж϶ǷǶ㷨ڵóflagóۡ
{ ift!=null && flag
{ Judgebstt->llink,flag//
ifpre==nullpre=t// ĵһ㲻ж
else ifpre->data<t->datapre=t//ǰָָǰ
else{flag=flase} //ȫ
Judgebst t->rlink,flag//
}//JudgeBST㷨


32#define maxsize ջռ

void InOutS(int s[maxsize])
//sԪΪջ㷨ջջ
{int top=0; //topΪջָ룬top=0ʱΪջա
for(i=1; i<=n; i++) //n
{scanf(%d,&x); //Ӽ̶С
if(x!=-1) // -1ʱջ
if(top==maxsize-1){printf(ջ\n);exit(0);}
else s[++top]=x; //xջ
else //-1ʱջ
{if(top==0){printf(ջ\n);exit(0);}
else printf(ջԪ%d\n,s[top--])}
}
}//㷨

33l*̨ijȣkָʾ*̨bеʼλã±꣩jסֲ*̨ʼλãiָʾɨb±꣬i0ʼκͺԪرȽϣֲ*̨ȣi-jlʱ޸*̨ijkl=i-jbеʼλãk=jֱblΪ
void Platform (int b[ ], int N)
//NԪصb*̨ijȡ
{l=1;k=0;j=0;i=0;
while(i<n-1)
{while(i<n-1 && b[i]==b[i+1]) i++;
if(i-j+1>l) {l=i-j+1;k=j;} //ֲ*̨
i++; j=i; } //*̨
printf(*̨%dbʼ±Ϊ%dlk)
}// Platform

34ͼGУrGеÿ㶼·ɴ*rΪGĸ㡣дһ㷨йܣ
1ͼGڽӱ洢ṹ
2жͼGǷиУӡиֵ

35ǿáȦȨͨͼһСνȦǡȡһȦȥȦȨıߡִһ裬ֱûȦΪֹáȦĴȨͨͼһСϸ㷨óʵ㷨עȦǻ·
36ɶǰȷΨһһöʵ֪ijǰУһöʾĶӡУдȱ䡣
#define MAX 100
typedef struct Node
{char info; struct Node *llink, *rlink; }TNODE;
char pred[MAX],inod[MAX];
main(int argc,int **argv)
{ TNODE *root;
if(argc<3) exit 0;
strcpy(pred,argv[1]); strcpy(inod,argv[2]);
root=restore(pred,inod,strlen(pred));
postorder(root);
}
TNODE *restore(char *ppos,char *ipos,int n)
{ TNODE *ptr char *rpos; int k;
if(n<=0) return NULL;
ptr->info=(1)_______;
for((2)_______ ; rpos<ipos+n;rpos++) if(*rpos==*ppos) break;
k=(3)_______;
ptr->llink=restore(ppos+1, (4)_______,k );
ptr->rlink=restore ((5)_______+k,rpos+1,n-1-k);
return ptr;
}
postorder(TNODE*ptr)
{ if(ptr=NULL) return;
postorder(ptr->llink); postorder(ptr->rlink); printf(%c,ptr->info);
}

37IOֱʾջͳջջij̬̬ΪգջͳջIJпɱʾΪIOɵУƿԲΪϷУΪǷС15֣
1ADǺϷУBC ǷǷС
2豻жIJѴһάAС
int Judge(char A[])
//жַAеǷǺϷСǣtrue򷵻false
{i=0; //iΪ±ꡣ
j=k=0; //jkֱΪIĸOĵĸ
while(A[i]!=\0) //δַβ
{switch(A[i])
{caseI: j++; break; //ջ1
caseO: k++; if(k>j){printf(зǷ\n)exit(0);}
}
i++; //A[i]ǡIOָiơ}
if(j!=k) {printf(зǷ\n)return(false);}
else {printf(кϷ\n)return(true);}
}//㷨

38 V1V2ÿ㣬ڽӵǷͬһУǣΪǶͼΪˣ12ʾϡһнṹͼзʵĶ㡣
int BPGraph (AdjMatrix g)
//жڽӾʾͼgǷǶͼ
{int s[]; //ԪֵʾǸϣֵ12ʾϣ
int Q[];//QΪУԪΪͼĶ㣬趥ϢǶš
int f=0,r,visited[]; //frֱǶеͷβָ,visited[]Ƿ
for (i=1;i<=n;i++) {visited[i]=0;s[i]=0;} //ʼδȷǸ
Q[1]=1; r=1; s[1]=1;//1뼯S1
while(f<r)
{v=Q[++f]; if (s[v]==1) jh=2; else jh=1;//׼vڽӵļϺ
if (!visited[v])
{visited[v]=1; //ȷÿһ㣬Ҫڽӵ㲻Ӧһ
for (j=1,j<=n;j++)
if (g[v][j]==1){if (!s[j]) {s[j]=jh; Q[++r]=j;} //ڽӵ
else if (s[j]==s[v]) return(0);} //Ƕͼ
}//if (!visited[v])
}//while
return(1); }//Ƕͼ
[㷨] Ŀͨͼͨ㷨Ҫ޸ġ

39nƷܷ뱳Ϊܷٴn-1Ʒѡɼ뱳ʱɷƷΪs-w[n]nƷܷ뱳Ǵn-1Ʒѡɼ뱳ʱɷƷΪss=0,һ⣻s<0Ȼs>0Ʒn<1,޽⡣
1s-w[n],n-1 //Knap(s-w[n],n-1)=true
2s,n-1 // KnapKnap(s,n-1)

40ABԪطֱϲC飬ʹCӦABCֻҪעABָʹãԼȷһݺһԪظƵCмɡ
void union(int A[],B[],C[],m,n)
//ABmnԪأǰߵ򣬺ߵݼ򣬱㷨AB鲢ΪC
{i=0; j=n-1; k=0;// ijkֱA,BC±꣬C±0ʼ
while(i<m && j>=0)
if(a[i]<b[j]) c[k++]=a[i++] else c[k++]=b[j--];
while(i<m) c[k++]=a[i++];
while(j>=0) c[k++]=b[j--];
}㷨
4Ҫʽ洢15
1дһ㷨2дһбĶǷȫ㷨
BiTree Creat() //ĶʽĴ洢ṹ
{ElemType xBiTree bt;
scanf(%d,&x); //ٶΪ
if(x==0) bt=null;
else if(x>0)
{bt=(BiNode *)malloc(sizeof(BiNode));
bt->data=x; bt->lchild=creat(); bt->rchild=creat();
}
else error()
return(bt);
}// BiTree
int JudgeComplete(BiTree bt) //ж϶Ƿȫ,ǣ1򣬷0
{int tag=0; BiTree p=bt, Q[]; // QǶУԪǶָ룬㹻
if(p==null) return (1);
QueueInit(Q); QueueIn(Q,p); //ʼУָ
while (!QueueEmpty(Q))
{p=QueueOut(Q); //
if (p->lchild && !tag) QueueIn(Q,p->lchild); //Ů
else {if (p->lchild) return 0; //ǰнΪգ㲻
else tag=1; //״γֽΪ
if (p->rchild && !tag) QueueIn(Q,p->rchild); //Ů
else if (p->rchild) return 0; else tag=1;
} //while
return 1; } //JudgeComplete

41 V1V2ÿ㣬ڽӵǷͬһУǣΪǶͼΪˣ12ʾϡһнṹͼзʵĶ㡣
int BPGraph (AdjMatrix g)
//жڽӾʾͼgǷǶͼ
{int s[]; //ԪֵʾǸϣֵ12ʾϣ
int Q[];//QΪУԪΪͼĶ㣬趥ϢǶš
int f=0,r,visited[]; //frֱǶеͷβָ,visited[]Ƿ
for (i=1;i<=n;i++) {visited[i]=0;s[i]=0;} //ʼδȷǸ
Q[1]=1; r=1; s[1]=1;//1뼯S1
while(f<r)
{v=Q[++f]; if (s[v]==1) jh=2; else jh=1;//׼vڽӵļϺ
if (!visited[v])
{visited[v]=1; //ȷÿһ㣬Ҫڽӵ㲻Ӧһ
for (j=1,j<=n;j++)
if (g[v][j]==1){if (!s[j]) {s[j]=jh; Q[++r]=j;} //ڽӵ
else if (s[j]==s[v]) return(0);} //Ƕͼ
}//if (!visited[v])
}//while
return(1); }//Ƕͼ
[㷨] Ŀͨͼͨ㷨Ҫ޸ġ

42֤ɶкͺУҲΨһȷһö
29. ҳĶ
1ͬ 2ͬ
3ͬ 4αͬ

43һʼ¼ؼУK1K2KnҪһ㷨ܹO(n)ʱ临ӶڽԱֳ֣벿ֵÿؼ־СKiҰ벿ֵÿؼ־ڵKi
void quickpass(int r[], int s, int t)
{
int i=s, j=t, x=r[s];
while(i<j){
while (i<j && r[j]>x) j=j-1; if (i<j) {r[i]=r[j];i=i+1;}
while (i<j && r[i]<x) i=i+1; if (i<j) {r[j]=r[i];j=j-1;}
}
r[i]=x;
}

44ԶijϵĽ㣬öнṹαˡ
int LeafKlevel(BiTree bt, int k) //bt ĵk(k>1) Ҷӽ
{if(bt==null || k<1) return(0);
BiTree p=bt,Q[]; //QǶУԪǶָ,㹻
int front=0,rear=1,leaf=0; //front rearǶͷͶβָ, leafҶӽ
int last=1,level=1; Q[1]=p; //lastǶͬҽָ룬level ǶIJ
while(front<=rear)
{p=Q[++front];
if(level==k && !p->lchild && !p->rchild) leaf++; //Ҷӽ
if(p->lchild) Q[++rear]=p->lchild; //Ů
if(p->rchild) Q[++rear]=p->rchild; //Ů
if(front==last) {level++; //ͬҽѴ,1
last=rear; } //lastƵָ²һԪ
if(level>k) return (leaf); //k ˳
}//while }//LeafKLevel

45һļ¼ؼΪ(131824354750628390)ҷöֲңҪҹؼ62ʱıȽϴҳɹʱ*ҳȡ
46IOֱʾջͳջջij̬̬ΪգջͳջIJпɱʾΪIOɵУƿԲΪϷУΪǷС15֣
1ADǺϷУBC ǷǷС
2豻жIJѴһάAС
int Judge(char A[])
//жַAеǷǺϷСǣtrue򷵻false
{i=0; //iΪ±ꡣ
j=k=0; //jkֱΪIĸOĵĸ
while(A[i]!=\0) //δַβ
{switch(A[i])
{caseI: j++; break; //ջ1
caseO: k++; if(k>j){printf(зǷ\n)exit(0);}
}
i++; //A[i]ǡIOָiơ}
if(j!=k) {printf(зǷ\n)return(false);}
else {printf(кϷ\n)return(true);}
}//㷨

47tǸһöĵݹcount(t):tозǿյ,ӵĽN2;ֻзǿӵĸNL;ֻзǿҶӵĽNRҶӽN0N2NLNRN0ȫڵcount(t)֮ǰΪ0.
typedef struct node
{int data; struct node *lchild,*rchild;}node;
int N2,NL,NR,N0;
void count(node *t)
{if (t->lchild!=NULL) if (1)___ N2++; else NL++;
else if (2)___ NR++; else (3)__ ;
if(t->lchild!=NULL)(4)____; if (t->rchild!=NULL) (5)____;
}
26.ǵݹ㷨
void example(b)
btree *b;
{ btree *stack[20], *p
int top;
if (b!=null)
{ top=1; stack[top]=b;
while (top>0)
{ p=stack[top]; top--;
printf(%d,p->data);
if (p->rchild!=null)
{(1)___; (2)___;
}
if (p->lchild!=null)
(3)___; (4)__;
}}}}

48һʼ¼ؼУK1K2KnҪһ㷨ܹO(n)ʱ临ӶڽԱֳ֣벿ֵÿؼ־СKiҰ벿ֵÿؼ־ڵKi
void quickpass(int r[], int s, int t)
{
int i=s, j=t, x=r[s];
while(i<j){
while (i<j && r[j]>x) j=j-1; if (i<j) {r[i]=r[j];i=i+1;}
while (i<j && r[i]<x) i=i+1; if (i<j) {r[j]=r[i];j=j-1;}
}
r[i]=x;
}

49nׯ֮Ľͨͼׯij֮е·򽫶ijñӣϵWijʾ·ijȣҪnׯѡһׯһҽԺҽԺӦĸׯʹҽԺԶĴׯҽԺ·?һ㷨Ӧø㷨ͼʾʵ20֣
50ɶǰȷΨһһöʵ֪ijǰУһöʾĶӡУдȱ䡣
#define MAX 100
typedef struct Node
{char info; struct Node *llink, *rlink; }TNODE;
char pred[MAX],inod[MAX];
main(int argc,int **argv)
{ TNODE *root;
if(argc<3) exit 0;
strcpy(pred,argv[1]); strcpy(inod,argv[2]);
root=restore(pred,inod,strlen(pred));
postorder(root);
}
TNODE *restore(char *ppos,char *ipos,int n)
{ TNODE *ptr char *rpos; int k;
if(n<=0) return NULL;
ptr->info=(1)_______;
for((2)_______ ; rpos<ipos+n;rpos++) if(*rpos==*ppos) break;
k=(3)_______;
ptr->llink=restore(ppos+1, (4)_______,k );
ptr->rlink=restore ((5)_______+k,rpos+1,n-1-k);
return ptr;
}
postorder(TNODE*ptr)
{ if(ptr=NULL) return;
postorder(ptr->llink); postorder(ptr->rlink); printf(%c,ptr->info);
}

51֤ɶкͺУҲΨһȷһö
29. ҳĶ
1ͬ 2ͬ
3ͬ 4αͬ

52һöĽṹΪ (LLINK,INFO,RLINK),ROOTΪָöָ룬pqֱΪָöָ룬Աдһ㷨ANCESTORROOTp,q,r,㷨ҵpq*ͬȽr
53֤ɶкͺУҲΨһȷһö
29. ҳĶ
1ͬ 2ͬ
3ͬ 4αͬ

54tǸһöĵݹcount(t):tозǿյ,ӵĽN2;ֻзǿӵĸNL;ֻзǿҶӵĽNRҶӽN0N2NLNRN0ȫڵcount(t)֮ǰΪ0.
typedef struct node
{int data; struct node *lchild,*rchild;}node;
int N2,NL,NR,N0;
void count(node *t)
{if (t->lchild!=NULL) if (1)___ N2++; else NL++;
else if (2)___ NR++; else (3)__ ;
if(t->lchild!=NULL)(4)____; if (t->rchild!=NULL) (5)____;
}
26.ǵݹ㷨
void example(b)
btree *b;
{ btree *stack[20], *p
int top;
if (b!=null)
{ top=1; stack[top]=b;
while (top>0)
{ p=stack[top]; top--;
printf(%d,p->data);
if (p->rchild!=null)
{(1)___; (2)___;
}
if (p->lchild!=null)
(3)___; (4)__;
}}}}

55#define maxsize ջռ

void InOutS(int s[maxsize])
//sԪΪջ㷨ջջ
{int top=0; //topΪջָ룬top=0ʱΪջա
for(i=1; i<=n; i++) //n
{scanf(%d,&x); //Ӽ̶С
if(x!=-1) // -1ʱջ
if(top==maxsize-1){printf(ջ\n);exit(0);}
else s[++top]=x; //xջ
else //-1ʱջ
{if(top==0){printf(ջ\n);exit(0);}
else printf(ջԪ%d\n,s[top--])}
}
}//㷨

56TһдһTתΪеĵݹ㷨
57ָpָ˫нAָqָ򱻲BҪڽAĺBIJУ˫нֱָΪllinkrlink
58ΪջбǰȵϢһջջָ룬ÿջʱջָڱջֵָʱ򽫸ջ븨ջУջʼձ·ϵĽ㣬ֱϣջݼΪ
void LongestPath(BiTree bt)//еĵһ·
{BiTree p=bt,l[],s[]; //l, sջԪǶָ룬lбǰ·еĽ
int itop=0,tag[],longest=0;
while(p || top>0)
{ while(p) {s[++top]=ptag[top]=0; p=p->Lc;} //֦
if(tag[top]==1) //ǰҷ֦ѱ
{if(!s[top]->Lc && !s[top]->Rc) //ֻеҶӽʱŲ鿴·
if(top>longest) {for(i=1;i<=top;i++) l[i]=s[i]; longest=top; top--;}
//ǰ·lջסջָ룬ջ
}
else if(top>0) {tag[top]=1; p=s[top].Rc;} //ӷ֦
}//while(p!=null||top>0)
}//LongestPath

59ͼbipartite graph G=VEһܽ㼯VΪཻӼV 1V2=V-V1ͼʹãV1еκͼGоڣV2еκνͼGҲڡ
1һΪ5ĶͼͷǶͼӡ
2CPASCALдһBIPARTITEжһͨͼGǷǶͼʱ临ӶȡGöάAʾСΪn*nnΪڳмӱҪע͡бҪֱöջв

60ͼbipartite graph G=VEһܽ㼯VΪཻӼV 1V2=V-V1ͼʹãV1еκͼGоڣV2еκνͼGҲڡ
1һΪ5ĶͼͷǶͼӡ
2CPASCALдһBIPARTITEжһͨͼGǷǶͼʱ临ӶȡGöάAʾСΪn*nnΪڳмӱҪע͡бҪֱöջв

61һдһĹؼK1K2KnҪKnڽԪȷλϣԱдʵָùܵ㷨ҪȽϹؼֵĴn
51. ڿ㷨˼룬һļ¼вҸؼֵkeyļ¼¼r[l..h]Сҳɹü¼rеλüֵʾnot findϢд㷨Ҫ˵㷨˼롣

62ǿáȦȨͨͼһСνȦǡȡһȦȥȦȨıߡִһ裬ֱûȦΪֹáȦĴȨͨͼһСϸ㷨óʵ㷨עȦǻ·
63ǿáȦȨͨͼһСνȦǡȡһȦȥȦȨıߡִһ裬ֱûȦΪֹáȦĴȨͨͼһСϸ㷨óʵ㷨עȦǻ·
64TһдһTתΪеĵݹ㷨
65һһ򡢺ȷһСһȵĽ㣬ݴʣɽһתΪһУһоȷһö
void PreToPost(ElemType pre[] ,post[],int l1,h1,l2,h2)
//תΪУl1,h1,l2,h2гʼ±ꡣ
{if(h1>=l1)
{post[h2]=pre[l1]; //
half=(h1-l1)/2; //Ľ
PreToPost(pre,post,l1+1,l1+half,l2,l2+half-1) //תΪ
PreToPost(pre,post,l1+half+1,h1,l2+half,h2-1) //תΪ
} }//PreToPost
32. .Ҷӽֻڱв֪ʹݹǰָpreʼΪաһҶӽָheadָ򣬱ҶӽʱͽǰrchildָָҶӽrchildΪա
LinkedList head,pre=null; //ȫֱ
LinkedList InOrder(BiTree bt)
//btҶӽһͷָΪhead
{if(bt){InOrder(bt->lchild); //
if(bt->lchild==null && bt->rchild==null) //Ҷӽ
if(pre==null) {head=bt; pre=bt;} //һҶӽ
else{pre->rchild=bt; pre=bt; } //Ҷӽ
InOrder(bt->rchild); //
pre->rchild=null; //β
}
return(head); } //InOrder
ʱ临ӶΪO(n),ʹheadpre,ջռ临ӶO(n)

66TһдһTתΪеĵݹ㷨
67֪ͼG=(V,E)V={V1,V2,V3,V4,V5,V6,V7}E={<V1,V2>,<V1,V3>,<V1,V4>,<V2,V5>,<V3,V5>,<V3,V6>,<V4,V6>,<V5,V7>,<V6,V7>}
дG*Ľ
G*ĽǣV1V2V4V3V5V6V7


68K1KnnؼʣԽ
öIJ㷨һöؼʵIJΪK1K2Knʱ㷨һLLINK / RLINK ӱʾĶ

69֤ɶкͺУҲΨһȷһö
29. ҳĶ
1ͬ 2ͬ
3ͬ 4αͬ

70l*̨ijȣkָʾ*̨bеʼλã±꣩jסֲ*̨ʼλãiָʾɨb±꣬i0ʼκͺԪرȽϣֲ*̨ȣi-jlʱ޸*̨ijkl=i-jbеʼλãk=jֱblΪ
void Platform (int b[ ], int N)
//NԪصb*̨ijȡ
{l=1;k=0;j=0;i=0;
while(i<n-1)
{while(i<n-1 && b[i]==b[i+1]) i++;
if(i-j+1>l) {l=i-j+1;k=j;} //ֲ*̨
i++; j=i; } //*̨
printf(*̨%dbʼ±Ϊ%dlk)
}// Platform

71ڽӾΪͼĴ洢ṹд㷨бڸͼǷһ·ڣԶеķʽû·ҵһɣעͼвڶ㵽ԼĻ
ͼжϻ·Ҫͼӡȱֳࣺδʣѷʵڽӵδ;ѷڽӵѷꡣ012ʾ״̬ǰᵽdfsvǰֶuvĻرߣͼбаvuĻ·Ӧv״̬Ϊ1uʵĶ㣬ҳuһڽӵ״̬Ϊ1Ϳ·ˡ
void Print(int v,int start ) //ӶstartʼĻ·
{for(i=1;i<=n;i++)
if(g[v][i]!=0 && visited[i]==1 ) //ڱߣv,iҶi״̬Ϊ1
{printf(%d,v);
if(i==start) printf(\n); else Print(i,start);break;}//if
}//Print
void dfs(int v)
{visited[v]=1;
for(j=1;j<=n;j++ )
if (g[v][j]!=0) //ڱ(v,j)
if (visited[j]!=1) {if (!visited[j]) dfs(j); }//if
else {cycle=1; Print(j,j);}
visited[v]=2;
}//dfs
void find_cycle() //жǷл·ڽӾvisitedΪȫֱ
{for (i=1;i<=n;i++) visited[i]=0;
for (i=1;i<=n;i++ ) if (!visited[i]) dfs(i);
}//find_cycle

72۰ҵĵݹ㷨㷨ʱ临ӶԷ
734 void LinkList_reverse(Linklist &L)
//ľ͵;Ϊ㷨,2
{
p=L->next;q=p->next;s=q->next;p->next=NULL;
while(s->next)
{
q->next=p;p=q;
q=s;s=s->next; //LԪ±ͷ
}
q->next=p;s->next=q;L->next=s;
}//LinkList_reverse

74һдһĹؼK1K2KnҪKnڽԪȷλϣԱдʵָùܵ㷨ҪȽϹؼֵĴn
51. ڿ㷨˼룬һļ¼вҸؼֵkeyļ¼¼r[l..h]Сҳɹü¼rеλüֵʾnot findϢд㷨Ҫ˵㷨˼롣

75֤ɶкͺУҲΨһȷһö
29. ҳĶ
1ͬ 2ͬ
3ͬ 4αͬ

76 V1V2ÿ㣬ڽӵǷͬһУǣΪǶͼΪˣ12ʾϡһнṹͼзʵĶ㡣
int BPGraph (AdjMatrix g)
//жڽӾʾͼgǷǶͼ
{int s[]; //ԪֵʾǸϣֵ12ʾϣ
int Q[];//QΪУԪΪͼĶ㣬趥ϢǶš
int f=0,r,visited[]; //frֱǶеͷβָ,visited[]Ƿ
for (i=1;i<=n;i++) {visited[i]=0;s[i]=0;} //ʼδȷǸ
Q[1]=1; r=1; s[1]=1;//1뼯S1
while(f<r)
{v=Q[++f]; if (s[v]==1) jh=2; else jh=1;//׼vڽӵļϺ
if (!visited[v])
{visited[v]=1; //ȷÿһ㣬Ҫڽӵ㲻Ӧһ
for (j=1,j<=n;j++)
if (g[v][j]==1){if (!s[j]) {s[j]=jh; Q[++r]=j;} //ڽӵ
else if (s[j]==s[v]) return(0);} //Ƕͼ
}//if (!visited[v])
}//while
return(1); }//Ƕͼ
[㷨] Ŀͨͼͨ㷨Ҫ޸ġ

77ڽӾΪͼĴ洢ṹд㷨бڸͼǷһ·ڣԶеķʽû·ҵһɣעͼвڶ㵽ԼĻ
ͼжϻ·Ҫͼӡȱֳࣺδʣѷʵڽӵδ;ѷڽӵѷꡣ012ʾ״̬ǰᵽdfsvǰֶuvĻرߣͼбаvuĻ·Ӧv״̬Ϊ1uʵĶ㣬ҳuһڽӵ״̬Ϊ1Ϳ·ˡ
void Print(int v,int start ) //ӶstartʼĻ·
{for(i=1;i<=n;i++)
if(g[v][i]!=0 && visited[i]==1 ) //ڱߣv,iҶi״̬Ϊ1
{printf(%d,v);
if(i==start) printf(\n); else Print(i,start);break;}//if
}//Print
void dfs(int v)
{visited[v]=1;
for(j=1;j<=n;j++ )
if (g[v][j]!=0) //ڱ(v,j)
if (visited[j]!=1) {if (!visited[j]) dfs(j); }//if
else {cycle=1; Print(j,j);}
visited[v]=2;
}//dfs
void find_cycle() //жǷл·ڽӾvisitedΪȫֱ
{for (i=1;i<=n;i++) visited[i]=0;
for (i=1;i<=n;i++ ) if (!visited[i]) dfs(i);
}//find_cycle

78ǿáȦȨͨͼһСνȦǡȡһȦȥȦȨıߡִһ裬ֱûȦΪֹáȦĴȨͨͼһСϸ㷨óʵ㷨עȦǻ·
79Ӧʹȱdfs(v)ʱ ʼ˳dfs()ǰѷͼȫ㣨ΪnͼиvΪ㡣n1nţһdfs()̣Ϳȫĸ㡣ͼڽӱ洢ṹǶıԼʱȾΪȫֱͼgڽӱ洢ṹμ2⣬ֻжͼǷи㷨
int num=0 visited[]=0 //numǷʶ,visitedʼ
const n=ûĶ;
AdjList g ; //ڽӱ洢ṹͼg
void dfs(v)
{visited [v]=1; num++; //ʵĶ1
if (num==n) {printf(%dͼĸ\n,v); num=0;}//if
p=g[v].firstarc;
while (p)
{if (visied[p->adjvex]==0) dfs (p->adjvex);
p=p->next;} //while
visited[v]=0; num--; //ָv
}//dfs
void JudgeRoot()
//жͼǷии֮
{static int i ;
for (i=1;i<=n;i++ ) //ÿdfs()һΡ
{num=0; visited[1..n]=0; dfs(i); }
}// JudgeRoot
㷨дӡʱڽӱеţ±꣩ҪϢʹg[i].vertex



80tǸһöĵݹcount(t):tозǿյ,ӵĽN2;ֻзǿӵĸNL;ֻзǿҶӵĽNRҶӽN0N2NLNRN0ȫڵcount(t)֮ǰΪ0.
typedef struct node
{int data; struct node *lchild,*rchild;}node;
int N2,NL,NR,N0;
void count(node *t)
{if (t->lchild!=NULL) if (1)___ N2++; else NL++;
else if (2)___ NR++; else (3)__ ;
if(t->lchild!=NULL)(4)____; if (t->rchild!=NULL) (5)____;
}
26.ǵݹ㷨
void example(b)
btree *b;
{ btree *stack[20], *p
int top;
if (b!=null)
{ top=1; stack[top]=b;
while (top>0)
{ p=stack[top]; top--;
printf(%d,p->data);
if (p->rchild!=null)
{(1)___; (2)___;
}
if (p->lchild!=null)
(3)___; (4)__;
}}}}

81ڽӾΪͼĴ洢ṹд㷨бڸͼǷһ·ڣԶеķʽû·ҵһɣעͼвڶ㵽ԼĻ
ͼжϻ·Ҫͼӡȱֳࣺδʣѷʵڽӵδ;ѷڽӵѷꡣ012ʾ״̬ǰᵽdfsvǰֶuvĻرߣͼбаvuĻ·Ӧv״̬Ϊ1uʵĶ㣬ҳuһڽӵ״̬Ϊ1Ϳ·ˡ
void Print(int v,int start ) //ӶstartʼĻ·
{for(i=1;i<=n;i++)
if(g[v][i]!=0 && visited[i]==1 ) //ڱߣv,iҶi״̬Ϊ1
{printf(%d,v);
if(i==start) printf(\n); else Print(i,start);break;}//if
}//Print
void dfs(int v)
{visited[v]=1;
for(j=1;j<=n;j++ )
if (g[v][j]!=0) //ڱ(v,j)
if (visited[j]!=1) {if (!visited[j]) dfs(j); }//if
else {cycle=1; Print(j,j);}
visited[v]=2;
}//dfs
void find_cycle() //жǷл·ڽӾvisitedΪȫֱ
{for (i=1;i<=n;i++) visited[i]=0;
for (i=1;i<=n;i++ ) if (!visited[i]) dfs(i);
}//find_cycle

82һдһĹؼK1K2KnҪKnڽԪȷλϣԱдʵָùܵ㷨ҪȽϹؼֵĴn
51. ڿ㷨˼룬һļ¼вҸؼֵkeyļ¼¼r[l..h]Сҳɹü¼rеλüֵʾnot findϢд㷨Ҫ˵㷨˼롣

83һʼ¼ؼУK1K2KnҪһ㷨ܹO(n)ʱ临ӶڽԱֳ֣벿ֵÿؼ־СKiҰ벿ֵÿؼ־ڵKi
void quickpass(int r[], int s, int t)
{
int i=s, j=t, x=r[s];
while(i<j){
while (i<j && r[j]>x) j=j-1; if (i<j) {r[i]=r[j];i=i+1;}
while (i<j && r[i]<x) i=i+1; if (i<j) {r[j]=r[i];j=j-1;}
}
r[i]=x;
}

84ͼGУrGеÿ㶼·ɴ*rΪGĸ㡣дһ㷨йܣ
1ͼGڽӱ洢ṹ
2жͼGǷиУӡиֵ

85һöĽṹΪ (LLINK,INFO,RLINK),ROOTΪָöָ룬pqֱΪָöָ룬Աдһ㷨ANCESTORROOTp,q,r,㷨ҵpq*ͬȽr
86 V1V2ÿ㣬ڽӵǷͬһУǣΪǶͼΪˣ12ʾϡһнṹͼзʵĶ㡣
int BPGraph (AdjMatrix g)
//жڽӾʾͼgǷǶͼ
{int s[]; //ԪֵʾǸϣֵ12ʾϣ
int Q[];//QΪУԪΪͼĶ㣬趥ϢǶš
int f=0,r,visited[]; //frֱǶеͷβָ,visited[]Ƿ
for (i=1;i<=n;i++) {visited[i]=0;s[i]=0;} //ʼδȷǸ
Q[1]=1; r=1; s[1]=1;//1뼯S1
while(f<r)
{v=Q[++f]; if (s[v]==1) jh=2; else jh=1;//׼vڽӵļϺ
if (!visited[v])
{visited[v]=1; //ȷÿһ㣬Ҫڽӵ㲻Ӧһ
for (j=1,j<=n;j++)
if (g[v][j]==1){if (!s[j]) {s[j]=jh; Q[++r]=j;} //ڽӵ
else if (s[j]==s[v]) return(0);} //Ƕͼ
}//if (!visited[v])
}//while
return(1); }//Ƕͼ
[㷨] Ŀͨͼͨ㷨Ҫ޸ġ

87IOֱʾջͳջջij̬̬ΪգջͳջIJпɱʾΪIOɵУƿԲΪϷУΪǷС15֣
1ADǺϷУBC ǷǷС
2豻жIJѴһάAС
int Judge(char A[])
//жַAеǷǺϷСǣtrue򷵻false
{i=0; //iΪ±ꡣ
j=k=0; //jkֱΪIĸOĵĸ
while(A[i]!=\0) //δַβ
{switch(A[i])
{caseI: j++; break; //ջ1
caseO: k++; if(k>j){printf(зǷ\n)exit(0);}
}
i++; //A[i]ǡIOָiơ}
if(j!=k) {printf(зǷ\n)return(false);}
else {printf(кϷ\n)return(true);}
}//㷨

88TһдһTתΪеĵݹ㷨
89K1KnnؼʣԽ
öIJ㷨һöؼʵIJΪK1K2Knʱ㷨һLLINK / RLINK ӱʾĶ

90һͷĵÿһinfoһָһָnext赥ѽ㷨ɾظֵĽ㣬ʹinfoȵĽֻһ
#include <stdio.h>
typedef char datatype;
typedef struct node{
datatype data;
struct node * next;
} listnode;
typedef listnode* linklist;
/*--------------------------------------------*/
/* ɾظĽ */
/*--------------------------------------------*/
linklist deletelist(linklist head)
{ listnode *p,*s,*q;
p=head->next;
while(p)
{s=p;
q=p->next;
while(q)
if(q->data==p->data)
{s->next=q->next;free(q);
q=s->next;}
else
{ s=q; /*PֵͬĽ*/
q=q->next;
}
p=p->next;
}
return head;
}

91дһбǷΪ㷨llink-rlink洢
92һһ򡢺ȷһСһȵĽ㣬ݴʣɽһתΪһУһоȷһö
void PreToPost(ElemType pre[] ,post[],int l1,h1,l2,h2)
//תΪУl1,h1,l2,h2гʼ±ꡣ
{if(h1>=l1)
{post[h2]=pre[l1]; //
half=(h1-l1)/2; //Ľ
PreToPost(pre,post,l1+1,l1+half,l2,l2+half-1) //תΪ
PreToPost(pre,post,l1+half+1,h1,l2+half,h2-1) //תΪ
} }//PreToPost
32. .Ҷӽֻڱв֪ʹݹǰָpreʼΪաһҶӽָheadָ򣬱ҶӽʱͽǰrchildָָҶӽrchildΪա
LinkedList head,pre=null; //ȫֱ
LinkedList InOrder(BiTree bt)
//btҶӽһͷָΪhead
{if(bt){InOrder(bt->lchild); //
if(bt->lchild==null && bt->rchild==null) //Ҷӽ
if(pre==null) {head=bt; pre=bt;} //һҶӽ
else{pre->rchild=bt; pre=bt; } //Ҷӽ
InOrder(bt->rchild); //
pre->rchild=null; //β
}
return(head); } //InOrder
ʱ临ӶΪO(n),ʹheadpre,ջռ临ӶO(n)

93ɶǰȷΨһһöʵ֪ijǰУһöʾĶӡУдȱ䡣
#define MAX 100
typedef struct Node
{char info; struct Node *llink, *rlink; }TNODE;
char pred[MAX],inod[MAX];
main(int argc,int **argv)
{ TNODE *root;
if(argc<3) exit 0;
strcpy(pred,argv[1]); strcpy(inod,argv[2]);
root=restore(pred,inod,strlen(pred));
postorder(root);
}
TNODE *restore(char *ppos,char *ipos,int n)
{ TNODE *ptr char *rpos; int k;
if(n<=0) return NULL;
ptr->info=(1)_______;
for((2)_______ ; rpos<ipos+n;rpos++) if(*rpos==*ppos) break;
k=(3)_______;
ptr->llink=restore(ppos+1, (4)_______,k );
ptr->rlink=restore ((5)_______+k,rpos+1,n-1-k);
return ptr;
}
postorder(TNODE*ptr)
{ if(ptr=NULL) return;
postorder(ptr->llink); postorder(ptr->rlink); printf(%c,ptr->info);
}

94nׯ֮Ľͨͼׯij֮е·򽫶ijñӣϵWijʾ·ijȣҪnׯѡһׯһҽԺҽԺӦĸׯʹҽԺԶĴׯҽԺ·?һ㷨Ӧø㷨ͼʾʵ20
void Hospital(AdjMatrix w,int n)
//ڽӴȨʾnׯУҽԺںδʹҽԺԶĴׯҽԺ·̡
{for (k=1;k<=n;k++) //·
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if (w[i][k]+w[k][j]<w[i][j]) w[i][j]=w[i][k]+w[k][j];
m=MAXINT; //趨mΪ
for (i=1;i<=n;i++) //·̵һ
{s=0;
for (j=1;j<=n;j++) //ijׯi1<=i<=nׯ·
if (w[i][j]>s) s=w[i][j];
if (s<=m) {m=s; k=i;}//·Уȡ̵һm·kdz±ꡣ
Printf(ҽԺӦ%dׯҽԺΪ%d\n,i,m);
}//for
}//㷨
ʵģĹԡ996799⼸СΪ6ҽԺӦڵׯ,ҽԺԶĴׯҽԺľ6

1ͼ1ʾͨGPrim㷨Сÿѡȡһ߻һͼ

95AͼBҪɼC=AB㷨мABCʽ洢ṹʾ
typedef struct node {int data; struct node *next;}lklist;
void intersection(lklist *ha,lklist *hb,lklist *&hc)
{
lklist *p,*q,*t;
for(p=ha,hc=0;p!=0;p=p->next)
{ for(q=hb;q!=0;q=q->next) if (q->data==p->data) break;
if(q!=0){ t=(lklist *)malloc(sizeof(lklist)); t->data=p->data;t->next=hc; hc=t;}
}
}

96һöĽṹΪ (LLINK,INFO,RLINK),ROOTΪָöָ룬pqֱΪָöָ룬Աдһ㷨ANCESTORROOTp,q,r,㷨ҵpq*ͬȽr



ӣ ʱ ܽ㱨 ׶ Сѧ ѧϰ