Download - 数 据 结 构
-
1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4
-
1.1 1 N (a1b1)(a2b2)(anbn)aibi(i=12n)
-
N(aibi) 1in
-
234 P3
-
1.2 (Data):(Data Element): (Data Object)(Data Structure)
-
Data-Structure=(DS)DSD Complex=(CR)CC1C2R={P}P{C1C2}
-
:
-
1 FORTRAN 2C3{-3-2-10123}{ABCDEF}
-
1.3 P11
-
1.4 1 2 3
-
4 5 1.4.2 :(1) (Correctness ) (2)(Readability) (3)(Robustness)
-
(4) 1.4.3 cn0nn0f(n) cg(n) f(n)=O(g(n))
- n T(n)=O(f(n))for(I=1I
- 1n: nnn=n3T(n)=O(n3) {++x;s=0;}x(1)s=0(1)for(I=1;I
- for(I=1;I
-
1+2+3++n-2=(1+n-2) (n-2)/2 =(n-1)(n-2)/2 =n2-3n+2 O(n2) . O(1)O(n2)
- O(1)
-
Void bubble-sort(int a[]int n) for(I=n-1;change=TURE;I>1 && change;--I) { change=false; for(j=0;ja[j+1]) { a[j] a[j+1]; change=TURE} } 0
-
1+2+3++n-1 =n(n-1)/2 :O(n2)
1.4.4:: S(n)=O(f(n)) n()
-
2.1 2.2 2.3 2.3.1 2.3.2 2.3.3 2.4
-
2.1 (Linear List) n(n)()a1a2 annn=0(n>0) (a1a2an) ai(1in)126 ABCZ219781983 617285092188
-
3
-
4 234JQKA a1a2ana n-1ai(2in-1)a i-1a i+1 P19
- 2.12-1 LALBABA=AB void union(List &LaList Lb) { La-len=listlength(La); Lb-len=listlength(Lb); for(I=1;I
-
2.22-2 LALBLALBLCLC
- void mergelist(list lalist lblist &lc) initlist(lc); I=j=1;k=0; la-len=listlength(la); lb-len=listlength(lb); while((I
- getelem(laIai);getelem(lbjbj); if(ai
-
2.2 2.2.1 lI+1LOC( a i+1)iLOC(a I ) LOC(a i+1)=LOC(a i)+l iai LOC(ai)=LOC(a1)+(I-1)*l
-
C # define ListSize 100 typedef int DataType; typedef struc{ DataType data[ListSize]; int length; } Sqlist;
-
2.2.2 i C0LSqlistil.data[I-1] 1 I(1in+1x
-
n (a1a i-1aian) n+1 (a1a i-1xaian) 2.3Void InsertList(Sqlist*LDataType xint I) { int j; if(Il.length+1) printf(Position error); return ERROR
-
if(l.length>=ListSize) printf(overflow); exit(overflow); for(j=l.length-1;j>=I-1;j--) l.data[j+1]=l.data[j]; l.data[I-1]=x; l.length++;}
-
O1=1n
-
On niEis(n)in-I+1 Eis(n)= pi(n-I+1) (1in+1) p1=p2=p3==p n+1=1/(n+1) Eis(n)= (n-I+1)/(n+1)=n/2
-
nEis(n)nO(n)
2 i(1in)n (a1a i-1aia i+1an) n-1 (a1a i-1a i+1an)
- Void deleteList(Sqlist*Lint I) { int j; if(Il.length) printf(Position error); return ERROR for(j=i;j
-
ni I=n I=1n-1O(1)O(n) nEde(n)in-i Ede(n)= pi(n-I)pii
-
p1=p2=p3==pn=1/n Ede(n)= (n-I)/n=(n-1)/2 O(n)
2.3
-
.(Linked List)2.3.1 (pointer)(link)
-
datanextnSingle Linked) nexthead null^) 1:(batcateatfathatjatlatmat)
datalink
-
110 130 135 160 head 165 170 200 205
hat200.cat135eat170.matNullbat130fat110jat205lat160
165
-
headbat cat eat mat ^ headheadCTypedef char datatype;
Typedef struct node{ datatype data; struct node *next;}listnode;
-
typedef listnode *linklist; listnode *p; linklist head;P p=(listnode*)malloc(sizeof(listnode));malloclistnodepp free(p)
-
P*P
-
\n 1
-
linklist createlistf(void) { char ch; linklist head; listnode *p; head=null; ch=getchar( ); while (ch!=\n{ p=(listnode*)malloc(sizeof(listnode)); p>data=ch; p>next=head;
-
head=p; ch=getchar( ); } return (head); }
-
listlink createlist(int n) { int data; linklist head; listnode *p head=null; for(i=n;i>0;--i){ p=(listnode*)malloc(sizeof(listnode)); scanf((%d&p>data); p>next=head; head=p; }
-
return(head); }2 r
-
linklist creater( ) { char ch; linklist head; listnode *p*r; //( *head;) head=NULL;r=NULL; while((ch=getchar( )!=\n){ p=(listnode *)malloc(sizeof(listnode)); p>data=ch; if(head=NULL) head=p; else
-
r>next=p; r=p; } if (r!=NULL) r>next=NULL; return(head); }
-
ififheadr*rhead*r a
-
b linklist createlistr1( ){ char ch; linklist head=(linklist)malloc(sizeof(listnode)); listnode *p*r
-
r=head; while((ch=getchar( ))!=\n{ p=(listnode*)malloc(sizeof(listnode)); p>data=ch; p>next=p; r=p; } r>next=NULL; return(head); }
-
p=(listnode*)malloc(sizeof(listnode)) if(p==NULL) error(No space for node can be obtained); return ERROR; O(n)
-
1 iinexti ni1ini0
-
Listnode * getnode(linklist head int i) { int j; listnode * p; p=head;j=0; while(p>next && jnext; j++; } if (i==j) return p;
-
else return NULL; }
2 keykeyNULLkey
-
Listnode * locatenode(linklist headint key) { listnode * p=head>next; while( p && p>data!=key) p=p>next; return p; } keyO(n)
-
xiai-1aiai-1px*p*paiai-1xai
-
void insertnode(linklist headdatetype xint i) { listnode * p*q; p=getnode(headi-1); if(p==NULL) error(position error); q=(listnode *)malloc(sizeof(listnode)); q>data=x; q>next=pnext; p>next=q; }
-
n1in+1i=1getnode i=n+1getnodeani-1getnodegetnodeO(n)
iaia a i-1next
-
a i-1pp>nextaiaiai
-
void deletelist(linklist head int i) { listnode * p *r; p=getnode(headi-1); if(p= =NULL || p>next= =NULL) return ERROR; r=p>next; p>next=r>next; free( r ) ; }
-
ni1ini=n+1*p*pp!=NULL*p p>next!=NULL O(n)
-
2.3.2 NULL
-
a1 an .head a1O(1)anO(n)
-
.reara1an(rear>next) >nextrearO(1) NULLpp>next
-
(a1a2a3an)(b1b2b3bn) linklist connect(linklist headalinklist headb) { linklist p=heada>next; heada>next=(headbnext)>next free(headb>next); headb>next=p; return(headb); }
-
2.3.3 (Double linked list):prior typedef struct dlistnode{ datatype data; struc dlistnode *prior*next; }dlistnode; typedef dlistnode * dlinklist; dlinklist head;
-
p (p>prior)>next=p=(p>next)>prior *p*(p>prior) *(p>next)
-
void dinsertbefor(dlistnode *pdatatype x) { dlistnode *q=malloc(sizeof(dlistnode)); q>data=x; q>prior=p>prior; q>next=p; p>prior>next=q; p>prior=q; }
-
void ddeletenode(dlistnode *p) { p>prior>next=p>next; p>next>prior=p>prior; free(p); } O(1)
-
3.1 3.1.1 3.1.2 3.2 3.2.1 3.2.2 3.2.4 3.2.5 3.2.5
-
3.1.1 3.1.1 (Stack)(Top)(Bottom) S=(a1a2a3an)a1ana1a2a3anLIFO
-
3.1.2 top
-
P44
a n a n-1
a2 a1
-
top7 6 5 4 3 2 1 -1
-
toptop # define StackSize 100 typedef char datatype; typedef struct { datatype data[stacksize]; int top; }seqstack;
-
SSeqStacks>data[0]s>tops>top1s>top 1s>toptop =stacksize-1;
-
3 int stackfull(seqstack *s) { return(s>top==stacksize-1); } 4 void push(seqstack *sdatatype x) { if (stackfull(s)) error(stack overflow); s>data[++s>top]=x; }
-
1 void initstack(seqstack *s) { s>top=-1; }2 int stackempty(seqstack *s) { return(s>top==-1); }
-
5 datatype pop(seqstack *s) { if(stackempty(s)) error(stack underflow); x=s>data[top]; s>top--; return(x) //return(s>data[s>top--]); }
-
6 Datatype stacktop(seqstack *s) { if(stackempty(s) error(stack is enpty); return s>data[s>top]; }
-
3.1.3 . typedef struct stacknode{ datatype data struct stacknode *next }stacknode;
-
Void initstack(seqstack *p) { p>top=null; } int stackempty(linkstack *p) { return p>top==null; }
-
Void push(linkstack *p,datatype x) { stacknode *q q=(stacknode*)malloc(sizeof(stacknode)); q>data=x; q>next=p>top; p>top=p; }
-
Datatype pop(linkstack *p){ datatype x; stacknode *q=p>top; if(stackempty(p) error(stack underflow.); x=q>data; p>top=q>next; free(q); return x; }
-
datatype stack top(linkstack *p) { if(stackempty(p)) error(stack is empty.); return p>top>data; }
-
3.2 3.2.1 N,,: N=(n div d)*d+n mod d ( :div,mod) (1348)10=(2504)8
-
n n div 8 n mod 8 1348 168 4 168 21 0 21 2 5 2 0 2
-
void conversion( ) { initstack(s); scanf (%,n); while(n){ push(s,n%8); n=n/8; } while(! Stackempty(s)){ pop(s,e); printf(%d,e); } }
-
3.2.2 , 3.2.3
-
: void lineedit( ){ initstack(s); ch=gether( ); while(ch!=eof){ while(ch!=eof && ch!=\n){ switch(ch){ case # : pop(s,c); case @ : clearstack(s); default : push(s,ch); }
-
ch=getchar( ); } clearstack(s); if(ch!=eof) ch=gethar( ); } destroystack(s); }
-
3.2.4
-
3.4 3.4.1 (Queue)(front)(rear)(First In First Out)FIFOa1,a2,ana1ana1,a2,an
-
a1a2an593.4.2
-
0 1 2 3FrontrearFront rear (a)bA,B,C
abc
-
front rear front rear c) a (d) b,c
b c
-
Circular Queue)1QueueSize-1101 if(I+1==QueueSize) i=0; else i++; i=(i+1)%QueueSize
-
front=rear 1rear
-
#define QueueSize 100 typedef char DataType; typedef Struct{ int front; int rear; int count; datatype data[queuesize] }cirqueue;
-
1 void initqueue(cirqueue *q){ q>front=q>rear=0; q>count=0; }2 int queueempty(cirqueue *q) { return q>count==0; }3 int queuefull(cirqueue *q){ return q>count==queuesize; }
-
4 void enqueue(cirqueue *q,datatype x) { if(queuefull(q)) error(queue overflow); q>count++; qdata[qrear]=x; qrear=(qrear+1)%queuesize; }
-
5 datatype dequeue(cirqueue *q) { datatype temp; if(queueempty(q)) error(queue underflow); temp=q>data[q>front]; q>count--; qfront=(q>front+1)%queuesize; return temp; }
-
6 datatype queuefront(cirqueue *q) { if(queueempty(q)) error(queue is empty.); return q>data[q>front]; }
-
3.4.3 LinkQueue typedef struct queuenode{ datatype data; struct queuenode *next; }queuenode;
-
typedef struct{ queuenode *front; queuenode *rear; }linkqueue;
void initqueue(linkqueue *q) { q>front=q>rear=null; }
-
int queueempty(linkqueue *q) { return q>front==null &&q>rear==null; } void enqueue(linkqueue *q,datatype x) { queuenode *p p=(queuenode * )malloc(sizeof(queuenode)); p>data=x; pnext=null; if(queueempty(q)) q>front=q>rear=p;
-
else{ q>rear>next=p; qrear=p; } }
Datatype dequeue(linkqueue *q) { datatype x; queuenode *p
-
if(queueempty(q)) error(queue underflow); p=q>front; x=p>data; q>front=p>next; if(q>rear==p) qrear=null; free(p); return x; }
-
datatype queuefront(linkqueue *q) { if(queueempty(q)) error(queue is empty.); return q>front>data; }
-
12341push(1)pop()push(2push(3)pop()pop( )push(4)pop( )2423432312342423
-
4n5sint stacksize(seqstack s)s6S7
- 8rearquelen9 (1) void demo1(seqstack *s){ int I;arr[64];n=0; while (!stackempty(s)) arr[n++]=pos(s); for(I=0;
-
while(! Stackempty(s)) if(I=pop(s)!=m) push(t,I);While(! Stackempty(t)) { i=pop(t); push(s,I);}}
-
4.1 4.2 4.2.1 4.2.2 4.2.3
-
4.1 (String)S=a1a2a3anS ai(1in)(Empty String) (Blank String) 10
-
AB A=This is a string B=isBAABA3BA3
-
Error(overflow)overflowC++ const char path[]=dir/bin/appl;path P71
-
C char s1[20]=dirtreeformat,s2[20]=file.mem; char s3[30],*p; int result;(length) int strlen(char s); // printf(%d,strlen(s1)); 13
-
2(copy) char *strcpy(char to,char from); fromtoto strcpy(s3,s1); //s3=dirtreeformat(3)(concatenation) char strcat(char to,char from) fromtoto strcat(s3,/) strcat(s3,s2); //s3=dirtreeformat/file.mem
- (4)compare) int strcmp(chars1,char s2); s1s2000s1s2 result=strcmp(baker,Baker) result>0 result=strcmp(12,12); result=0 result=strcmp(Joe,Joseph); result
-
strncpystrncatstrncmpstrnchr
1 SposlenT void substr(string sub,string s,int pos,int len) { if(posstrlen(s)-1 || len
-
2index(s,t,pos) iTTi1ST
- int index(string s,string t,int pos){ if(pos>0){ n=strlen(s); m=strlen(t); i=pos; while(i
-
4.2 4.2.1 , #define maxstrlen 256 typedef char sstring[maxstrlen]; sstring s; //s255
-
C\0maxstrlen256255\01 typedef struct{ char ch[maxstrlen]; int length; }sstring; //
-
4.2.2 C typedef char *string; //c typedef struct{ char *ch; int length; }hsring;
-
status sinsert(hstring s,int pos,hstring t){ if(poss.length+1) return error; if(t.length){if(!(s.ch=(char*)realloc(s.ch,(s.length+t.length)*sizeof(char))) exit(overflow); for(i=s.length-1;i>pos-1;--i) s.ch[I+t.length]=s.ch[i]; s.ch[pos-1..pos+t.length-2]=t.ch[0..t.length-1];
-
s.length+=t.length; } return ok; }
-
int strlen(hstring s){ return s.length; }
status clearstring(hstring s){ if(s.ch){free(s.ch);s.ch=NULL;} s.length=0; }
-
status strassign(hstring t,char *chars){ //charst if(t.ch) free(t.ch); for(i=0,(c=chars;c;++i),++c); if(!i) { t.ch=null; t.length=0; } else{ if(!(t.ch=(char *)malloc(I*sizeof(char)))) exit(overflow);
-
t.ch[0..i-1]=chars[0..i-1]; t.length=i; } }
- int strcmp(hstring s,hstring t){ for(i=0;i
-
status concat(hstring t,hstring s1,hstring s2){ if(!(t.ch)=(char*)malloc(s1.length+s2.length)*sizeof(char))) exit(overflow); t.ch[0..s1.length-1]=s1.ch[0..s1.length-1]; t.length=s1.length+s2.length; t.ch[s1.length..t.length-1]=s2.ch[0..s2.length-1]; }
-
Status substr(hstring sub,hstring s,int pos,int len){ if(poss.length || lens.length-pos+1) return error; if(sub.ch) free(sub.ch); if(!len){ sub.ch=null; sub.length=0; }
-
else{ sub.ch=(char *)malloc(len*sizeof(char)); sub.ch[0..len-1]=s[pos-1..pos+len-2]; s.length=len; } }
-
4.2.3 typedef struct node{ char data; struct node *next; }lstring;
-
1 1 #define nodesize 80 typedef struct node{ char data[nodesize]; struct node *next; }lstring;
-
4.3 (Pattern Matching)(String Matching) ST S=s0s1s2sn-1 T=t0t1tm-1 0in-ms[i..i+m-1]t[0..m-1]s[i..i+m-1]=t[0..m-1]
-
itss[i..i+m-1] t[0..m-1]iis[i..i+m-1]=t[0..m-1]is[i..i+m-1] t[0..m-1]iTT n-m+1i(0in-m)
- for(i=0;i
- while(j
-
1 lstring *lindex(lstring s,lstring t){ lstring *shift,*q,*p; shift=S; q=shift;p=t;
-
while(q && p){ if(q->data==p->data){ q=q->next; p=p->next; } else{ shift=shift->next; q=shift; p=t; } }
-
if(p==null) return shift; else return null; }
-
5.1 5.2 5.3 5.3.1 5.3.2 5.4 5.5
-
5.1 a11 a12 a1n a21 a22 a2n am1 am2 amn Amn=
-
C typedef elemtype array2[m][n]; typedef elemtype array1[n]; typedef array1 array2[m];
-
5.2
-
i+1i a11,a12,,a1n,a21,a22,a2n,,am1,am2,,amn PASCALCj+1jAm*na11,a21,,am1,a12,a22,am2,,an1,an2,,anmFORTRAN
-
Amnd aijaijaijiji-1(i-1) niaijj-1(i-1) n+j-1aij LOC(aij)=LOC(a11)+[(i-1)*n+j-1]*dAijkLOC(aijk)=LOC(a111)+[(i-1)*n*p+(j-1)*p +(k-1)]*d
-
1A[c1..d1,c2..d2]c1,c21aiji-c1d2-c2+1i-c1(i-c1)*(d2-c2+1)iaijj-c2aij LOC(aij)=LOC(ac1c2)+[(i-c1)*(d2-c2+1)+j-c2)]*d C0C LOC(aij)=LOC(a00)+(i*(d2+1)+j)*d
-
5.3 11
-
5.3.1 1 nA aij=aji 0i,jn-1A5.15
-
1 5 1 3 7 a00 5 0 8 0 0 a10 a 11 1 8 9 2 6 a20 a21 a23 3 0 2 5 1 .. 7 0 6 1 3 an-1 0 a n-1 1 a n-1 2 a n-1 n-1 5.1 ii+1 (i+1)=n(n+1)/2 sa[0..n(n+1)/2-1]Aaijsa[k]
- ijai j ai ji0i-11+2++i=i(i+1)/2i ai jjai0,ai1,ai2,,aij-1 k=i*(i+1)/2+j 0k
-
aij LOC(aij)=LOC(sa[k]) =LOC(sa[0])+k*d=LOC(sa[0]+[I*(I+1)/2+J]*d (ij)sa[k]aijk=0,1,2,n(n-1)/2-1sa[k](i,j)sa[n(n+1)/2]A
k=0 1 2 3 n(n-1)/2 n(n-1)/2-1a21a12 sa[4] k=I*(I+1)/2+J=2*(2+1)/2+1=4
a00a10a11a20an-1 0 an-1,n-1
-
2 a00 a01 a 0 n-1 a00 c c c a11 a 1 n-1 a10 a11 c .. .. c c a n-1 n-1 an-1 0 an-1 1 an-1 n-1 (a) (b) 5.2
-
cn(n+1)/2sa[0..n(n+1)/2]c p(0pj
k=
-
sa[k]aij i(i+1)/2+j ij n(n+1)/2 i>j 3 a00 a01 a10 a11 a12 a21 a22 a23 . .. . 5.3 an-2 n-3 an-2 n-2 an-2 n-1 an-1 n-2 an-1 n-1k=
-
(aii,0in-1(aii+1,0in-2)(ai+1 i,0in-2)i-j>1aij=0k(k)Ai-j>(k-1)/2 aij=0
- i=0j=01i=n-1j=n-2n-11
-
LOC(i,j)=LOC(0,0)+[3*i-1+(j-i+1)]*d =LOC(0,0)+(2i+j)*d a34sa[10] k=2*i+j=2*3+4=10 a21sa[5] k=2*2+1=5sa[0..3*n-2]A
-
5.3.2 AsssmnA
-
As e=s/(m*n)ee0.05i,j)(i,j,aij)A
-
((1,2,12)(1,3,9),(3,1,- 3),(3,6,14),(4,3,24), (5,2,18),(6,1,15),(6,4,-7)) (6,7)M 0 12 9 0 0 0 0 0 0 -3 0 0 15 0 0 0 0 0 0 0 12 0 0 0 18 0 -3 0 0 0 0 14 0 9 0 0 24 0 0 0 0 24 0 0 0 0 0 0 0 0 0 7 0 18 0 0 0 0 0 0 0 14 0 0 0 15 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5.4 MTM=T=
-
#define maxsize 10000 typedef int datatype; typedef struct{ int i,j; datatype v; }triple;
-
typedef struct{ triple data[maxsize]; int m,n,t; }tripletable; Atripletable5.4 i j v 1 2 12 1 3 9 3 1 -3 3 6 14 4 3 24 5 2 18 6 1 15 6 4 -7
-
mnABnma[i][j]=b[j][i]0im0jnABAB ABAa.dataBb.dataa.dataijb.dataBb.data ABa.dataBb.data
-
A col(0coln-1)a.datacolb.dataB i j v 1 3 -3 1 6 15 2 1 12 2 5 18 3 1 9 3 4 24 4 6 -7 6 3 14
- Void transmatrix(tripletable a,tripletable b) { int p q col; b.m=a.n; b.n=a.m; b.t=a.t; if(b.t
- for(col=1;col
- pcolO(n*t) for(col=0;col
- t
-
MBMMB num[0..n]cpot[0..n]num[0..n]Mnum[col]A
- cpot[0..n]MB cpot cpot[1]=1 cpot[col]=cpot[col-1]+num[col-1] 2
-
A i j v numcpotq=cpot[col]pp
1 2 v
q
2 1 v
- void fasttranstri(tritupletable b,tritupletable a){ int p,q,col,k; int num[0..a.n],copt[0..a.n]; b.m=a.n; b.n=a.m; b.t=a.t; if(b.t
- cpot[0]=1; for(col=2;col
-
#define maxrow 100 typedef struct{ triple data[maxsize]; int rpos[maxrow]; int n,m,t; }rtripletable
- Q=M*N Mm1*n1Nm2*n2n1=m2 for(i=1;i
-
MNMN 0 0 50 -1 0 02 0 0 0M= 0 2 1 0-2 4 0 0N=Q=M*N 0 6-1 0 0 4Q=
-
i j v i j v i j v 1 1 3 1 2 2 1 2 6 1 4 5 2 1 1 2 1 -1 3 2 -1 3 1 -2 3 2 4 3 1 2 3 2 4 q.data m.data n.data
-
MMN QM
- void multsmatrix( rtripletable a, rtripletable b, rtripletable c){ if(a.n!=b.m){ printf(error\n); exit(0); } c.m=a.m; c.n=b.n; c.t=0; if(a.t*b.t!=0){ for(arow=1;arow
- c.rpos[arow]=c.t+1; for(p=a.rops[arow];p
-
for(ccol=1;ccolmaxsize) exit(0); c.data[c.t]={arow,ccol,ctemp[ccol]}; } } } }
-
5.4 Lists2n>=0a1,a2,a3,,an n(n>=0)a1,a2,a3,,anaiLS=a1,a2,a3,,an)LSnaiLS
-
6.1 6.2 6.2.1 6.2.2 6.2.3 6.3 6.3.1 6.3.2 6.4 6.4.1 6.4.2
-
LSn>=1)a1LS(a1,a2,an)LS 1A=A2B=eBeB13C=a,(b,c,d))C2 a(b,c,d)4D=ABCD3
-
D=(( ),(e),(a,(b,c,d)))5E=E2EE=(a,(a,(a,(a,)))). 1P10824ABCDD3
-
gethead(B)=e gettail(B)=( ) gethead(D)=A gettail(D)=(B,C) BC gethead(B,C)=B gettail(B,C)=(C) ( ( ) )01
-
5.5 (a1,a2,a3,an) 1
tag=1 hp tp
tag=0 atom
-
typedef enum{ATOM,LIST}elemtag; typedeg struct glnode{ elemtag tag; union{ atomtype atom; struct { struct glnode *hp,*tp; }ptr; }; } *glist;P109
-
2
tag=1hptp typedef enum{atom,list}elemtag;Typedef struct glnode{ elemtag tag; union{ atomtype atom; struct glnode *hp; };
tag=0atomtp
-
struct glnode *tp; } *glist; P110
-
6.1 6.2 6.2.1 6.2.2 6.2.3 6.3 6.3.1 6.3.2 6.4 6.4.1 6.4.2
-
6.4.36.6 6.6.1 6.6.2
-
6.1 (Tree)n(n>=0)TT 1(Root)
-
(2)m(m>=0)T1,T2,T3Tm(Subtree)
-
6.2 6.2.1 n(n>=0)
-
6.856.8(C) 6.8d
(a)AABABACB (b) (c)(d) (e)6.8 5
- 6.2.2 1 i2i-1(i>=1) i=12i-1=20 =1, j1
-
2k2k1k>=1).k1 EkI=1i= EkI=12i-1=2k 1 3 n02n2n0n211n1N2Nn0n1n2 (6-1)B NB1
-
12 Bn1+2*n2 NB1n12n21 62)6162 n0+n1+n2=n1+2*n2+1 n0n21 k2k-16.9
-
knk1n
24536716.9
-
12345612345712367(a)(b)( c)6.10 6..10bc2
- 1)kk1 211l1 4n[log2n]1xx k22k-11
-
5 n1log2n+1),i1ni2i 32i1>ni2i1
-
[I/2]iI+12i2i+12(I+1)2i+3I+12(I+1)2i+3i2i2i+16.11 Ii+1(a)Ii+1 (b)Ii+16.11
- 23123 i122>n2ii333>nii>1 1j1
- j12i12i+1>n 2j1
-
6.2.3 1. #define max-tree-size 100Typedef telemtype sqbitree[max-tree-size];Sqbitree bt HH2h-1
-
1 2 3 4 5 6 7 8 9 10 11 12abcdefghijkl
ABCDEFGHIJKL
-
A
B
C
D
E
F
G
ABCDEFG
-
Status CreateBiTree(BiTree *T) { scanf(&ch); if(ch= =") T=NULL; else{ if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) exit(OVERFLOW); T>data=ch; CreateBiTree(T>lchild); CreateBiTree(T>rchildd); } return OK; }
-
2
A
^B
C^
D
^E
^F^
^G^
^H^
lchildDatarchild
-
Typedef struct BiTNode { TelemType data; struct BiTNode *lchild,*rchild;} BiTNode,*BiTree;,Data ,lchild,rchild ,;
-
6.3 6.3.1
bca()()()
-
LDRDLRLDRLRDDRLRDLRLD DLR LDR LRD1123212
-
33123
-
1(a+b*(c-d)-e/f),,-+a*b-cd/ef ,a+b*c-d-e/f
,abcd-*+ef/-,, 1
*a/b-dcfe
-
TREENODE *creat_tree(){ TREENODE *t; char c; c=getchar(); if(c= =#) return(NULL); else{ t=(TREENODE *)malloc(sizeof(TREENODE)) t >data=c; t >lchild=create_tree(); t >rchild=create tree(); } return(t); }
-
: #include#include#define NULL 0Typedef struct node{ char data; struct node *lchild,*rchild; }TREENODE;TREENODE *root;TREENODE *creat_tree();Void inorder(TREENODE *);Void inorder(TREENODE *p) { if(p!=NULL)
-
{ inorder(p>lchild); printf(%c,p>data) inorder(p>rchild); }}
-
3
P127
lchilddataparentrchild
-
,,,,,;
: 0 lchild ltag={ 1 lchild 0 rchild rtag={ 1 rchild ,,.
lchildltagdatartagrchild
-
: Typedef enum{Link,Thread}PointerTag; Link= =0:,Thread= =1:Typedef struct BiThrNode{ TelemType data; struct BiTreeNode *lchild,*rchild; PointerTag LTag, Rtag; }BiTreeNode,*BiThrTree; ,,lchild,rchild;,lchild rchild;,,.
-
Status InorderTraverse_Thr(BiThrTree T,status(*visit)(TElemType)){ //T,lchild //, //Visit P=T>lchild; while(p!=T){ while(p >LTag = =Link) p=p >lchild; if(!visit(p >data)) return error; while(p >RTag = =Thread&&p >rchild!=T) { p=p >rchild; Visit(p >data); } p= p>rchild; } return OK;}//InorderTraverse_Thr
-
P134 : Status InorderThreading(BiThrTree &Thrt,BiThrTree T){ if(!(Thrt =(BiThrTree)malloc(sizeof(BiThrNode)))) exit(OVERFLOW); Thrt >LTag =Link; Thrt >RTag =Thread; Thrt >rchild=Thrt; if(!T) Thrt >lchild=Thrt; else{ Thrt >lchild=T; pre=Thrt; InThrTreading(T); pre >rchild=Thrt; pre > RTag =Thread; Thrt >rchild=pre; } return OK; }//InorderThreading
-
Void InThreading(BiThrTree p) { if(p){ InThreading(p >lchild); if(!p >lchild) {p > LRag =Thread; p >lchild=pre;} if(!pre >rchild) {pre >rchild){pre >RRag =Thread;pre >rchild=p;} pre=p; InThreading(p >rchild); }}(Ins_lchild)(Del_lchild)