13. Red-Black Trees 1

Definition
A red-black tree (RBT) is a binary search tree with one extra bit of storage per node: its colour can be either RED or BLACK.
Each node in a RBT contains at least the following fields:

  1. color
  2. key
  3. left child pointer left()
  4. right child pointer right()
  5. parent pointer p()

Properties of red-black trees

  1. Every node is colored only with either RED or BLACK.
  2. The root node must be BLACK.
  3. Every leaf (NIL) must be BLACK.
  4. If a node is RED, then both its children are BLACK. This implies the red node must be an internal node.
  5. Each path from a node x to any one of its descendant leaf nodes contains the same number of BLACK nodes. Note that the color of node x will be not counted.
  1. The black height bh(x) of a node x in a RBT is the number of BLACK nodes on the path from node x down to a leaf, not counting x itself but counting the leaf.
    Lemma: A red-black tree with n internal nodes has height h at most 2 log(n + 1).
    The height of the red-black tree h is at most twice the black height of the root 2 · bh(root ), i.e., h ≤ 2 · bh(root ).
    The subtree rooted at any node x contains at least 2^bh(x) ? 1 internal nodes.

Operations on a red-black tree

A red-black tree can support the operations SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR in time O(log n), where n is the number of internal nodes in the RBT.

INSERT and DELETE operations on a red-black tree might destroy its defining properties.
For this, we may have to change:

  1. the color of some nodes in the RBT.
  2. or the pointer structure of nodes in the RBT.

To maintain a balanced tree T, we change the pointer structure by using Rotations:
(1) The left Rotation;
(2) The right rotation.

Notice that the operation of either the left rotation or the right rotation only takes O(1) time by changing the pointers of several nodes, the binary search tree property of the resulting tree is still held.

示例

Insertion in a red-black tree

We use the TREE_INSERT procedure for binary search trees to insert a node x into a red-black tree T as if it were a binary search tree, and then colour the new inserted node RED.

2 violated situation:

  1. The root might be RED (the original tree is empty, or the root is colored with red after a sequence of coloring changes on the tree).
  2. A RED node might have a RED parent.

Solution

  1. If the second violation occurs, we do some operations that either fix it or move it to a higher level, where a level near to the tree root is higher.
  2. If it has not been fixed before we get to the tree root, the first violation occurs. We can fix the first violation by just setting the colour of the tree root to BLACK.

Suppose we have a RED node z which has a RED parent. There are 3 cases.

  1. If case 1 occurs, the prescribed operations either fix the error or move it towards the root.
  2. If cases 2 or 3 occur, the prescribed operations fix the tree always.
  3. If the root is RED, making it BLACK fixes the tree.
示例

The time complexity is O(logn) as its largest height is 2log(n+1).
視頻教學(xué)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末俺猿,一起剝皮案震驚了整個(gè)濱河市市埋,隨后出現(xiàn)的幾起案子姐浮,更是在濱河造成了極大的恐慌砰粹,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡蜗字,警方通過(guò)查閱死者的電腦和手機(jī)打肝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)挪捕,“玉大人粗梭,你說(shuō)我怎么就攤上這事〖读悖” “怎么了断医?”我有些...
    開封第一講書人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)奏纪。 經(jīng)常有香客問(wèn)我鉴嗤,道長(zhǎng),這世上最難降的妖魔是什么序调? 我笑而不...
    開封第一講書人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任醉锅,我火速辦了婚禮,結(jié)果婚禮上发绢,老公的妹妹穿的比我還像新娘硬耍。我一直安慰自己,他們只是感情好边酒,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開白布默垄。 她就那樣靜靜地躺著,像睡著了一般甚纲。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上朦前,一...
    開封第一講書人閱讀 52,584評(píng)論 1 312
  • 那天介杆,我揣著相機(jī)與錄音,去河邊找鬼韭寸。 笑死春哨,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的恩伺。 我是一名探鬼主播赴背,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼晶渠!你這毒婦竟也來(lái)了凰荚?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤褒脯,失蹤者是張志新(化名)和其女友劉穎便瑟,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體番川,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡到涂,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年脊框,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片践啄。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡浇雹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出屿讽,到底是詐尸還是另有隱情昭灵,我是刑警寧澤,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布聂儒,位于F島的核電站虎锚,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏衩婚。R本人自食惡果不足惜窜护,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望非春。 院中可真熱鬧柱徙,春花似錦、人聲如沸奇昙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)储耐。三九已至羊初,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間什湘,已是汗流浹背长赞。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留闽撤,地道東北人得哆。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像哟旗,于是被迫代替她去往敵國(guó)和親贩据。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容