1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問(wèn)題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
      RBTree(RED,BLACK)Tree-創(chuàng)新互聯(lián)

      紅黑樹(shù)是一棵二叉搜索樹(shù),它在每個(gè)節(jié)點(diǎn)上增加了一個(gè)存儲(chǔ)位來(lái)表示節(jié)點(diǎn)的顏色,可以是Red或Black。通過(guò)對(duì)任何一條從根到葉子簡(jiǎn)單路徑上的顏色來(lái)約束,紅黑樹(shù)保證最長(zhǎng)路徑不超過(guò)最短路徑的兩倍,因而近似于平衡。

      成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供孟津企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為孟津眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
      • 紅黑樹(shù)是滿足下面紅黑性質(zhì)的二叉搜索樹(shù)

      1. 每個(gè)節(jié)點(diǎn),不是紅色就是黑色的

      2. 根節(jié)點(diǎn)是黑色的

      3. 如果一個(gè)節(jié)點(diǎn)是紅色的,則它的兩個(gè)子節(jié)點(diǎn)是黑色的(沒(méi)有連續(xù)的紅節(jié)點(diǎn))

      4. 對(duì)每個(gè)節(jié)點(diǎn),從該節(jié)點(diǎn)到其所有后代葉節(jié)點(diǎn)的簡(jiǎn)單路徑上,均包含相同數(shù)目的黑色節(jié)點(diǎn)。(每條路徑的黑色節(jié)點(diǎn)的數(shù)量相等)

      5. 每個(gè)葉子節(jié)點(diǎn)都是黑色的(這里的葉子節(jié)點(diǎn)是指的NIL節(jié)點(diǎn)(空節(jié)點(diǎn)))

      #include
      using namespace std;
      enum COL
      {
      	RED,
      	BLACK
      };
      template
      struct RBTreeNode
      {
      	K _key;
      	V _val;
      	RBTreeNode* _left;
      	RBTreeNode* _right;
      	RBTreeNode* _parent;
      	COL _col;
      	RBTreeNode(K& key, V& val)
      		:_key(key)
      		, _val(val)
      		, _left(NULL)
      		, _right(NULL)
      		, _parent(NULL)
      		, _col(RED)
      	{}
      };
      template
      class RBTree{
      	typedef RBTreeNode Node;
      public:
      	RBTree()
      		:_root(NULL)
      	{}
      	bool Insert(K& key, V& val)
      	{
      		if (_root == NULL)
      		{
      			_root = new Node(key, val);
      		}
      		else
      		{
      			Node* parent = NULL;
      			Node* cur = _root;
      			while (cur)
      			{
      				if (cur->_key < key)
      				{
      					parent = cur;
      					cur = cur->_right;
      				}
      				else if (cur->_key>key)
      				{
      					parent = cur;
      					cur = cur->_left;
      				}
      				else
      				{
      					return false;
      				}
      			}
      			cur = new Node(key, val);
      			if (parent->_key < key)
      				parent->_right = cur;
      			else
      				parent->_left = cur;
      			cur->_parent = parent;
      			//符合紅黑樹(shù)規(guī)范
      
      			while (cur != _root&&parent->_col == RED)
      			{
      				Node* grandfather = parent->_parent;
      				if (grandfather->_left == parent)
      				{
      					Node* uncle = grandfather->_right;
      					if (uncle&&uncle->_col == RED)//1.
      					{
      						parent->_col = uncle->_col=BLACK;
      						grandfather->_col = RED;
      
      						//繼續(xù)往上調(diào)
      						cur = grandfather;
      						parent = cur->_parent;
      					}
      					else
      					{
      						if (cur == parent->_right)
      						{
      							RotateL(parent);
      							swap(parent, cur);//*左旋后,指針位置yibian
      						}
      						parent->_col = BLACK;
      						grandfather->_col = RED;
      						RotateR(grandfather);
      						break;
      					}
      				}
      				else//反的情況
      				{
      					Node* uncle = grandfather->_left;
      					if (uncle&&uncle->_col == RED)
      					{
      						parent->_col = uncle->_col = BLACK;
      						grandfather->_col = RED;
      						//繼續(xù)往上調(diào)
      						cur = grandfather;
      						parent = cur->_parent;
      					}
      					else
      					{
      						if (cur == parent->_left)
      						{
      							RotateR(parent);
      							swap(cur, parent);
      						}
      						parent->_col = BLACK;
      						grandfather->_col = RED;
      						RotateL(grandfather);
      						break;
      					}
      				}
      			}
      		}
      		_root->_col = BLACK;
      		return true;
      	}
      	Node* Find(const K& key)
      	{
      		Node* cur = _root;
      		while (cur)
      		{
      			if (cur->_key < key)
      				cur = cur->_right;
      			else if (cur->_key>key)
      				cur = cur->_left;
      			else
      				return cur;
      		}
      		return NULL;
      	}
      	bool Remove(const K& key)
      	{
      
      	}
      	bool isBalance()
      	{
      		if (_root == NULL)
      			return true;
      		if (_root->_col == RED)
      			return false;
      		int k = 0;
      		Node* cur = _root;
      		while (cur)
      		{
      			if (cur->_col==BLACK)
      				++k;
      			cur = cur->_left;
      		}
      		int count = 0;
      		return _IsBalance(_root, k, count);
      	}
      	void InOrder()
      	{
      		_InOrder(_root);
      	}
      protected:
      	void _InOrder(Node* root)
      	{
      		if (root == NULL)
      			return;
      		_InOrder(root->_left);
      		cout << root->_key << " ";
      //		cout << root->_key << "color"<_col << "  ";
      		_InOrder(root->_right);
      	}
      	bool _IsBalance(Node* root, const int k, int count)
      	{
      		if (root == NULL)
      			return true;
      		if (root->_col == RED)
      		{
      			if (root->_parent->_col == RED)
      			{
      				cout << "顏色不正確" << root->_key << endl;
      				return false;
      			}
      		}
      		else
      			++count;
      		if (root->_left == NULL&&root->_right == NULL)
      		{
      			if (count == k)
      				return true;
      			else
      			{
      				cout << "黑色節(jié)點(diǎn)個(gè)數(shù)不對(duì)" << root->_key<_left, k, count) && _IsBalance(root->_right, k, count);
      	}
      	void RotateL(Node* parent)
      	{
      		Node* subR = parent->_right;
      		Node* subRL = subR->_left;
      		parent->_right = subRL;
      		if (subRL)
      			subRL->_parent = parent;
      
      		Node* ppNode = parent->_parent;
      		subR->_left = parent;
      		parent->_parent = subR;
      		if (ppNode == NULL)
      		{
      			subR->_parent = NULL;
      			_root = subR;
      		}
      		else
      		{
      			if (ppNode->_left == parent)
      			{
      				ppNode->_left = subR;
      			}
      			else
      			{
      				ppNode->_right = subR;
      			}
      			subR->_parent = ppNode;
      		}
      	}
      	void RotateR(Node* parent)
      	{
      		Node* subL = parent->_left;
      		Node* subLR = subL->_right;
      		parent->_left = subLR;
      		if (subLR)
      			subLR->_parent = parent;
      
      		Node* ppNode = parent->_parent;
      		subL->_right = parent;
      		parent->_parent = subL;
      		if (ppNode == NULL)
      		{
      			subL->_parent = NULL;
      			_root = subL;
      		}
      		else
      		{
      			if (ppNode->_left == parent)
      			{
      				ppNode->_left = subL;
      			}
      			else
      			{
      				ppNode->_right = subL;
      			}
      			subL->_parent = ppNode;
      		}
      	}
      private:
      	Node* _root;
      };
      void Test1()
      {
      	RBTree t;
      	int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
      	for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
      	{
      		t.Insert(a[i], i);
      	}
      	t.InOrder();
      	t.isBalance();
      }
      
      #include"RBtree.h"
      int main()
      {
      	Test1();
      	system("pause");
      	return 0;
      }

      另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。


      標(biāo)題名稱:RBTree(RED,BLACK)Tree-創(chuàng)新互聯(lián)
      網(wǎng)頁(yè)網(wǎng)址:http://www.ef60e0e.cn/article/doiihd.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        固始县| 北川| 昭觉县| 新野县| 称多县| 紫金县| 四川省| 乐平市| 黄骅市| 蓝田县| 蚌埠市| 九龙城区| 鹿邑县| 海林市| 灵台县| 西华县| 湘潭县| 焉耆| 额敏县| 家居| 台南县| 东乌珠穆沁旗| 彰化市| 出国| 彰化市| 南丰县| 黔西县| 罗源县| 汪清县| 尚义县| 富源县| 如皋市| 调兵山市| 眉山市| 龙门县| 东安县| 莱西市| 莱州市| 沛县| 汪清县| 腾冲县|