数据结构基础(24) --红黑树的设计与实现(下)

完整源代码: http://download.csdn.net/detail/hanqing280441589/8450041

 

红黑节点设计与实现

  1. template <typename Comparable>  
  2. class RedBlackNode  
  3. {  
  4.     friend class RedBlackTree<Comparable>;  
  5. //所有的成员都是private  
  6. private:  
  7.     RedBlackNode(const Comparable &theElement = Comparable(),  
  8.                  RedBlackNode *theLeft = NULL,  
  9.                  RedBlackNode *theRight = NULL,  
  10.                  int theColor = RedBlackTree<Comparable>::BLACK)  
  11.         : element(theElement), left(theLeft), right(theRight), color(theColor) {}  
  12.   
  13.     //数据成员  
  14.     Comparable element;  
  15.     RedBlackNode *left;  
  16.     RedBlackNode *right;  
  17.     int color;  
  18. };  

红黑树的设计

  1. template <typename Comparable>  
  2. class RedBlackTree  
  3. {  
  4. //类型定义  
  5. public:  
  6.     typedef RedBlackNode<Comparable> Node;  
  7.     enum COLOR {RED, BLACK};  
  8.   
  9. //开放的接口  
  10. public:  
  11.     explicit RedBlackTree(const Comparable & negInf);  
  12.     ~RedBlackTree();  
  13.   
  14.     void insert(const Comparable &x);  
  15.     bool isEmpty() const;  
  16.     void makeEmpty();  
  17.   
  18.     Gref<Comparable> find(const Comparable & x) const;  
  19.     Gref<Comparable> findMin() const;  
  20.     Gref<Comparable> findMax() const;  
  21.   
  22. //实用的私有操作  
  23. private:  
  24.     //自动处理: [1]重新染色; [2]:自动旋转  
  25.     void handleReorient(const Comparable &item);  
  26.     //自动旋转函数(返回旋转以后的theParent子树的根)  
  27.     Node *rotate(const Comparable & item, Node *theParent);  
  28.   
  29.     /**单旋转**/  
  30.     //向右转(带着右孩子)  
  31.     void rotateWithLeftChild(Node *& k2);  
  32.     //向左转(带着左孩子)  
  33.     void rotateWithRightChild(Node *& k1);  
  34.   
  35.     //递归删除所有节点  
  36.     void reclainMemory(Node *t) const;  
  37.   
  38. private:  
  39.     //指向红黑树的头(伪根节点)  
  40.     Node *header;  
  41.     Node *nullNode;  
  42.   
  43.     //在insert时使用  
  44.     Node *current;  //当前节点  
  45.     Node *parent;   //父节点  
  46.     Node *grand;    //祖父节点  
  47.     Node *great;    //曾祖父节点  
  48. };  

红黑树的实现

  1. //红黑树构造函数  
  2. template <typename Comparable>  
  3. RedBlackTree<Comparable>::RedBlackTree(const Comparable & negInf)  
  4. {  
  5.     nullNode = new RedBlackNode<Comparable>;  
  6.     //nullNode 的左右子节点都指向自己  
  7.     nullNode->left = nullNode->right = nullNode;  
  8.   
  9.     header = new RedBlackNode<Comparable>(negInf, nullNode, nullNode);  
  10. }  
  1. //红黑树析构函数: 完善版本  
  2. template <typename Comparable>  
  3. RedBlackTree<Comparable>::~RedBlackTree()  
  4. {  
  5.     if (!isEmpty())  
  6.         makeEmpty();  
  7.   
  8.     delete nullNode;  
  9.     delete header;  
  10. }  
  1. /**红黑树最复杂的操作 ** insert **/  
  2. template <typename Comparable>  
  3. void RedBlackTree<Comparable>::insert(const Comparable &x)  
  4. {  
  5.     current = parent = grand = great = header;  
  6.     nullNode->element = x;  
  7.   
  8.     while (current->element != x)  
  9.     {  
  10.         //让祖父成为曾祖父, 父亲成为祖父, 自己成为父亲  
  11.         //每个节点都成长一辈  
  12.         great = grand;  
  13.         grand = parent;  
  14.         parent = current;  
  15.         current = (x < current->element) ? current->left : current->right;  
  16.   
  17.         //处理1. 如果current节点有两个红色孩子  
  18.         if ((current->left->color == RED) && (current->right->color == RED))  
  19.             handleReorient( x );  
  20.     }  
  21.   
  22.     //如果树中包含相同的元素  
  23.     if (current != nullNode)  
  24.         throw DuplicateItemException();  
  25.   
  26.     current = new Node(x, nullNode, nullNode);  
  27.     if (x < parent->element)  
  28.         parent->left = current;  
  29.     else  
  30.         parent->right = current;  
  31.   
  32.     //+ 处理2. 如果新插入的节点破坏了红黑规则  
  33.     handleReorient( x );  
  34. }  
  1. /**自动平衡函数: 
  2.     [1]重新染色 
  3.     [2]自动旋转 
  4. */  
  5. template <typename Comparable>  
  6. void RedBlackTree<Comparable>::handleReorient(const Comparable & item)  
  7. {  
  8.     // 将current节点染成红色  
  9.     current->color = RED;  
  10.     // 将current的left和right节点染成黑色  
  11.     current->left->color = current->right->color = BLACK;  
  12.   
  13.     // 如果current节点的父节点也是红的 -> 单旋转 or 双旋转  
  14.     if( parent->color == RED )  
  15.     {  
  16.         //则将其祖父(爷爷)的颜色染成红色  
  17.         grand->color = RED;  
  18.   
  19.         //然后判断新插入的节点是否是内部孙子?  
  20.         //如果是, 则增加一次旋转->构成双旋转  
  21.   
  22.         //if注释: 如果该节点小于爷爷, 小于爸爸, 这两种情况不同时满足  
  23.         //则说明其是爷爷的内孙子  
  24.         if( (item < grand->element) != (item < parent->element) )  
  25.         {  
  26.             // 则依grand(祖父)节点进行旋转  
  27.             parent = rotate( item, grand );  // Start double rotate  
  28.         }  
  29.         // 则依great(曾祖父)节点进行旋转  
  30.         current = rotate( item, great );  
  31.   
  32.         //令当前节点为黑色  
  33.         current->color = BLACK;  
  34.     }  
  35.   
  36.     //根节点必须是黑色的  
  37.     header->right->color = BLACK; // Make root black  
  38. }  
  1. // 自动判断并进行旋转函数  
  2. template <typename Comparable>  
  3. typename RedBlackTree<Comparable>::Node *  
  4. RedBlackTree<Comparable>::rotate(const Comparable &item,  
  5.                                  Node *theParent )  
  6. {  
  7.     //位于theParent的左子树  
  8.     if( item < theParent->element )  
  9.     {  
  10.         //如果为真, 则说明theParent->left有左孩子,  
  11.         //否则, 有右孩子  
  12.         item < theParent->left->element ?  
  13.         //如果theParent左边有一棵子树, 则以theParent->left  
  14.         //为轴, 向右转  
  15.         rotateWithLeftChild( theParent->left )  :  // LL  
  16.         //如果theParent右边有一棵子树, 则以theParent->left  
  17.         //为轴, 向左转  
  18.         rotateWithRightChild( theParent->left ) ;  // LR  
  19.   
  20.         return theParent->left;     //返回左子树  
  21.     }  
  22.     else    //位于右子树  
  23.     {  
  24.         //如果为真, 则说明theParent->right有左孩子,往右转  
  25.         //否则, 有右孩子, 往左转  
  26.         item < theParent->right->element ?  
  27.         rotateWithLeftChild( theParent->right ) :  // RL  
  28.         rotateWithRightChild( theParent->right );  // RR  
  29.   
  30.         return theParent->right;    //返回右子树  
  31.     }  
  32. }  
  1. /** 右(单)旋转 **/  
  2. template <typename Comparable>  
  3. void RedBlackTree<Comparable>::rotateWithLeftChild(Node *& k2)  
  4. {  
  5.     Node *k1 = k2->left;  
  6.     k2->left = k1->right;  
  7.   
  8.     k1->right = k2;  
  9.     k2 = k1;  
  10. }  
  1. /** 左(单)旋转 **/  
  2. template <typename Comparable>  
  3. void RedBlackTree<Comparable>::rotateWithRightChild(Node *& k1)  
  4. {  
  5.     Node * k2 = k1->right;  
  6.     k1->right = k2->left;  
  7.   
  8.     k2->left = k1;  
  9.     k1 = k2;  
  10. }  
  1. template <typename Comparable>  
  2. Gref<Comparable> RedBlackTree<Comparable>::find(const Comparable &x) const  
  3. {  
  4.     if (isEmpty())  
  5.         return Gref<Comparable>();  
  6.   
  7.     nullNode->element = x;  
  8.     Node *iter = header->right;  
  9.   
  10.     while (true)  
  11.     {  
  12.         if (x < iter->element)  
  13.             iter = iter->left;  
  14.         else if (x > iter->element)  
  15.             iter = iter->right;  
  16.   
  17.         //如果 x == iter->element  
  18.         else if (iter != nullNode)  
  19.             return Gref<Comparable>(iter->element) ;  
  20.         else  
  21.             return Gref<Comparable>();  
  22.     }  
  23. }  
  1. template <typename Comparable>  
  2. Gref<Comparable> RedBlackTree<Comparable>::findMax() const  
  3. {  
  4.     if (isEmpty())  
  5.         return Gref<Comparable>();  
  6.   
  7.     Node *iter = header->right;  
  8.     while (iter->right != nullNode)  
  9.     {  
  10.         // 一直向右走  
  11.         iter = iter->right;  
  12.     }  
  13.   
  14.     return Gref<Comparable>(iter->element);  
  15. }  
  1. template <typename Comparable>  
  2. Gref<Comparable> RedBlackTree<Comparable>::findMin() const  
  3. {  
  4.     if (isEmpty())  
  5.         return Gref<Comparable>();  
  6.   
  7.     Node *iter = header->right;  
  8.     while (iter->left != nullNode)  
  9.     {  
  10.         // 一直向左走  
  11.         iter = iter->left;  
  12.     }  
  13.   
  14.     return Gref<Comparable>(iter->element);  
  15. }  
  1. template <typename Comparable>  
  2. bool RedBlackTree<Comparable>::isEmpty() const  
  3. {  
  4.     if (header->right == nullNode)  
  5.         return true;  
  6.     return false;  
  7. }  
  1. template <typename Comparable>  
  2. void RedBlackTree<Comparable>::makeEmpty()  
  3. {  
  4.     reclainMemory(header->right);  
  5.     header->right = nullNode;  
  6. }  
  1. template <typename Comparable>  
  2. void RedBlackTree<Comparable>::reclainMemory(Node *t) const  
  3. {  
  4.     //t == t->left的时候, 是当t==nullNode时  
  5.     if (t != t->left)  
  6.     {  
  7.         reclainMemory(t->left);  
  8.         reclainMemory(t->right);  
  9.         delete t;  
  10.     }  
  11. }  

Gref包装器的设计与实现

  1. template <typename Object>  
  2. class Gref  
  3. {  
  4. public:  
  5.     Gref(): obj(NULL) {}  
  6.     explicit Gref(const Object &x)  
  7.         : obj(& x) {}  
  8.   
  9.     const Object &get() const  
  10.     {  
  11.         if (isNull())  
  12.             throw NullPointerException();  
  13.         else  
  14.             return * obj;  
  15.     }  
  16.   
  17.     bool isNull() const  
  18.     {  
  19.         if (obj == NULL)  
  20.             return true;  
  21.         return false;  
  22.     }  
  23.   
  24. private:  
  25.     const Object * obj;  
  26. };  

Exception的设计与实现

  1. class DSException  
  2. {  
  3. public:  
  4.     typedef std::string string;  
  5.   
  6. public:  
  7.     DSException(const string &_msg = string())  
  8.         :message(_msg) {}  
  9.     virtual ~DSException() {}  
  10.   
  11.   
  12.     virtual string what() const  
  13.     {  
  14.         return message;  
  15.     }  
  16.     virtual string toString() const  
  17.     {  
  18.         return "Exception " + message;  
  19.     }  
  20.   
  21. private:  
  22.     string message;  
  23. };  
  24.   
  25. class DuplicateItemException : public DSException  
  26. {  
  27. public:  
  28.     DuplicateItemException(const string &_msg = string())  
  29.         : DSException(_msg) {}  
  30. };  
  31.   
  32. class NullPointerException : public DSException  
  33. {  
  34. public:  
  35.     NullPointerException(const string &_msg = string())  
  36.         : DSException(_msg) {}  
  37. };  

测试代码

  1. int main()  
  2. {  
  3.     const int NEG_INF = -999999;  
  4.     RedBlackTree<int> tree(NEG_INF);  
  5.   
  6.     tree.insert(50);  
  7.     tree.insert(40);  
  8.     tree.insert(30);  
  9.     tree.insert(10);  
  10.     tree.insert(55);  
  11.     tree.insert(88);  
  12.     tree.insert(200);  
  13.     tree.insert(100);  
  14.     tree.insert(70);  
  15.     tree.insert(80);  
  16.     tree.insert(650);  
  17.   
  18.   
  19.     Gref<int> g = tree.findMin();  
  20.     cout << "Min = " << g.get() << endl;  
  21.     g = tree.findMax();  
  22.     cout << "Max = " << g.get() << endl;  
  23.   
  24.     int searchVal;  
  25.     while (cin >> searchVal)  
  26.     {  
  27.         g = tree.find(searchVal);  
  28.         if (g.isNull())  
  29.             cout << "not found" << endl;  
  30.         else  
  31.             cout << g.get() << " founded" << endl;  
  32.     }  
  33.   
  34.     tree.makeEmpty();  
  35.     if (tree.isEmpty())  
  36.     {  
  37.         cout << "is Empty" << endl;  
  38.     }  
  39.     else  
  40.     {  
  41.         cout << "not Empty" << endl;  
  42.     }  
  43.   
  44.     return 0;  


原文地址:http://blog.csdn.net/zjf280441589/article/details/43865483

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值