红黑树RBTree

#pragma once
enum colour    //子节点的颜色
{
    RED,
    BLANK,
};
template<class K,class V>
struct RBTreeNode
{
    K _key;
    V _value;
    RBTreeNode<K, V>* _left;
    RBTreeNode<K, V>* _right;
    RBTreeNode<K, V>* _parent;
    colour _col;

    RBTreeNode(const K& key, const V& value)
        :_key(key)
        , _value(value)
        , _left(NULL)
        , _right(NULL)
        , _parent(NULL)
        , _col(RED)
    {}
};

template<class K,class V>
class RBTree
{
    typedef RBTreeNode<K, V> Node;
public:
    RBTree()
        :_root(NULL)
    {}

    ~RBTree()
    {
        Destory(_root);
    }
    
    bool Insert(const K& key, const V& value)    //插入节点
    {
        if (_root == NULL)
        {
            _root = new Node(key, value);
            _root->_col = BLANK;
            return true;
        }

        Node* parent = NULL;
        Node* cur = _root;
        while (cur)     //寻找插入的位置,如果当前节点的key值比要查找的值小,则往树的右边走,如果当前节点的key值比查找的值大,则往树的左边走。否则就是              树中已经有这个值了,不能插入了
        {
            if (cur->_key < key)
            {
                parent = cur;
                cur = cur->_right;
            }
            else if (cur->_key>key)
            {
                parent = cur;
                cur = cur->_left;
            }
            else
            {
                return false;
            }
        }

         cur = new Node(key, value);
        if (parent->_key<key)      //选择将节点插入到cur的左边还是右边,需要跟parent->_key比较
        {
            parent->_right = cur;
            cur->_parent = parent;
        }
        else
        {
            parent->_left = cur;
            cur->_parent = parent;
        }
        
        while (cur != _root&&parent->_col == RED)
        {
            Node* grandparent = parent->_parent;
            //uncle存在且为红
            if (grandparent->_left == parent)
            {
                Node* uncle = grandparent->_right;
                if (uncle&&uncle->_col == RED)
                {
                    parent->_col = uncle->_col = BLANK;
                    grandparent->_col = RED;

                    cur = parent;
                    parent = cur->_parent;
                }
                else   //uncle不存在或者uncle存在且为黑
                {
                    if (parent->_right == cur)
                    {
                        _RotateL(parent);
                        //parent = cur;
                        swap(parent, cur);
                    }
                    _RotateR(grandparent);

                    parent->_col = BLANK;
                    grandparent->_col = RED;

                    cur = grandparent;
                    parent = cur->_parent;
                    //break;
                }
            }
            else if (parent == grandparent->_right)
            {
                Node* uncle =grandparent->_left;
                if (uncle&&uncle->_col==RED)
                {
                    parent->_col = uncle->_col = BLANK;
                    grandparent->_col = RED;

                    cur = grandparent;
                    parent = cur->_parent;
                }
                else   //uncle不存在或者uncle为黑
                {
                    if (parent->_left == cur)
                    {
                        _RotateR(parent);
                        //cur = parent;
                        swap(cur, parent);
                    }
                    _RotateL(grandparent);

                    parent->_col = BLANK;
                    grandparent->_col = RED;

                    cur = parent;
                    parent = cur->_parent;
                    //break;
                }
                }
            }
            _root->_col = BLANK;
            return true;
        }
        
    
    bool Isblance()
    {
        if (_root == NULL)
            return true;

        int count = 0;
        Node* cur = _root;
        while (cur)
        {
            if (cur->_col == BLANK)
                count++;
                cur = cur->_left;
        }
        int k = 0;
        return _Isblance(_root, count, k);
    }

    void InOrder()
    {
        _InOrder(_root);
    }
protected:
    void _InOrder(Node* root)
    {
        if (root == NULL)
            return;

        _InOrder(root->_left);
        cout << root->_key << "  " << root->_value << "  ";

        if (root->_col == RED)
            cout << "红" << endl;
        else
            cout << "黑" << endl;

        _InOrder(root->_right);
    }
    void _RotateR(Node* root)    //右旋
    {
        Node* subL = root->_left;
        Node* subLR = subL->_right;
        Node* ppNOde = root->_parent;

        root->_left = subLR;
        if (subLR)
            subLR->_parent = root;

        subL->_right = root;
        //root->_parent = subL;
        if (ppNOde==NULL)
        {
            _root = subL;
            subL->_parent = NULL;
        }
        else
        {
            if (ppNOde->_left == root)
            {
                ppNOde->_left = subL;
                subL->_parent = ppNOde;
            }
            else
            {
                ppNOde->_right = subL;
                subL->_parent = ppNOde;
            }    
        }
    }

    void _RotateL(Node* root)     //左旋
    {
        Node* subR = root->_right;
        Node* subRL = subR->_left;
        Node* ppNOde = root->_parent;

        root->_right = subRL;
        if (subRL)
            subRL->_parent = root;

            subR->_left = root;
        //root->_parent = subR;
        if (ppNOde==NULL)
        {
            _root = subR;
            subR->_parent = NULL;
        }
        else
        {
            if (ppNOde->_left == root)
            {
                ppNOde->_left = subR;
                subR->_parent = ppNOde;
            }
            else
            {
                ppNOde->_right = subR;
                subR->_parent = ppNOde;
            }
        }
    }

    bool _Isblance(Node* root, int count, int k)
    {
        if (root == NULL)
        {
            return count == k;
        }

        Node* parent = root->_parent;
        if (parent && (parent->_col == RED&&root->_col == RED))
            return false;

        if (root->_col == BLANK)
            k++;
        return _Isblance(root->_left, count, k) && _Isblance(root->_right, count, k);
    }

    void Destory(Node* root)
    {
        if (root == NULL)
            return;

        Destory(root->_left);
        Destory(root->_right);
        delete root;
        root = NULL;
    }
private:
    Node* _root;
};

posted @ 2016-11-04 16:58  请叫我小小兽  阅读(403)  评论(0编辑  收藏  举报