4、任何节点到其子孙的外部节点的每条简单路径都包含相同数目的黑色节点
一、向红黑树中插入节点
因为红黑树上面的第4个特点,因此当向红黑树中插入新的节点时,应该将新节点标注为红色。向红黑树中插入节点看的是插入节点的父节点和叔父节点。二、删除红黑树中的节点
对于删除节点,如果我们要删除的节点刚好是叶节点,那么就将其进行删除然后进行调整,如果要删除的节点不是叶节点,那么就寻找要删除节点的左子树的最大值节点M,然后将该最大值转存到要删除的节点处,然后将节点M删除,也就是说删除的也是叶子节点,因此我们讨论删除叶子节点时对红黑树的调整。 #ifndef CM_RBTREE_HPP
#define CM_RBTREE_HPP
#include "BinarySearchTree.hpp"
namespace cm
{
template class RBTreeNode;
template class RBTreeIterator;
template class RBTree;
template
class RBTreeNode
{
public:
typedef T VALUE_TYPE;
enum Color_t {
COLOR_BLACK,
COLOR_RED
};
public:
explicit RBTreeNode(const T& v=T(), Color_t c=COLOR_BLACK)
: value_(v)
, left_(NIL)
, right_(NIL)
, parent_(NIL)
, color_(c)
{
}
RBTreeNode* left() { return left_; }
RBTreeNode* right() { return right_; }
RBTreeNode* parent(){ return parent_; }
RBTreeNode* left() const { return left_; }
RBTreeNode* right() const { return right_; }
RBTreeNode* parent() const { return parent_; }
const T& value() const { return value_; }
Color_t color() const { return color_; }
void left(RBTreeNode* node) { left_ = node; }
void right(RBTreeNode* node) { right_ = node; }
void parent(RBTreeNode* node){ parent_ = node; }
void value(const T& value) { value_ = value; }
void color(Color_t c) { color_ = c; }
operator bool() const
{
return this != NIL;
}
bool operator !() const
{
return this == NIL;
}
public:
static RBTreeNode* NIL;
private:
T value_;
RBTreeNode* left_;
RBTreeNode* right_;
RBTreeNode* parent_;
Color_t color_;
};
template
RBTreeNode* RBTreeNode::NIL(new RBTreeNode());
template
class RBTree: public BinarySearchTree_T< RBTreeNode >
{
public:
typedef RBTreeNode Node;
public:
size_t bh() const
{
return this->bh(this->getRoot());
}
size_t bh(const Node* node) const
{
Node* n = node;
size_t h = ;
while (n) {
if (n->color() == Node::COLOR_BLACK) {
++h;
}
n = n->left();
}
return h;
}
size_t insert(const T& key)
{
Node* z = new Node(key, Node::COLOR_RED); // 要插入的节点
Node* y = Node::NIL; // y 是 x 的父节点
Node* x = this->root();
while (x != Node::NIL) {
y = x;
if (key == x->value()) {
return 0;
}
if (key < x->value()) {
x = x->left();
}
else {
x = x->right();
}
}
z->parent(y);
if (y == Node::NIL) {
this->root_ = z;
this->root_->parent(Node::NIL);
}
else {
if(key < y->value()) {
y->left(z);
}
else {
y->right(z);
}
}
z->left(Node::NIL);
z->right(Node::NIL);
insert_fixup(z);
++this->size_;
return ;
}
void remove(Node* z)
{
Node* y = Node::NIL;
Node* x = Node::NIL;
if (z->left() == Node::NIL || z->right() == Node::NIL) {
y = z;
}
else {
//y = this->successor(z);
y = z->right();
while (y->left() != Node::NIL) {
y = y->left();
}
}
// x 是 y的唯一孩子
if (y->left() != Node::NIL) {
x = y->left();
}
else {
x = y->right();
}
x->parent(y->parent());
if(y->parent() == Node::NIL) {
this->root_ = x;
}
else {
if (y == y->parent()->left()) {
y->parent()->left(x);
}
else {
y->parent()->right(x);
}
}
if (y != z) {
z->value(y->value());
}
if (y->color() == Node::COLOR_BLACK) {
this->delete_fixup(x);
}
delete y;
--this->size_;
}
/// 如果key存在,删除并返回 ,否则返回
size_t remove(const T& key)
{
Node* node = this->find(key);
if (node != Node::NIL) {
this->remove(node);
return ;
}
return ;
}
private:
void left_rotate(Node* x)
{
Node* y = x->right();
// 把y的左子数变成x的右子树
x->right(y->left());
if (y->left() != Node::NIL) {
y->left()->parent(x);
}
if (y != Node::NIL) {
y->parent(x->parent());
}
if (x->parent() == Node::NIL) {
this->root_ = y;
}
else {
if (x == x->parent()->left()) {
x->parent()->left(y);
}
else {
x->parent()->right(y);
}
}
// 把x变成y的左子节点
y->left(x);
if (x != Node::NIL) {
x->parent(y);
}
}
void right_rotate(Node* x)
{
Node* y = x->left();
x->left(y->right());
if (y->right() != Node::NIL) {
y->right()->parent(x);
}
if (y != Node::NIL) {
y->parent(x->parent());
}
if (x->parent() == Node::NIL) {
this->root_ = y;
}
else {
if (x == x->parent()->left()) {
x->parent()->left(y);
}
else {
x->parent()->right(y);
}
}
y->right(x);
if (x != Node::NIL) {
x->parent(y);
}
}
void insert_fixup(Node* z)
{
Node* y = Node::NIL;
while (z != this->root() && z->parent()->color() == Node::COLOR_RED) {
if (z->parent() == z->parent()->parent()->left()) {
y = z->parent()->parent()->right();
if (y->color() == Node::COLOR_RED) {
z->parent()->color(Node::COLOR_BLACK);
y->color(Node::COLOR_BLACK);
z->parent()->parent()->color(Node::COLOR_RED);
z = z->parent()->parent();
}
else {
if (z == z->parent()->right()) {
z = z->parent();
this->left_rotate(z);
}
z->parent()->color(Node::COLOR_BLACK);
z->parent()->parent()->color(Node::COLOR_RED);
this->right_rotate(z->parent()->parent());
}
}
else {
y = z->parent()->parent()->left();
if (y->color() == Node::COLOR_RED) {
z->parent()->color(Node::COLOR_BLACK);
y->color(Node::COLOR_BLACK);
z->parent()->parent()->color(Node::COLOR_RED);
z = z->parent()->parent();
}
else {
if (z == z->parent()->left()) {
z = z->parent();
this->right_rotate(z);
}
z->parent()->color(Node::COLOR_BLACK);
z->parent()->parent()->color(Node::COLOR_RED);
this->left_rotate(z->parent()->parent());
}
}
}
this->root()->color(Node::COLOR_BLACK);
}
void delete_fixup(Node* x)
{
Node* w = Node::NIL;
while (x != this->root() && x->color() == Node::COLOR_BLACK) {
if (x == x->parent()->left()) {
w = x->parent()->right();
if (w->color() == Node::COLOR_RED) {
w->color(Node::COLOR_BLACK);
x->parent()->color(Node::COLOR_RED);
this->left_rotate(x->parent());
w = x->parent()->right();
}
if (w->left()->color() == Node::COLOR_BLACK && w->right()->color() == Node::COLOR_BLACK) {
w->color(Node::COLOR_RED);
x = x->parent();
} else {
if (w->right()->color() == Node::COLOR_BLACK) {
w->left()->color(Node::COLOR_BLACK);
w->color(Node::COLOR_RED);
right_rotate(w);
w = x->parent()->right();
}
w->color(x->parent()->color());
x->parent()->color(Node::COLOR_BLACK);
w->right()->color(Node::COLOR_BLACK);
left_rotate(x->parent());
x = this->root();
}
} else {
w = x->parent()->left();
if (w->color() == Node::COLOR_RED) {
w->color(Node::COLOR_BLACK);
x->parent()->color(Node::COLOR_RED);
right_rotate(x->parent());
w = x->parent()->left();
}
if (w->right()->color() == Node::COLOR_BLACK && w->left()->color() == Node::COLOR_BLACK) {
w->color(Node::COLOR_RED);
x = x->parent();
} else {
if (w->left()->color() == Node::COLOR_BLACK) {
w->right()->color(Node::COLOR_BLACK);
w->color(Node::COLOR_RED);
left_rotate(w);
w = x->parent()->left();
}
w->color(x->parent()->color());
x->parent()->color(Node::COLOR_BLACK);
w->left()->color(Node::COLOR_BLACK);
right_rotate(x->parent());
x = this->root();
}
}
}
x->color(Node::COLOR_BLACK);
}
};
template
class RBTreeIterator
{
public:
RBTreeIterator()
: tree_(RBTreeNode::NIL), current_(RBTreeNode::NIL)
{
}
RBTreeIterator(RBTree& tree, typename RBTree::Node* current)
: tree_(tree), current_(current)
{
}
const T& operator*() const
{
return current_->value();
}
T* operator->()
{
return current_;
}
bool operator==(const RBTreeIterator& rhs) const
{
return current_ == rhs.current_;
}
bool operator!=(const RBTreeIterator& rhs) const
{
return current_ != rhs.current_;
}
RBTreeIterator& operator++()
{
current_ = tree_.successor(current_);
return *this;
}
private:
RBTree& tree_;
typename RBTree::Node* current_;
};
} // end namespace cm
#endif // CM_RBTREE_HPP