一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 通過java.util.TreeMap源碼加強(qiáng)紅黑樹的理解

通過java.util.TreeMap源碼加強(qiáng)紅黑樹的理解

2021-02-07 12:09fireway Java教程

通過分析java.util.TreeMap源碼來對經(jīng)典問題紅黑樹加強(qiáng)理解和理清思路。

在此之前,服務(wù)器之家已經(jīng)為大家整理了很多關(guān)于經(jīng)典問題紅黑樹的思路和解決辦法。本篇文章,是通過分析java.util.treemap源碼,讓大家通過實(shí)例來對紅黑樹這個(gè)問題有更加深入的理解。

本篇將結(jié)合jdk1.6的treemap源碼,來一起探索紅-黑樹的奧秘。紅黑樹是解決二叉搜索樹的非平衡問題。

當(dāng)插入(或者刪除)一個(gè)新節(jié)點(diǎn)時(shí),為了使樹保持平衡,必須遵循一定的規(guī)則,這個(gè)規(guī)則就是紅-黑規(guī)則: 
1) 每個(gè)節(jié)點(diǎn)不是紅色的就是黑色的 
2) 根總是黑色的 
3) 如果節(jié)點(diǎn)是紅色的,則它的子節(jié)點(diǎn)必須是黑色的(反之倒不一定必須為真) 
4) 從跟到葉節(jié)點(diǎn)或者空子節(jié)點(diǎn)的每條路徑,必須包含相同數(shù)目的黑色節(jié)點(diǎn)

插入一個(gè)新節(jié)點(diǎn)

紅-黑樹的插入過程和普通的二叉搜索樹基本一致:從跟朝插入點(diǎn)位置走,在每個(gè)節(jié)點(diǎn)處通過比較節(jié)點(diǎn)的關(guān)鍵字相對大小來決定向左走還是向右走。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public v put(k key, v value) {
entry<k,v> t = root;
int cmp;
entry<k,v> parent;
comparable<? super k> k = (comparable<? super k>) key;
do {
parent = t;
cmp = k.compareto(t.key);
if (cmp < 0) {
t = t.left;
} else if (cmp > 0) {
t = t.right;
} else {
// 注意,return退出方法
return t.setvalue(value);
}
} while (t != null);
entry<k,v> e = new entry<k,v>(key, value, parent);
if (cmp < 0) {
parent.left = e;
} else {
parent.right = e;
}
fixafterinsertion(e);
size++;
modcount++;
return null;
}

但是,在紅-黑樹種,找到插入點(diǎn)更復(fù)雜,因?yàn)橛蓄伾儞Q和旋轉(zhuǎn)。fixafterinsertion()方法就是處理顏色變換和旋轉(zhuǎn),需重點(diǎn)掌握它是如何保持樹的平衡(use rotations and the color rules to maintain the tree's balance)。

下面的討論中,使用x、p、g表示關(guān)聯(lián)的節(jié)點(diǎn)。x表示一個(gè)特殊的節(jié)點(diǎn), p是x的父,g是p的父。

x is a node that has caused a rule violation. (sometimes x refers to a newly inserted node, and sometimes to the child node when a parent and child have a redred conflict.)

on the way down the tree to find the insertion point, you perform a color flip whenever you find a black node with two red children (a violation of rule 2). sometimes the flip causes a red-red conflict (a violation of rule 3). call the red child x and the red parent p. the conflict can be fixed with a single rotation or a double rotation, depending on whether x is an outside or inside grandchild of g. following color flips and rotations, you continue down to the insertion point and insert the new node.

after you've inserted the new node x, if p is black, you simply attach the new red node. if p is red, there are two possibilities: x can be an outside or inside grandchild of g. if x is an outside grandchild, you perform one rotation, and if it's an inside grandchild, you perform two. this restores the tree to a balanced state.

按照上面的解釋,討論可分為3個(gè)部分,按復(fù)雜程度排列,分別是: 
1) 在下行路途中的顏色變換(color flips on the way down) 
2) 插入節(jié)點(diǎn)之后的旋轉(zhuǎn)(rotations after the node is inserted) 
3) 在向下路途上的旋轉(zhuǎn)(rotations on the way down)

在下行路途中的顏色變換(color flips on the way down)

here's the rule: every time the insertion routine encounters a black node that has two red children, it must change the children to black and the parent to red (unless the parent is the root, which always remains black)

the flip leaves unchanged the number of black nodes on the path from the root on down through p to the leaf or null nodes.

盡管顏色變換不會(huì)違背規(guī)則4,但是可能會(huì)違背規(guī)則3。如果p的父是黑色的,則p由黑色變成紅色時(shí)不會(huì)有任何問題,但是,如果p的父是紅色的,那么在p的顏色變化之后,就有兩個(gè)紅色節(jié)點(diǎn)相連接了。這個(gè)問題需要在繼續(xù)向下沿著路徑插入新節(jié)點(diǎn)之前解決,可以通過旋轉(zhuǎn)修正這個(gè)問題,下文將會(huì)看到。

插入節(jié)點(diǎn)之后的旋轉(zhuǎn)(rotations after the node is inserted)

新節(jié)點(diǎn)在插入之前,樹是符合紅-黑規(guī)則,在插入新節(jié)點(diǎn)之后,樹就不平衡了,此時(shí)需要通過旋轉(zhuǎn)來調(diào)整樹的平衡,使之重新符合紅-黑規(guī)則。

可能性1:p是黑色的,就什么事情也不用做。插入即可。

可能性2:p是紅色,x是g的一個(gè)外側(cè)子孫節(jié)點(diǎn),則需要一次旋轉(zhuǎn)和一些顏色的變化。 
以插入50,25,75,12,6為例,注意節(jié)點(diǎn)6是一個(gè)外側(cè)子孫節(jié)點(diǎn),它和它的父節(jié)點(diǎn)都是紅色。 

通過java.util.TreeMap源碼加強(qiáng)紅黑樹的理解

在這個(gè)例子中,x是一個(gè)外側(cè)子孫節(jié)點(diǎn)而且是左子節(jié)點(diǎn),x是外側(cè)子孫節(jié)點(diǎn)且為右子節(jié)點(diǎn),是一種與此對稱的情況。通過用50,25,75,87,93創(chuàng)建樹,同理再畫一畫圖,這里就省略了。

可能性3:p是紅色,x是g的一個(gè)內(nèi)側(cè)子孫節(jié)點(diǎn),則需要兩次旋轉(zhuǎn)和一些顏色的改變。 
以插入50,25,75,12,18為例,注意節(jié)點(diǎn)18是一個(gè)內(nèi)側(cè)子孫節(jié)點(diǎn),它和它的父節(jié)點(diǎn)都是紅色。 

通過java.util.TreeMap源碼加強(qiáng)紅黑樹的理解

在向下路途上的旋轉(zhuǎn)(rotations on the way down)

在插入新節(jié)點(diǎn)之前,實(shí)際上樹已經(jīng)違背了紅-黑規(guī)則,所以需要插入新節(jié)點(diǎn)之前做調(diào)整。所以我們本次討論的主題是“在向下路途準(zhǔn)備插入新節(jié)點(diǎn)時(shí),上面先進(jìn)行調(diào)整,使上面成為標(biāo)準(zhǔn)的紅黑樹后,再進(jìn)行新節(jié)點(diǎn)插入”。

外側(cè)子孫節(jié)點(diǎn)

以插入50,25,75,12,37,6,18,3為例,例子中違背規(guī)則的節(jié)點(diǎn)是一個(gè)外側(cè)子孫節(jié)點(diǎn)。 

通過java.util.TreeMap源碼加強(qiáng)紅黑樹的理解

內(nèi)側(cè)子孫節(jié)點(diǎn)

以插入50,25,75,12,37,31,43為例,例子中違背規(guī)則的節(jié)點(diǎn)是一個(gè)內(nèi)側(cè)子孫節(jié)點(diǎn)。
通過java.util.TreeMap源碼加強(qiáng)紅黑樹的理解

紅-黑樹的效率

和一般的二叉搜索樹類似,紅-黑樹的查找、插入和刪除的時(shí)間復(fù)雜度為o(log2n)。

紅-黑樹的查找時(shí)間和普通的二叉搜索樹的查找時(shí)間應(yīng)該幾乎完全一樣。因?yàn)樵诓檎疫^程中并沒用到紅-黑特征。額外的開銷只是每個(gè)節(jié)點(diǎn)的存儲(chǔ)空間都稍微增加了一點(diǎn),來存儲(chǔ)紅黑顏色(一個(gè)boolean變量)。

?
1
final entry<k, v> getentry(object key) {<br>comparable <? super k > k = (comparable <? super k > ) key;<br>entry<k, v> p = root;<br>while (p != null) {<br>int cmp = k.compareto(p.key);<br>if (cmp < 0) {<br>p = p.left;<br>} else if (cmp > 0) {<br>p = p.right;<br>} else {<br>return p;<br>}<br>}<br>return null;<br>}

插入和刪除的時(shí)間要增加一個(gè)常數(shù)因子,因?yàn)椴坏貌辉谙滦械穆窂缴虾筒迦朦c(diǎn)執(zhí)行顏色變換和旋轉(zhuǎn)。平均起來一次插入大約需要一次旋轉(zhuǎn)。

因?yàn)樵诖蠖鄶?shù)應(yīng)用中,查找的次數(shù)比插入和刪除的次數(shù)多,所以應(yīng)用紅-黑樹取代普通的二叉搜索樹總體上不會(huì)增加太多的時(shí)間開銷。

原文鏈接:https://www.cnblogs.com/fireway/p/7862577.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 娇妻被朋友征服中文字幕 | 99在线视频观看 | 国产午夜永久福利视频在线观看 | 男女男在线精品网站免费观看 | 国产精品久久99 | 丝袜白浆 | 亚洲欧美日韩另类在线一 | 国产日韩一区二区三区 | 欧美三级一区 | 91久| 国产里番 | 校园全黄h全肉细节文 | 成人福利在线播放 | 小鸟酱视频在线观看 | 精品国产人成亚洲区 | 91久久偷偷做嫩草影院免费 | 禁忌4中文 | 91赵邦贺 | 久久性综合亚洲精品电影网 | 精品国产一区二区在线观看 | 国产激情影院 | jizz女16处 | 免费成年网 | 2019nv天堂| www视频免费 | 91天堂素人97年清纯嫩模 | 午夜深情在线观看免费 | 亚洲www在线 | 国产一区二区精品久久 | 无人区在线观看免费完整版免费 | 91久久偷偷做嫩草影院电 | 国产图片一区 | 亚洲mm色国产网站 | 久久免费国产视频 | 午夜亚洲一区二区福利 | 美女的隐私视频免费看软件 | 欧亚专线欧洲m码可遇不可求 | 草莓香蕉绿巨人丝瓜榴莲18 | 亚洲欧美日韩精品久久亚洲区 | 国产精品va在线观看无 | 日韩网站在线观看 |