沈阳建站平台,网络设计课程设计前言,网站建设属于什么税种,国外服务器租用价格表目录
资源已上传
实现代码
测试代码 资源已上传
部分图片 实现代码
注意判断是否为红黑树的代码实现#xff0c;实现代码中红黑树的删除
#pragma once
#includeiostream
using namespace std;enum Color_Type
{Red,Black
};templateclass K,class V
str…目录
资源已上传
实现代码
测试代码 资源已上传
部分图片 实现代码
注意判断是否为红黑树的代码实现实现代码中红黑树的删除
#pragma once
#includeiostream
using namespace std;enum Color_Type
{Red,Black
};templateclass K,class V
struct RBTreeNode
{pairK, V _kv;RBTreeNode* _parent;RBTreeNode* _left;RBTreeNode* _right;Color_Type _color;RBTreeNode(const pairK, V kv): _kv(kv), _parent(nullptr), _left(nullptr), _right(nullptr), _color(Red){}};templateclass K, class V
class RBTree
{typedef RBTreeNodeK, V Node;
private:Node* _root;
public:RBTree():_root(nullptr){}bool Insert(const pairK, V kv){if (_root nullptr){_root new Node(kv);_root-_color Black;return true;}Node* cur _root;//指向当前节点的位置Node* parent nullptr;//当前节点的父节点//找到插入位置while (cur){if (cur-_kv.first kv.first) {parent cur;cur cur-_right;}else if (cur-_kv.first kv.first) {parent cur;cur cur-_left;}else {return false;}}cur new Node(kv);cur-_color Red;if (cur-_kv.first parent-_kv.first) {parent-_left cur;cur-_parent parent;}else{parent-_right cur;cur-_parent parent;}//父节点为红色插入后还需做调整while (parent parent-_color Red){Node* grandfather parent-_parent;if (parent grandfather-_left){Node* uncle grandfather-_right;if (uncle uncle-_color Red){// 变色parent-_color uncle-_color Black;grandfather-_color Red;// 继续向上处理cur grandfather;parent cur-_parent;}else//uncle不存在或uncle为黑{if (cur parent-_left){// g// p//crotateR(grandfather);parent-_color Black;grandfather-_color Red;}else{// g//p // crotateL(parent);rotateR(grandfather);cur-_color Black;grandfather-_color Red;}break;}}else{Node* uncle grandfather-_left;// u存在且为红if (uncle uncle-_color Red){// 变色parent-_color uncle-_color Black;grandfather-_color Red;// 继续向上处理cur grandfather;parent cur-_parent;}else{if (cur parent-_right){// g// p// crotateL(grandfather);grandfather-_color Red;parent-_color Black;}else{// g// p// crotateR(parent);rotateL(grandfather);cur-_color Black;grandfather-_color Red;}break;}}}_root-_color Black;return true;}bool IsBalance(){return _IsBalance(_root);}int Height(){return _Height(_root);}bool checkColor(Node* root, int blacknum, int basenum){if (root nullptr){if (blacknum ! basenum){return false;}return true;}if (root-_color Black){blacknum;}if (root-_parent root-_parent-_color Red root-_color Red){cout root-_kv.first 出现连续红色节点 endl;return false;}return checkColor(root-_left, blacknum, basenum) checkColor(root-_right, blacknum, basenum);}
private:void rotateL(Node* parent){Node* subR parent-_right;Node* subRL subR-_left;Node* pparent parent-_parent;//1.建立subRL与parent之间的关系//左子树滑动parent-_right subRL;if (subRL) {subRL-_parent parent;}//2.建立subR和parent之间的关系//更新右子树的左子树subR-_left parent;parent-_parent subR;//3.建立pparent和subR之间的关系//与上一个节点建立连接if (parent _root){_root subR;subR-_parent nullptr;}else{subR-_parent pparent;if (parent pparent-_left) {pparent-_left subR;}else {pparent-_right subR;}}}void rotateR(Node* parent){Node* subL parent-_left;Node* subLR subL-_right;Node* pparent parent-_parent;//滑动parent-_left subLR;if (subLR) {subLR-_parent parent;}//更新左子树的右子树subL-_right parent;parent-_parent subL;if (parent _root) {_root subL;subL-_parent nullptr;}else {subL-_parent pparent;if (parent pparent-_left) {pparent-_left subL;}else {pparent-_right subL;}}}int _Height(Node* root){if (!root) {return 0;}int left _Height(root-_left);int right _Height(root-_right);return left right ? left 1 : right 1;}bool _IsBalance(Node* root){if (root nullptr){return true;}if (root-_color ! Black){return false;}int basenum 0;Node* cur _root;while (cur){if (cur-_color Black) {basenum;}cur cur-_left;}return checkColor(root, 0, basenum);}};
测试代码
#include RB_tree.h
#includevectorint main()
{const int N 10;vectorint v;v.reserve(N);srand(time(0));for (size_t i 0; i N; i){v.push_back(i);}for (auto i : v){cout i ;}cout endl;RBTreeint, int rbt;for (auto e : v){rbt.Insert(make_pair(e, e));cout Insert: e endl;}cout rbt.Height() endl;if (rbt.IsBalance()){cout ok endl;}return 0;
}
文章转载自: http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.rymd.cn.gov.cn.rymd.cn http://www.morning.qglqb.cn.gov.cn.qglqb.cn http://www.morning.nsrtvu.com.gov.cn.nsrtvu.com http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.mrfgy.cn.gov.cn.mrfgy.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.pdynk.cn.gov.cn.pdynk.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.pnntx.cn.gov.cn.pnntx.cn http://www.morning.wgzzj.cn.gov.cn.wgzzj.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.hnkkm.cn.gov.cn.hnkkm.cn http://www.morning.fxzgw.com.gov.cn.fxzgw.com http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.nwnbq.cn.gov.cn.nwnbq.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.lskrg.cn.gov.cn.lskrg.cn http://www.morning.kpnpd.cn.gov.cn.kpnpd.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.mhxlb.cn.gov.cn.mhxlb.cn http://www.morning.sfgtp.cn.gov.cn.sfgtp.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.dsprl.cn.gov.cn.dsprl.cn http://www.morning.srgwr.cn.gov.cn.srgwr.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.rwpfb.cn.gov.cn.rwpfb.cn http://www.morning.lfcnj.cn.gov.cn.lfcnj.cn http://www.morning.rpgdd.cn.gov.cn.rpgdd.cn http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.ntqlz.cn.gov.cn.ntqlz.cn http://www.morning.lbjdx.cn.gov.cn.lbjdx.cn http://www.morning.yxshp.cn.gov.cn.yxshp.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.tqpnf.cn.gov.cn.tqpnf.cn http://www.morning.nnwmd.cn.gov.cn.nnwmd.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.fbzyc.cn.gov.cn.fbzyc.cn http://www.morning.kzcfr.cn.gov.cn.kzcfr.cn http://www.morning.dwrbn.cn.gov.cn.dwrbn.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.ktcrr.cn.gov.cn.ktcrr.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.kgcss.cn.gov.cn.kgcss.cn http://www.morning.bnpn.cn.gov.cn.bnpn.cn http://www.morning.xqcbz.cn.gov.cn.xqcbz.cn http://www.morning.qqxmj.cn.gov.cn.qqxmj.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.fpzpb.cn.gov.cn.fpzpb.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.wkmrl.cn.gov.cn.wkmrl.cn