1. 程式人生 > 實用技巧 >關聯容器 set(集合)的簡單實現

關聯容器 set(集合)的簡單實現

#include <iostream>
using namespace std;

template <typename Comparable>
class Set
{
private:
    struct BinaryNode
    {
        Comparable element;
        BinaryNode* left;
        BinaryNode* right;
        BinaryNode* parent;
        
        BinaryNode(const Comparable& theElement, BinaryNode* lt, BinaryNode* rt, BinaryNode* pt)
          : element(theElement), left(lt), right(rt), parent(pt) {}
    };
    
public: class const_iterator { public: const_iterator(BinaryNode* p): current(p){} const Comparable& operator*() const { return current->element; } const_iterator& operator++() { if(current->right!=NULL) { current
=current->right; while(current->left!=NULL) current=current->left; } else { if(current->parent==NULL) current=NULL; else if(current->element<current->parent->element) current
=current->parent; else if(current->parent->parent==NULL) current=NULL; else { while(current->parent->element>current->parent->parent->element) { current=current->parent; if(current->parent->parent==NULL) break; } if(current->parent->parent==NULL) current=NULL; else current=current->parent->parent; } } return *this; } const_iterator& operator++(int) { const_iterator old = *this; ++(*this); return old; } bool operator==(const const_iterator& rhs) const { return current == rhs.current; } bool operator!=(const const_iterator& rhs) const { return !(*this == rhs); } private: BinaryNode* current; }; class iterator { public: iterator(BinaryNode* p): current(p){} Comparable& operator*() { return current->element; } iterator& operator++() { if(current->right!=NULL) { current=current->right; while(current->left!=NULL) current=current->left; } else { if(current->parent==NULL) current=NULL; else if(current->element<current->parent->element) current=current->parent; else if(current->parent->parent==NULL) current=NULL; else { while(current->parent->element>current->parent->parent->element) { current=current->parent; if(current->parent->parent==NULL) break; } if(current->parent->parent==NULL) current=NULL; else current=current->parent->parent; } } return *this; } iterator operator++(int) { iterator old = *this; ++(*this); return old; } bool operator==(const iterator& rhs) const { return current == rhs.current; } bool operator!=(const iterator& rhs) const { return !(*this == rhs); } private: BinaryNode* current; }; public: Set() { root = NULL; } Set(const Set& rhs) { root = NULL; *this = rhs; } const Set& operator=(const Set& rhs) { if(this!=&rhs) { makeEmpty(); root = clone(rhs.root); } return *this; } Set(initializer_list<Comparable> rhs) { root = NULL; for(auto ele : rhs) insert(ele); } ~Set() { makeEmpty(); } iterator begin() { return iterator(findMin(root)); } const_iterator cbegin() const { return const_iterator(findMin(root)); } iterator end() { return iterator(findMax(root)->right); } const_iterator cend() const { return const_iterator(findMax(root)->right); } const Comparable& findMin() const { return findMin(root)->element; } const Comparable& findMax() const { return findMax(root)->element; } bool contains(const Comparable& x) const { return contains(x, root); } bool isEmpty() const { return root==NULL; } void print() const { print(root); } void makeEmpty() { makeEmpty(root); } void insert(const Comparable& x) { insert(x, root); } void remove(const Comparable& x) { remove(x, root); } private: BinaryNode* root; BinaryNode* par; void insert(const Comparable& x, BinaryNode* &t) { if(isEmpty()) t=new BinaryNode(x, NULL, NULL, NULL); else { if(t==NULL) t=new BinaryNode(x, NULL, NULL, par); if(x<t->element) { par = t; insert(x, t->left); } else if(x>t->element) { par = t; insert(x, t->right); } else ; } } void remove(const Comparable& x, BinaryNode* &t) { if(t==NULL) return; if(x<t->element) remove(x, t->left); else if(x>t->element) remove(x, t->right); else if(t->left!=NULL && t->right!=NULL) { t->element = findMin(t->right)->element; remove(t->element, t->right); } else { BinaryNode *oldNode = t; t = (t->left != NULL)?t->left:t->right; delete oldNode; } } BinaryNode* findMin(BinaryNode* t) const { if(t==NULL) return NULL; if(t->left==NULL) return t; return findMin(t->left); } BinaryNode* findMax(BinaryNode* t) const { if(t!=NULL) while(t->right!=NULL) t=t->right; return t; } bool contains(const Comparable& x, BinaryNode* t) const { if(t==NULL) return false; else if(x<t->element) return contains(x, t->left); else if(x>t->element) return contains(x, t->right); else return true; } void makeEmpty(BinaryNode* t) { if(t!=NULL) { makeEmpty(t->left); makeEmpty(t->right); delete t; } t=NULL; } void print(BinaryNode* t) const { if(t!=NULL) { print(t->left); cout << t->element << endl; print(t->right); } } BinaryNode* clone(BinaryNode* t) const { if(t==NULL) return NULL; return new BinaryNode(t->element, clone(t->left), clone(t->right), t->parent); } };