日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C++ 泛型编程 实现红黑树RBTree

發布時間:2023/12/4 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ 泛型编程 实现红黑树RBTree 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼如下:

#include <iostream> #include <ctime> using namespace std;enum COLOR {BLACK,RED };template<typename T> struct RBTreeNode {RBTreeNode<T> * _parent;RBTreeNode<T> * _left;RBTreeNode<T> * _right;T _val;COLOR _color;RBTreeNode(const T & val = T()):_parent(nullptr),_left(nullptr),_right(nullptr),_val(val),_color(RED){}};template<typename T> class RBTree { public:typedef RBTreeNode<T> Node;RBTree():_root(nullptr){}bool insert(const T & val){if (_root == nullptr) {_root = new Node(val);_root->_color = BLACK;return true;}Node *cur = _root;Node *parent = nullptr;//1.尋找要插入的結點的位置while (cur){parent = cur;if (cur->_val == val) return false;else if (cur->_val > val) cur = cur->_left;else cur = cur->_right;}//2.創建結點cur = new Node(val);if (parent->_val > cur->_val) parent->_left = cur;else parent->_right = cur;cur->_parent = parent;//3.顏色的修改或者結構的調整while (cur != _root && cur->_parent->_color == RED)//不為根且存在連續紅色,則需要調整{parent = cur->_parent;Node *gfather = parent->_parent;if (gfather->_left == parent){Node *uncle = gfather->_right;//情況1.uncle存在且為紅if (uncle && uncle->_color == RED){parent->_color = uncle->_color = BLACK;gfather->_color = RED;//向上追溯cur = gfather;}else{if (parent->_right == cur){RotateL(parent);swap(cur, parent);}//情況2.uncle不存在或者uncle為黑RotateR(gfather);parent->_color = BLACK;gfather->_color = RED;break;}}else{Node *uncle = gfather->_left;if (uncle && uncle->_color == RED){parent->_color = uncle->_color = BLACK;gfather->_color = RED;//向上追溯cur = gfather;}else{if (parent->_left == cur){RotateR(parent);swap(cur, parent);}RotateL(gfather);parent->_color = BLACK;gfather->_color = RED;break;}}}//根結點都為黑色_root->_color = BLACK;return true;}void RotateL(Node *parent){Node *subR = parent->_right;Node *subRL = subR->_left;parent->_right = subRL;if (subRL) subRL->_parent = parent;if (parent == _root){_root = subR;subR->_parent = nullptr;}else{Node *gfather = parent->_parent;if (gfather->_left == parent) gfather->_left = subR;else gfather->_right = subR;subR->_parent = gfather;}subR->_left = parent;parent->_parent = subR;}void RotateR(Node * parent){Node *subL = parent->_left;Node *subLR = subL->_right;parent->_left = subLR;if (subLR) subLR->_parent = parent;if (parent == _root){_root = subL;subL->_parent = nullptr;}else{Node *gfather = parent->_parent;if (gfather->_left == parent) gfather->_left = subL;else gfather->_right = subL;subL->_parent = gfather;}subL->_right = parent;parent->_parent = subL;}void inorder(){_inorder(_root);cout << endl;} //紅黑樹的性質: // 1.每個結點的顏色只有紅色和黑色,且根結點必須為黑色 // 2.不能存在連續的紅色結點 // 3.對于每個結點來說,從該結點到其所有后代結點的簡單路徑上,經過的黑色結點的數目應該是相同的bool isRBTree(){if (_root == nullptr) return true;//空樹也屬于紅黑樹if (_root->_color == RED) return false;//不滿足性質2:根結點必須為黑色int bCount = 0;//從某條路徑上的黑色結點數Node *cur = _root;while (cur){if (cur->_color == BLACK) ++bCount;cur = cur->_left;}int pathCount = 0;return _isRBTree(_root, bCount, pathCount);}bool _isRBTree(Node *root, const int bCount, int pathCount){if (root == nullptr)//路徑走完,判斷黑色結點個數{if (pathCount == bCount) return true;else return false;//不滿足性質3:每一條路徑上的黑色結點個數相同}if (root->_color == BLACK) ++pathCount;Node *parent = root->_parent;if (parent && parent->_color == RED && root->_color == RED) return false;//不滿足性質2:不能存在連續的紅色結點return _isRBTree(root->_left, bCount, pathCount) && _isRBTree(root->_right, bCount, pathCount);}private:Node *_root;void _inorder(Node *root){if (root){_inorder(root->_left);cout << root->_val << " ";_inorder(root->_right);}} };int main() {RBTree<int> rbt;srand(time(nullptr));int n;cout << "num = " << endl;cin >> n;for (int i = 0; i < n; i++){rbt.insert(rand());}rbt.inorder();if (rbt.isRBTree()) cout << "yes" << endl;else cout << "no" << endl;return 0; }

測試結果:

總結

以上是生活随笔為你收集整理的C++ 泛型编程 实现红黑树RBTree的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。