C++中Set容器类的简单实现

代码(这是我的第一个文章,写的不好请大家见谅)


using namespace std;
class  Set{
private:
    class Node;
    class iterator{
    private:
        Node *current;
        friend class Set;
    public:
         iterator(Node *p):current(p){}
         iterator():current(NULL){}
         const int operator*() const
         {
             return current->data;
         }
         iterator & operator++()
         {
             current = current->right;
             return *this;
         }
         iterator & operator++(int)
         {
             iterator old = *this;
             current = current->right;
             return old;
         }
         bool operator==(const iterator &rhs) const
         {
             if (current == rhs.current)
             {
                 return true;
             }
             else
             {
                 return false;
             }
         }
         bool operator!=(const iterator &rhs) const
         {
             if (!(operator==(rhs)))
             {
                 return true;
             }
             else
             {
                 return false;
             }
         }

    };

    class Node{
    private:
        int data;
        Node *left;
        Node *right;
        Node(int a = 0, Node *p1 = NULL,Node *p2 = NULL):data(a),left(p1),right(p2){}

        friend class Set;
        friend class iterator;
    };
    iterator begin( Node *& tep) const
        {
            if (tep->left != NULL)
            {
                begin(tep->left);
            }
            else
            {
                return iterator(tep);
            }
        }
    iterator end( Node *& tep) const
        {
            if ( tep->right != NULL)
            {
                end(tep->right);
            }
            else
            {
                iterator(tep->right);
            }
        }
    const iterator find( Node* & tep,const int & a) const
    {
        if (tep != NULL)
        {
             if (a < tep->data)
            {
                find(tep->left,a);
            }
            else if (tep->data < a)
            {
                find(tep->right,a);
            }
            else
            {
                return iterator(tep);
            }
        }
        else
        {
            return iterator(NULL);
        }

    }
    Node * insert( Node * & tep,const int & x) const
    {
        if (tep == NULL)
        {
            return tep = new Node(x,NULL,NULL);
        }
        else if (x < tep->data)
        {
            return insert(tep->left, x);
        }
        else if(tep->data < x)
        {
            return insert(tep->right,x);
        }
        else
        {
            ;
        }
    }
    Node * clone(Node * & t) const
    {
        if (t == NULL)
            return NULL;
        return new Node(t->data,clone(t->left),clone(t->right));
    }
    void makeEmpty(Node * & t)
    {
        if ( t != NULL)
        {
            makeEmpty(t->left);
            makeEmpty(t->right);
            delete t;
        }
        t = NULL;
    }
    Node * findmin(Node * & tep) const
    {
        if (tep == NULL)
        {
            return NULL;
        }
        else if (tep->left == NULL)
        {
            return tep;
        }
        else
        {
            findmin(tep->left);
        }

    }
    Node * findmax(Node * & tep) const
    {
        if (tep == NULL)
        {
            return NULL;
        }
        else if (tep->right == NULL)
        {
            return tep;
        }
        else
        {
            findmin(tep->right);
        }

    }
    Node * erase(Node * & tep,const  int & x)
    {
            if (tep == NULL)
            {
                return NULL;
            }
            else if (x < tep->data)
            {
                erase(tep->left,x);
            }
            else if(tep->data < x)
            {
                erase(tep->right,x);
            }
            else if(tep->left != NULL && tep->right != NULL)
            {
                tep->data = findmin(tep->right)->data;
                erase(tep->right,tep->data);
                return tep;
            }
            else
            {
                Node * oldNode = tep;
                if (tep->left != NULL)
                    tep = tep->left;
                else
                    tep = tep->right;

                delete oldNode;
                return tep;
            }

    }
    void print(Node * &tep)
    {
        if ( tep != NULL)
        {
            print(tep->left);
            cout << tep->data;
            print(tep->right);

        }

    }
private:

    Node *root;

public:
    void makeEmpty()
    {
        makeEmpty(root);
    }
    Set(Node * p = NULL):root(p){}
    Set(const Set & rhs)
    {
        root = rhs.root;
    }
    const Set & operator=( Set & rhs)
    {
        if(this != &rhs)
        {
            makeEmpty();
            root = clone(rhs.root);
        }
        return *this;
    }
    ~Set()
    {
        makeEmpty();
    }
    iterator begin()
    {
        if(root != NULL)
        {
            begin(root);
        }

    }
    iterator end()
    {
        end();
    }
    iterator find(const int & a)
    {
      return  find(root,a);
    }
    iterator insert(const int & x)
    {
        return iterator(insert(root,x));
    }
    iterator erase(const int x)
    {
        return iterator(erase(root,x));
    }
    void print()
    {
        print(root);
    }

};
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值