红黑树的进阶


template<class T, class Ref, class Ptr>
struct RBTreeIterator
{
    typedef RBTreeNode<T> Node;
    typedef RBTreeIterator<T, Ref, Ptr> Self;

    Node* _node;

    RBTreeIterator(Node* node)

        :_node(node)

    {}

    Self& operator++()

    {

        if (_node->_right)

        {

            Node* leftmost = _node->_right;

            while (leftmost->_left)

            {

                leftmost = leftmost->_left;

            }

            _node = leftmost;

        }

        else

        {

            cur = _node;

            parent = cur->_parnet;

            while (parnet && cur == parnet->_right)

            {

                cur = parent;

                parent = cur->_parnet;

            }

            _node = parent;

        }

        return *this;

    }
    Ref operator*()
    {
        return _node->_data;
    }
    Ptr operator()
    {
        return _node->_data;
    }
    bool operator==(const Self& s)
    {
        return _node == s._node;
    }
    bool operator!=(const Self& s)
    {
        return _node != s._node;
    }

};
template<class K, class T, class KeyOfT>

class RBTree

{

    typedef RBTreeNode<T> Node;

public:

    Iterator begin()

    {

        Node* leftmost = _root;

        while (leftmost && leftmost->_left)

        {

            leftmost = leftmost->_left;

        }

        return Iterator(leftmost);

    }

    bool insert(const T& data)

    {

        if (_root == nullptr)

        {

            _root = new(data);

            _root->_col = BLACK;

            return true;

        }

        KeyOfRT kot;

        Node* parent = nullptr;

        Node* cur = _root;

        while (cur)

        {

            if (kot(cur->data) < kot(data))

            {

                parent = cur;

                cur = cur->_right;

            }

            else if (kot(cur->data) > kot(data))

            {

                parent = cur;

                cur = cur->_left;

            }

            else

            {

                return false;

            }

        }

        cur = new Node(data);

        cur->_col = RED;

        if (kof(parent->_data) < kof(data))

        {

            parent->_right = cur;

        }

        else

        {

            parent->_left = cur;

        }

        cur->_parent = parent;

        while (parent && parent->_col == RED)

        {

            Node* grandfather = parent->_parent;

            if (parent == grandfather->_left)

            {

                Node* uncle = grandfather->_right;

                if (uncle && uncle->_col == RED)

                {

                    parent->_col = uncle->_col = BLACK;

                    grandfather->_col = RED;

                    cur = grandfather;

                    parent = cur->_parent;

                }

                else

                {

                    if (cur == parent->_left)

                    {

                        RotateR(grandfather);

                        parent->_col = BLACK;

                        grandfather->_col = RED;

                    }

                    else

                    {

                        RotateL(parnet);

                        RotateR(grandfather);

                        cur->_col = BLACK;

                        grandfather->_col = RED;

                    }

                    braeak;

                }

            }

        }

        _root->_col = BLACK;

        return true;

    }

private:

    Node* _root = nullptr;

};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值