课 程 简 介

download 课 程 简 介

If you can't read please download the document

description

课 程 简 介. 先修课: 《C++ 语言程序设计 》 任务 研究数据的存储和处理方法; 讨论数据的逻辑结构、存储结构及算法。. 目的. 掌握数据结构特性,能选择适当数据逻辑结构、存储结构及相应的算法 了解算法的时间和空间复杂度分析 培养数据抽象能力和程序设计能力. 线性表. A .线性结构. 栈. 队. 1 .数据的逻辑结构. 数据结构的三个方面. 树形结构. B .非线性结构. 图形结构. A 顺序存储. 2 、数据的存储结构. B 链式存储. 3 、数据的运算:检索、排序、插入、删除、修改等。. 课 程 内 容. - PowerPoint PPT Presentation

Transcript of 课 程 简 介

  • C++

  • 1 2 A BA B 3

  • 111

  • 1.1 (Data)

    (Data Element)

  • (Data Record)

    (Item)

  • 1-1 (Key Item) (Key Word Key)

  • B=(K,R)

    K={ki | 1in, n0} R={ | ku,kvK}

    kukv (Data Structure)

  • 1-2

  • 1 set = (KR) K={01,02,03,04,05,06,07,08,09,10} R={}

  • 2 linearity = (KR) K={01,02,03,04,05,06,07,08,09,10}R={,,,,,,,,}

    050107031008020406091:1

  • 3 tree = (KR) K={01,02,03,04,05,06,07,08,09,10}R={,,,,,,,,}010406020507030809101 :N(N0),

  • 4 graph = (K,R)K={01,02,03,04,05,06,07} R={r}r={,,,,,,, , ,, ,,, ,}r={(01,02),(01,04),(02,03),(02,06),(04,06),(02,07),(03,07),(05,07)}0104060205070301040602050703

  • (M=1)

    (N=1)

    M:N(M0 N0),

  • (Data Type)

  • array = (AR) A = {a[i] | 0in-1,n1} R = {a[i],a[i+1] | 0in-2}

    a[n] Address(a[i])=a+i*L 0in-1 La

  • Address(b[i])=b+i*n*L (0im-1) Address(b[i][j])=b+i*n*L+j*L (0im-1,0jn-1)

    c[p][m][n] Address(c[k])=c+k*m*n*L (0kp-1)Address(c[k][i])=c+k*m*n*L+i*n*L (0kp-1,0im-1) Address(c[k][i][j])=c+k*m*n*L+i*n*L+j*L (0kp-1,0im-1,0jn-1)b[m][n]

  • (Abstract Data Type, ADT) ADT is Data: Operations: end

  • ADT RECtangle is Data: // float length, width; Operations:// void InitRectangle(Rectangle& r, float len, float wid);// float Circumference(Rectangle& r);// float Area(Rectangle& r);// end RECtangle

  • (Algorithm) (Data Object)

  • 1.2 n 1a1an2a1x3i 24ix aix6 i 17 4

  • na1~an a1 x 2 iix ai x i +1 ixNNYYC++

  • 1.2.1 1#includechar, short, int, long, float, double, long doublechar *>>
  • 2#includevoid exit(int), int rand(void), void srand(unsigned)3#include ifstream input(qabc1.dat",ios::in|ios::nocreate); ofstream output1(qabc2.dat",ios::out) ofstream output2("a:qabc.txt",ios::app); fstream inout(qabc3.cpp",ios::in|ios::out);

  • 1int strlen(const char* s);2char* strcpy(char* dest, const char* src);3char* strcat(char* dest, const char* src);4int strcmp(const char* s1, const char* s2);5char* strchr(const char* s, int c);6char* strrchr(const char* s, int c);7char* strstr(const char* s1, const char* s2);4. #include

  • 1.2.2 C++main

  • (&)i, j ; ij20 int i; int &j=i;

  • void swap(int *x,int *y){int temp; temp=*x; *x=*y; *y=temp;}main(){ int m=10,n=5; swap(&m,&n);printf(m=%d,n=%d,m,n);}105&n&mmnyx

  • void swap(int &x,int &y){int temp; temp=x; x=y; y=temp;}void main(){ int m=10,n=5; swap(m,n);cout
  • 1.2.3 C++

  • 1.3

  • nnf(n)1int Sum(int b[],int n) { int s=0; //1 for(int i=0;i
  • 2void MatrixAdd(int a[MS][MS], int b[MS][MS], int c[MS][MS], int n) { int i,j; for(i=0;i
  • 3 void SelectSort(int b[],int n){int i,j,k,x;for(i=0;i
  • f(n) f(n)g(n)nn0 n0ABAB,

    A B

    g(n)f(n)f(n)f(n)=O(g(n))O(1)

  • 3 int SequenceSearch(int a[], int n, int key){ for (int i=0; i
  • 2.1 2.1.1

    (Linear List)

    A= (a1a2aiai+1an)

    a1 ann n=0

  • linear_list=(AR) A={ ai | 1in, n0, aiElemType} R={ | 1in-1 }

  • ().

    ().

    ,.

  • L1=( this, is, a, english , book ) L2=(2101, 2102, 2103, 2104, 2105, 2106, 2110, 2118)

  • 2.1.2 ADT LinearList is Data: L=(a1,a2,,ai,ai+1,,an) Operation: void InitList(&L); //L void ClearList(&L); //L int ListSize(&L); //L bool ListEmpty(&L); //L ElemType GetElem(&L,int pos); //Lpos

  • void TraverseList(&L); //Lbool Find(&L,ElemType& item); //Litembool Update(&L,const ElemType& item); //Litemvoid InsertRear(&L,const ElemType& item); //itemvoid InsertFront(&L,const ElemType& item); //item

  • void Insert(&L,const ElemType& item); //itemL ElemType DeleteFront(&L); //L bool Delete(&L,const ElemType& item); //Litem void Sort(&L); //Lend LinearList

  • 2.2 2.2.1 ,

  • LOC(ai) = LOC(ai-1) + L L=sizeof(ElemType)

    LOC(ai) = LOC(a1) + (i-1)L

  • const int MaxSize=50;struct List { ElemType list[MaxSize]; int size; // }; 0 1 i-1 i n-1 MaxSize-1List La;La.list[0]= a1;

  • 2.2.2 1. void InitList(List &L) { L.size=0; } L 2. void ClearList(List& L){L.size=0; } L

  • 3. bool ListEmpty(List& L) {return (L.size==0);}// { if L.size==0// return true;// else return false;} L 1. L.list[0] L.list[L.size-1]

  • void TraverseList(List& L) { for(int i=0; i
  • 3. bool Find(List& L, ElemType& item) { for(int i=0; i
  • 4. bool Update(List& L, const ElemType& item){ for (int i=0; i
  • 1. Liitembool Inserti(List&L,int i,const ElemType &item ) (a1, , ai-1, ai, , an) (a1, , ai-1, e, ai, , an) , 1

  • (1) (2)i i (3)i(4)1

  • for ( int j=L.size; j
  • { if(L.size==MaxSize) { cerr
  • 2.void InsertFront(List& L,const ElemType& item){ if(L.size==MaxSize) { cerr
  • 3.void InsertRear(List& L, const ElemType& item){ if(L.size==MaxSize) { cerr
  • 4.void Insert(List& L, const ElemType& item){ if(L.size==MaxSize) { cerr
  • 1. Liitembool Deletei(List&L,int i, ElemType &item ) (a1, , ai-1, ai, ai+1, , an) (a1, , ai-1, ai+1, , an)

  • (1) (2)iitem(3)i+1 i(4)1

  • { //(1) item = L.list[i] ; //(2) for(int j=i+1;j
  • 2.ElemType DeleteFront(List& L){ if(L.size==0) { cerr
  • 3.bool Delete(List& L, const ElemType& item){ if (L.size==0){ cerr
  • (1) 42 65 80 74 28 44 36 65 (2) 42 65 80 74 28 44 36 65 (3) 42 65 74 80 28 44 36 65 (4) 28 42 65 74 80 44 36 65 (5) 28 42 44 65 74 80 36 65 (6) 28 36 42 44 65 74 80 65 (7) 28 36 42 44 65 65 74 80 n-1

  • x=list[j] j
  • void Sort(List& L)

    { int i, j; ElemType x; for (i=1; i=0; j--) if (x

  • Insert void Sort(List& L){ List a; InitList(a); for(int i=0; i
  • nvoid SeqSort(ElemType a[], int n) { List b; InitList(b); int i; for(i=0; i
  • 2.2.3

    MaxSize*sizeof(ElemType)+sizeof(int)

    new

    struct List{ ElemType* list; int size; };

  • void InitList(List& L) { L.list=new ElemType[MaxSize]; if(L.list==NULL) { cerr
  • 2.3 goods.txt

  • 2.4 1. datap1,p2,...,pm(m1)

  • 2. A=(a1,a2,,ai,ai+1,,an) HB

  • 3. sqs

  • 3. rs

  • 4. struct LNode { ElemType data; LNode* next; };

    Program2-2.cpp

    //P71~72 Program2-3.cpp

  • struct ALNode{ ElemType data; int next; };typedef ALNode ALinkList[MaxSize];f

  • (35, 68, 57, 26, 70) 3,5,8 MaxSize=10

  • i while (a[j].next !=i) j= a[j].next;x563p= a[1].next ; a[p].data=x;j=0;a[j].next= p;a[1].next= a[p].next;a[p].next=i;

  • 5. struct Dnode { ElemType data; DNode *left; DNode *right;};

    struct ADNode{ ElemType data; int left; int right;};

  • qq->right = p->right; p->right = q;q->right->left = q; q->left = p;p

  • p->right = p->right->right;p->right->left = p;p

  • 6.

  • 7.

  • HbTa, TbHb=Tb->next; Tb->next=Ta->next; Ta->next=Hb->next; delete Hb; a1 a2 ... an b1 b2 ... bm TaTb

  • { if(Tb->next!=Tb) { Hb=Tb->next; Ta->next=Hb->next; Tb->next=Ta->next; Ta=Tb; delete Hb; } } Void CLink(LNode &Ta,LNode &Tb)

  • 2.5 1.

    void InitList(LNode*& HL) { HL=NULL; }//O(1)

    2. bool ListEmpty(LNode* HL) { return (HL==NULL); } // O(1)

  • 3. void ClearList(LNode*& HL){ LNode *cp, *np; cp=HL; while(cp!=NULL) { np=cp->next; delete cp; cp=np;} HL=NULL; } //O(n)

  • 4. int ListSize(LNode* HL) { int i=0; while(HL!=NULL) { i++; HL=HL->next; } return i;} //O(n)

  • 5. pos ElemType GetElem(LNode* HL, int pos){ if(pos
  • 6. void TraverseList(LNode* HL){ while(HL!=NULL) { cout
  • 7. bool Find(LNode* HL, ElemType& item) { LNode* p=HL; while(p!=NULL) if (p->data==item) { item=p->data; return true; } else p=p->next; return false;} O(n)

  • 8. bool Update(LNode* HL, const ElemType& item){ LNode* p=HL; while(p!=NULL) if (p->data==item) break; else p=p->next; if (p==NULL) return false; else { p->data=item; return true; }} O(n)

  • 9. void InsertRear (LNode*& HL, const ElemType& item){ LNode* newptr; newptr=new LNode; if(newptr==NULL) { cerrnext; p->next=newptr; }} O(n)

  • 10. void InsertFront(LNode*& HL, const ElemType& item){ LNode* newptr; newptr= new LNode; if(newptr==NULL) { cerr
  • 11. void Insert(LNode*& HL, const ElemType& item){ LNode* newptr; newptr=new LNode; if(newptr==NULL){ cerr
  • HL=newptr; return; } LNode* cp; LNode* ap; ap=HL; cp=HL->next; while(cp!=NULL) if(itemdata) break; else { ap=cp; cp=cp->next; } newptr->next=cp; ap->next=newptr;} O(n)

  • 12. ElemType DeleteFront(LNode*& HL){ if(HL==NULL){ cerr
  • 13. bool Delete(LNode*& HL, const ElemType& item){if(HL==NULL){ cerr
  • LNode *ap, *cp;ap=HL; cp=HL->next; while(cp!=NULL) if(cp->data==item) break; else { ap=cp; cp=cp->next; }if(cp==NULL){ cerr
  • 14. void LinkSort(ElemType a[ ], int n){ LNode* head=NULL; int i; for(i=0; idata; p=p->next; } ClearList(head); Program2-4.cpp} // O(n2) link.h//

  • 3.1 3.1.1

    (Sparse Matrix) :

  • ((1,1,3),(1,4,5),(2,3,-2),(3,1,1),(3,3,4),(3,5,6),(5,3,-1))

  • 12

  • row col val 1234567:.

    MaxTerms

  • Struct Triple{ int row,col; ElemType val;}Struct SMatrix{ int m,n,t; Triple sm[MaxTems+1];};

  • (1) Struct TripleNode{ int row,col; ElemType val; TripleNode *next;}Struct LMatrix{ int m,n,t; TripleNode * vector[MaxRows+1];};

  • 1 2 3 4 5 6 1 2 3 4 5

  • (2) Struct CrossNode{ int row,col; ElemType val; CrossNode *down,*rigth;}Struct CLMatrix{ int m,n,t; CrossNode * rv [MaxRows+1]; CrossNode * cv [MaxColumns+1];};

  • ADT SparseMatrix is Data: SMatrixCLMatrix Operation:void InitMatrix (SMatrix& M); SMatrix Transpose(SMatrix& M);SMatrix Add(SMatrix& M1, SMatrix& M2);SMatrix Multiply(SMatrix& M1, SMatrix& M2);void InputMatrix(SMatrix& M, int m,int n);void OutputMatrix(SMatrix& M);end SparseMatrix

  • 3.1.3 1.void InitMatrix (SMatrix& M){ M.m=0;M.n=0;M.t=0;}void InitMatrix (LMatrix& M){ M.m=0;M.n=0;M.t=0; for (int i=1;i
  • 2void InputMatrix(SMatrix& M, int m,int n){ M.m=m;M.n=n; int row,col,val;int k=0;cin>>row>>col>>val;While (row !=0){k++;M.sm[k].row=row;M.sm[k].col=col;M.sm[k].val=val; cin>>row>>col>>val;} M.t=k;}

  • void InputMatrix(CLMatrix& M, int m,int n);{ M.m=m;M.n=n; int row,col,val;int k=0;cin>>row>>col>>val;while (row !=0){k++;CrossNode *cp,*newptr;newptr=new CrossNode;newptr->row=row;newptr-> col=col;newptr-> val=val;

  • newptr->right=newptr->down=NULL;cp=M.rv[row];if (cp==NULL) M.rv[row]=newptr; else { while (cp->right!=NULL) cp=cp->right; cp->right=newptr;}cp=M.cv[col];if (cp==NULL) M.cv[col]=newptr; else { while (cp->down !=NULL) cp=cp->down; cp->down=newptr;}cin>>row>>col>>val;}M.t=k;}

  • 3void OutputMatrix(SMatrix &M) { cout
  • 4SMatrix Transpose(SMatrix &M) { SMatrix S;InitMatrix(S);int m,n,t;m=M.m;n=M.n;t=M.t;S.m=n;S.n=m;S.t=t;if (t==0) return S;int k=1;for (int col=1;col
  • 5LMatrix Add(LMatrix &M1, LMatrix &M2) { LMatrix M; InitMatrix(M); if ((M1.m!=M2.m) || (M1.n!=M2.n)) { cerr
  • for(int i=1;icolcol) { *newptr=*p1; p1=p1->next;} else if (p1->col>p2->col) { *newptr=*p2; p2=p2->next;}

  • else if (p1->val+p2->vol==0) { p1=p1->next; p2=p2->next; delete newptr; continue;} else { *newptr=*p1; newptr->val+= p2->vol; p1=p1->next; p2=p2->next; } if (p==NULL) M.vector[i]=newptr; else p->next=newptr; p=newptr; k++;}

  • while(p1!=NULL){ TripleNode * newptr =new TripleNode; *newptr=*p1; newptr->next=NULL; if (p==NULL) M.vector[i]=newptr; else p->next=newptr; p=newptr; p1=p1->next; k++; }

  • while(p2!=NULL){ TripleNode * newptr =new TripleNode; *newptr=*p2; newptr->next=NULL; if (p==NULL) M.vector[i]=newptr; else p->next=newptr; p=newptr; p2=p2->next; k++; }}M.t=k;return M;}

  • 3.2 3.2.1

    n ( 0 ) LS = (a0, a1, a2, , an-1) LSai()() nn = 0 n > 0 (head) (tail)

  • A = ( ) B = ( 6, 2 ) C = ( a, ( 5, 3, x ) ) D = ( B, C, A )

  • 3.2.2 struct GLNode{ bool tag; union{ ElemType data; GLNode * sublist; }; GLNode *next; };

  • A = ( ) B = ( 6, 2 ) C = ( a, ( 5, 3, x ) ) A=NULL

  • A = ( ) B = ( 6, 2 ) C = ( a, ( 5, 3, x ) ) A

  • 3.2.3 1int Lenth(GLNode * GL){ if (GL!=NULL) return 1+Lenth(G->next); else return 0;}

  • 2int Depth(GLNode * GL){ int max=0; while (GL!=NULL){ if(GL->tag==true){ int dep=Depth(GL->sublist); if (dep>max) max=dep;} GL=GL->next; } return max+1;;}

  • 3void Create(GLNode * &GL){ char ch; cin>>ch; if (ch==#) GL=NULL; else if (ch==( { GL=new GLNode; GL->tag=true; Create(GL->sublist);}

  • else { GL=new GLNode; GL->tag=false; GL->data=ch;} cin>>ch if (GL==NULL); else if (ch==,) Create(GL->next); else if (ch==)|| ch==;) GL->next=NULL;}

  • 4void Print(GLNode * &GL){ if (GL->tag= =true){ cout
  • 3.2.4 Program3-1.cpp

    genlist.cpp

  • 4.1 ( Stack ) (top)(bottom)

    (LIFO)

  • ADT STACK is Data D{ai | aiElemType, i=1,2,...,n, n0 } R1{ | ai-1, aiD, i=2,...,n } // an a1 Operation ... ...end STACK

  • void InitStack(Stack& S); // void ClearStack(Stack& S); // bool StackEmpty(Stack& S); // ElemType Peek(Stack& S) //S void Push(Stack& S, const ElemType& item) //item ElemType Pop(Stack& S) // bool StackFull(Stack& S) //

  • 4.1.3 Const StackMaxSize 50; struct Stack { ElemType stack[StackMaxSize]; int top; // }; top-1top1

  • :s.top==-1 :s.top==StackMaxSize-1

  • 1. void InitStack(Stack &S) { S.top=-1; }

    2. void ClearStack(Stack &S) { S.top=-1; }

  • bool StackEmpty(Stack &S) { return S.top==-1; }

    4. bool StackFull(Stack &S) { return S.top==StackMaxSize-1; }3.

  • void Push(Stack&S,ElemType&item) { if (S.top==StackMaxSize-1) { cerr
  • ElemType Pop(Stack& S) { if(S.top==-1) { cerr
  • ElemType Peek(Stack& S) { if (S.top==-1) { cerr
  • struct LNode { ElemType data; struct LNode *next; };

  • void InitStack(LNode *& HS) { HS=NULL; }2. bool StackEmpty(LNode* HS) { return HS==NULL; }1.

  • an an-1 an-2 ... a1 /\cp=HS; np=cp->next; delete cp;cp=np;while(cp!=NULL){

    }HS3.

  • { LNode *cp, *np; cp=HS; //cp while(cp!=NULL) { np=cp->next; delete cp; cp=np; } HS=NULL; // }void ClearStack(LNode *& HS)

  • itemHsnewptrHsnewptr=new LNode;newptr->data=item;newptr->next=Hs;Hs=newptr;4.

  • { LNode* newptr= new LNode; if (newptr==NULL) { cerr
  • Hsa1anan-1p=Hs;item=Hs->data;:pHsHs=Hs->next;delete p;5.

  • { if (HS==NULL) { cerr
  • 1. 12 34 56 78 1 78 56 34 1234125678Program4-1.cppStack.h

  • {[][]} {[[][]}

    12 a b c3 a b2.C++Program4-2.cpp

  • Program4-3.cpp3.

  • 4.2

    ::=() () ()::=|||

    : (a +b)c-(e+f)/6 : a b+ c e f+6 / -

  • 3/5+616-9*(4+3)2*(x+y)/(1-x)(25+x)*(a*(a+b)+b)3 5 /162 x y + * 1 x - /25 x +6 +9 4 3 + *-a a b + * b +*

  • : a b + c e f + 6 / -a+b(a+b)ce+f(e+f)/6(a+b)c-(e+f)/6

  • a char a[30]="12 3 20 4 / * 8 - 6 * +@"51574254Program4-4.cpp

  • S1S2 R S1S2S2S2

    S1@

  • :S1 10+(18+9*3)/15-6@ S2R@1 0+(+*1 8931 5*+/+6-@-/program4-5.cpp

  • 4.3 , , ,

  • Main( ) void a( ) { { a( ); b( ); } }

  • Main( ) void a( ) void b( ){ { { a( ); b( ); } } }Main( )a( )b()

  • nn! 1 (n=0) f(n)= n*f(n-1) (n>0)

    n! = n * (n-1)! long f (int n){ if (n==0) return 1; else return n*f(n-1);}

  • f(4)f(n)

  • f(4)f(n)

  • void converse(LNode *L) { while (L) { converse(L->next)) ; cout
  • x void deletex(LNode *L,ElemType x) { LNode p; if (L) { if (L->data==x) { p=L; L=L->next; free(p); deletex(L,x);} else deletex(L->next,x); }}L

  • 1 2 3 4 5 6 7 81 2 3 4 5 61 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1111 1 1 1 1 111

  • maze[m+2][n+2]mark[m+2][n+2]move[4][2]

    Program4-6(1,0)(-1,0)(0,1)(0,-1)

    01100-1-10

    101100001

  • 4.4 ( Queue ) (rear) (front) (FIFO)

  • ADT QUEUE is Data QueueType ... ... Operation void InitQueue(QueueType& Q); void ClearQueue(QueueType& Q); bool QueueEmpty(QueueType& Q); ElemType QFront(QueueType& Q); void QInsert(QueueType& Q, const ElemType& item); ElemType QDelete(QueueType& Q); bool QueueFull(QueueType& Q)end QUEUE

  • 4.4.3 Const QueueMaxSize 50; struct Queue { ElemType queue[StackMaxSize]; int front,rear; };

  • rear=(rear+1)%QueueMaxSize (rear+1)%QueueMaxSize==front 1 2front

  • (1) void InitQueue(Queue& Q) { Q.front=Q.rear=0; }

    (2)

    void ClearQueue(Queue& Q) { Q.front=Q.rear=0; }

  • (3) bool QueueEmpty(Queue& Q) { return Q.front==Q.rear; }

    (4) ElemType QFront(Queue& Q) { if(Q.front==Q.rear) { cerr

  • (5) void QInsert(Queue& Q, const ElemType& item) {int k=(Q.rear+1)%QueueMaxSize; if(k==Q.front) { cerr
  • (6) ElemType QDelete(Queue& Q) { if(Q.front==Q.rear) { cerr
  • struct LinkQueue { LNode *front; LNode *rear;};struct LNode { ElemType data; LNode *next; };

  • (1) void InitQueue(LinkQueue& HQ) { HQ.front=HQ.rear=NULL; } (2) bool QueueEmpty(LinkQueue& HQ) { return HQ.front==NULL; }

  • (3) void ClearQueue(LinkQueue& HQ) { LNode* p=HQ.front; while(p!=NULL) { HQ.front=HQ.front->next; delete p; p=HQ.front; } HQ.rear=NULL; }

  • (4) ElemType QFront(LinkQueue& HQ) { if(HQ.front==NULL) { cerr
  • (5) void QInsert(LinkQueue& HQ, const ElemType& item) { LNode* newptr=new LNode; if(newptr==NULL) {cerr
  • (6) ElemType QDelete(LinkQueue& HQ) { if(HQ.front==NULL) { cerr
  • 1 Program4-7.cpp queue.h 2 Program4-8.cpp linkqueue.h

  • 5.1 m (m 0)T0, T1, , Tm-1(subTree)0(Tree) n (n 0)n = 0n > 0

    (root)

  • (A(B(D,E(H,I),F),C(G))) Tree=(K,R)

  • (degree) (degree)(branch)0 (leaf)0 (child) (parent)

  • (sibling)(ancestor) (descendant) (level)(depth)

  • m(m0)

  • 1 1[]2 kiki-1(i1) 1k1-1 =k0=1 i-1k(i-1)-1=ki-2 i ki-2*k= ki-1 kk

  • 3 hk k k

  • 4 nk logk(n(k-1)+1) kh-1
  • 5.2 (Binary Tree) 2

  • 1 , n0 , 2 n2 , n0n21

    0 n0,(),n1+2n2 n1+n2+n0=n1+2n2+1: n0=n2+1

  • 2 i 2i -1(i 1) []3 h 2h-1(h 0) [hk(kh-1)/(k-1)]

    i 2i -1

  • (Full Binary Tree) i2i-1 (Complete Binary Tree)

  • 4 n1, 2, , n in/22in n nn/2)i,2i;i,2i+1

  • i = 1, i; i > 1, i i/2i, i/2,i, (i-1)/2, i , i != 1, i-1 i , i != n i+1

  • 5 n (n>0) log2(n+1) h 2h-1- 1 < n 2h- 1 2h-1n
  • ADT BinaryTree {data: Operations: void InitBTree (BTreeType &BT ); void CreateBTree (BTreeType &BT,char *a ); bool BTreeEmpty (BTreeType &BT ); void TraverseBTree(BTreeType &BT); int BTreeDepth(BTreeType &BT ); void PrintBTree (BTreeType &BT ); void ClearBTree (BTreeType &BT ); End BinaryTree

  • 141237648912

  • leftChild data rightChilddataleftChildrightChild

  • leftChild data parent rightChildparentdataleftChildrightChild

  • AABBCCDDFFEErootroot

  • ABCDFEroot

  • struct BTreeNode { ElemType data; BTreeNode* left; BTreeNode* right; }; struct ABTreeNode { ElemType data; int left,right; };typedef ABTreeNode ABTList[BTreeMaxSize];

  • 5.3 (Preorder)DLR void Preorder(BTreeNode* BT){ if(BT!=NULL) { cout
  • (Inorder)LDR void Inorder(BTreeNode* BT){ if(BT!=NULL) { Inorder(BT->left); // cout
  • (Postorder) LRD void Postorder(BTreeNode* BT){ if(BT!=NULL) { Postorder(BT->left); // Postorder(BT->right); // cout
  • - + a * b - c d / e fa b c d - * + e f / -a + b * c - d - e / f-

  • Ap C B D A E G F

  • void Levelorder(BTreeNode* BT){ const MaxLength=30; BTreeNode* q[MaxLength]; // int front=0, rear=0; // BTreeNode* p; if(BT!=NULL) { // rear=(rear+1)% MaxLength; q[rear]=BT; }

  • while (front!=rear) { front=(front+1)% MaxLength; p=q[front]; coutright!=NULL) { rear=(rear+1)% MaxLength; q[rear]=p->right; }} //while end}

  • 5.4 1. void InitBTree(BTreeNode*& BT) { BT=NULL;}

    2. bool BTreeEmpty(BTreeNode* BT) { return BT==NULL;}

  • 3. A(B(C,D),E(,F(G,)))@

  • a()(k=1)(k=2)k1k2 @

  • void CreateBTree(BTreeNode*& BT, char* a)

    { BTreeNode* s[10]; int k ,top=-1; BT=NULL; BTreeNode* p; istrstream ins(a); char ch; ins>>ch; while (ch!='@') { switch(ch) { case '(': top++; s[top]=p; k=1; break; case ')': top--; break;

  • case ',': k=2; break;default: p=new BTreeNode; p->data=ch; p->left=p->right=NULL; if(BT==NULL) BT=p; else { if(k==1) s[top]->left=p; else s[top]->right=p; } } //switch end ins>>ch; } //while end}

  • 4. int BTreeDepth(BTreeNode* BT) { if (BT==NULL) return 0; else { int dep1=BTreeDepth(BT->left); int dep2=BTreeDepth(BT->right); if(dep1>dep2) return dep1+1; else return dep2+1; } }

  • 5. () void PrintBTree(BTreeNode* BT) { if(BT!=NULL) { coutleft!=NULL || BT->right!=NULL) { coutright!=NULL) cout
  • 6. void DeleteBTree(BTreeNode* BT){ if(BT!=NULL) { DeleteBTree(BT->left); // DeleteBTree(BT->right); // delete BT; // }} void ClearBTree(BTreeNode*& BT){ DeleteBTree(BT); BT=NULL; }

  • 5.5 struct GTreeNode { char data; GTreeNode* t[3]; };1.

  • 2.

  • 3.

  • 1 A(B(D,E(H,I),F),C(G))@ SK

  • void CreateGTree(GTreeNode*& GT, char* a){ GTreeNode* s[10]; int k[10]; int top=-1; GT=NULL; GTreeNode* p; char ch; istrstream ins(a); ins>>ch; while (ch!='@') { switch(ch) { case '(': top++; s[top]=p; k[top]=0; break; case ')': top--; break; case ',': k[top]++; break;

  • default: p=new GTreeNode; p->data=ch; for(int i=0; it[i]=NULL; if(GT==NULL) GT=p; else s[top]->t[k[top]]=p; } ins>>ch; } //while end}

  • 2. void PreRoot(GTreeNode* GT){ if(GT!=NULL) { cout
  • void PostRoot(GTreeNode* GT){ if(GT!=NULL) { for(int i=0; it[i]); // cout
  • void LayerOrder(GTreeNode* GT){ Queue q; InitQueue(q); GTreeNode* p; if(GT!=NULL) QInsert(q,GT); while (!QueueEmpty(q)) { p=QDelete(q); cout
  • 3. (1) (2) (3)

  • void PrintGTree(BTreeNode* BT){ if(BT!=NULL) { coutleft!=NULL) { coutright!=NULL) { cout
  • :194---195

  • 6.1 (Binany SearchingTree)(Binany SortingTree) 123

  • 351545504025102030

  • ADT BinarySearchingTree {data: public: bool Find(BTreeNode *BST,ElemType& item); bool Update(BTreeNode *BST, const ElemType& item); void Insert(BTreeNode *&BST, const ElemType& item); bool Delete(BTreeNode *&BST, const ElemType& item);

    End BinaryTree

  • xNULLx

  • 35154550402510203045 28

  • bool Find(BTreeNode *BST,ElemType& item) { if (BST==NULL) return false; else{ if (item==BST->data){item=BST->data;return true;} else if (itemdata)return Find(BST->left,item); elsereturn Find(BST->right,item);}}

  • bool Find(BTreeNode *BST,ElemType& item) { while (BST!=NULL) { if (item==BST->data){item=BST->data;return true;} else if (itemdata) BST=BST->left; else BST=BST-> right;}return false; }

  • bool Update(BTreeNode *BST, const ElemType& item) {if (BST==NULL) return false;else{if (item==BST->data){ BST->data=item; return true;}else if (itemdata)return Update(BST->left,item); elsereturn Update(BST->right,item);}}

  • 35154550402510203028 28

  • : ,:

  • { 53, 78, 65, 17, 87, 09, 81, 15 }535378537865537865175378658717537865091787537865811787095378651517870981

  • void Insert(BTreeNode *&BST, const ElemType& item){if (BST==NULL){BTreeNode *p=new BTreeNode;p->data=item;p->left=p->right=NULL;BST=p;}else if (itemdata) Insert(BST->left,item); else Insert(BST->right,item);}

  • void Insert(BTreeNode *&BST, const ElemType& item){ BTreeNode *t= BST , *parent=NULL; while (t!=NULL){ parent=t; if (itemdata) t=t->left; else t=t->right;} BTreeNode *p=new BTreeNode; p->data=item; p->left=p->right=NULL; if (parant==NULL) BST=p; else if (itemdata) parent->left=p; else parent->right= p;}

  • void CreateBSTree(BTreeNode *&BST, ElemType a[],int n){BST=NULL;for (int i=0;i
  • 3 { 1, 2, 3 } {2, 1, 3} {1, 2, 3} {1, 3, 2} {2, 3, 1} {3, 1, 2} {3, 2, 1} 123111132223323

  • (),537865178709234545, 53786517870923

  • 885378881794092378, 53948817092353788117940945782365538188179409452365program6-1.cpp

  • n n! ()

  • 3 122133132123123{123} {132} {213} {231} {312} {321}

  • Ki K2i+1 && Ki K2i+26.2 ( Heap )

  • Ki K2i+1 && Ki K2i+2

  • ADT HEAP is Data: HeapType HBTOperations void InitHeap(HeapType&HBT); void ClearHeap(HeapType&HBT); bool HeapEmpty (HeapType&HBT); void InsertHeap(HeapType&HBT,const ElemType item); ElemType DeleteHeap(HeapType&HBT);end HEAP

  • struct Heap{ElemType heap[HeapMaxSize];int size;};7474 53 42 25 36 35 20 18 22 53251822363520420 1 2 3 4 5 6 7 8 9 073451682

  • 1void InitHeap(HeapType&HBT) { HBT.size=0;}2void ClearHeap(HeapType&HBT) { HBT.size=0;}3bool HeapEmpty (HeapType&HBT) { return HBT.size==0; }

  • 4

  • void InsertHeap(HeapType&HBT,const ElemType item){HBT.heap[HBT.size]=item;HBT.size++;ElemType x=item;int i=HBT.size-1;while (i!=0){int j=(i-1)/2;if (x>=HBT.heap[j])break;HBT.heap[i]=HBT.heap[j];i=j;}HBT.heap[i]=x;}

  • 5877845655323

  • ElemType DeleteHeap(HeapType&HBT){if (HBT.size==0){cerr
  • while (j
  • 6.3 (Huffman Tree)k1,k2,,kj,kiki+1(1i
  • 1122334455667788PL = 3*1+2*3 = 9 PL = 1*1+2*1+3*1+4*1 = 10

  • (Weighted Path Length, WPL)

  • WPL = 2*2+ WPL = 2*1+ WPL = 7*1+ 4*2+5*2+ 4*2+5*3+ 5*2+2*3+ 7*2 = 36 7*3 = 46 4*3 = 35

    222444555777

  • (1) n {w0, w1, w2, , wn-1} n F = { T0, T1, T2, , Tn-1 } Ti wi ,

  • (2) , F F , F F

  • F : {7} {5} {2} {4}F : {7} {5} {6}7524{2} {4}F : {7} {11} 752475246611{5} {6}F : {18} 5{5} {6}27461118

  • CAST CAST SAT AT A TASA { C, A, S, T }() W{ 2, 7, 4, 5 }

  • A : 00 T : 10 C : 01 S : 11 ( 2+7+4+5 ) * 2 = 36 { 2/18, 7/18, 4/18, 5/18 }, { 2, 7, 4, 5 }.

  • , 0 1() A : 0 T : 10 C : 110 S : 1117*1+5*2+( 2+4 )*3 = 35 WPL 2457000111

  • 7.1 (vertex) G( V, E )

    V = { vi | 0i n-1,n1,vi VertexType} E = {(x, y) | x, y V } E = { | x, y V && Path (x, y)} (edge)Path (x, y) x y ,

  • (u, v) E(G) u v vD(v), v v , ID(v); v v , OD(v)

  • (x, y) n n(n-1)/2 , n n(n-1) , 0011226543

  • G(V, E) G (V, E) V V E E, G G

  • G(V, E) , vi , vp1, vp2, , vpmvj (vi vp1 vp2 ... vpm vj) vi vj (vi, vp1)(vp1, vp2)...(vpm, vj) E

  • v1,v2,...,vm , v1 vm , 012301230123

  • , v1v2, v1v2,

  • , vivj, vivjvjvi, 0 134 2 5

  • nn-1 ,

  • 7.2 A = (V, E) n , A.edge[n][n], (Adjacency Matrix)

  • ;0123012

  • , i 1 i j 1 j , i () 1 i

  • const int MaxEdgeNum = 50;const int MaxVertexNum = 10;typedef int VertexType verlist[MaxVertexNum];typedef int adjmatrix[MaxVertexNum][MaxVertexNum];

  • void Create1(vexlist GV,adjmatrix GA,int n,int e){ int i,j,k,w; cout
  • (Adjacency List)ABCDdata adjABCD0123dest nextdest next130210(), dest next

  • ABCdata adjABC012dest linkdest link ()data adjABC012dest link ()102011

  • () BACD69528data adjABCD0123dest weihgt link1 53 62 83 21 9()()

  • weight i adj i n e n 2e n e

  • const int MaxEdgeNum = 50;const int MaxVertexNum = 10;typedef int VertexType verlist[MaxVertexNum];struct edgenode { int adjvex; int weight; edgenode *next;}typedef edgenode * adjlist[MaxVertexNum];

  • void Create2(vexlist GV,adjlist GL,int n,int e){ int i,j,k; cout
  • edgeset array203169528struct edge { int fromvex; int endvex; int weight;}typedef edge edgeset[MaxEdgeNum];

    001121223365928

  • void Create3(vexlist GV,adgeset GE,int n,int e){ int i,j,k,w; cout
  • 7.3 ( Graph Traversal ) visited [ ]

  • visited [ ] 0, , i , visited [i] 1, : DFS (Depth First Search) BFS (Breadth First Search)

  • DFS ( Depth First Search )ACDEGBFIHACDEGBFIH123456789123456789

  • DFS v , v , w1; w1 , w1 w2; w2 , , , u , , , , , , ; , ,

  • void dfs1(adjmatrix GA,int i,int n) { cout
  • v11,0,2,6,3,4,5

  • void dfs2(adjlist GL,int i,int n) { cout
  • v11, 6, 2, 0, 3,5,4

  • BFS ( Breadth First Search )ACDEGBFIHACDEGBFH123456789123456789 I

  • BFS v , v , v w1, w2, , wt , w1, w2, , wt

  • , , , , ,,, visited [ ]

  • void bfs1(adjmatrix GA,int i,int n) { Queue Q; InitQueue(Q); cout
  • v11,0, 4, 5, 6, 2, 3

  • void bfs2(adjmatrix GL,int i,int n) { Queue Q; InitQueue(Q); cout
  • v11, 6 ,5 ,4, 0,2, 3

  • (Connected component), , , ()

  • , ,

  • ACDEIBFOGHJNMLKAHKCDEIBFOGJNML

  • 7.4 n n n-1 n-1 n

  • (Prim) G = { V, E } v0 , ( v0, v1 ), T={ U, TE }U U , U (vi, vj), U , U

  • 252510504613228102514242216185046132504613210 (a) (b)504613210(c) (d) (e) (f)50461321022125046121025142216123252212

  • 504613231061297150 U={0} TE={ } LW={(0,1)8,(0,2),(0,3)5,(0,4),(0,5),(0,6)}1 U={0,3} TE={(0,3)5} LW={(3,1)3, (0,2), (0,4), (3,5)7, (3,6)15}852

  • 504613231061297152 U={0 ,3 ,1} TE={(0,3)5, (3,1)3} LW={(1,2)12,(1,4)10, (3,5)7, (3,6)15}3 U={0,3 ,1,5} TE={(0,3)5 , (3,1)3 ,(3,5)7} LW={(5,2)2,(5,4)9, (3,6)15}851 U={0,3} TE={(0,3)5} LW={(3,1)3, (0,2), (0,4), (3,5)7, (3,6)15}2

  • 504613231061297154 U={0 ,3 ,1, 5,2} TE={(0,3)5, (3,1)3 ,(3,5)7, (5,2)2} LW={ (2,4)6 ,(3,6)15}5 U={0,3 ,1,5,2,4} TE={(0,3)5 , (3,1)3 ,(3,5)7 , (5,2)2, (2,4)6 } LW={ (3,6)15}853 U={0,3 ,1,5} TE={(0,3)5 , (3,1)3 ,(3,5)7} LW={(5,2)2,(5,4)9, (3,6)15}2

  • 504613231061297156 U={0 ,3 ,1, 5,2,4,6} TE={(0,3)5, (3,1)3 ,(3,5)7, (5,2)2 , (2,4)6, (3,6)15} LW={ }855 U={0,3 ,1,5,2,4} TE={(0,3)5 , (3,1)3 ,(3,5)7 , (5,2)2, (2,4)6 } LW={ (3,6)15}

    2

  • void Prim ( adjmatrix GA, edgeset CT,int n ) //CT GA { int i,j,k,min,t,m,w; for (i=0;j

  • edge temp=CT[k-1]; CT[k-1] = CT[m]; CT[m] = temp; j= CT[k-1] .endvex; for(i=k;i
  • (Kruskal) n G = { V, E }, n , T = { V, }, E , T ; ,

  • 10504613228102514242216181250461325046132 (a) (b)

  • 1012504613228102514242216181250461325046132101412 (c) (d)504613210141612(e) (f) (g)504613210142216125046121025142216123

  • void Kruskal ( edgeset GE,edgest C,int n )// C GE ; { int i; edgenode *p;adjlist s; for (i=0;iadjvex=i; p->next=NULL; s[i]=p;} int m1,m2 ,k=1, d=0; while (k

  • while (p!=NULL){ if(p->adjvex==GE[d].fromvex) m1=i; if(p->adjvex==GE[d].endvex) m2=i; p=p->next;} } if(m1!=m2){ C[k-1]=GE[d];k++; p=s[m1]; while (p ->next!=NULL) p=p->next; p->next= s[m2]; s[m2]= NULL; } d++;}//while

  • for (i=0;inext; delete p; p= s[i]; }}}

  • 7.5 : (Activity Network) (AOV)

  • C1 C2 C3 C1, C2 C4 C3, C2 C5 C2 C6 C5, C4 C7 C4, C9 C8 C1 C9 C8

  • C8C3C5C4C9C6C7C1C2

  • Vi Vj AOV (Activity On Vertices) AOV, AOV

  • AOV ()AOV AOVAOV,

  • AOVAOV, , C1 , C2 , C3 , C4 , C5 , C6 , C8 , C9 , C7 C1 , C8 , C9 , C2 , C5 , C3 , C4 , C7 , C6C8C3C5C4C9C6C7C1C2

  • AOV n AOV, ; , ; , , ,

  • C0C1C2C3C4C5(a) C2C5C1C0C3(b) C4C1C2C5C3(c) C0C4C0C2C5C1C3(d) C3

  • C1C2C5(e) C2C5C1(f) C1C5(g) C5 C4 , C0 , C3 , C2 , C1 , C5 C4C2(h)

  • AOVC0C1C2C3C4C5 C0 C1 C2 C3 0 C4 C5 0data adj 1 dest link 3 0 5 1 5 0 0 1 5 0

  • , , ;, , ; AOV, ; 0, ; AOV,

  • , ,, d[ ]top, , top = -1i d[i] = top; top = i ; j = top; top = d[top];

  • d[]C0C1C2C3C4C513010313-112322-112221-1222012345012345012345012345toptoptoptoptoptop4toptop0

  • 21-12222-1-12212-1-122-12-1-122-1012345012345012345012345toptoptoptoptop1top532d[]C0C1C2C3C4C5

  • void TopoSort ( adjlist GL,int n) { int i,j,k,top,m=0; edgenode *p; int *d=new int[n]; for ( int i = 0; i < n; i++ ) d[i]=0; for ( int i = 0; i < n; i++ ) { p=GL[i]; while (p!= NULL ){ j=p->adjvex; d[j]++; p=p->next;} }

  • top = -1; for ( i = 0; i < n; i++ ) if d[i] == 0 ) { d[i] =top; top = i; } while ( top != -1 ) { j = top; top = d[top]; cout
  • d[k]--; if ( d[k] == 0 ) { d[k] = top; top = k; } p=p->next; } } cout
  • B-

  • 8.1 (Search) ,, , , ,

  • ()ASL(Average Search Length) n n , x, xx

  • 8.2

    , (Sequential Search) n, x, , ; x,

  • int Search ( ElemTypeA[],int n,KeyType K ) { for (int i=0;i
  • i pi , i ci , :ci = i , i = 1 ,2,, n

  • pi = 1/n, i = 1, 2, , n ASLunsucc = n+1.

  • n, midx: Element[mid].key == x Element[mid].key > x Element[mid].key < x

  • -1 0 1 3 4 6 8 10 1260 1 2 3 4 5 6 7 8lowmidhigh6 6 8 10 125 6 7 8lowmidhigh665lowmidhigh6

  • -1 0 1 3 4 6 8 10 1250 1 2 3 4 5 6 7 8lowmidhigh5 6 8 10 125 6 7 8lowmidhigh655lowmidhigh5

  • int Binary (ElemType A[],int low,int high,KeyType K) //{ if ( low
  • int Binary (ElemType A[],int n,KeyType K) // { int high = n-1, low = 0; while ( low
  • 239658548260267523NULL

  • n = 2h-1( h 2h-1) h 2h = n+1, h = log2 (n+1) h = log2 n +1 11, 11; 22, 22; , i (1 i h) 2i-1 , i i pi = 1/n

  • 8.3 , 1k , n , , , , ,

  • 100140180220260300340380key addr 03 180 08 140 17 340 24 260 47 300 51 380 83 100 95 220 83 08 ... 03 ... 95 ... 24 ... 47 ... 17 ... 51 ...

  • 22 12 13 30 29 3336 42 44 48 39 4060 74 56 79 80 6692 82 88 98 94 1234334880981234key addr

  • struct IndexItem{IndexKeyType index;int start;int length;}; typedef IndexItem indexlist[ILMSize];typedef ElemType mainlist[MaxSize];

  • JS00168042/05/13JS00244063/07/25JS00346060/12/08JS00455048/06/09DZ00138068/05/24DZ00248059/05/30DZ00360046/02/24JJ00145062/11/17JJ00244064/04/28HG00172043/06/03HG00258056/02/19HG00340062/06/20

  • JS001JS002JS003JS004DZ001DZ002DZ003JJ001JJ002HG001HG002HG003 (JS= JS001JS002JS003JS004)DZ=(DZ001DZ002DZ003) JJ=(JJ001JJ002)HG=(HG001HG002HG003)

    JS04DZ43JJ72HG93

  • ( JSH=(JS001HG001) FJS=(JS004, DZ003 HG002) JIA=(JS002JS003 DZ002, JJ001 JJ002) HG=(DZ001 HG003)JSH 0 9JIA 1 2 5 78

  • (

    02331542

    JS0010JS0021JS0032JS0043DZ0014DZ0025DZ0036JJ0017JJ0028HG0019HG00210HG00311

  • () n b ()

  • int Indsch(mainlist A,indexlist B,int m,IndexKeyType K1, KeyType K2){ int i,j; for (i=0;i
  • int Indsch1(mainlist A,indexlist B,int m,IndexKeyType K1, KeyType K2){ int i,j; for (i=0;i
  • ASLIndexSeq = ASLIndex + ASLSubList, ASLIndex ASLSubList n b s b = n/s1/b 1/s

  • ASLIndexSeq = (b+1)/2+(s+1)/2 = (b+s)/2 +1 n s n s , s = , ASLIndexSeq +1

  • (Block Search) indexstartlength

    3472980510453

    01234567891011121314152618343672405743869398

  • int Blocksch(mainlist A,indexlist B,int m, KeyType K){ int i,j; for (i=0;i
  • int Blocksch(mainlist A,indexlist B,int m, KeyType K){ int i,j; int low=0,high=m-1; while (low
  • 8.4 (Hashing)Hash( ) Address Hash ( Rec.key ), ,, , , ,

  • , , A=(18,75,60,43,54,90,46)h(K)=K%m ( n=7,m=13)h(18)=18%13=5 h(75)=75%13=10h(60)=60%13=8 h(43)=43%13=4h(54)=54%13=2 h(90)=90%13=12h(46)=46%13=7H

    012345678910111254431846607590

  • 12361, 07251, 03309, 30976 hash(x) = x % 73 + 13420 hash(12361) = hash(07250) = hash(03309) = hash(30976) = 13444

  • , , :

    , ,

  • 1, n m

  • , m , 0 m-1 : key , 0 m-1

  • h ( K ) K + c

  • { 942148, 941269, 940527, 941630, 941805, 941558, 942047, 940001 } h (K) = K - 940000 h (942148) = 2148 h (941269) = 1269h (940527) = 527 h (941630) = 1630h (941805) = 1805 h (941558) = 1558h (942047) = 2047 h (940001) = 1

  • m( 0.60.9), h ( K ) = K % m: key = 962148, m =23 h ( 962148 ) = 962148 % 23 = 12, 0 22

  • KKASCIIhh3ab1h=97*26+98*23+49=7041, m=127, h(ab1)=7041%127=56 int Hash(char *K,int m){ int len=strlen(K); unsighted int h=0; for (int i=0:i
  • ( 923176029232687592739628923436349270681692706816927746389238126292394220 )457878 275283416386220

  • , , ,

  • HT m , m , i (0 i < m) i s ,

  • s s , ,.

  • d= h ( K ) di = ( di-1 +1 ) % m (1 i m-1 d0=d)

    ( s = 1 ) R1 , R2

  • H3158 h(31)=31%13=5h(58)=58%13=6H

    012345678910111254431846607590

    0123456789101112544318314660587590

  • , , ,

  • d= h ( K ) di = ( di-1 +(i-1)2 ) % m (1 i m-1 d0=d)

  • h1 h2

    d= h1 ( K ) di = ( di-1 + h2(K) ) % m (1 i m-1 d0=d)

  • h(K)=K%13 B=(18,75,60,43,54,90,46,31,58,73,15,34)

    ASL=(81+ 3 2+1 3/12=17/12

    0123456789101112 0

    43

    18

    31

    60

    73

    34

    75

    90

    54

    15

    58

    46

  • B=(18,75,60,43,54,90,46,31,58,73,15,34)h(K)=K%13ASL=(71+ 2 2+2 4+ 16 /12=25/12H56678989101123891011120

    0123456789101112345415431831466058757390

  • typedef ElemType hashlist1[HashMaxSize]

    void InitHashlist(hashlist1 HT){ for (int i=0;i

  • void ClearHashlist(hashlist1 HT){ for (int i=0;i
  • bool Insert(hashlist1 HT,int m,ElemType item){ int d=H(item.key,m); int temp=d; while ( HT[d].key!=NullTag){ d=(d+1)%m; if (d==temp) return false ;} HT[d]=item; return true;}

  • int Search(hashlist1 HT,int m,ElemType item){ int d=H(item.key,m); int temp=d; while ( HT[d].key!=NullTag){ if (HT[d].key==item.key) return d else d=(d+1)%m; if (d==temp) return -1 ;} return -1;}

  • int Delete(hashlist1 HT,int m,ElemType item){ int d=H(item.key,m); int temp=d; while ( HT[d].key!=NullTag){ if (HT[d].key==item.key){ HT[d].key= DeleteTag; return 1} else d=(d+1)%m; if (d==temp) return 0 ;} return 0;}

  • typedef LNode * hashlist2[HashMaxSize] struct LNode{ElemType data;LNone * next;};void InitHashlist(hashlist2 HT){ for (int i=0;i
  • void ClearHashlist(hashlist2 HT){ LNode *p; for (int i=0;inext; delete p; p= HT[i];} }}

  • int Insert(hashlist2 HT,int m,ElemType item){ int d=Hash(item.key,m); LNode * p=new LNode; if( p==NULL) return 0; p->data=item; p->next=HT[d]; HT[d]=p; return 1;}

  • ElemType * Search(hashlist2 HT,int m,ElemType item){ int d=Hash(item.key,m); LNode * p= HT[d]; while ( p!=NULL) if(p->data.key==item.key) return &(p->data); else p=p->next; return NULL;}

  • int Delete(hashlist2 HT,int m,ElemType item){ int d=Hash(item.key,m); LNode * p= HT[d],*q; if ( p==NULL) return 0; if(p->data.key==item.key) {HT[d]=p->next; delete p; return 1;} q=p->next; while (q!=NULL){ if(p->data.key==item.key) {p->next=q->next;delete q;return 1;} else {p=q;q=q->next;}return 0;}

  • 1234

    ASL

  • 8.5 B ,

    Ki < Ki+1, 1 i < m (m3), m/2 -1n m-1 1n m-1 2 m m/2 mB-

    nparP0K1P1K2P2KnPnKmPm

  • 262

    180

    18

    26573

    3202532

    154

    28796

    21538

  • B-const int m={B-}struct MBNode { int keynum; MBNode *parant; KeyType key[m+1]; MBNode *ptr[m+1]; int recptr[m+1];};

  • B- int Seaarch(MBNode *MT,KeyType K){ int i,n; MBNode *p=MT; while (p!=NULL) { n=p->keynum; i=1; p->key[n+1]=MaxKey; while (K>p->key[i]) i++; if (K==p->key[i]) return p->recptr[i]; else p=p->ptr[i-1];} return 1;}

  • B- 22 m/2 2 m/2 22 m/2 ( h-1 )C1=C2 - C3C2=N+C4C4=C3+1 C1=C2-C3=N+C4-C3 =N+C3+1-C3=N+12 m/2 ( h-1 ) N+1 mhlogm(N+1) h1+logm/2[(N+1)/2]

  • B- a m-1 m, P0, (K1, P1 ) , (K2, P2 ), , ( Km, Pm ) a a a am/2 -1, P0, ( K1, P1), , ( Km/2 -1, Pm/2 -1) ) am-m/2, Pm/2, ( Km/2+1, Pm/2+1 ), , ( Km, Pm) Km/2 a ( Km/2, a )

  • 3B- 6524 4618 58 65325024 46 5818 65325024 18 653250465824 18 6532 38504658

  • B-

    4 1 n m/2 -1

  • 2n < m/2 -1()n > m/2 -1,()()pn(p0)p0pn

  • 3n < m/2 -1() m/2 -1,()

    24 5818 65465818 24 65

  • 12 26 3 915 2028 323540 5236 3845 4960 65 7412 20 3 91528 323540 5236 3845 4960 65 745B- 20 3 9 12 1528 323540 5236 3845 4960 65 743 9 12 1528 3220 35 40 5236 3845 4960 65 74

  • B+ m B+ m () 2 , m/2 ; n n-1 , ,

  • mB+mB-B- nn+1 B+ nnB-n m/2-1 n m-1B+n m/2 n m1 n m

  • B+, B+,

  • 15 44 59 10 1521 37 4459 9772 9751 59 63 72 85 91 973B+rootsqt

  • 9.1 (datalist): (key): , , ,

  • : r[i]r[j], k[i] == k[j] , , r[i]r[j], r[i]r[j], , :

  • : :

  • 9.2 : ( i , i = 1, , n-1) n-i+1 , i -1 n-1 , 1,

  • , ; V[i+1]V[n-1], , V[i]V[n-1] (Select Sort)

  • void SelectSort (ElemType A[],int n ) { ElemType x; int i,j,k; for ( int i = 1; i
  • 21254925*16080 1 2 3 4 54925160825*4921i = 1i = 2081625*2521 0821,08 1625,16 2149,21

  • 4925*0 1 2 3 4 525*i = 42516084925*4921i = 308162521 25* 2525211608

  • 0 1 2 3 4 549160825*49210825*2521i =1 i k j 49250825*1621i k j 49 2525* 251625i k j 16 < 25

  • 49250825*16210 1 2 3 4 5i k j 21 16k KCN n , i n-i

  • , RMN = 0 RMN = 3(n-1)

  • (Heap Sort), ;

  • 453653187230012345i = 3 486158937369453653187230012345486158937369

    45361853723048931536

    45361893723048531536

  • i = 2 453653187230012345486158937369453653187230012345486158937369i = 1

    45934853723018361536

    45364893723018531536

  • i = 0 453653187230012345486158937369453653187230012345486158937369

    93724853453018361536

    36724853453018361593

  • 453653187230012345486158933694536531872300123454861589336977

    72534836453018361593

    15534836453018367293

  • 45365318723001234548615893369453653187230123454861589336977

    53454836153018367293

    36454836153018537293

  • 4536531872301234548615893369453653187230123454861589336977

    48453636153018537293

    18453636153048537293

  • 4536531872301234548615893369745365318723012345486158933697

    45363618153048537293

    30363618154548537293

  • 4536531872301234548615893369745365318723012345486158933697

    36303618154548537293

    15303618364548537293

  • 4536531872301234548615893369745365318723012345486158933697

    36301518364548537293

    18301536364548537293

  • 4536531872301234548615893369745365318723012345486158933697

    30181536364548537293

    15183036364548537293

  • 4536531872301234548615893369745365318723012345486158933697

    18153036364548537293

    15183036364548537293

  • void Sift(ElemType A[],int n,int i ){ ElemType x= A[i]; int j=2*i+1; while (j
  • void HeapSort (ElemType A[],int n ) { ElemType x; int i; for ( int i = n/ 2-1; i >= 0; i-- ) Sift( A, n,i ); for ( i = 1; i

  • forn-1Sift( ), O(nlog2n), O(nlog2n)forO(1)

  • 9.3

    ,()

  • (Bubble Sort) n n-1 i = 1, 2, , n-2 i V[n -j-1]V[n-j] j=1,2,, n-i

  • 21254925*16080 1 2 3 4 52125*i = 149251625160849flag=10825*4921flag=1i = 2i = 30816flag=125*2521

  • 25*0 1 2 3 4 5i = 44916flag=0082521

    void BubbleSort (ElemType A[],int n) { ElemType x; int i,j,flag; for ( int i = 1; i = i; j--)

  • if (A[j] .stn< A[j-1].stn ) { x=A[j];A[j]=A[j-1]; A[j-1]=x;flag=1;} if (flag==0) return;} }iV[i-1],V[i],,V[n-1], (i-1), ,

  • n-1,,n-1,n-1,i (1 i n) n- i , n-i KCNRMN

  • (Quick Sort) () , ,

  • ()

  • 21254925*16080 1 2 3 4 52125*i = 14925*1625160849pivot08254921i = 2i = 308162525*21pivotpivotpivot

  • 21254925*16080 1 2 3 4 525*i = 1251625160849pivot0825*49081625*25212125,08ii21

    49,164921,16jjij

  • void QuickSort (ElemType A[],int s,int t) { int i=s,j=t+1; ElemType x=A[s]; do{ do i++;while (A[i].stnx.stn); if(i
  • quicksort, 2125*25490816

  • , , , , n, O(n)

  • quicksortO(nlog2n): , , log2(n+1) O(log2n)

  • , , , n-1 , i n-i i

  • 08 16 21 25 25* 49080 1 2 3 4 5 pivot 16 21 25 25* 49 0816 21 25 25* 4921 08 1625 25 25* 49 08 16 21 25* 4925* 08 16 21 2549 08 16 21 25 25*i = 1i = 2i = 3i = 4i = 5

  • 08 16 21 25 25* 490 1 2 3 4 5 pivot21 08 16 21 25 25* 490825* 08 16 21 25 25*49i = 1i = 2, ()O(n): 3

  • n , , n ,

  • 9.4initListA[s] A[m]A[m+1] A[t], mergedListR[s] R[t] (2-way merging) i j A[s] A[m]A[m+1] A[t] k

  • i j , , k i j 08 21 25 25* 49 62 72 93 16 37 54 s m m+1 tinitListi j08 16 21 25 25* 37 49 54 62 72 93 s tkmergeList

  • void TwoMerge (ElemType A[], ElemType R[], int s,int m,int t) { int i, j, k; i=s;j=m+1;k=s; while ( i
  • n n 1 () n / 2 2 ( n 1) n

  • A[0]A[n-1] n len , , 2len , R[n]n2len, , lenlen, 2len len, R

  • void MergePass (ElemType A[], ElemType R[], int n,int len ) { int p = 0; while (p+2*len -1
  • 212525*25*936272083716544921254962930872163754212525*490862729316375408082116252125*254925*623772499354163754627293len=1len=2len=4len=8len=16

  • ()

    void MergeSort (ElemType A[],int n) { ElemType *R =new ElemType[n]; int len = 1; while ( len

  • , MergePass( ) , TwoMerge ( ) n/(2*len) O(n/len) , MergeSort( )MergePass( )log2n ,MergePass ( )O(n), O(nlog2n),

  • n

    n2

    0

    n2

    (

    1

    nlog2n

    n2

    n log2n

    n2

    (

    log2n

    n2

    n2

    0

    n

    (

    1

    n log2n

    n log2n

    (

    1

    n log2n

    n log2n

    (

    n