滁州学院数据结构课件第七章 集合与搜索(C++语言版).pptVIP

  • 2
  • 0
  • 约2.17万字
  • 约 147页
  • 2019-05-06 发布于广东
  • 举报

滁州学院数据结构课件第七章 集合与搜索(C++语言版).ppt

集合及其表示 并查集 静态搜索表 二叉搜索树 AVL树;集合基本概念; colour = { red, orange, yellow, green, black, blue, purple, white } name = { “An”, “Cao”, “Liu”, “Ma”, “Peng”, “Wang”, “zhang” } 集合中的成员一般是无序的,但在表示它时,常写在一个序列里。 常设定集合中的单元素具有线性有序关系,此关系可记作“”,表示“优先于”。 整数、字符和字符串都有一个自然线性顺序。指针也可依据其在序列中安排的位置给予一个线性顺序。;集合(Set)的抽象数据类型; void Assign (Set s1, Set s2); void Union (Set s1, Set s2); void Intersection (Set s1, Set s2); void Difference (Set s1, Set s2); int Contains (Set s, const Type x); int Equal (Set s1, Set s2); int SubSet (Set s1, Set s2); };用位向量实现集合抽象数据类型;集合的位向量(bit Vector)类的定义 #include assert.h 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 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 ); };; s3 = s1+s2; //求s1与s2的并 { 0, 1, …, 16 } s4 = s1*s2; //求s1与s2的交 { 7, 8, 9 } s5 = s1-s2; //求s1与s2的差 { 0, 1, …, 6 } // s1 : { 0, 1, 2, …, 9 } index = s1.SubSet ( s4 ); //s4在s1中首次匹配 cout index endl; //位置,index = 7 // s1 : { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } // s4 : { 7, 8, 9 } equal = s1 == s2; //集合s1与s2比较相等 cout equal endl; //为0, 两集合不等;用位向量实现集合时部分操作的实现 Set :: Set (int MaxSetSize) : MaxSize (MaxSetSize) { assert ( MaxSize 0 ); bitVector = new int [MaxSize]; assert ( bitVector != 0 ); for ( int i = 0; i MaxSize; i++ )

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档