#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;
};

红黑树RBTree的更多相关文章

  1. 高级搜索树-红黑树(RBTree)解析

    目录 红黑树的定义 节点与树的定义 旋转操作 插入操作 情况1:p的兄弟u为黑色 情况2: p的兄弟u为红色 插入操作性能分析 代码实现 删除操作 情况1:x的接替者succ为红色 情况2:x的接替者 ...

  2. 平衡搜索树--红黑树 RBTree

    红黑树是一棵二叉搜索树,它在每个节点上增加了一个存储位来表示节点的颜色,可以是Red或Black. 通过对任何一条从根到叶子节点简单路径上的颜色来约束树的高度,红黑树保证最长路径不超过最短路径的两倍, ...

  3. java——红黑树 RBTree

    对于完全随机的数据,普通的二分搜索树就很好用,只是在极端情况下会退化成链表. 对于查询较多的情况,avl树很好用. 红黑树牺牲了平衡性,但是它的统计性能更优(综合增删改查所有的操作). 红黑树java ...

  4. 红黑树(RBTREE)之上-------构造红黑树

    该怎么说呢,现在写代码的速度还是很快的,很高兴,o(^▽^)o. 光棍节到了,早上没忍住,手贱了一般,看到*D的优惠,买了个机械键盘,晚上就到了,敲着还是很舒服的,和老婆炫耀了一把哈哈. 光棍节再去* ...

  5. 高级搜索树-红黑树(RBTree)代码实现

    代码实现 代码参考了<数据结构(c++语言版)>--清华大学邓俊辉 "RBTree.h" #pragma once //#include"pch.h" ...

  6. 【转】B树、B-树、B+树、B*树、红黑树、 二叉排序树、trie树Double Array 字典查找树简介

    B  树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: ...

  7. RBTree 红黑树

    红黑树 一.红黑树概述 红黑树不仅是一个二叉搜索树,并且满足以下规则: 1>每个节点不是红的就是黑的, 2>根结点为黑色, 3>如果节点为红色,其子节点必须为黑色, 4>任一节 ...

  8. 红黑树(RB-tree)比AVL树的优势在哪?

    1. 如果插入一个node引起了树的不平衡,AVL和RB-Tree都是最多只需要2次旋转操作,即两者都是O(1):但是在删除node引起树的不平衡时,最坏情况下,AVL需要维护从被删node到root ...

  9. RB-tree (红黑树)相关问题

    今天被问到了红黑树的规则,简述总结一下: 1.每个节点不是红色就是黑色. 2.根节点为黑色. 3.如果节点为红,其子节点必须为黑. 4.任一节点至NULL(树尾端)的任何路径,所含之黑节点数必须相同. ...

随机推荐

  1. c# vs c++

    [c# vs c++] 1.在 C++ 中,类和结构实际上是相同的,而在 C# 中,它们很不一样.C# 类可以实现任意数量的接口,但只能从一个基类继承.而且,C# Struct不支持继承,也不支持显式 ...

  2. java基础强化——深入理解反射

    目录 1.从Spring容器的核心谈起 2. 反射技术初探 2.1 什么是反射技术 2.2 类结构信息和java对象的映射 3 Class对象的获取及需要注意的地方 4. 运行时反射获取类的结构信息 ...

  3. 阻塞和唤醒线程——LockSupport功能简介及原理浅析

    目录 1.LockSupport功能简介 1.1 使用wait,notify阻塞唤醒线程 1.2 使用LockSupport阻塞唤醒线程 2. LockSupport的其他特色 2.1 可以先唤醒线程 ...

  4. nyoj27-水池数目 (求连通块数目)【dfs】

    http://acm.nyist.net/JudgeOnline/problem.php?pid=27 水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳 ...

  5. APP安全之代码混淆防止反编译查看真实的头文件函数声明

    现在有的公司对自己的爱屁屁(APP)安全上有重视,所以本篇讲一下代码混淆,即使别人反编译出来,也看不出来头文件的信息. 上菜: 1.首先安装class-dump,下载地址:http://steveny ...

  6. 673. Number of Longest Increasing Subsequence最长递增子序列的数量

    [抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...

  7. IIS7日志中时间与系统时间不一致的原因

    最近在分析web日志,发现IIS7日志中时间与系统时间不一致,即本该上班时间才产生的产并发访问日志,全部发生在凌晨至上班前. 本以为是系统时间设置错误,检查后一切正常.后查询资料,原来是这个原因: 日 ...

  8. (转)Android SDK Manager国内无法更新的解决方案

    转载地址:http://www.linuxidc.com/Linux/2015-01/111958.htm 现在由于GWF,google基本和咱们说咱见了,就给现在在做Android  或者想学习An ...

  9. 908D New Year and Arbitrary Arrangement

    传送门 分析 代码 #include<iostream> #include<cstdio> #include<cstring> #include<string ...

  10. 修改apache的默认访问路径