ADT template class Type class Vector public Vector
静态查找表(ADT) template <class Type> class Vector { public: Vector ( int size); // 构造函数,静态查找表的结点个数为 size. ~ Vector ( ) { delete [ ]Array; } const Type & operator [ ] ( int index ) const; //具有越界检查的下标操作。 const Vector & operator = ( const Vector & R); //大小相等的向量的复制。 int Length( ) const { return Array. Size; } void Double( ) { Resize( Array. Size * 2 ); } // 在向量的单元用完时,容量加倍。 void Resize( int Newsize); // 修改向量的大小。 void Sentinel( const Type key ){ Array[0] = key; } //置 0号单元的内容为待查 key。 protected: Type * Array; // 保存结点的数组 int Array. Size; // 大小。 void Get. Array( ); Vector(const Vector & R ); // 冻结使用构造函数复制另一向量的功能。 };
静态查找表(ADT) • 部分操作的实现: template <class Type> const Type & Vector<Type> : : operator [ ] ( int index ) const { Exception( index ≤ 0 || index > Array. Size, “Out of boundary!”); return Array[index]; } template <class Type> const Vector<Type> & Vector<Type> : : operator = ( const Vector<Type> R ){ if ( this != &R ) { Exception( Array. Size != R. Array. Size, “Size is not same!”); for(int k = 1; k <= Array. Size; k++ ) Array[k] = R. Array[k]; } return *this; }
静态查找表(ADT) • 部分操作的实现: template <class Type> void Vector<Type> : : Resize( int New. Size ){ Type * old. Array = Array; const int minsize = Min(Array. Size, New. Size); // 取二者小者。 Array. Size = New. Size; Get. Array( ); for(int k = 1; k <= minsize; k++ ) Array[k] = old. Array[k]; delete [ ]old. Array; } template <class Type> void Vector<Type> : : Get. Array( ){ Array = new Type[Array. Size+1]; } template <class Type> Vector<Type> : : Vector( int Size){ Array. Size = Size; Get. Array( ); }
折半查找的性能 mid= 4 0 4 8 9 10 11 13 19 1 2 3 4 5 6 7 high=7 low=1 mid= 4 0 4 8 9 10 11 13 19 1 2 3 4 5 6 7 low=1 20 8 high=8
二叉排序树(ADT) template <class Type> class BST { // 二叉排序树的 ADT public: BST ( ) { } // 二叉排序树的构造函数。 ~BST ( ) { } // 二叉排序树的析构函数。 virtual int Insert( const Type & x) = 0; // 插入 x。 virtual int Remove( const Type & x ) = 0; // 删除 x。 virtual const Type & Find(const Type & x) = 0; // 查找值为 x 的结点。 virtual int Is. Found( const Type & x )= 0; // 若 x 找到,则返回 1。 virtual const Type & Find. Min( ) = 0; // 返回最小值。 virtual const Type & Find. Max( ) = 0; //返回最大值。 virtual int Is. Empty( ) const = 0; // 二叉排序树为空,返回 1,否则为 0。 virtual int Is. Full( )const = 0; // 二叉排序树为满,返回 1,否则为 0。 virtual void Make. Empty( ) = 0; }; //清除二叉排序树中的所有结点。
二叉排序树的结点类 • 二叉排序树中的结点类的实现,为了表示简单,没有采用继承的方式,采用结构。 二叉排序树BST的结点表示。 template <class Type> struct BSTNode { // 二叉排序树的结点的表示。 Type data; BSTNode * left; // 结点的数据场。 // 给出结点的左儿子的地址。 BSTNode * right; // 给出结点的右儿子的地址。 int Balance. Factor; // 结点的平衡度,用于 AVL 树。 int Size; // 以本结点为根的子树的所有结点的个数,用于顺序统计。 BSTNode ( ): left(NULL), right(NULL), Size(1), Balance. Factor(1) { } BSTNode ( const Type x ) : data(x), left(NULL), right(NULL), Size(1), Balance. Factor(1) { } BSTNode (const Type x, BSTNode * L, BSTNode * R ): data(x), left(L), right(R), Size(1), Balance. Factor(1) { } ~BSTNode( ) { } };
二叉排序树类 template <class Type> class Binary. Search. Tree : public BST<Type> { public: Binary. Search. Tree ( ) : Last. Find(NULL), Root(NULL) { } ~Binary. Search. Tree ( ) { Free. Tree(Root); } int Insert( const Type & x) { return Insert(x, Root); } //插入 x 到以 Root 为根的二叉排序树。成功则返回 1。 const Type & Find( const Type & x ) { return ( Last. Find = Find(x, Root)) ? Last. Find->data: Item. Not. Found } // 若查找 x 成功,则返回二叉排序树中的相应数据项。否则,返回Item. Not. Found。 const Type & Find. Min( )const { const BSTNode * p = Find. Min(Root); return p ? p->data: Item. Not. Found; } //返回二叉排序树中的最小的数据项。若树为空,返回 Item. Not. Found。 const Type & Find. Max( ) const { const BSTNode * p = Find. Max(Root); return p ? p->data: Item. Not. Found; } //返回二叉排序树中的最大的数据项。若树为空,返回 Item. Not. Found。
二叉排序树类 int Is. Found (const Type & x) { return Find(x, Root)!= NULL; } //若 x找到,则返回 True。 int Was. Found ( ) const { return Last. Find != NULL; }//最近一次查找成功,则返回 True。 int Remove( const Type & x ) { return Remove(x, Root); } // 从二叉排序树中删除值为 x 的结点,删除成功返回 True。 int Remove. Min( ) { return Remove. Min(Root); } // 从二叉排序树中删除最小结点,删除成功返回 True。 int Is. Empty( ) const { return Root == NULL; } //二叉排序树为空,返回 True,否则为 False。 void Make. Empty ( ){ Free. Tree(Root); Root = NULL; } //清除二叉排序树中的所有结点。 protected: BSTNode<Type> * Root; const BSTNode<Type> * Last. Find; Type Item. Not. Found; // 用于查找失败时返回。 const BSTNode<Type> * Find ( const Type & x,const BSTNode<Type> * T) const; const BSTNode<Type> * Find. Min ( const BSTNode<Type> * T) const; const BSTNode<Type> * Find. Max ( const BSTNode<Type> * T) const; int Insert( const Type & x,BSTNode<Type> * & T); int Remove. Min( BSTNode<Type> * & T); int Remove( const Type & x,BSTNode<Type> * & T); };
删除操作的实现 template <class Type> int Binary. Search. Tree<Type>: : Remove( const Type & x, BSTNode<Type > * & T ) { BSTNode<Type > * p; if ( T == NULL ) return 0; // 删除失败,返回 0。 else if ( x < T->data ) return Remove( x, T->left ); // 转向左子树。 else if ( x > T->data ) return Remove( x, T->right ); // 转向右子树。 else if ( T->left != NULL && T->right != NULL ) { // 被删结点的左右儿子非空。 p=( BSTNode<Type> * )Find. Min(T->right); T->data=p->data; //复制右子树最小结点数据值。 return Remove. Min( T->right ); //删除被删结点的右子树的最小结点。 } 被删结 点 p = T; // 处理被删结点的至少一个儿子结点为空的情况。 return 1; } // 删除成功,返回 1。删除失败,返回 0。 450 122 T = ( T->left == NULL ) ? T->right : T->left; delete p; 400 99 110 105 500 250 200 300 替 身
删除操作的实现 template <class Type> int Binary. Search. Tree<Type> : : Remove. Min( BSTNode<Type > * & T ) { 400 if ( T == NULL ) return 0; else if ( T->left != NULL ) 450 122 return Remove. Min( T->left ); BSTNode<Type > * p; p = T; T = T->right; delete p; return 1; } // 删除 T 的最左方的左后代,删除成功,返回 1。删除失败,返回 0。 99 110 105 250 200 300 500
顺序统计 实现方法:建立类 Order. Statistics. Tree 。 template <class Type> class Order. Statistics. Tree : public Binary. Search. Tree<Type> { public: Order. Statistics. Tree ( ) { } const Type & Find. Kth ( int K ) const { if ( Root = = NULL ) return Item. Not. Found; else return ( Find. Kth( K, Root))->data; } // 查找第 K 个最小的结点。 成功,则返回的相应数据值。否则,返回Item. Not. Found。 int Insert( const Type & x) { return Insert(x, Root); } //插入 x 到以 Root 为根的二叉排序树中。成功则返回 1。 int Remove( const Type & x ) { return Remove(x, Root); } // 删除数据值为 x 的结点,删除成功返回 1。 int Remove. Min( ) { return Remove. Min(Root); } // 删除最小结点,删除成功返回 True。 private: int Insert( const Type & x,BSTNode<Type> * & T); int Remove. Min( BSTNode<Type> * & T); int Remove( const Type & x,BSTNode<Type> * & T); const BSTNode<Type> * Find. Kth ( int K, const BSTNode<Type> * T ) const; int Treesize( ) const { return Root ? Root->Size : 0; } };
顺序统计及其实现:求出序列中的第 k 个大的或小的结点。主要操作的实现。 template <class Type> const BSTNode<Type> * Order. Statistics. Tree<Type> : : Find. Kth ( int K, const BSTNode<Type > * T ) const { if ( T == NULL ) return NULL; // 错,返回。 int Left. Size = T->left ? T->left->Size : 0; // 求得左子树的结点数。 if ( K == Left. Size + 1 ) return T; // 求得第 K 个结点。 if ( K < Left. Size + 1 ) return Find. Kth( K, T->left ) // 转向左子树。 return Find. Kth( K - Left. Size – 1, T->right ); // 转向右子树。 } // 求第K个最小的结点。失败,返回NULL,否则返回第K个最小结点的地址。 template <class Type> int Order. Statistics. Tree<Type> : : Insert (const Type & x, BSTNode<Type > * & T ) { if ( T == NULL ) return ( T = new BSTNode<Type> (x) ) != NULL; //新结点插入。 else if ( x < T->data ) return Insert( x, T->left ) ? ++T->Size : 0; //转向左子树。 else if ( x>T->data ) return Insert( x, T->right ) ? ++T->Size : 0; //转向右子树。 else return 0; // 若结点已经存在,返回不必重复插入标志。 } // 成功插入时,其祖先结点的 Size值加 1。
例:红黑树的插入 • 插入调整实例,以在下图的红黑树中插入结点 18 为例。 14 14 5 25 12 3 20 15 5 3 50 15 14 20 12 15 23 23 14 50 30 3 60 25 9 5 26 S 60 30 26 28 G 50 25 9 X X 25 20 P S P 12 3 60 30 26 23 5 G 28 9 20 12 15 28 23 18 50 26 30 60
例:B-树的插入 例: 3 阶 B_ 树的插入操作。m=3, m/2 - 1 = 1; 至少 1 个关键字,二个儿子结点。 45,70 24,30 3,12 26 53 39 50 7 24 30 90 61 85 100 3 12 26 39 50 90 61 85 100 45 7插入 24 45 70 7 3 53 12 24 30 26 53 39 50 90 61 85 7 30 12 26 70 53 90 100 3 39 50 61 85 100
Hash表的实现 template <class Type> class Hash. Table { public: enum Kindof. Cell { Active, Empty, Deleted }; // 标志 Hash表中单元的状态。Active:保存结点。Empty:单元为空。 // Deleted: 保存的结点已被删除。 Hash. Table ( ); ~Hash. Table ( ) { delete[ ] arr; } const Hash. Table & operator = ( const Hash. Table & R ); int Insert( const Type & x); //插入 x。 int Remove( const Type & x); //删除 x。 const Type & Find( const Type & x); // 查找成功,返回结点数据值。可用函数 Was. Found 确定查找是否成功。 int Is. Found ( const Type & x) const; //测试 x 是否存在,存在返回 1,否则为 0。 int Was. Found( ) const; //最近一次查找成功,则返回 1。 int Is. Empty( ) const; // Hash 表为空,返回 1,否则为 0。 void Make. Empty( ); // 清空 Hash 表。
Hash表的实现 private: struct Hash. Cell { Type Element; // 保存结点数据值。 Kindof. Cell info; Hash. Cell( ): info(Hash. Table<Type> : : Empty ) { } Hash. Cell( const Type & E, Kindof. Cell i=Empty ): Element(E), info(i) { } }; enum { Default. Size = 11 } ; int Arr. Size; // 散列表用的所在的单元个数。 int Current. Size; // 散列表中当前保存的结点个数。 int Last. Find. Ok; // 最后一次查找成功则为 1。 Hash. Cell * Arr; // 保存散列表用的数组。 void Allocate. Arr( ); // 创建一个数组。 unsigned int Find. Pos( const Type & x ) const; // 查找值为 x 的结点在散列表中的单元地址。 };
Hash表的实现 template <class Type > void Hash. Table<Type> : : Allocate. Arr( ) { Arr = new Hash. Cell[ Arr. Size ]; } // 创建散列表用的数组。 template <class Type> Hash. Table<Type> : : Hash. Table ( ): Arr. Size(Default. Size ) { Allocate. Arr( ); Current. Size = 0; } // 构造函数。 template <class Type > void Hash. Table<Type> : : Make. Empty( ) { Current. Size = 0; for ( int k = 0; k< Arr. Size; k++) Arr[k]. info = Empty; } // 清空散列表用的数组。 Element info 0 1 2 3 4 5 6 7 8 9 10 Hash 表 info 用 以 标 志 Hash表 中 单 元 的 状 态 。 Active:已保存结点;Empty:单元为空 而 Deleted: 表示保存的结点已被删除。
Hash表的实现 template <class Type > unsigned int Hash. Table<Type> : : Find. Pos( const Type & x ) const { unsigned int k = 0; // 用于记录探测失败的次数。 unsigned int Current. Pos = Hash(x, Arr. Size); while( Arr[ Current. Pos ]. info != Empty && Arr[ Current. Pos ]. Element != x ) { Current. Pos += 2 * ++k – 1; while ( Current. Pos >= Arr. Size ) Current. Pos -= Arr. Size; } return Current. Pos; } // 探测包含 x 的单元地址。 template <class Type > const Type & Hash. Table<Type> : : Find( const Type & x ) { unsight int Current. Pos = Find. Pos(x); Last. Find. Ok = Arr[ Current. Pos ]. info = = Active; return Arr[Current. Pos]. Element; } // 查找成功,返回结点数据值。可用函数 Was. Found 确定查找是否成功。 Element info 0 1 2 3 4 5 6 7 8 9 10 Hash 表
Hash表的实现 template < class Type > int Hash. Table<Type> : : Is. Found(const Type & x ) const { int Current. Pos = Find. Pos(x); return Arr[Current. Pos]. Info == Active; } //. 测试 x 是否被保存在散列表之中。 template < class Type > int Hash. Table<Type> : : Remove(const Type & x ) { unsigned int Current. Pos = Find. Pos(x); if ( Arr[Current. Pos]. info != Active ) return 0; Arr[Current. Pos]. Info = Deleted return 1; } // 删除包含 x 的单元返回 1,否则返回 0。 template < class Type > int Hash. Table<Type> : : Insert(const Type & x ) { unsigned int Current. Pos = Find. Pos(x); if ( Arr[Current. Pos]. info = = Active ) return 0; // 已经存在,插入失败。 Arr[Current. Pos] = Hash. Cell(x, Active); return 1; } // 将值 x 插入,返回 1。若散列表中已经存在值为 x 的单元,则插入失败,返回 0。 Element info 0 1 2 3 4 5 6 7 8 9 10 Hash 表
- Slides: 135