[转]红黑树理论和实现

概念
  红黑树是一种自平衡二叉查找树,是在计算机科学中用到的一种数据结构,典型的用途是实现关联数组。它是在1972年由Rudolf Bayer发明的,他称之为"对称二叉B树",它现代的名字是在 Leo J. Guibas 和 Robert Sedgewick 于1978年写的一篇论文中获得的。它是复杂的,但它的操作有着良好的最坏情况运行时间,并且在实践中是高效的: 它可以在O(log n)时间内做查找,插入和删除,这里的n 是树中元素的数目。  红黑树是一种很有意思的平衡检索树。它的统计性能要好于平衡二叉树(有些籍根据作者姓名,Adelson-Velskii和Landis,将其称为AVL-树),因此,红黑树在很多地方都有应用。在C++ STL中,很多部分(目前包括set, multiset, map, multimap)应用了红黑树的变体(SGI STL中的红黑树有一些变化,这些修改提供了更好的性能,以及对set操作的支持)。  
背景和术语
        红黑树是一种特定类型的二叉树,它是在计算机科学中用来组织数据比如数字的块的一种结构。所有数据块都存储在节点中。这些节点中的某一个节点总是担当启始位置的功能,它不是任何节点的儿子;我们称之为根节点或根。它有最多两个"儿子",都是它连接到的其他节点。所有这些儿子都可以有自己的儿子,以此类推。这样根节点就有了把它连接到在树中任何其他节点的路径。         如果一个节点没有儿子,我们称之为叶子节点,因为在直觉上它是在树的边缘上。子树是从特定节点可以延伸到的树的某一部分,其自身被当作一个树。在红黑树中,叶子被假定为 null 或空。         由于红黑树也是二叉查找树,它们当中每一个节点都的比较值都必须大于或等于在它的左子树中的所有节点,并且小于或等于在它的右子树中的所有节点。这确保红黑树运作时能够快速的在树中查找给定的值。
用途和好处         红黑树和AVL树一样都对插入时间、删除时间和查找时间提供了最好可能的最坏情况担保。这不只是使它们在时间敏感的应用如即时应用(real time application)中有价值,而且使它们有在提供最坏情况担保的其他数据结构中作为建造板块的价值;例如,在计算几何中使用的很多数据结构都可以基于红黑树。         红黑树在函数式编程中也特别有用,在这里它们是最常用的持久数据结构之一,它们用来构造关联数组和集合,在突变之后它们能保持为以前的版本。除了O(log n)的时间之外,红黑树的持久版本对每次插入或删除需要O(log n)的空间。         红黑树是 2-3-4树的一种等同。换句话说,对于每个 2-3-4 树,都存在至少一个数据元素是同样次序的红黑树。在 2-3-4 树上的插入和删除操作也等同于在红黑树中颜色翻转和旋转。这使得 2-3-4 树成为理解红黑树背后的逻辑的重要工具,这也是很多介绍算法的教科在红黑树之前介绍 2-3-4 树的原因,尽管 2-3-4 树在实践中不经常使用。属性
        红黑树是每个节点都有颜色特性的二叉查找树,颜色的值是红色或黑色之一。除了二叉查找树带有的一般要求,我们对任何有效的红黑树加以如下增补要求:  
       1.节点是红色或黑色。    
       2.根是黑色。 
       3.所有叶子(外部节点)都是黑色。
       4.每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)。
       5.从每个叶子到根的所有路径都包含相同数目的黑色节点。   
      这些约束强制了红黑树的关键属性: 从根到叶子的最长的可能路径不大于最短的可能路径的两倍长。结果是这个树大致上是平衡的。因为操作比如插入、删除和查找某个值都要求与树的高度成比例的最坏情况时间,这个在高度上的理论上限允许红黑树在最坏情况下都是高效的,而不同于普通的二叉查找树。     

实现
      RBTree.h
#ifndef __RBTREE_H__
#define __RBTREE_H__

#include
using namespace std;

typedef enum {
 RBTREE_CLR_BLACK,
 RBTREE_CLR_RED
} RbTreeNodeColor;

class  RbTreeData
{
public:
 // constructor
 RbTreeData() { payload = this; };
 virtual ~RbTreeData() {};

 // exposed functions
 void *payload;

 // methods
 virtual int operator < (RbTreeData &cmp) = 0;
 virtual int perator == (RbTreeData &cmp) = 0;
 //virtual void clear(void *userData = 0);

 // debugging only
 virtual void Dump(void *userData) {};
};

class  RbTreeNode {
public:
 // constructor
 RbTreeNode();

 // exposed members
 RbTreeNode *left;
 RbTreeNode *right;
 RbTreeNode *parent;
 RbTreeNodeColor color;
 RbTreeData *data;

 // methods
 RbTreeNode &operator=(RbTreeNode &source);
};

class  RbTreeTree {
public:
 // constructor
 RbTreeTree() { root = &nil; };

 // methods
 RbTreeData *Insert(RbTreeData *data);
 RbTreeData *Find(RbTreeData *data);
 RbTreeData *Remove(RbTreeData *data);
 void Clear(void *userData = 0);

 // debugging only
 void Dump(RbTreeNode *x = 0, void *userData = 0);

private:
 RbTreeNode *root;
 RbTreeNode nil;

 void FixupInsert(RbTreeNode *x);
 void FixupRemove(RbTreeNode *x);
 void RotateLeft(RbTreeNode *x);
 void RotateRight(RbTreeNode *x);
};

 

#endif /* __RBTREE_H__ */

RBTree.cpp

#include "od_utl_rbtree.h"


RbTreeNode &RbTreeNode::operator=
(RbTreeNode &source)
{
 left = source.left;
 right = source.right;
 parent = source.parent;
 color = source.color;
 data = source.data;
 return (*this);
}

RbTreeNode::RbTreeNode()
{
 left = right = this;
 parent = (0);
 color = RBTREE_CLR_BLACK;
 data = (0);
}

void RbTreeTree::RotateLeft(RbTreeNode *x)
{
 RbTreeNode *y = x->right;

 // establish x->right link
 x->right = y->left;
 if (y->left != &nil) {
  y->left->parent = x;
 }

 // establish y->parent link
 if (y != &nil) {
  y->parent = x->parent;
 }
 if (x->parent) {
  if (x == x->parent->left)
   x->parent->left = y;
  else
   x->parent->right = y;
 } else {
  root = y;
 }

 // link x and y
 y->left = x;
 if (x != &nil) {
  x->parent = y;
 }
}

void RbTreeTree::RotateRight(RbTreeNode *x)
{
 RbTreeNode *y = x->left;

 // establish x->left link
 x->left = y->right;
 if (y->right != &nil) {
  y->right->parent = x;
 }

 // establish y->parent link
 if (y != &nil) {
  y->parent = x->parent;
 }
 if (x->parent) {
  if (x == x->parent->right)
   x->parent->right = y;
  else
   x->parent->left = y;
 } else {
  root = y;
 }

 // link x and y
 y->right = x;
 if (x != &nil) {
  x->parent = y;
 }
}

void RbTreeTree::FixupRemove(RbTreeNode *x)
{
 while (x != root && x->color == RBTREE_CLR_BLACK) {
  if (x == x->parent->left) {
   RbTreeNode *w = x->parent->right;
   if (w->color == RBTREE_CLR_RED) {
    w->color = RBTREE_CLR_BLACK;
    x->parent->color = RBTREE_CLR_RED;
    RotateLeft (x->parent);
    w = x->parent->right;
   }
   if (w->left->color == RBTREE_CLR_BLACK &&
    w->right->color == RBTREE_CLR_BLACK) {
     w->color = RBTREE_CLR_RED;
     x = x->parent;
    } else {
     if (w->right->color == RBTREE_CLR_BLACK) {
      w->left->color = RBTREE_CLR_BLACK;
      w->color = RBTREE_CLR_RED;
      RotateRight (w);
      w = x->parent->right;
     }
     w->color = x->parent->color;
     x->parent->color = RBTREE_CLR_BLACK;
     w->right->color = RBTREE_CLR_BLACK;
     RotateLeft (x->parent);
     x = root;
    }
  } else {
   RbTreeNode *w = x->parent->left;
   if (w->color == RBTREE_CLR_RED) {
    w->color = RBTREE_CLR_BLACK;
    x->parent->color = RBTREE_CLR_RED;
    RotateRight (x->parent);
    w = x->parent->left;
   }
   if (w->right->color == RBTREE_CLR_BLACK &&
    w->left->color == RBTREE_CLR_BLACK) {
     w->color = RBTREE_CLR_RED;
     x = x->parent;
    } else {
     if (w->left->color == RBTREE_CLR_BLACK) {
      w->right->color = RBTREE_CLR_BLACK;
      w->color = RBTREE_CLR_RED;
      RotateLeft (w);
      w = x->parent->left;
     }
     w->color = x->parent->color;
     x->parent->color = RBTREE_CLR_BLACK;
     w->left->color = RBTREE_CLR_BLACK;
     RotateRight (x->parent);
     x = root;
    }
  }
 }
 x->color = RBTREE_CLR_BLACK;
}

void RbTreeTree::FixupInsert(RbTreeNode *x)
{
 // check Red-Black properties
 while (x != root && x->parent->color == RBTREE_CLR_RED) {
  // violation
  if (x->parent == x->parent->parent->left) {
   RbTreeNode *y = x->parent->parent->right;
   if (y->color == RBTREE_CLR_RED) {

    // uncle is RBTREE_CLR_RED
    x->parent->color = RBTREE_CLR_BLACK;
    y->color = RBTREE_CLR_BLACK;
    x->parent->parent->color = RBTREE_CLR_RED;
    x = x->parent->parent;
   } else {

    // uncle is RBTREE_CLR_BLACK
    if (x == x->parent->right) {
     // make x a left child
     x = x->parent;
     RotateLeft(x);
    }

    // recolor and rotate
    x->parent->color = RBTREE_CLR_BLACK;
    x->parent->parent->color = RBTREE_CLR_RED;
    RotateRight(x->parent->parent);
   }
  } else {

   // mirror image of above code
   RbTreeNode *y = x->parent->parent->left;
   if (y->color == RBTREE_CLR_RED) {

    // uncle is RBTREE_CLR_RED
    x->parent->color = RBTREE_CLR_BLACK;
    y->color = RBTREE_CLR_BLACK;
    x->parent->parent->color = RBTREE_CLR_RED;
    x = x->parent->parent;
   } else {

    // uncle is RBTREE_CLR_BLACK
    if (x == x->parent->left) {
     x = x->parent;
     RotateRight(x);
    }
    x->parent->color = RBTREE_CLR_BLACK;
    x->parent->parent->color = RBTREE_CLR_RED;
    RotateLeft(x->parent->parent);
   }
  }
 }
 root->color = RBTREE_CLR_BLACK;
}

RbTreeData *RbTreeTree::Insert(RbTreeData *data)
{
 RbTreeNode *parent = (0), *current = root;

 while (current != &nil) {
  if (*(current->data) == (*data)) {
   return (current->data);
  }
  parent = current;
  current = ((*data) < (*(current->data))) ?
   current->left : current->right;
 }

 current = new RbTreeNode;
 if (! current) {
  return (0);
 }

 current->left = current->right = &nil;
 current->parent = parent;
 current->color = RBTREE_CLR_RED;
 current->data = data;

 if (parent) {
  if ((*data) < (*(parent->data))) {
   parent->left = current;
  }
  else {
   parent->right = current;
  }
 }
 else {     
  root = current;
 }

 FixupInsert(current);
 return (current->data);
}

RbTreeData *RbTreeTree::Find(RbTreeData *data)
{
 RbTreeNode *current = root;

 while (current != &nil) {
  if (*(current->data) == (*data)) {
   return (current->data);
  }
  current = ((*data) < (*(current->data))) ?
   current->left : current->right;
 }

 return (0);
}

RbTreeData *RbTreeTree::Remove(RbTreeData *data)
{
 RbTreeData *val;
 RbTreeNode *x, *y, *z = root;

 while (z != &nil) {
  if (*(z->data) == (*data)) {
   break;
  }
  z = ((*data) < (*(z->data))) ? z->left : z->right;
 }

 if (!z || z == &nil) {
  return (0);
 }

 if (z->left == &nil || z->right == &nil) {
  // y has a nil node as a child
  y = z;
 } else {
  // find tree successor with a nil node as a child
  y = z->right;
  while (y->left != &nil) {
   y = y->left;
  }
 }

 // x is y's only child
 if (y->left != &nil) {
  x = y->left;
 } else {
  x = y->right;
 }

 // remove y from the parent chain
 x->parent = y->parent;
 if (y->parent) {
  if (y == y->parent->left) {
   y->parent->left = x;
  } else {
   y->parent->right = x;
  }
 } else {
  root = x;
 }

 if (y != z) {
  val = z->data;
  z->data = y->data;
 }
 else {
  val = y->data;
 }

 if (y->color == RBTREE_CLR_BLACK) {
  FixupRemove(x);
 }

 delete y;
 return (val);
}

void RbTreeTree::Clear(void *userData)
{
 RbTreeData *val;
 while (root != &nil) {
  val = RbTreeTree::Remove(root->data);
  //val->clear(userData);
  delete val;
 }
}

void RbTreeTree::Dump(RbTreeNode *x,
        void *userData)
{
 if (x == 0) {
  x = root;
 }
 if (x == &nil) {
  return;
 }
 if (x->left != &nil) {
  Dump(x->left, userData);
 }
 if (x->data) {
  x->data->Dump(userData);
 } else {
 }
 if (x->right != &nil) {
  Dump(x->right, userData);
 }
}

最后写一个类继承RbTreeData类并实现其中的方法,一个类继承RbTreeTree就可以完成所要的功能了

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12925485/viewspace-450278/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12925485/viewspace-450278/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值