# Venn Set Template class Type class Set Set

• Slides: 163

void Assign ( Set& s 1, Set& s 2 ); void Union ( Set& s 1, Set& s 2 ); void Intersection ( Set& s 1, Set& s 2 ); void Difference ( Set& s 1, Set& s 2 ); int Contains ( Set &s, const Type & x ); int Equal ( Set &s 1, Set &s 2 ); int Sub. Set ( Set &s 1, Set &s 2 ); }

private: int * bit. Vector; int Max. Size; public: Set ( int Max. Set. Size = Default. Size ); ~Set ( ) { delete [ ] bit. Vector; } void Make. Empty ( ) { for ( int i = 0; i < Max. Size; i++ ) bit. Vector[i] = 0; } int Add. Member ( const int x ); int Del. Member ( 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 Sub. Set ( Set & right ); int operator == ( Set & right ); }; 使用示例 Set s 1, s 2, s 3, s 4, s 5; int index, equal; //构造集合 for ( int k = 0; k < 10; k++ ) //集合赋值 { s 1. Add. Member( k ); s 2. Add. Member( k +7 ); } // s 1 : { 0, 1, 2, …, 9 }, s 2 : { 7, 8, 9, …, 16 }

s 3 = s 1 + s 2; //求s 1与s 2的并 { 0, 1, …, 16 } s 4 = s 1 * s 2; //求s 1与s 2的交 { 7, 8, 9 } s 5 = s 1 - s 2; //求s 1与s 2的差 { 0, 1, 2, 3, 4, 5, 6 } index = s 1. Sub. Set ( s 4 ); //求s 4在s 1中首次匹配 cout << index << endl; //位置， index = 7 equal = s 1 == s 2; //集合s 1与s 2比较相等 cout << equal << endl; //equal为 0, 两集合不等 用位向量实现集合时部分操作的实现 Set : : Set (int Max. Set. Size) : Max. Size (Max. Set. Size) { assert ( Max. Size > 0 ); bit. Vector = new int [Max. Size]; assert ( bit. Vector != 0 );

for ( int i = 0; i < Max. Size; i++ ) bit. Vector[i] = 0; } int Set : : Addmember ( const int x ) { assert ( x >= 0 && x < Max. Size ); if ( ! bit. Vector[x] ) { bit. Vector[x] = 1; return 1; } return 0; } int Set : : Del. Member ( const int x ) { assert ( x >= 0 && x < Max. Size ); if ( bit. Vector[x] ) { bit. Vector[x] = 0; return 1; } return 0; }

Set& Set : : operator = ( Set& right ) { assert ( Max. Size == right. Max. Size ); for ( int i =0; i

Set& Set : : operator * ( Set & right ) { assert ( Max. Size == right. Max. Size ); for ( int i = 0; i < Max. Size; i++) bit. Vector[i] = bit. Vector[i] && right. bit. Vector[i]; return *this; } Set& Set : : operator - ( Set & right ) { assert ( Max. Size == right. Max. Size ); for ( int i = 0; i < Max. Size; i++ ) bit. Vector[i] = bit. Vector[i] && !right. bit. Vector[i]; return *this; }

int Set : : Contains ( const int x ) { assert ( x >= 0 && x < Max. Size ); return bit. Vector[x]; } int Set : : operator == ( Set & right ) { assert ( Max. Size == right. Max. Size ); for ( int i = 0; i < Max. Size; i++) if ( bit. Vector[i] != right. bit. Vector[i] ) return 0; return 1; } int Set : : Sub. Set (Set & right ) { assert ( Max. Size == right. Max. Size );

template class Set. Node { friend class Set. List; public: Set. Node (const Type & item ) : data (item), link (NULL); private: Type data; Set. Node *link; }; template class Set. List { private: Set. Node *first, *last; public:

Set. List ( ); void Make. Empty ( ); int Add. Member ( const Type & x ); int Del. Member ( const Type & x ); Set. List & operator = ( Set. List & right ); Set. List & operator + ( Set. List & right ); Set. List & operator * ( Set. List & right ); Set. List & operator - ( Set. List & right ); int Contains ( const Type & x );

int operator == ( Set. List & right ); Type & Min ( ); Type & Max ( ); } 集合构造函数 template void Set. List : : Set. List ( ) { Set. Node *first = *last = new Set. Node(0); }

template int Set. List : : Add. Member ( const Type & x ) { Set. Node *p = first→link, *q = first; while ( p != NULL && p→data < x ) { q = p; p = p→link; } if ( p != NULL && p→data == x ) return 0; Set. Node *s = new Set. Node (x); s→link = p; q→link = s; if ( p == NULL ) last = s; return 1; }

template int Set. List : : Del. Member ( const Type & x ) { Set. Node *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; } else return 0; }

template Set. List& Set. List : : operator + ( Set. List & right ) { Set. Node *pb = right. first→link; Set. Node *pa = first→link; Set. Node *pc = first; while ( pa != NULL && pb != NULL ) { if ( pa→data == pb→data ) { pc→link = pa; pa = pa→link; pb = pb→link; } else if ( pa→data < pb→data ) { pc→link = pa; pa = pa→link; } else

{ pc→link = new Set. Node (pb→data); pb = pb→link; } pc = pc→link; } if ( pa != NULL ) pc→link = pa; else { while ( pb != NULL ) { pc→link = new Set. Node (pb→data); pc = pc→link; pb = pb→link; } pc→link = NULL; last = pc; } return *this; }

template Set. List& Set. List: : operator * ( Set. List & right ) { Set. Node *pb = right. first→link; Set. Node *pa = first→link; Set. Node *pc = first; while ( pa != NULL && pb != NULL ) { if ( pa→data == pb→data ) { pc = pc→link; pa = pa→link; pb = pb→link; } else if ( pa→data < pb→data ) { pc→link = pa→link; delete pa; pa = pc→link; } else pb = pb→link; }

while ( pa != NULL ) { pc→link = pa→link; delete pa; pa = pc→link; } last = pc; return *this; } template Set. List& Set. List : : operator - ( Set. List & right ) { Set. Node *pb = right. first→link; Set. Node *pa = first→link;

Set. Node *pc = first; while ( pa != NULL && pb != NULL ) { if ( pa→data == pb→data ) { pc→link = pa→link; delete pa; pa = pc→link; pb = pb→link; } else if ( pa→data < pb→data ) { pc = pc→link; pa = pa→link; } else pb = pb→link; } if ( pa == NULL ) last = pc; return *this; }

template int Set. List : : operator == ( Set. List & right ) { Set. Node *pb = right. first→link; Set. Node *pa = first→link; while ( pa != NULL && pb != NULL ) if ( pa→data == pb→data ) { pa = pa→link; pb = pb→link; } else return 0; if ( pa != NULL || pb != NULL ) return 0; return 1; }

const int Default. Size = 10; class UFSets { //并查集的类定义 public: UFSets ( int s = Default. Size ); ~UFSets ( ) { delete [ ] parent; } const UFSets & operator = ( UFSets const & Value ); void Union ( int Root 1, int Root 2 ); int Find ( int x ); void Union. By. Height ( int Root 1, int Root 2 ); private: int *parent; int size; };

UFSets : : UFSets ( int s ) { //构造函数 size = s; parent = new int [size+1]; for ( int i = 0; i <= size; i++ ) parent[i] = -1; } unsigned int UFSets : : Find ( int x ) { //搜索操作 if ( parent[x] <= 0 ) return x; else return Find ( parent[x] ); } void UFSets : : Union ( int Root 1, int Root 2 ) { //并 parent[Root 2] = Root 1; //Root 2指向Root 1 }

void UFSets : : Weighted. Union ( int Root 1, int Root 2 ) { //按Union的加权规则改进的算法 int temp = parent[Root 1] + parent[Root 2]; if ( parent[Root 2] < parent[Root 1] ) { parent[Root 1] = Root 2; //Root 2中结点数多 parent[Root 2] = temp; //Root 1指向Root 2 } else { parent[Root 2] = Root 1; //Root 1中结点数多 parent[Root 1] = temp; //Root 2指向Root 1 } }

template class data. List { public: data. List ( int sz = 10 ) : Array. Size (sz), Element (new Node [sz]) { } virtual ~data. List ( ) { delete [ ] Element; } friend ostream &operator << (ostream & Out. Stream, const data. List & Out. List ); friend istream & operator >> ( istream & In. Stream, data. List & In. List ); protected: Type *Element; int Array. Size, Current. Size; };

template class search. List : public data. List { //作为派生类的静态搜索表的类定义 public: search. List ( int sz = 10 ) : data. List (sz) { } virtual ~search. List ( ) { } virtual int Search ( const Type & x ) const; };

template istream & operator >> ( istream & In. Stream, data. List & In. List ) { //从输入流对象In. Stream输入数据到数据表In. List cout << “输入数组当前大小 : ”; Instream >> In. List. Current. Size; cout << “输入数组元素值 : n”; for ( int i = 0; i < In. List. Current. Size; i++ ) { cout << “元素 ” << i << “ : ”; In. Stream >> In. List. Element[i]; } return In. Stream; }

template ostream & operator << ( ostream & Out. Stream, const data. List & Out. List ) { //将数据表Out. List内容输出到输出流对象Out. Stream << “数组内容 : n”; for ( int i = 0; i < Out. List. Current. Size; i++ ) Out. Stream << Out. List. Element[i] << ‘ ’; Out. Stream << endl; Out. Stream << “数组当前大小 : ” << Out. List. Current. Size << endl; return Out. Stream; }

template int search. List : : Search ( const Type & x ) const { //顺序搜索的迭代算法，0号元素为监视哨 Element[0]. set. Key ( x ); int i = Current. Size; while (Element[i]. get. Key ( ) != x ) i --; return i; } template int earch. List : : Search ( const Type & x, int & loc ) const { //顺序搜索的递归算法 if ( loc >= Current. Size ) return -1; else if ( Element[loc]. get. Key( ) == x ) return loc; else return Search ( x, loc+1 ); }

const int Size = 10; main ( ) { //顺序搜索的主过程 search. List List 1 (Size); float Target; int Location; cin >> List 1; cout << List 1; //输入/输出数据表List 1 cout << “搜索浮点数 : ”; cin >> Target; if ( ( Location = List 1. search (Target ) ) != 0 ) cout << “ 找到下标 ” << Location << endl; else cout << “ 没有找到. n”; }

template class ordered. List : public data. List { //有序表的类定义, 继承了数据表 public: ordered. List (int sz = 10) : data. List (sz) { } virtual ~ordered. List ( ) { } virtual int Search ( const Type & x ) const; };

template int ordered. List : : Binary. Search ( const Type & x, const int low, const int high ) const { //折半搜索的递归算法 int mid = -1; if ( low <= high ) { mid = ( low + high ) / 2; if ( Element[mid]. get. Key( ) < x ) mid = Binary. Search ( x, mid +1, high ); else if ( Element[mid]. get. Key( ) > x ) mid = Binary. Search ( x, low, mid -1 ); } return mid; }

template in ordered. List : : Binary. Search ( const Type & x ) const { //折半搜索的迭代算法 int high = Current. Size-1, low = 0, mid; while ( low <= high ) { mid = ( low + high ) / 2; if ( Element[mid]. get. Key ( ) < x ) low = mid + 1; else if ( Element[mid]. get. Key ( ) > x ) high = mid - 1; else return mid; } return -1; }

template Class Bst. Node : public Bin. Tree. Node { //二叉搜索树结点类 friend class BST ; public: Bst. Node ( ) : left. Child (NULL), right. Child (NULL) { } Bst. Node ( const Type d ) : data (d), left. Child (NULL), right. Child (NULL) { } Bst. Node ( const Type d = 0, Bst. Node *L = NULL, Bst. Node *R = NULL) : data (d), left. Child (L), right. Child (R) { } ~Bst. Node ( ) { }

protected: Type data; Bst. Node *left. Child, *right. Child; }; template class BST : public : Binary. Tree { private: Bst. Node *root; //二叉搜索树的根指针 Type Ref. Value; //数据输入停止的标志 Bst. Node *lastfound; //最近搜索到的结点的指针 void Make. Empty ( Bst. Node *& ptr );

void Insert ( const Type & x, Bst. Node *& ptr ); //插入 void Remove ( const Type & x, Bst. Node *& ptr ); //删除 void Print. Tree ( Bst. Node *ptr ) const; Bst. Node *Copy (const Bst. Node *ptr ); //复制 Bst. Node *Find (const Type & x, Bst. Node * ptr ) const; //搜索 Bst. Node *Min ( Bst. Node * ptr ) const; //求最小 Bst. Node *Max ( Bst. Node * ptr ) const; //求最大

friend class BSTIterator; //中序游标类 public: BST ( ) : root (NULL) { } BST ( Type value ) : Ref. Value (value), root (NULL) { } ~BST ( ); const BST & operator = ( const BST & Value ); void Make. Empty ( ) { Make. Empty ( ); root = NULL; } void Print. Tree ( ) const { Print. Tree ( root ); } int Find ( const Type & x ) const { return Find ( x, root ) != NULL; }

Type Min ( ) { return Min ( root )→data }; Type Max ( ) { return Max ( root )→data }; void Insert ( const Type & x ) { Insert ( x, root ); } void Remove ( const Type & x ) { Remove ( x, root ); } }

template Bst. Node * BST : : Find (const Type & x, Bst. Node * ptr ) const { //二叉搜索树的递归的搜索算法 if ( ptr == NULL ) return NULL; //搜索失败 else if ( x < ptr→data ) //在左子树递归搜索 return Find ( x, ptr→left. Child ); else if ( x > ptr→data ) //在右子树递归搜索 return Find ( x, ptr→right. Child ); else return ptr; //相等, 搜索成功 }

template Bst. Node * BST : : Find (const Type & x, Bst. Node * ptr ) const { //二叉搜索树的迭代的搜索算法 if ( ptr != NULL ) { Bst. Node * temp = ptr; while ( temp != NULL ) { if ( temp→data == x ) return temp; //成功 if ( temp→data < x ) temp = temp→right. Child; //右子树 else temp = temp→left. Child; //左子树 } } return NULL; //搜索失败 }

template void BST: : Insert (const Type & x, Bst. Node * & ptr) { //递归的二叉搜索树插入算法 if ( ptr == NULL ) { //空二叉树 ptr = new Bst. Node (x); //创建含 x 结点 if ( ptr == NULL ) { cout << "Out of space" << endl; exit (1); } } else if ( x < ptr→data ) //在左子树插入 Insert ( x, ptr→left. Child ); else if ( x > ptr→data ) //在右子树插入 Insert ( x, ptr→right. Child ); }

} else if ( ptr→left. Child != NULL && ptr→right. Child != NULL ) { temp = Min ( ptr→right. Child ); ptr→data = temp→data; Remove ( ptr→data, temp ); } else { temp = ptr; if ( ptr→left. Child == NULL ) ptr = ptr→right. Child; else if ( ptr→right. Child == NULL ) ptr = ptr→left. Child; delete temp; }

0 1 2 3 0 15 75 90 100 1 10 25 35 2 5 15 3 5 W[i][j] 0 0 0 1 2 3 1 1 0 R[i][j] 2 1 2 0 3 1 2 3 0 0 75 115 150 1 0 25 50 2 0 15 3 0 C[i][j] 3个关键码 { do, if, to } 的 最优二叉搜索树 p 1=50, p 2=10, p 3=5 q 0=15, q 1=10, q 2= 5, q 3= 5

AVL树的类定义 template class AVLTree { public: struct AVLNode { Type data; AVLNode *left, *right; int balance; 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 Ref. Value; AVLNode *root; int Insert ( AVLNode* &tree, Type x, int &taller ); void Rotate. Left ( AVLNode *Tree, AVLNode * &New. Tree ); void Rotate. Right ( AVLNode *Tree, AVLNode * &New. Tree ); void Left. Balance ( AVLNode * &Tree, int &taller ); void Right. Balance ( AVLNode * &Tree, int &taller );

int Depth ( AVLNode *t ) const; public: AVLTree ( ) : root (NULL) { } AVLNode ( Type Ref ) : Ref. Value (Ref), root (NULL) { } int Insert ( Type x ) { int taller; return Insert ( root, x, taller ); } friend istream& operator >> ( istream& in, AVLTree& Tree ); friend ostream& operator << ( ostream& out, const AVLTree& Tree ); int Depth ( ) const; }

template void AVLTree : : Rotate. Left ( AVLNode *Tree, AVLNode * &New. Tree ) { //左单旋转的算法 New. Tree = Tree→right; Tree→right = New. Tree→left; New. Tree→left = Tree; }

template void AVLTree: : Rotate. Right ( AVLNode *Tree, AVLNode * &New. Tree) { //右单旋转的算法 New. Tree = Tree→left; Tree→left = New. Tree→right; New. Tree→right = Tree; }

template void AVLTree: : Left. Balance ( AVLNode * &Tree, int & taller ) { //左平衡化的算法 AVLNode *leftsub = Tree→left, *rightsub; switch ( leftsub→balance ) { case -1 : Tree→balance = leftsub→balance = 0; Rotate. Right ( Tree, Tree ); taller = 0; break; case 0 : cout << “树已经平衡化. n"; break; 树已经平衡化

case 1 : rightsub = leftsub→right; switch ( rightsub→balance ) { 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; break; }

rightsub→balance = 0; Rotate. Left ( leftsub, Tree→left ); Rotate. Right ( Tree, Tree ); taller = 0; } }

template void AVLTree: : Right. Balance ( AVLNode * &Tree, int & taller ) { //右平衡化的算法 AVLNode *rightsub = Tree→right, *leftsub; switch ( rightsub→balance ) { case 1 : Tree→balance = rightsub→balance = 0; Rotate. Left ( Tree, Tree ); taller = 0; break; case 0 : cout << “树已经平衡化. n"; break; 树已经平衡化 case -1 : leftsub = rightsub→left;

switch ( leftsub→balance ) { 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; Rotate. Right ( rightsub, Tree→left ); Rotate. Left ( Tree, Tree ); taller = 0; } }

2 1 11 7 3 -2 左右双旋 18 -2 9 16 14 11 7 26 18 0 3 9 15 0 1 -1 14 0 15 从空树开始的建树过程 26 0 16

} } case -1 : tree→balance = 0; taller = 0; break; case 0 : tree→balance = 1; break; case 1 : Right. Balance ( tree, taller ); break; } return success;

% 并查集操作的算法 n 查找 0 -5 1 0 2 1 3 2 4 3 Find (4) Find (3) =3 Find (2) =2 Find (1) =1 Find (0) = 0 = -5 < 0 结束

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

n 合并 3 4 -2 3 0 -1 2 3 merge(3, 4) 4 1 2 -1 -1 1 -3 2 2 3 3 merge(2, 3) 4 3 -1 -4 1 2 3 4 -1 0 1 2 3 4 -5 0 1 2 3 merge(1, 2) merge(0, 1)