iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 25
0
自我挑戰組

資料結構大便當系列 第 25

[Day 25] Red–Black Tree III

  • 分享至 

  • xImage
  •  

紅黑樹(Red–Black Tree)

Deletion

Case 1: x 的兄弟 w 為紅色

https://ithelp.ithome.com.tw/upload/images/20191007/20120250WXt8R7FbNK.png

Case 2: x 的兄弟 w 為黑色,且 w 的兩個子節點皆為黑

https://ithelp.ithome.com.tw/upload/images/20191007/20120250dn6GmsqKpX.png

Case 3: x 的兄弟 w 為黑色,w 的左節點為紅,右節點為黑

https://ithelp.ithome.com.tw/upload/images/20191007/20120250dZddgkaqXa.png

Case 4: x 的兄弟 w 為黑色,且 w 的兩個子節點皆為紅

https://ithelp.ithome.com.tw/upload/images/20191007/20120250MiGiQVNUnp.png

RB-DELETE-FIXUP(T, x)
    while x != T.root and x.color == BLACK
        if x == x.p.left
            w = w.p.right
            
            // Case 1
            if w.color == RED
                w.color = BLACK
                x.p.color = RED
                LEFT-ROTATE(T.x, p)
                w = x.p.right
            
            // Case 2
            if w.left.color == BLACK and w.right.color == BLACK
                w.color = RED
                x = x.p
            
            // Case 3
            else if w.right.color == BLACK
                w.left.color = BLACK
                w.color = RED
                RIGHT-ROTATE(T, w)
                w = x.p.right
            
            //Case 4
            w.color = x.p.color
            x.p.color = BLACK
            w.right.color = BLACK
            LEFT-ROTATE(T, x.p)
            x = T.root
        else (same as then clause with "right" and "left" exchanged)
    x.color = BLACK

上一篇
[Day 24] Red–Black Tree II
下一篇
[Day 26] Red–Black Tree IIII
系列文
資料結構大便當30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言