简聊红黑树

来源:互联网 发布:注册淘宝食品店铺流程 编辑:程序博客网 时间:2024/06/15 02:00

一:红黑树的基本概念:由红黑两个色节点组成的二叉搜索树满足下面的条件就叫做红黑树:

1,每个节点不是红色就是黑色。

2,根节点是黑节点。

3,如果有一个节点是红节点,那么它的两个子节点就肯定是黑节点。

4,对于每个节点,从该节点到其所有后代叶节点的简单路径上,均包含相同数目的黑节点。

二:红黑树的插入我们可以分好几种情况

1,(cur有可能是一个新增节点,也有可能是由于下面的操作变红)cur为红,之后它的parent是红,uncle存在且也是红,就把parent与uncle变黑,让祖父grandfather变红,这个时候如果grandfather是根节点那么,那么出去之后再变黑;如果grandfather不是根节点,就看grandfather的father是否为黑,如果是黑,那么就不用处理,如果grandfather的father是红,就继续像上面一样推。如图所示:


2,cur为红(同样的cur有可能是一个新增节点,也有可能是由于下面的节点新增了一个节点而变红),parent为红,grandfather为黑,但是uncle不存在或者为黑,这个时候如果parent是grandfather的左孩子,cur是parent的左孩子,那么就进行右旋转,最后grandfather变成红,parent变成黑;如果parent是grandfather的右孩子,cur也是parent的右孩子,那么就进行左旋转,最后grandfather变成红,parent变成黑。如图所示:


3,cur为红(同上),parent为红,grandfather为黑,uncle不存在或为黑,parent是grandfather的左孩子,cur是parent的右孩子,则进行左右旋转;相反如果parent是grandfather的右孩子,cur是parent的左孩子,则进行右左旋转,如图所示:


二:红黑树实现的代码:

#pragma once#include<iostream>using namespace std;enum Color{RED,BLACK,};template<class KV>struct RBTreeNode{RBTreeNode<KV>* _left;RBTreeNode<KV>* _right;RBTreeNode<KV>* _parent;Color _color;KV _kv;RBTreeNode(const KV& kv):_kv(kv),_left(NULL),_right(NULL),_parent(NULL),_color(RED){}};template<class KV, class KVRef, class KVPtr>struct RBTreeIterator{typedef RBTreeNode<KV> Node;typedef RBTreeIterator<KV, KVRef, KVPtr> Self;Node* _node;RBTreeIterator(Node* node):_node(node){}KVRef operator*(){return _node->_kv;}KVPtr operator->(){return &(operator*());}Self operator++(){if (_node->_right == NULL){Node* cur = _node;Node* parent = cur->_parent;while (parent){if (cur == parent->_left){break;}else{cur = parent;parent = cur->_parent;}}_node = parent;}else{Node* left = _node->_right;while(left->_left)left = left->_left;_node = left;}return *this;}bool operator != (const Self& s) const{return _node != s._node;}};template<class K, class V>class RBTree{typedef RBTreeNode<pair<K, V>> Node;public:typedef RBTreeIterator<pair<K, V>, pair<K, V>&, pair<K, V>*> Iterator;RBTree():_root(NULL){}Iterator Begin(){Node* left = _root;while (left && left->_left){left = left->_left;}return Iterator(left);}Iterator End(){return Iterator(NULL);}V& operator[](const K& key){pair<Iterator, bool> ret = Insert(make_pair(key, V()));return ret.first->second;}pair<Iterator, bool> Insert(const pair<K, V>& kv){if (_root == NULL){_root = new Node(kv);_root->_color = BLACK;return make_pair(Iterator(_root), true);}Node* parent = NULL;Node* cur = _root;while (cur){if (cur->_kv.first < kv.first){parent = cur;cur = cur->_right;}else if (cur->_kv.first > kv.first){parent = cur;cur = cur->_left;}else{//return false;return make_pair(Iterator(cur), false);}}cur = new Node(kv);Node* newNode = cur;if (parent->_kv.first < kv.first){parent->_right = cur;}else{parent->_left = cur;}cur->_parent = parent;// 1.// 2.// 3.while (parent && parent->_color == RED){Node* grandfather = parent->_parent;if (parent == grandfather->_left){Node* uncle= grandfather->_right;if (uncle && uncle->_color == RED){// 1parent->_color = uncle->_color = BLACK;grandfather->_color = RED;cur = grandfather;parent = cur->_parent;}else{//2 or 3if (parent->_right == cur){RotateL(parent);swap(cur, parent);}RotateR(grandfather);parent->_color = BLACK;grandfather->_color = RED;break;}}else{Node* uncle= grandfather->_left;if (uncle && uncle->_color == RED){parent->_color = uncle->_color = BLACK;grandfather->_color = RED;cur = grandfather;parent = cur->_parent;}else{if (cur == parent->_left){RotateR(parent);swap(cur, parent);}RotateL(grandfather);parent->_color = BLACK;grandfather->_color = RED;break;}}}_root->_color = BLACK;return make_pair(Iterator(newNode), true);}void InOrder(){_InOrder(_root);cout<<endl;}bool IsBalance(){if (_root == NULL){return true;}if (_root->_color == RED){return false;}int blackNum = 0;Node* cur = _root;while (cur){if(cur->_color == BLACK)++blackNum;cur = cur->_left;}int count = 0;return _IsBalance(_root, blackNum, count);}protected:bool _IsBalance(Node* root, const int blackNum, int count){if (root == NULL){if (blackNum != count){cout<<"黑色节点的数量不相等"<<endl;return false;}return true;}if (root->_color == BLACK){++count;}if (root->_color == RED && root->_parent->_color == RED){cout<<"存在连续的红节点"<<root->_key<<endl;return false;}return _IsBalance(root->_left, blackNum, count)&& _IsBalance(root->_right, blackNum, count);}void _InOrder(Node* root){if (root == NULL)return;_InOrder(root->_left);cout<<root->_key<<" ";_InOrder(root->_right);}void RotateR(Node* parent){Node* subL = parent->_left;Node* subLR = subL->_right;parent->_left = subLR;if(subLR)subLR->_parent = parent;subL->_right = parent;Node* ppNode = parent->_parent;parent->_parent = subL;if (ppNode == NULL){_root = subL;subL->_parent = NULL;}else{if (ppNode->_left == parent){ppNode->_left = subL;}else{ppNode->_right = subL;}subL->_parent = ppNode;}}void RotateL(Node* parent){Node* subR = parent->_right;Node* subRL = subR->_left;parent->_right = subRL;if (subRL)subRL->_parent = parent;subR->_left = parent;Node* ppNode = parent->_parent;parent->_parent = subR;if (parent == _root) // ppNode==NULL{_root = subR;_root->_parent = NULL;}else{if (ppNode->_left == parent){ppNode->_left = subR;}else{ppNode->_right = subR;}subR->_parent = ppNode;}}protected:Node* _root;};

原创粉丝点击