DataStructure——红黑树学习笔记

Posted Avery-Lyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataStructure——红黑树学习笔记相关的知识,希望对你有一定的参考价值。

1.前言

  • 本文伪码和解释参考:

http://blog.csdn.net/v_JULY_v/article/details/6105630

  • C实现的源码本文未贴出,请见:

http://blog.csdn.net/v_july_v/article/details/6114226

  • July的博客对红黑树的分析很精彩,请见:

http://blog.csdn.net/v_JULY_v/article/category/774945

 

本文对参考资料有所修订和补完(侵删)。

2.正文

伪码中使用的符号:

 1 N:node,当前节点
 2 S:sibling,当前节点的兄弟节点
 3 P:praent,当前节点的父节点
 4 U:uncle,当前节点的叔节点,即父节点的兄弟节点
 5 G:graparent,当前节点的祖辈节点,即父节点的父节点
 6 ←:赋值
 7 ==或=:是否相等
 8 ≠:不等
 9 x.p:x的父节点
10 N:L或N:R:节点N是父节点的左孩子(大写L,Left),或,节点N是父节点的右孩子(大写R,Right)
11 N:r或N:b:节点N是红色的(小写r,red),或,节点N是黑色的(小写b,black)
12 <N:r>:条件判断,判断N:r是否成立
13 #n:相当于#Case n,即情况n(n=1,2,3.....)
14 L_rt(N)或R_rt(N):以节点N为轴心,进行左旋转操作(L_rt),或,右旋操作(R_rt)

伪码:

  1 //-------------RBTree:Insesrt-----------//
  2 RB-INSERT(T, z)
  3 y ← nil
  4 x ← T.root
  5 while x ≠ nil
  6     do y ← x
  7     if z.key < x.key
  8         then x ← x.left
  9     else x ← x.right
 10 z.p ← y
 11 if y == nil
 12     then T.root ← z
 13 else if z.key < y.key
 14     then y.left ← z
 15 else y.right ← z
 16 z.left ← nil
 17 z.right ← nil
 18 z.color ← RED
 19 RB-INSERT-FIXUP(T, z)
 20 
 21 //-------------RBTree:Insert-FIXUP-----------//
 22 RB-INSERT-FIXUP(T, z)
 23 while z.p.color == RED                            //fixup only in case P:r
 24     do if z.p == z.p.p.left                     //P:L
 25         then y ← z.p.p.right                     
 26         if y.color == RED                        //U:r , # Case  
 27             then z.p.color ← BLACK              //P ← b,U ← b,G ← r # 
 28             y.color ← BLACK                     
 29             z.p.p.color ← RED                   
 30             z ← z.p.p                           //N ← G
 31         else                                     //U:b , # Case  -> # Case 
 32             if z == z.p.right                     //N:R , 
 33                 then z ← z.p                    //    N ← P,L_rt(T,N) 
 34                 LEFT-ROTATE(T, z)               
 35             z.p.color ← BLACK                    //<U:b> , P ← b,G ← r,R_rt(T,G) # Case 
 36             z.p.p.color ← RED                   
 37             RIGHT-ROTATE(T, z.p.p)              
 38     else if z.p == z.p.p.right                     //<P:R>
 39         y ← z.p.p.left                              
 40         if y.color == RED                        //<U:r> , # Case  
 41             then z.p.color ← BLACK              //P ← b,U ← b,G ← r # 
 42             y.color ← BLACK                     
 43             z.p.p.color ← RED                   
 44             z ← z.p.p                           //N ← G
 45         else                                     //<U:b> , # Case  -> # Case 
 46             if z == z.p.left                     //<U:b,N:L> , N ← P,R_rt(T,N) 
 47                 then z ← z.p                    
 48                 RIGHT-ROTATE(T, z)               
 49             z.p.color ← BLACK                    //<U:b> , P ← b,G ← r,L_rt(T,G) # Case 
 50             z.p.p.color ← RED                   
 51             LEFT-ROTATE(T, z.p.p)    
 52 T.root.color ← BLACK
 53 
 54 //-------------RBTree:Delete-----------//
 55 RB-DELETE(T, z)                                                    //y was deleted , x
 56 if z.left = NIL or z.right = NIL                                  //N doesnot have child node
 57    then y ← z                                                      
 58    else y ← TREE-SUCCESSOR(z)                                      //TREE-SUCCESSOR(z):find the Lmax in subtree:z
 59 if y.left ≠ NIL  
 60    then x ← y.left  
 61 else x ← y.right
 62 if x ≠ NIL
 63     x.p ← y.p
 64 if y.p = NIL                                                    //in case N is root
 65    then T.root ← x  
 66 else if y = y.p.left                                            //in case single child
 67     then y.p.left ← x  
 68 else y.p.rght ← x  
 69 if y ≠ z                                                          //in case double child
 70    then z.key ← y.key                                            //cover but not remove
 71         copy ys satellite data into z  
 72 if y.color = BLACK                                              //fixup only when <N:b>=true
 73    then RB-DELETE-FIXUP(T, x)  
 74 return y
 75 
 76 //-------------RBTree:Delete-Fixup-----------//
 77 RB-DELETE-FIXUP(T, x)
 78 while x ≠ T.root and x.color = BLACK  
 79     do 
 80         if x = x.p.left                                          //<N:L>
 81             then w ← x.p.right                                  //w ← S:R
 82             if w.color = RED                                      //<w:r>    #1 
 83                 then w.color ← BLACK                               //S:b , P:r , L_rt(P) ,w ← S:R
 84                     x.p.color ← RED                               
 85                     LEFT-ROTATE(T, x.p)                            
 86                     w ← x.p.right                                
 87             if w.left.color = BLACK and w.right.color = BLACK   //<S.left:b & S.right:b> #2
 88                 then w.color ← RED                              //S:r , N ← P
 89                     x ← x.p                                        
 90             else if w.right.color = BLACK                         //<S.right:b> #3
 91                 then w.left.color ← BLACK                          //S.left:b , S:r , R_rt(S) , 
 92                     w.color ← RED                                  
 93                     RIGHT-ROTATE(T, w)                          
 94                     w ← x.p.right                                 //new N‘s S cos of the R_rt()
 95                 w.color ← x.p.color                             //S.color ← P.color,P:b,S.right:b,L_rt(P), #4
 96                 x.p.color ← BLACK                                    
 97                 w.right.color ← BLACK                            
 98                 LEFT-ROTATE(T, x.p)                               
 99                 x ← T.root                                        //break,exit while{}
100         else                                         
101             w ← x.p.left                                  
102             if w.color = RED                                      
103                 then w.color ← BLACK                               
104                     x.p.color ← RED                               
105                     RIGHT-ROTATE(T, x.p)                            
106                     w ← x.p.left                                
107             if w.left.color = BLACK and w.right.color = BLACK   
108                 then w.color ← RED                              
109                     x ← x.p                                        
110             else if w.left.color = BLACK                         
111                 then w.right.color ← BLACK                          
112                     w.color ← RED                                  
113                     LEFT-ROTATE(T, w)                          
114                     w ← x.p.left                                 
115                 w.color ← x.p.color                                 
116                 x.p.color ← BLACK                                    
117                 w.left.color ← BLACK                            
118                 RIGHT-ROTATE(T, x.p)                               
119                 x ← T.root                                            
120 x.color ← BLACK  

 

以上是关于DataStructure——红黑树学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

红黑树探索笔记

数据结构 - 学习笔记 - 红黑树前传——234树

数据结构 - 学习笔记 - 红黑树前传——234树

算法学习笔记:红黑树

学习笔记:红黑树

学习笔记:红黑树