集合及其表示 等价类与并查集 静态搜索表 二叉搜索树 最佳二叉搜索树 avl...

Download 集合及其表示 等价类与并查集 静态搜索表 二叉搜索树 最佳二叉搜索树 AVL 树

If you can't read please download the document

Upload: heinz

Post on 16-Mar-2016

153 views

Category:

Documents


8 download

DESCRIPTION

第七章 集合与搜索. 集合及其表示 等价类与并查集 静态搜索表 二叉搜索树 最佳二叉搜索树 AVL 树. 集合及其表示. 集合基本概念. 集合是成员 ( 对象或元素 ) 的一个群集。集合中的成员可以是原子 ( 单元素 ) ,也可以是集合。 集合的成员必须互不相同。 在算法与数据结构中所遇到的集合,其单元素通常是整数、字符、字符串或指针,且同一集合中所有成员具有相同的数据类型。. colour = { red, orange, yellow, green, black, blue, purple, white } - PowerPoint PPT Presentation

TRANSCRIPT

  • AVL

  • ()()

  • colour = { red, orange, yellow, green, black, blue, purple, white } name = { An, Cao, Liu, Ma, Peng, Wang, zhang }
  • (Set)template class Set { Set (int MaxSetSize) : MaxSize(MaxSetSize); void MakeEmpty (Set& s); int AddMember (Set& s, const Type x); int DelMember (Set& s, const Type& x);AB A+B AB AB A-BAAABBB

  • void Assign (Set& s1, Set& s2); Set Union (Set& s1, Set& s2); Set Intersection (Set& s1, Set& s2); Set Difference (Set& s1, Set& s2); int Contains (Set& s, const Type& x); int Equal (Set& s1, Set& s2); int SubSet (Set& s1, Set& s2);}

  • { 0, 1, 2, , n } n(0, 1) 0, 1, 2,

  • (bit Vector)

    #include const int DefaultSize = 100;class Set {private: int * bitVector; // int MaxSize; //public: Set ( int MaxSetSize = DefaultSize ); Set ( Set& right ); ~Set ( ) { delete [ ] bitVector; }

  • void MakeEmpty ( ) { // for ( int i = 0; i < MaxSize; i++ ) bitVector[i] = 0; } int GetMember ( const int x ) { // return x >= 0 && x < MaxSize ? bitVector[x] : -1; } int AddMember ( const int x ); // int DelMember ( const int x ); // Set operator = ( Set& right ); // Set operator + ( Set& right ); //

  • Set operator * ( Set& right ); // Set operator - ( Set& right ); // int Contains ( const int x ); // int SubSet ( Set& right ); // int operator == ( Set& right ); //};

    Set s1, s2, s3, s4, s5; int index, equal; for ( int k = 0; k < 10; k++ ) //{ s1.AddMember( k ); s2.AddMember( k +7 ); } // s1 : { 0, 1, 2, , 9 }, s2 : { 7, 8, 9, , 16 }

  • s3 = s1+s2; //s1s2 { 0, 1, , 16 } s4 = s1*s2; //s1s2 { 7, 8, 9 } s5 = s1-s2; //s1s2 { 0, 1, , 6 } // s1 : { 0, 1, 2, , 9 } index = s1.SubSet ( s4 ); //s1s4 cout
  • Set :: Set (int MaxSetSize) : MaxSize (MaxSetSize) { assert ( MaxSize > 0 ); // bitVector = new int [MaxSize]; // assert ( bitVector != NULL ); for ( int i = 0; i < MaxSize; i++ ) // bitVector[i] = 0;}

  • Set :: Set ( Set& right ) { // MaxSize = right.MaxSize; // bitVector = new int [MaxSize]; // assert ( bitVector != NULL ); for ( int i = 0; i < MaxSize; i++ ) // bitVector[i] = right.bitVector[i];}

  • int Set :: Addmember ( const int x ) { if ( x >= 0 && x < MaxSize ) { bitVector[x] = 1; return 1; } return 0;}

    int Set :: DelMember ( const int x ) { if ( x >= 0 && x < MaxSize ) { bitVector[x] = 0; return 1; } return 0; }

  • void Set :: operator = ( Set& right ) { // assert ( MaxSize == right.MaxSize ); for ( int i = 0; i < MaxSize; i++ ) // bitVector[i] = right.bitVector[i];}

    int Set :: Contains ( const int x ) { // assert ( x >= 0 && x < MaxSize ); return bitVector[x];}

  • Set Set :: operator + ( Set& right ) { // assert ( MaxSize == right.MaxSize ); Set temp ( MaxSize ); for ( int i = 0; i < MaxSize; i++ ) temp.bitVector[i] = bitVector[i] || right.bitVector[i]; return temp;}thisrighttemp0 1 1 1 0 0 0 0 1 1 00 0 1 0 0 1 0 1 0 1 00 1 1 1 0 1 0 1 1 1 0

  • Set Set :: operator * ( Set & right ) { // assert ( MaxSize == right.MaxSize ); Set temp ( MaxSize ); for ( int i = 0; i < MaxSize; i++) temp.bitVector[i] = bitVector[i] && right.bitVector[i]; return temp;}thisrighttemp0 1 1 1 0 0 0 0 1 1 00 0 1 0 0 1 0 1 0 1 00 0 1 0 0 0 0 0 0 1 0

  • Set Set :: operator - ( Set & right ) { // assert ( MaxSize == right.MaxSize ); Set temp ( MaxSize ); for ( int i = 0; i < MaxSize; i++ ) temp. bitVector[i] = bitVector[i] && !right.bitVector[i]; return temp;}thisrighttemp0 1 1 1 0 0 0 0 1 1 00 0 1 0 0 1 0 1 0 1 00 1 0 1 0 0 0 0 1 0 0

  • int Set :: operator == ( Set& right ) { // assert ( MaxSize == right.MaxSize ); for ( int i = 0; i < MaxSize; i++) if ( bitVector[i] != right.bitVector[i] ) return 0; return 1;}thisright0 0 1 1 0 0 0 0 1 1 00 0 1 0 0 1 0 1 0 1 0i

  • int Set :: SubSet ( Set& right ) {// this right assert ( MaxSize == right.MaxSize ); for ( int i = 0; i < MaxSize; i++) if ( bitVector[i] && ! right.bitVector[i]) return 0; return 1;}thisright0 0 1 1 0 0 0 0 1 1 00 0 1 0 0 1 0 1 0 1 00 0 0 1 i

  • firstfirst081723354972 e0, e1, , en e0 < e1 < < en

  • template class SetList;

    template class SetNode {friend class SetList;public: SetNode ( ) : link(NULL) { }; SetNode ( Type item ) : data (item), link (NULL) { };private: Type data; SetNode * link;};

  • template class SetList {private: SetNode *first, *last;public: SetList ( ) // { first = last = new SetNode; } SetList ( SetList& right ); ~SetList ( ) { MakeEmpty( ); delete first; } void MakeEmpty ( ); // int AddMember ( const Type& x ); int DelMember ( const Type& x );

  • void operator = ( SetList& right ); //rightthis SetList operator + ( SetList& right ); //thisrightSetList operator * ( SetList& right ); //thisrightSetList operator - ( SetList& right ); //thisrightint Contains ( const Type& x ); //xint operator == ( SetList& right ); //thisright

  • int Min (Type&); // int Max (Type&); //}

    template SetList ::SetList ( SetList & right ) { SetNode * srcptr = right.first->link; first = last = new SetNode; while ( srcptr != NULL ) { last = last->link = new SetNode (srcptr->data);

  • srcptr = srcptr->link; }// last->link = NULL;}

    template int SetList ::Contains ( const Type& x ) {//x, 1, 0 SetNode * temp = first->link; while ( temp != NULL && temp->data < x ) temp = temp->link; //

  • if ( temp != NULL && temp->data == x ) return 1;//, 1 else return 0;//, 0}

    template int SetList ::AddMember ( const Type& x ) { SetNode *p = first->link, *q = first; while ( p != NULL && p->data < x ) { q = p; p = p->link; } // if ( p != NULL && p->data == x ) return 0; //

  • SetNode * s = new SetNode (x); s->link = p; q->link = s; // if ( p == NULL ) last = s; // return 1;}

    template int SetList ::DelMember ( const Type& x ) { SetNode * p = first->link, * q = first; while ( p != NULL && p->data < x ) { q = p; p = p->link; } //

  • if ( p != NULL && p->data == x ) { // q->link = p->link;// if ( p == last ) last = q; // delete p; return 1;//x } else return 0; //}

    template void SetList ::

  • operator = ( SetList& right ) {//rightthis SetNode * pb = right.first->link; // SetNode * pa = first = // new SetNode; while ( pb != NULL ) { // pa->link = new SetNode(pb->data); pa = pa->link; pb = pb->link; } pa->link = NULL; last = pa; //}

  • //thisright, //tempthisright

    template SetList SetList :: operator + ( SetList& right ) {firstright.first08172349temp.first231735papbpc0817233549

  • SetNode *pb = right.first->link; SetNode *pa = first->link; SetList temp; SetNode *pc = temp.first; while ( pa != NULL && pb != NULL ) { if ( pa->data == pb->data ){ // pc->link=new SetNode(pa->data); pa = pa->link; pb = pb->link; } else if ( pa->data < pb->data ) { pc->link=new SetNode(pa->data); pa = pa->link; } else{ //pa->data > pb->data

  • pc->link=new SetNode(pb->data); pb = pb->link; } pc = pc->link; } if ( pa != NULL ) pb = pa; //pb while ( pb != NULL ) { // pc->link = new SetNode(pb->data); pc = pc->link; pb = pb->link; } pc->link = NULL; temp.last = pc;// return temp;}

  • template int SetList :: operator == ( SetList & right ) { SetNode * pb = right.first->link; SetNode * pa = first->link; while ( pa != NULL && pb != NULL ) if ( pa->data == pb->data ) //, { pa = pa->link; pb = pb->link; } else return 0; //, 0 if ( pa != NULL || pb != NULL ) return 0; //, 0 return 1;}

  • (Equivalence Class)()x, y, zx x () x y, y x x y y z, x z

  • (=) S S1, S2, S3, S ( i, j )

  • void equivalence ( ) { ; while { ( i, j ); ; } ; for ( ) ;} S = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 },: 0 4, 3 1, 6 10, 8 9, 7 4, 6 8, 3 5, 2 11, 11 0

  • {0},{1},{2},{3},{4},{5},{6},{7},{8},{9}, {10},{11}0 4 {0,4},{1},{2},{3},{5},{6},{7},{8},{9},{10},{11}3 1 {0,4}, {1,3},{2},{5},{6},{7},{8},{9},{10},{11}6 10{0,4},{1,3},{2},{5},{6,10},{7},{8},{9},{11} 8 9 {0,4},{1,3},{2},{5},{6,10},{7},{8,9},{11}7 4 {0,4,7},{1,3},{2},{5},{6,10},{8,9},{11}6 8 {0,4,7},{1,3},{2},{5},{6,8,9,10},{11}3 5 {0,4,7},{1,3,5},{2},{6,8,9,10},{11}2 11{0,4,7},{1,3,5},{2,11},{6,8,9,10}11 0{0,2,4,7,11},{1,3,5},{6,8,9,10}

  • m, n seq[n] seq[i] i , i data i

  • ( i, j ) , i j j i , out[n], out[i] i

  • i = 0 , i = 0 , 0 ( 0, j ), 0 j ,( j, k ), k 0 0 , i = 1

  • enum Boolean { False, True }; class ListNode { // friend void equivalence ( ); private: int data;// ListNode *link;// ListNode ( int d ) { data = d; link = NULL; } };

  • typedef ListNode *ListNodePtr;

    ()

    out[i] == False i

    void equivalence ( ) { ifstream inFile ( "equiv.in", ios::in ); //

  • if ( !inFile ) { cerr
  • while ( inFile.good ( ) ) { // x = new ListNode ( j ); // j x->link = seq[i]; seq[i] = x; //i y = new ListNode ( i ); //i y->link = seq[j]; seq[j] = y; //j inFile >> i >> j; // } for ( i = 0; i < n; i++ ) if ( out[i] == False ) { //, cout
  • ListNode *top = NULL; // while (1) { // while ( x ) { //, x=0 j = x->data; //j if ( out[j] == False ) { //, cout link = top; top = x; //x x = y; //x } else x = x->link; //, }

  • if ( top == NULL ) break; // else { x = seq[top->data]; top = top->link; } //, , x // } } delete [ ] seq; delete [ ] out;}

  • (Union-Find Sets) Union (Root1, Root2) // Find (x) // UFSets (s) //0 n-1 n

  • i i -1

  • S1= {0, 6, 7, 8 }, S2= { 1, 4, 9 }, S3= { 2, 3, 5 } 0 S11 S22 S30427681935

  • , UFSets(s) , ,

    Find(i) i i j, Find(i) == Find(j), i j , Union(i, j)

  • S1parent S1, S2 S3 -4 4 -3 2 -3 2 0 0 0 40 1 2 3 4 5 6 7 8 90768419235S2S3

  • S1 S2parent S1 S2 S3 -7 4 -3 2 0 2 0 0 0 40 1 2 3 4 5 6 7 8 907684194190876

  • const int DefaultSize = 10;class UFSets { //private: int *parent; // int size; //public: UFSets ( int s = DefaultSize ); // ~UFSets ( ) { delete [ ] parent; } // const UFSets& operator = (UFSets& right); // void Union ( int Root1, int Root2 );// :

  • int Find ( int x );// : x void UnionByHeight ( int Root1, int Root2 );// : };

    UFSets :: UFSets ( int s ) { // size = s; // parent = new int [size]; // for ( int i = 0; i < size; i++ ) parent[i] = -1;//}

  • -5012301234Find (4)Find (3) = 3 Find (2) =2 Find (1) = 1Find (0) = 0 = -5 < 0

  • int UFSets :: Find ( int x ) { if ( parent [x] < 0 ) return x; else return Find ( parent [x] ); } -5 0 1 2 3parentParent[4] =3 Parent[3] =2Parent[2] =1Parent[1] =0Parent[0] =-50 1 2 3 4

  • void UFSets :: Union ( int Root1, int Root2 ) {//Root1Root2 parent[Root1] += parent[Root2]; parent[Root2] = Root1; //Root2Root1}

    FindUnion n n parent[i] = -1Union(n-2, n-1), , Union(1, 2), Union(0, 1)

  • -1-1-1-1-10234-3-503213341332202314Union(0,1)-23-41421234Union(1,2)Union(2,3)Union(3,4)

  • Union O(1)n-1UnionO(n) Find(0), Find(1), , Find(n-1), i Find(i) O(i) n

  • -1-1-1-1-101234-1-10-7256-5-222332013456233020562314Union(2, 0)

  • void UFSets :: WeightedUnion ( int Root1, int Root2 ) {//Union int temp = parent[Root1] + parent[Root2]; if ( parent[Root2] < parent[Root1] ) { parent[Root1] = Root2; //Root2 parent[Root2] = temp; //Root1Root2 } else { parent[Root2] = Root1; //Root1 parent[Root1] = temp; //Root2Root1 }}

  • -0-0-0-0-001234-0-00-2256-2-122332013456233020562314Union(2, 0)

  • Union j i parent[j] root[j] parent[j] root[i]0067867819193535 i = 5

  • int UFSets :: CollapsingFind ( int i ) {// for ( int j = i; parent[j] >= 0; j = parent[j] ); // j while ( parent[i] != j ) { // parent[i] j int temp = parent[i]; parent[i] = j; i = temp; } return j;}

  • (Search) ,, , , ,

  • ()

  • ,

    template class dataList;

    template class Node {friend class dataList;private: Type key;

  • other;public: Node ( const Type& value ) : key (value) { } Type getKey ( ) const { return key; } void setKey ( Type k ) { key = k; }};

    template class dataList {protected: Node *Element; // int ArraySize; // int CurrentSize; //

  • public: dataList ( int sz = 10 ) : ArraySize (sz) { Element = new Node [sz]; CurrentSize = 0; } virtual ~dataList ( ) { delete [ ] Element; } int Length( ) { return CurrentSize; } friend ostream& operator > ( istream& InStream, dataList& InList );};

  • template class searchList : public dataList {//public: searchList ( int sz = 10 ) : dataList (sz) { } virtual ~searchList ( ) { } virtual int Search ( const Type & x ) const;};

    template istream& operator >> ( istream& InStream, dataList& InList )

  • {//InStreamInList cout > InList.CurrentSize; cout
  • template ostream& operator
  • ()ASL (Average Search Length) n n , x, xx

  • (Sequential Search), , CurrentSize , x, , ; x,

  • template int searchList :: Search ( const Type& x ) const {// x , CurrentSize// Element[CurrentSize].key = x; int i = 0; //xCurrentSize while ( Element[i].key != x ) i++; // return i;}

  • const int Size = 10;main ( ) { // searchList List1 (Size); // float Target; cin >> List1; cout Target; if ( (Location = List1.search (Target) ) != len ) cout
  • i pi, i ci, : ci = i +1, i = 0, 1, , n-1

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

  • template int SearchList ::Search ( const Type& x, int loc ) const {// Element[0..n-1] //, // loc if ( loc >= CurrentSize ) return -1; // else if ( Element[loc].key == x ) return loc; // else return Search ( x, loc+1 ); //}

  • template int searchList :: Search ( const Type& x ) const {//x for ( int i = 0; i < CurrentSize; i++ ) if ( Element[i].key == x ) return i; // else if ( Element[i].key > x ) break; return -1; //, }

  • ( 10, 20, 30, 40, 50, 60 )105060======203040

  • 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

  • template class orderedList : public dataList { //,public: orderedList (int sz = 10) : dataList (sz) { } ~orderedList ( ) { } int BinarySearch ( const Type& x ) const;};

    template int orderedList :: //

  • BinarySearch ( const Type & x, const int low, const int high ) const {// int mid = -1; if ( low x ) mid = BinarySearch ( x, low, mid -1 ); } return mid;}

  • template int orderedList :: BinarySearch ( const Type & x ) const { // int high = CurrentSize-1, low = 0, mid; while ( low x ) high = mid - 1; // else return mid; // } return -1; //}

  • ( 10, 20, 30, 40, 50, 60 )1050======30

    204060ASLunsucc = (2*1+3*6)/7 = 20/7 ASLsucc = (1+2*2+ 3*3)/6 = 14/6

  • 35154550251020302245

  • n = 2h-1 h-1 2h = n+1, h = log2(n+1)01, 01; 12, 12; , i (0 i h) 2i , i i+1 pi = 1/n

  • ( Binary Search Tree ) (key) () ()

  • 351545504025102030

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

  • #include template class BST;

    template Class BstNode : public BinTreeNode { friend class BST ;

  • protected: Type data; BstNode *leftChild, *rightChild; };public: BstNode ( ) : leftChild (NULL), rightChild (NULL) { } // BstNode ( const Type d, BstNode * L = NULL, BstNode *R = NULL) : data (d), leftChild (L), rightChild (R) { } void setData ( Type d ) { data = d; }

  • Type GetData ( ) { return data; } ~BstNode ( ) { }//};

    template class BST : public BinaryTree {private: BstNode *root; // Type RefValue; // void MakeEmpty ( BstNode *& ptr ); void Insert ( Type x, BstNode *& ptr ); //

  • void Remove ( Type x, BstNode *& ptr ); // void PrintTree ( BstNode * ptr ) const; BstNode *Copy ( const BstNode * ptr ); // BstNode *Find ( Type x, BstNode * ptr ); // BstNode *Min ( BstNode * ptr ) const; // BstNode *Max ( BstNode * ptr ) const; //public:

  • BST ( ) : root (NULL) { } // BST ( Type value ); // ~BST ( ); // const BST& operator = ( const BST& Value ); void MakeEmpty ( ) { MakeEmpty ( root ); root = NULL; } void PrintTree ( ) const { PrintTree ( root ); } int Find ( Type x ) // { return Find ( x, root ) != NULL; } Type Min ( ) { return Min ( root )->data; } Type Max ( ) { return Max ( root )->data; }

  • void Insert ( Type x ) { Insert ( x, root ); } // void Remove ( Type x ) { Remove ( x, root ); } //x}

  • xNULLx

  • 35154550402510203045 28

  • template BstNode * BST :: Find ( Type x, BstNode * ptr ) const {// if ( ptr == NULL ) return NULL; // else if ( x < ptr->data ) // return Find ( x, ptr->leftChild ); else if ( x > ptr->data ) // return Find ( x, ptr->rightChild ); else return ptr; //,}

  • template BstNode * BST :: Find ( Type x, BstNode * ptr ) const {// if ( ptr != NULL ) { BstNode * temp = ptr; // while ( temp != NULL ) { if ( temp->data == x ) return temp; if ( temp->data < x ) temp = temp->rightChild; // else temp = temp->leftChild; // }

  • } return NULL; //}35154550402510203028 28

  • : ,:

  • template void BST :: Insert ( Type x, BstNode * & ptr) { if ( ptr == NULL ){ // ptr = new BstNode (x); // if ( ptr == NULL ) { cout
  • { 53, 78, 65, 17, 87, 09, 81, 15 }535378537865537865175378658717537865091787537865811787095378651517870981

  • template BST :: BST ( Type value ) {//, RefValue ////, , //RefValue0 Type x; root = NULL; RefValue = value; cin >> x; while ( x != RefValue ) { Insert ( x, root ); cin >> x; }}

  • 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, 53948817092353788117940945782365538188179409452365

  • template void BST ::Remove (const Type& x, BstNode * & ptr) { BstNode * temp; if ( ptr != NULL ) if ( x < ptr->data ) // Remove ( x, ptr->leftChild ); else if ( x > ptr->data ) // Remove ( x, ptr->rightChild ); else if ( ptr->leftChild != NULL && ptr->rightChild != NULL ) {

  • temp = Min( ptr->rightChild ); //ptr ptr->data = temp->data; // Remove ( ptr->data, ptr->rightChild ); //ptrtemp } else {// ptr temp = ptr; if ( ptr->leftChild == NULL ) ptr = ptr->rightChild; // else if ( ptr->rightChild == NULL ) ptr = ptr->leftChild; //

  • n n! (), , () delete temp; }}

  • { a1, a2, a3 } = { do, if, to } p1, p2, p3, q0, q1, q2, q3doiftodoiftoq0q1p1q2p2q3p3q0q1q2q3p1p2p3(a)(b)

  • doiftoq0q1p1q2p2q3p3doiftoq0q1p1q2p2q3p3(d)(c)doiftoq0q1p1q2p2q3p3(e)

  • ASLsuccp[i]c[i] (= l[i] + 1)

  • p[i] = 1 / n ASLunsuccq[j]c'[j](= l'[j]) q[j] = 1 / (n+1)

  • (1) p[i] = 1/3, 1 i 3, q[j] = 1/4, 0 j 3 (a): ASLsucc = 1/3*3+1/3*2+1/3*1 = 6/3, ASLunsucc = 1/4*3*2+1/4*2+1/4*1 = 9/4 (b): ASLsucc = 1/3*2*2+1/3*1 = 5/3, ASLunsucc = 1/4*2*4 = 8/4 (c): ASLsucc = 1/3*1+1/3*2+1/3*3 = 6/3, ASLunsucc = 1/4*1+1/4*2+1/4*3*2 = 9/4 (d): ASLsucc = 1/3*2+1/3*3+1/3*1 = 6/3, ASLunsucc = 1/4*2+1/4*3*2+1/4*1 = 9/4

  • 1 k 2k k = 0, 1, n I (e): ASLsucc = 1/3*1+1/3*3+1/3*2 = 6/3, ASLunsucc = 1/4*1+1/4*3*2+1/4*2 = 9/4 (b)

  • 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, n :

  • p[1] = 0.5, p[2] = 0.1, p[3] = 0.05 q[0] = 0.15, q[1] = 0.1, q[2] = 0.05, q[3] = 0.05(2)

  • doiftodoiftoq0=0.15q1=0.1p1=0.5q2=0.05p2=0.1q3=0.05p3=0.05q0=0.15q1=0.1q2=0.05q3= 0.05p1=0.5p2=0.1p3=0.05(a)(b)(a): ASLsucc = 0.5*3+0.1*2+0.05*1 = 1.75 ASLunsucc = 0.15*3+0.1*3+0.05*2+ 0.05*1 = 0.9(b): ASLsucc = 0.5*2+0.1*1+0.05*2 = 1.2 ASLunsucc = (0.15+0.1+0.05+0.05)*2 = 0.7

  • (c): ASLsucc = 0.5*1+0.1*2+0.05*3 = 0.85, ASLunsucc = 0.15*1+0.1*2+0.05*3+0.05*3 = 0.75.(d) : ASLsucc = 0.5*2+0.1*3+0.05*1 = 1.35, ASLunsucc = 0.15*2+0.1*3+0.05*3+0.05*1 = 0.8.doifto q0=0.15q1=0.1p1=0.5q2=0.05p2=0.1q3=0.05p3=0.05doiftoq0=0.15q1=0.1p1=0.5q2=0.05p2=0.1q3=0.05p3=0.05(d)(c)

  • doiftoq0=0.15q1=0.1p1=0.5q2=0.05p2=0.1q3=0.05p3=0.05(e) (c)(e)(c)(e) (e) : ASLsucc = 0.5*1+ 0.1*3+0.05*2 = 0.9; ASLunsucc = 0.15*1+ 0.1*3+0.05*3+0.05*2 = 0.7;

  • template class InorderIterator { private: BST & ref; // Stack < BstNode * > itrStack; //public: InorderIterator ( BST & Tree ) : ref (Tree) { Init ( ); } // int Init ( ); //

  • int operator ! ( ); // Type operator ( ) ( ); // int operator ++ ( ); //,}

    template int InorderIterator :: Init ( ) { itrStack.MakeEmpty ( ); // if ( ref.root != NULL ) //, itrStack.Push ( ref.root ); return ! itrStack.IsEmpty ( ); //0}

  • template BST ::BST ( const BST & T ) : root (NULL) { InorderIterator itr ( ); for ( itr.init ( ); ! itr; itr++ ) Insert ( itr ( ) );}

    template int InorderIterator :: operator ! ( ) { return ! itrStack.IsEmpty ( ); //0}

  • template int InorderIterator :: operator ++ ( ) { BstNode * current = itrStack.GetTop ( ); BstNode * next = current->leftChild; if ( next != NULL ) // { itrStack.Push ( next ); return 1; } while ( ! itrStack.IsEmpty ( ) ) { // current = itrStack.Pop ( ); next = current->rightChild; if ( next != NULL ) //, { itrStack.Push ( next ); return 1; } }

  • return 0;}

    template Type InorderIterator :: operator ( ) ( ) { BstNode * current = itrStack.GetTop ( ); return current->data; //}

    template BST :: ~BST ( ){ MakeEmpty ( ); //}

  • n n! ()

  • (1) , , 1 k 2k k = 0, 1, n I 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, n

  • (2) { key[1], key[2], key[3],key[n] } T[0][n]

  • l [i] a[i] l [j] j C[0][n] p[i] q[j]

  • T[i][j], { key[i+1], key[i+2],, key[j] } { p[i+1], p[i+2],, p[j] } { q[i], q[i+1], q[i+2],, q[j] }W[i][j] W[i][j] = q[i] + p[i+1] + q[i+1] + p[i+2] + + q[i+2]++ p[j] + q[j]. 0 i j nW[i][j]: W[i][i] = q[i] //,

  • W[i][i+1] = q[i] + p[i+1] + q[i+1] = W[i][i] + p[i+1] + q[i+1] // W[i][i+2] = W[i][i+1] + p[i+2] + q[i+2] // W[i][j] = W[i][j-1] + p[j] + q[j] // { key[i+1], , key[k-1], key[k], key[k+1], , key[j] }T[i][j], ki < k j

  • C[i][j] = p[k]+C[i][k-1]+W[i][k-1]+C[k][j]+W[k][j]C[i][k-1] { key[i+1], key[i+2],, key[k-1] } T[i][k-1]C[i][k-1]+W[i][k-1] 1 C[k][j] { key[k+1], key[k+2],, key[j] } T[k][j]C[k][j] + W[k][j] 1

  • C[i][j] = p[k]+C[i][k-1]+W[i][k-1]+C[k][j]+W[k][j] : W[i][j] = p[k] + W[i][k-1] + W[k][j] C[i][j] = W[i][j] + C[i][k-1] + C[k][j] k = i+1, i+2, , j , C[i][j] kT[i][j]

  • T[0][1]T[1][2]T[n-1][n]T[i-1][i] (1 i n) key[i] C[i-1][i] = W[i-1][i] R[0..n][0..n] R[0][1] = 1, R[1][2] = 2, , R[n-1][n] = n, T[0][2], T[1][3], , T[n-2][n]T[i-2][i] key[i-1], key[i]

  • key[i-1], key[i] C[i-2][i] 3 4 n , k, k R[0][n] , C[0][n] , R[0][k-1] , R[k][n]

  • : / key1 key2 key3 do if to p1=50 p2=10 p3=5 q0=15 q1=10 q2=5 q3=5501510doifto10105555T[0][1] T[1][2] T[2][3]

  • C 115 165 50 60 W 90 90 35 35 R 1 2 2 3 T[0,0] T[0,1] T[1,1] T[1,2] T[1,2] T[2,2] T[2,3] T[3,3] 501510dodoifif155010101055toif10510101055555toifT[0][2] T[1][3]

  • C 150 190 215 W 100 100 100 R 1 2 3 T[0][0] T[0][1] T[0][2] T[1][3] T[2][3] T[3][3]501510doif10555to15105510550ifdoto501510doif10555toT[0][3] T[0][3] T[0][3]

  • 0 0 75 115 1501 0 25 502 0 153 00 15 75 90 1001 10 25 352 5 153 5W[i][j]C[i][j]R[i][j]3 { do, if, to } 0 1 2 30 1 2 30 1 2 30 0 1 1 11 0 2 22 0 33 0 p1=50, p2=10, p3=5q0=15, q1=10, q2= 5, q3= 5

  • AVL AVL AVLAVL1 ABCABCDEDE

  • balance (balance factor), ,balance AVL -1, 0, 11,, AVL, n O(log2n),O(log2n)

  • AVLtemplate class AVLTree {public: struct AVLNode {//AVL Type data; int balance; AVLNode * left, * right; AVLNode ( ) : left (NULL), right (NULL), balance (0) { } AVLNode ( Type d, AVLNode * l = NULL, AVLNode *r = NULL) : data (d), left (l), right (r), balance (0) { } };

  • protected: Type RefValue; AVLNode *root; int Insert (AVLNode*& Tree, Type x); void RotateLeft (AVLNode * Tree, AVLNode *& NewTree); void RotateRight (AVLNode * Tree, AVLNode *& NewTree); void LeftBalance (AVLNode *& Tree); void RightBalance (AVLNode *&Tree); int Height (AVLNode * t) const;public:

  • AVLTree ( ) : root (NULL) { } AVLNode (Type Ref ) : RefValue (Ref), root (NULL) { } int Insert (Type x) { return Insert (root, x); } friend istream& operator >> (istream& in, AVLTree& Tree); friend ostream& operator
  • () (), AVL ,

  • ,

  • (RotateLeft )

  • AE1A2A3ACECA

  • template void AVLTree ::RotateLeft ( AVLNode * Tree, AVLNode *& NewTree ) {// NewTree = Tree->right; Tree->right = NewTree->left; NewTree->left = Tree;} (RotateRight )

  • hAD1A-2A3 ABDBA

  • template void AVLTree ::RotateRight ( AVLNode * Tree, AVLNode *& NewTree) {// NewTree = Tree->left; Tree->left = NewTree->right; NewTree->right = Tree;} (RotationLeftRight)

  • A1A-2EBEB

  • EAtemplate void AVLTree :: LeftBalance ( AVLNode * &Tree, int & taller ) {

  • AVLNode *leftsub = Tree->left, *rightsub; switch ( leftsub->balance ) { case -1 : Tree->balance = leftsub->balance = 0; RotateRight ( Tree, Tree ); taller = 0; break; case 0 : cout
  • case -1: Tree->balance = 1; leftsub->balance = 0; break; case 0 : Tree->balance = leftsub->balance = 0; break; case 1 : Tree->balance = 0; leftsub->balance = -1; } rightsub->balance = 0; RotateLeft ( leftsub, Tree->left ); RotateRight ( Tree, Tree ); taller = 0; } }

  • (RotationRightLeft)A1A2DCDC

  • DAtemplate void AVLTree::

  • RightBalance ( AVLNode * &Tree, int& taller ) { AVLNode *rightsub = Tree->right, *leftsub; switch ( rightsub->balance ) { case 1 : Tree->balance = rightsub->balance = 0; RotateLeft ( Tree, Tree ); taller = 0; break; case 0 : cout
  • case 1 : Tree->balance = -1; rightsub->balance = 0; break; case 0 : Tree->balance = rightsub->balance = 0; break; case -1 : Tree->balance = 0; rightsub->balance = 1; break; } leftsub->balance = 0; RotateRight ( rightsub, Tree->left ); RotateLeft ( Tree, Tree ); taller = 0; }}

  • AVLAVL |balance| > 1AVL>>
  • 1616 { 16, 3, 7, 11, 9, 26, 18, 14, 15 }03163-10701-2731600073110-117316161190-1-23716900013711269161101122

  • 181803163-101602739000182611-1732616119-1971614001711262691111

  • 1518231816-273000117149-1161501112626141-29

  • success()success1taller1

  • template int AVLTree ::Insert ( AVLNode* &tree, Type x, int &taller ) {//AVL int success; if ( tree == NULL ) { tree = new AVLNode (x); success = ( tree != NULL ) ? 1 : 0; if ( success ) taller = 1; } else if ( x < tree->data ) { success = Insert ( tree->left, x, taller ); if ( taller )

  • switch ( tree->balance ) { case -1 : LeftBalance ( tree, taller ); break; case 0 : tree->balance = -1; break; case 1 : tree->balance = 0; taller = 0; } } else if ( x > tree->data ) { success = Insert ( tree->right, x, taller ); if ( taller ) switch ( tree->balance ) { case -1 : tree->balance = 0; taller = 0; break; case 0 : tree->balance = 1; break; case 1 : RightBalance ( tree, taller );

  • } } return success;}

  • AVL(1) x:xxxxxxNULLx1

  • (2) x: x y () y x yyxy(1)

  • x shorter shorter balance balance shorter True x p shorter True shorter False

  • case 1 : p balance0 balance 1 -1 shorter False0hhh-1,1hh-1pp

  • case 2 : p balance0 p balance0 shorter True-1h+1hh,0hhpp1

  • case 3 : p balance0 p p q (), q balance 3 case 3a, 3b3c p

  • case 3a : q () balance 0 p shorterFalse1hhh-1ph0q1hh-1phq-1

  • case 3b : q balance p balance, p q balance0, shorter True1hh-1ph1q0h-1phq0h-1h-1

  • case 3c : p q balance, , q p balance0balance, shorterTrue01hh-1p-1qh-1h-2h-1h-2h-1rh-1h-1h-1h-100pqr1

  • ABCDEFGHIJKLMNOPQRST0000000-1-1-1-1-1-1-111111

  • ABCDEFGHIJKLMNOPQRST0000000-1-1-1-1-1-1-111111P PO, OP, OOP

  • ABCDEFGHIJKLMNOQRST000000-1-1-1-1-1-1-111111P OR, R, M 1

  • ABCDEFGHIJKLMNOQRST000000-1-1-1-1-1-1-11001P M 1, MME, 0

  • ABCDEFGHIJNKLMOR00000-100-1-1-1-10100P00TQ0S

  • AVLAVL h nO(h)AVLh Nh h AVL h-1 h-2 N-1 = 0 () N0 = 1 () Nh = Nh-1 + Nh-2 +1 , h > 0

  • , h 0, Nh = Fh+3 -1 n AVL 1.44*log2(n+1)-1AVL O(log2n)()BB+