轉載:http://blog.csdn.net/v_JULY_v/article/details/6285620
前言:
本人的原創作品紅黑樹系列文章,至此,已經寫到第5篇了。雖然第三篇文章:紅黑樹的c源碼實現與剖析,用c語言完整實現過紅黑樹,但個人感覺,代碼還是不夠清晰。特此,再奉獻出一份c++的完整實現源碼,以饗讀者。
此份c++實現源碼,代碼緊湊了許多,也清晰了不少,同時採取c++類實現的方式,代碼也更容易維護以及重用。ok,有任何問題,歡迎指正。
第一部分、紅黑樹的c++完整實現源碼
本文包含紅黑樹c++實現的完整源碼,所有的解釋都含在註釋中,所有的有關紅黑樹的原理及各種插入、刪除操作的情況,都已在本人的紅黑樹系列的前4篇文章中,一一闡述。且在此紅黑樹系列第五篇文章中:紅黑樹從頭至尾插入和刪除結點的全程演示圖,把所有的插入、刪除情況都一一展示盡了。
因此,有關紅黑樹的全部原理,請參考其它文章,重點可參考此文:紅黑樹算法的實現與剖析。因此,相關原理,本文不再贅述。
ok,以下,即是紅黑樹c++實現的全部源碼,先是RBTree.h,然後是RBTree.cpp。
RBTree.h
- //file RBTree.h
- //written by saturnman,20101008。
- //updated by July,20110329。
- /*-----------------------------------------------
- 版權聲明:
- July和saturnman對此份紅黑樹的c++實現代碼享有全部的版權,
- 謝絕轉載,侵權必究。
- ------------------------------------------------*/
- #ifndef _RB_TREE_H_
- #define _RB_TREE_H_
- #include<iostream>
- #include<string>
- #include<sstream>
- #include<fstream>
- using namespace std;
- template<class KEY,class U>
- class RB_Tree
- {
- private:
- RB_Tree(const RB_Tree& input){}
- const RB_Tree& operator=(const RB_Tree& input){}
- private:
- enum COLOR{RED,BLACK};
- class RB_Node
- {
- public:
- RB_Node()
- {
- //RB_COLOR = BLACK;
- right = NULL;
- left = NULL;
- parent = NULL;
- }
- COLOR RB_COLOR;
- RB_Node* right;
- RB_Node* left;
- RB_Node* parent;
- KEY key;
- U data;
- };
- public:
- RB_Tree()
- {
- this->m_nullNode = new RB_Node();
- this->m_root = m_nullNode;
- this->m_nullNode->right = this->m_root;
- this->m_nullNode->left = this->m_root;
- this->m_nullNode->parent = this->m_root;
- this->m_nullNode->RB_COLOR = BLACK;
- }
- bool Empty()
- {
- if(this->m_root == this->m_nullNode)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //查找key
- RB_Node* find(KEY key)
- {
- RB_Node* index = m_root;
- while(index != m_nullNode)
- {
- if(key<index->key)
- {
- index = index->left; //比當前的小,往左
- }
- else if(key>index->key)
- {
- index = index->right; //比當前的大,往右
- }
- else
- {
- break;
- }
- }
- return index;
- }
- //--------------------------插入結點總操作----------------------------------
- //全部的工作,都在下述僞代碼中:
- /*RB-INSERT(T, z)
- 1 y ← nil[T] // y 始終指向 x 的父結點。
- 2 x ← root[T] // x 指向當前樹的根結點,
- 3 while x ≠ nil[T]
- 4 do y ← x
- 5 if key[z] < key[x] //向左,向右..
- 6 then x ← left[x]
- 7 else x ← right[x] //爲了找到合適的插入點,x探路跟蹤路徑,直到x成爲NIL 爲止。
- 8 p[z] ← y //y置爲 插入結點z 的父結點。
- 9 if y = nil[T]
- 10 then root[T] ← z
- 11 else if key[z] < key[y]
- 12 then left[y] ← z
- 13 else right[y] ← z //此 8-13行,置z 相關的指針。
- 14 left[z] ← nil[T]
- 15 right[z] ← nil[T] //設爲空,
- 16 color[z] ← RED //將新插入的結點z作爲紅色
- 17 RB-INSERT-FIXUP(T, z)
- */
- //因爲將z着爲紅色,可能會違反某一紅黑性質,
- //所以需要調用下面的RB-INSERT-FIXUP(T, z)來保持紅黑性質。
- bool Insert(KEY key,U data)
- {
- RB_Node* insert_point = m_nullNode;
- RB_Node* index = m_root;
- while(index!=m_nullNode)
- {
- insert_point = index;
- if(key<index->key)
- {
- index = index->left;
- }
- else if(key>index->key)
- {
- index = index->right;
- }
- else
- {
- return false;
- }
- }
- RB_Node* insert_node = new RB_Node();
- insert_node->key = key;
- insert_node->data = data;
- insert_node->RB_COLOR = RED;
- insert_node->right = m_nullNode;
- insert_node->left = m_nullNode;
- if(insert_point==m_nullNode) //如果插入的是一顆空樹
- {
- m_root = insert_node;
- m_root->parent = m_nullNode;
- m_nullNode->left = m_root;
- m_nullNode->right = m_root;
- m_nullNode->parent = m_root;
- }
- else
- {
- if(key<insert_point->key)
- {
- insert_point->left = insert_node;
- }
- else
- {
- insert_point->right = insert_node;
- }
- insert_node->parent = insert_point;
- }
- InsertFixUp(insert_node); //調用InsertFixUp修復紅黑樹性質。
- }
- //---------------------插入結點性質修復--------------------------------
- //3種插入情況,都在下面的僞代碼中(未涉及到所有全部的插入情況)。
- /*
- RB-INSERT-FIXUP(T, z)
- 1 while color[p[z]] = RED
- 2 do if p[z] = left[p[p[z]]]
- 3 then y ← right[p[p[z]]]
- 4 if color[y] = RED
- 5 then color[p[z]] ← BLACK ? Case 1
- 6 color[y] ← BLACK ? Case 1
- 7 color[p[p[z]]] ← RED ? Case 1
- 8 z ← p[p[z]] ? Case 1
- 9 else if z = right[p[z]]
- 10 then z ← p[z] ? Case 2
- 11 LEFT-ROTATE(T, z) ? Case 2
- 12 color[p[z]] ← BLACK ? Case 3
- 13 color[p[p[z]]] ← RED ? Case 3
- 14 RIGHT-ROTATE(T, p[p[z]]) ? Case 3
- 15 else (same as then clause with "right" and "left" exchanged)
- 16 color[root[T]] ← BLACK
- */
- //然後的工作,就非常簡單了,即把上述僞代碼改寫爲下述的c++代碼:
- void InsertFixUp(RB_Node* node)
- {
- while(node->parent->RB_COLOR==RED)
- {
- if(node->parent==node->parent->parent->left) //
- {
- RB_Node* uncle = node->parent->parent->right;
- if(uncle->RB_COLOR == RED) //插入情況1,z的叔叔y是紅色的。
- {
- node->parent->RB_COLOR = BLACK;
- uncle->RB_COLOR = BLACK;
- node->parent->parent->RB_COLOR = RED;
- node = node->parent->parent;
- }
- else if(uncle->RB_COLOR == BLACK ) //插入情況2:z的叔叔y是黑色的,。
- {
- if(node == node->parent->right) //且z是右孩子
- {
- node = node->parent;
- RotateLeft(node);
- }
- else //插入情況3:z的叔叔y是黑色的,但z是左孩子。
- {
- node->parent->RB_COLOR = BLACK;
- node->parent->parent->RB_COLOR = RED;
- RotateRight(node->parent->parent);
- }
- }
- }
- else //這部分是針對爲插入情況1中,z的父親現在作爲祖父的右孩子了的情況,而寫的。
- //15 else (same as then clause with "right" and "left" exchanged)
- {
- RB_Node* uncle = node->parent->parent->left;
- if(uncle->RB_COLOR == RED)
- {
- node->parent->RB_COLOR = BLACK;
- uncle->RB_COLOR = BLACK;
- uncle->parent->RB_COLOR = RED;
- node = node->parent->parent;
- }
- else if(uncle->RB_COLOR == BLACK)
- {
- if(node == node->parent->left)
- {
- node = node->parent;
- RotateRight(node); //與上述代碼相比,左旋改爲右旋
- }
- else
- {
- node->parent->RB_COLOR = BLACK;
- node->parent->parent->RB_COLOR = RED;
- RotateLeft(node->parent->parent); //右旋改爲左旋,即可。
- }
- }
- }
- }
- m_root->RB_COLOR = BLACK;
- }
- //左旋代碼實現
- bool RotateLeft(RB_Node* node)
- {
- if(node==m_nullNode || node->right==m_nullNode)
- {
- return false;//can't rotate
- }
- RB_Node* lower_right = node->right;
- lower_right->parent = node->parent;
- node->right=lower_right->left;
- if(lower_right->left!=m_nullNode)
- {
- lower_right->left->parent = node;
- }
- if(node->parent==m_nullNode) //rotate node is root
- {
- m_root = lower_right;
- m_nullNode->left = m_root;
- m_nullNode->right= m_root;
- //m_nullNode->parent = m_root;
- }
- else
- {
- if(node == node->parent->left)
- {
- node->parent->left = lower_right;
- }
- else
- {
- node->parent->right = lower_right;
- }
- }
- node->parent = lower_right;
- lower_right->left = node;
- }
- //右旋代碼實現
- bool RotateRight(RB_Node* node)
- {
- if(node==m_nullNode || node->left==m_nullNode)
- {
- return false;//can't rotate
- }
- RB_Node* lower_left = node->left;
- node->left = lower_left->right;
- lower_left->parent = node->parent;
- if(lower_left->right!=m_nullNode)
- {
- lower_left->right->parent = node;
- }
- if(node->parent == m_nullNode) //node is root
- {
- m_root = lower_left;
- m_nullNode->left = m_root;
- m_nullNode->right = m_root;
- //m_nullNode->parent = m_root;
- }
- else
- {
- if(node==node->parent->right)
- {
- node->parent->right = lower_left;
- }
- else
- {
- node->parent->left = lower_left;
- }
- }
- node->parent = lower_left;
- lower_left->right = node;
- }
- //--------------------------刪除結點總操作----------------------------------
- //僞代碼,不再貼出,詳情,請參考此紅黑樹系列第二篇文章:
- //經典算法研究系列:五、紅黑樹算法的實現與剖析:
- //http://blog.csdn.net/v_JULY_v/archive/2010/12/31/6109153.aspx。
- bool Delete(KEY key)
- {
- RB_Node* delete_point = find(key);
- if(delete_point == m_nullNode)
- {
- return false;
- }
- if(delete_point->left!=m_nullNode && delete_point->right!=m_nullNode)
- {
- RB_Node* successor = InOrderSuccessor(delete_point);
- delete_point->data = successor->data;
- delete_point->key = successor->key;
- delete_point = successor;
- }
- RB_Node* delete_point_child;
- if(delete_point->right!=m_nullNode)
- {
- delete_point_child = delete_point->right;
- }
- else if(delete_point->left!=m_nullNode)
- {
- delete_point_child = delete_point->left;
- }
- else
- {
- delete_point_child = m_nullNode;
- }
- delete_point_child->parent = delete_point->parent;
- if(delete_point->parent==m_nullNode)//delete root node
- {
- m_root = delete_point_child;
- m_nullNode->parent = m_root;
- m_nullNode->left = m_root;
- m_nullNode->right = m_root;
- }
- else if(delete_point == delete_point->parent->right)
- {
- delete_point->parent->right = delete_point_child;
- }
- else
- {
- delete_point->parent->left = delete_point_child;
- }
- if(delete_point->RB_COLOR==BLACK && !(delete_point_child==m_nullNode && delete_point_child->parent==m_nullNode))
- {
- DeleteFixUp(delete_point_child);
- }
- delete delete_point;
- return true;
- }
- //---------------------刪除結點性質修復-----------------------------------
- //所有的工作,都在下述23行僞代碼中:
- /*
- RB-DELETE-FIXUP(T, x)
- 1 while x ≠ root[T] and color[x] = BLACK
- 2 do if x = left[p[x]]
- 3 then w ← right[p[x]]
- 4 if color[w] = RED
- 5 then color[w] ← BLACK ? Case 1
- 6 color[p[x]] ← RED ? Case 1
- 7 LEFT-ROTATE(T, p[x]) ? Case 1
- 8 w ← right[p[x]] ? Case 1
- 9 if color[left[w]] = BLACK and color[right[w]] = BLACK
- 10 then color[w] ← RED ? Case 2
- 11 x p[x] ? Case 2
- 12 else if color[right[w]] = BLACK
- 13 then color[left[w]] ← BLACK ? Case 3
- 14 color[w] ← RED ? Case 3
- 15 RIGHT-ROTATE(T, w) ? Case 3
- 16 w ← right[p[x]] ? Case 3
- 17 color[w] ← color[p[x]] ? Case 4
- 18 color[p[x]] ← BLACK ? Case 4
- 19 color[right[w]] ← BLACK ? Case 4
- 20 LEFT-ROTATE(T, p[x]) ? Case 4
- 21 x ← root[T] ? Case 4
- 22 else (same as then clause with "right" and "left" exchanged)
- 23 color[x] ← BLACK
- */
- //接下來的工作,很簡單,即把上述僞代碼改寫成c++代碼即可。
- void DeleteFixUp(RB_Node* node)
- {
- while(node!=m_root && node->RB_COLOR==BLACK)
- {
- if(node == node->parent->left)
- {
- RB_Node* brother = node->parent->right;
- if(brother->RB_COLOR==RED) //情況1:x的兄弟w是紅色的。
- {
- brother->RB_COLOR = BLACK;
- node->parent->RB_COLOR = RED;
- RotateLeft(node->parent);
- }
- else //情況2:x的兄弟w是黑色的,
- {
- if(brother->left->RB_COLOR == BLACK && brother->right->RB_COLOR == BLACK)
- //且w的倆個孩子都是黑色的。
- {
- brother->RB_COLOR = RED;
- node = node->parent;
- }
- else if(brother->right->RB_COLOR == BLACK)
- //情況3:x的兄弟w是黑色的,w的右孩子是黑色(w的左孩子是紅色)。
- {
- brother->RB_COLOR = RED;
- brother->left->RB_COLOR = BLACK;
- RotateRight(brother);
- }
- else if(brother->right->RB_COLOR == RED)
- //情況4:x的兄弟w是黑色的,且w的右孩子時紅色的。
- {
- brother->RB_COLOR = node->parent->RB_COLOR;
- node->parent->RB_COLOR = BLACK;
- brother->right->RB_COLOR = BLACK;
- RotateLeft(node->parent);
- node = m_root;
- }
- }
- }
- else //下述情況針對上面的情況1中,node作爲右孩子而闡述的。
- //22 else (same as then clause with "right" and "left" exchanged)
- //同樣,原理一致,只是遇到左旋改爲右旋,遇到右旋改爲左旋,即可。其它代碼不變。
- {
- RB_Node* brother = node->parent->left;
- if(brother->RB_COLOR == RED)
- {
- brother->RB_COLOR = BLACK;
- node->parent->RB_COLOR = RED;
- RotateRight(node->parent);
- }
- else
- {
- if(brother->left->RB_COLOR==BLACK && brother->right->RB_COLOR == BLACK)
- {
- brother->RB_COLOR = RED;
- node = node->parent;
- }
- else if(brother->left->RB_COLOR==BLACK)
- {
- brother->RB_COLOR = RED;
- brother->right->RB_COLOR = BLACK;
- RotateLeft(brother);
- }
- else if(brother->left->RB_COLOR==RED)
- {
- brother->RB_COLOR = node->parent->RB_COLOR;
- node->parent->RB_COLOR = BLACK;
- brother->left->RB_COLOR = BLACK;
- RotateRight(node->parent);
- node = m_root;
- }
- }
- }
- }
- m_nullNode->parent = m_root; //最後將node置爲根結點,
- node->RB_COLOR = BLACK; //並改爲黑色。
- }
- //
- inline RB_Node* InOrderPredecessor(RB_Node* node)
- {
- if(node==m_nullNode) //null node has no predecessor
- {
- return m_nullNode;
- }
- RB_Node* result = node->left; //get node's left child
- while(result!=m_nullNode) //try to find node's left subtree's right most node
- {
- if(result->right!=m_nullNode)
- {
- result = result->right;
- }
- else
- {
- break;
- }
- } //after while loop result==null or result's right child is null
- if(result==m_nullNode)
- {
- RB_Node* index = node->parent;
- result = node;
- while(index!=m_nullNode && result == index->left)
- {
- result = index;
- index = index->parent;
- }
- result = index; // first right parent or null
- }
- return result;
- }
- //
- inline RB_Node* InOrderSuccessor(RB_Node* node)
- {
- if(node==m_nullNode) //null node has no successor
- {
- return m_nullNode;
- }
- RB_Node* result = node->right; //get node's right node
- while(result!=m_nullNode) //try to find node's right subtree's left most node
- {
- if(result->left!=m_nullNode)
- {
- result = result->left;
- }
- else
- {
- break;
- }
- } //after while loop result==null or result's left child is null
- if(result == m_nullNode)
- {
- RB_Node* index = node->parent;
- result = node;
- while(index!=m_nullNode && result == index->right)
- {
- result = index;
- index = index->parent;
- }
- result = index; //first parent's left or null
- }
- return result;
- }
- //debug
- void InOrderTraverse()
- {
- InOrderTraverse(m_root);
- }
- void CreateGraph(string filename)
- {
- //delete
- }
- void InOrderCreate(ofstream& file,RB_Node* node)
- {
- //delete
- }
- void InOrderTraverse(RB_Node* node)
- {
- if(node==m_nullNode)
- {
- return;
- }
- else
- {
- InOrderTraverse(node->left);
- cout<<node->key<<endl;
- InOrderTraverse(node->right);
- }
- }
- ~RB_Tree()
- {
- clear(m_root);
- delete m_nullNode;
- }
- private:
- // utility function for destructor to destruct object;
- void clear(RB_Node* node)
- {
- if(node==m_nullNode)
- {
- return ;
- }
- else
- {
- clear(node->left);
- clear(node->right);
- delete node;
- }
- }
- private:
- RB_Node *m_nullNode;
- RB_Node *m_root;
- };
- #endif /*_RB_TREE_H_*/
RBTree.cpp
- //file RBTree.cpp
- //written by saturnman,20101008。
- //updated by July,20110329。
- //所有的頭文件都已補齊,現在您可以直接複製此份源碼上機驗證了(版權所有,侵權必究)。
- //July、updated,2011.05.06。
- #include<iostream>
- #include<algorithm>
- #include<iterator>
- #include<vector>
- #include<sstream>
- #include"RBTree.h" //如果.h文件,和cpp文件放在一個文件裏,此句去掉
- using namespace std;
- int main()
- {
- RB_Tree<int,int> tree;
- vector<int> v;
- for(int i=0;i<20;++i)
- {
- v.push_back(i);
- }
- random_shuffle(v.begin(),v.end());
- copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
- cout<<endl;
- stringstream sstr;
- for(i=0;i<v.size();++i)
- {
- tree.Insert(v[i],i);
- cout<<"insert:"<<v[i]<<endl; //添加結點
- }
- for(i=0;i<v.size();++i)
- {
- cout<<"Delete:"<<v[i]<<endl;
- tree.Delete(v[i]); //刪除結點
- tree.InOrderTraverse();
- }
- cout<<endl;
- tree.InOrderTraverse();
- return 0;
- }
運行效果圖(先是一一插入各結點,然後再刪除所有的結點):
第二部分、程序有bug?
2.1、紅黑樹要求絕對平衡麼?
據網友鑫反饋,上述c++源碼雖說從上面的測試結果來看,沒有問題。但程序還是有隱藏的bug,下面,分兩個步驟再來測試下此段源碼:
1、首先在RBTree.h的最後裏添加下述代碼:
- public:
- void PrintTree()
- {
- _printNode(m_root);
- }
- private:
- void _printNode(RB_Node *node)
- {
- if(node == NULL || node == m_nullNode) return;
- if(node->parent == NULL || node->parent == m_nullNode){
- printf("root:%d/n", node->data);
- }else if(node->parent->left == node){
- printf("left:%d, parent:%d/n", node->data, node->parent->data);
- }else if(node->parent->right == node){
- printf("right:%d, parent:%d/n", node->data, node->parent->data);
- }
- _printNode(node->left);
- _printNode(node->right);
- }
2、改寫RBTree.cpp文件,如下:
- //file RBTree.cpp
- //written by saturnman,20101008。
- //updated by July,20110329。
- //所有的頭文件都已補齊,現在您可以直接複製此份源碼上機驗證了(版權所有,侵權必究)。
- //July、updated,2011.05.06。
- #include<iostream>
- #include<algorithm>
- #include<iterator>
- #include<vector>
- #include<sstream>
- //#include"RBTree.h" //如果.h文件,和cpp文件放在一個文件裏,此句去掉
- using namespace std;
- int main()
- {
- RB_Tree<int,int> tree;
- tree.Insert(12, 12);
- tree.Insert(1, 1);
- tree.Insert(9, 9);
- tree.Insert(2, 2);
- tree.Insert(0, 0);
- tree.Insert(11, 11);
- tree.Insert(7, 7);
- tree.Delete(9);
- tree.PrintTree();
- /*vector<int> v;
- for(int i=0;i<20;++i)
- {
- v.push_back(i);
- }
- random_shuffle(v.begin(),v.end());
- copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
- cout<<endl;
- stringstream sstr;
- for(i=0;i<v.size();++i)
- {
- tree.Insert(v[i],i);
- cout<<"insert:"<<v[i]<<endl; //添加結點
- }
- for(i=0;i<v.size();++i)
- {
- cout<<"Delete:"<<v[i]<<endl;
- tree.Delete(v[i]); //刪除結點
- tree.InOrderTraverse();
- }
- cout<<endl;
- tree.InOrderTraverse();*/
- return 0;
- }
後經測試,結果,的確有誤,即依次插入以下節點,12,1,9,0,2,11,7後,紅黑樹變爲如下:
然後刪除根節點9,經過上述程序運行後,運行結果,如下:
即上述運行結果,所對應的紅黑樹的狀態如下(此時,紅黑樹已經不再平衡,存在的問題確實已經很明顯了):
是的,如你所見,上述程序刪除根節點9之後,正確的紅黑樹的狀態應該爲7代替根節點9,7成爲新的根節點,且節點7着爲黑色,而上述結果則是完全錯誤,紅黑樹已經完全不平衡。至此,終於發現,此c++程序存在隱藏bug了。至於修正,則還得等一段時間。
說明:此程序的bug是經網友鑫指出的,同時,他還發現,網上不少的程序,都存在這個問題,比如這裏:http://sd.csdn.net/a/20110506/297285.html的紅黑樹的flash演示版本,也存在此類的問題。已在原文下發表了以下評論:
很遺憾,經反覆測試,紅黑樹的flash版本有問題(其它的暫還沒發現問題):http://www.cs.usfca.edu/~galles/visualization/flash.html。
如依次往插入這個序列,15,1,9,2,0,12,16,7,11,13,17,14,然後再刪除根節點9,嚴重的錯誤就出來了。上面的版本只是簡單的一個步驟用7代替9,成爲根節點,然後把7節點着爲黑色。樹卻沒有後續調整,完全不平衡。
特此,把問題指出來,希望,這個紅黑樹的錯誤flash版本不致誤導更多的人,同時,問題是朋友鑫提出的)。
我會記住這個問題,如果解決了,再發布在博客裏。
後續:鑫指出:avl樹也有問題。
July、結構之法 算法之道 博主。
2011.05.07。
但事實是,果真如此麼?請看下文2.1節的修正。
2.1、紅黑樹不要求嚴格平衡
修正:本程序沒有任何問題。有一點非常之重要,之前就是因爲未意識到而造成上述錯覺,即:紅黑樹並非嚴格意義上的二叉查找樹,它只要滿足它本身的五點性質即可,不要求嚴格平衡。所以,上面的例子中,12,1,9,0,2,11,7,然後刪除根結點9,只要着色適當,同樣不違反紅黑樹的五點性質。所以,結論是,我庸人自擾了,sorry。
還是這句話,有任何問題,歡迎任何人提出或指正。
第三部分、讀者反饋
關於RB_Tree插入刪除操作的討論
July:
你好!關於RB_Tree的完整實現代碼,你已經在你的博客中寫出了。但我認爲,你的代碼中有需要改正的地方。
起 因
我這段時間正好在學習RB_Tree,由於我忽略了RB_Tree的性質(3):每個葉子結點都是黑色的,導致我對RB_Tree的操作糾結了好幾天。在我還沒意識到的時候,偶然間看到你的博客,想從中獲得答案。然後就發現其中有值得商榷的地方。
錯 誤
下圖是你寫的插入修正函數InsertFixUp的部分截圖:
你的文章地址:http://blog.csdn.net/v_july_v/article/details/6285620
圖 1
正如《算法導論》所言,InsertFixUp 中每一次while循環都要面對3種情況:
case 1:z的叔叔y是紅色的;
case 2:z的叔叔y是黑色的,且z是右孩子;
case 3:z的叔叔y是黑色的,且z是左孩子.
並且case 2是落在case 3內的,所以這兩種情況不是相互排斥的!而在你的代碼中,將case 2和case 3分別放在if和else中,導致它們相互獨立。這是不對的。
修 正
所以,在圖1中“標記①”處的else是不能加的,應將其刪除。
遺憾的是,我認爲你的RB_Tree的刪除修正操作DeleteFixUp也出現了類似的錯誤:對於DeleteFixUp所處理的4種情況也同樣不是相互排斥的,而你用一組if…else if…else if…將case 2, 3, 4全部獨立開來。
以上便是鄙人的一點拙見,如果你認爲有錯誤的地方,歡迎再討論!
楊 超
CSDN ID: crisischaos
2011.10.06
考證:非常感謝楊兄來信指導。從算法導論一書原來的插入情況的修復僞代碼來看:
- //---------------------插入結點性質修復--------------------------------
- //3種插入情況,都在下面的僞代碼中(未涉及到所有全部的插入情況)。
- /*
- RB-INSERT-FIXUP(T, z)
- 1 while color[p[z]] = RED
- 2 do if p[z] = left[p[p[z]]]
- 3 then y ← right[p[p[z]]]
- 4 if color[y] = RED
- 5 then color[p[z]] ← BLACK ? Case 1
- 6 color[y] ← BLACK ? Case 1
- 7 color[p[p[z]]] ← RED ? Case 1
- 8 z ← p[p[z]] ? Case 1
- 9 else if z = right[p[z]]
- 10 then z ← p[z] ? Case 2
- 11 LEFT-ROTATE(T, z) ? Case 2
- 12 color[p[z]] ← BLACK ? Case 3
- 13 color[p[p[z]]] ← RED ? Case 3
- 14 RIGHT-ROTATE(T, p[p[z]]) ? Case 3
- 15 else (same as then clause with "right" and "left" exchanged)
- 16 color[root[T]] ← BLACK
- */
- //然後的工作,就非常簡單了,即把上述僞代碼改寫爲下述的c++代碼: ....
確實如楊兄所說,理應如此(包括其後的對刪除情況的修復)。日後,再做統一修改,再次謝謝。July、2011.10.06更新。
參考文獻,本人的原創作品紅黑樹系列的前五篇文章:
完。