第五章集合与查找
集合静态查找哈希表 二叉查找树B树和B+树;集合基本概念; colour = { red, orange, yellow, green, black, blue, purple, white } name = { “An”, “Cao”, “Liu”, “Ma”, “Peng”, “Wang”, “zhang” }集合中的成员一般是无序的,但在表示它时,常写在一个序列里。常设定集合中的单元素具有线性有序关系,此关系可记作“
#include const int DefaultSize = 100;class Set {private: int * bitVector; //集合的位向量数组 int MaxSize; //最大元素个数public: Set ( int MaxSetSize = DefaultSize ); ~Set ( ) { delete [ ] bitVector; } ; void MakeEmpty ( ) { //置空 for ( int i = 0; i < MaxSize; i++ ) bitVector[i] = 0; } 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 :: 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;};int Set :: Addmember ( const int x ) { if ( x >= 0 && x < MaxSize ) { bitVector[x] = 1; 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];};Set & Set :: operator + ( Set& right ) { //求并 assert ( MaxSize == right.MaxSize ); for ( int i = 0; i < MaxSize; i++ ) bitVector[i] = bitVector[i] || right.bitVector[i]; return *this;};Set