RedBlack Tree

insert grammar

1)every node’s color is red or black
2)root 's color must be black
3)every leaf 's color is black
4)assume one node’s color is red,that it’s children’s color should be black
5)from one node to the leaf node, every route contains black node amount must be equivalent。

illustration:

[] is double black color
{} is red black color
<> is red color,
insert node’s default color is red
example , insert 1,2,3,4,5,6,7,8

insert 1 , root is null. return 1
1
insert 2 , 2 > 1. 1’s right child is null
 1
  \
  <2>
insert 3 , 3 > 2 > 1. 2’s right child is null
  1
    \
    <2>
      \
      <3>
<2>'s color is red, <3>'s color is same as <2> that it is contrary of condition 4
if change <3>'color to black that it will be contrary of condition 5
so, we need leftrotate 1 and change 1 and 2 's color
                  <2>
                 /   \
                1    <3>
                    2
                 /     \
                <1>    <3>
insert 4 , 4 > 3 > 2 > 1. 3’s right child is null
     2
  /     \
 <1>    <3>
          \
          <4>
current situation can't use rotate,because rotate with 2 can't solve the condition 4
and parent and parent sibling's color is red, we should change them color to black
                    2
                 /     \
                <1>    <3>
                         \
                         <4>
                    2
                 /     \
                1       3
                         \
                         <4>
insert 5 , 5>4 > 3 > 2 > 1. 4’s right child is null
                    2
                 /     \
                1       3
                         \
                         <4>
                           \
                           <5>
 <5> parent <4> has no sibling 
 left rotate grandparent 3
                    2
                 /     \
                1       4
                       /  \
                     <3>  <5>
insert 6 , 6>5>4 > 3 > 2 > 1. 5’s right child is null
                    2
                 /     \
                1       4
                       /  \
                     <3>  <5>  
                            \
                            <6>
        <6> parent <5>'s color is red and <5> has a red sibling <3>, 
        set grandparent'color is red, parent and parent's sibling color black
                    2
                 /     \
                1       <4>
                       /   \
                      3     5 
                             \
                             <6>
insert 7 , 7>6>5>4 > 3 > 2 > 1. 6’s right child is null
                2
             /     \
            1       <4>
                   /   \
                  3     5 
                         \
                         <6>
                           \
                           <7>
    <7> parent <6> has no sibling 
    left rotate grandparent 5
                2
             /     \
            1       <4>
                   /   \
                  3     6 
                      /   \
                     <5>   <7>
insert 8 , 8>7>6>5>4 > 3 > 2 > 1. 7’s right child is null
                2
             /     \
            1       <4>
                   /   \
                  3     6 
                      /   \
                     <5>   <7> 
                            \
                            <8>  
    <8> parent <7>'s color is red and <7> has a red sibling <5>, 
    set grandparent'color is red, parent and parent's sibling color black 
                2
             /     \
            1       <4>
                   /   \
                  3     <6> 
                       /   \
                      5     7
                             \
                             <8>      
    but <4> and <6> always contract of condition 4, <4> has one black sibling, we can left rotate <6> grandparent 2
                4
             /      \
            <2>       <6>
           /  \    /   \
          1    3  5     7
                         \
                         <8> 

delete grammar

     Assume the delete node is X ,这时,有三种情况:

     1)Node X has no child,that delete the node ;check X's color ,if it is black ,that cause redblack grammatic broken.于是需要对红黑树进行调整,见第二部分。

     2)Node X has only one child C,that use C replace X.然后判断删除的X节点是否为黑,若为黑,则需对红黑树进行调整。

     3)Node X has two children L and R,that we need find the X 's successor S, 
     two condition will be happen:
     fist is S is R,that use S replace X 
     second is S is not R,that use S replace X and use S's right child replace S position 

     notice, X's successor is minimnum of the X's right nodes normally

     now we start illustrate: []is double black color {} is red black color <> is red color
                      4            
		         /         \         
		      <2>           <6>      
		     /   \         /   \    
		    1     3       5     7  
		                         \ 
		                         <8>
     1-1) delete 8 , 8 has no child and 8 is red. delete 8 will not break grammatical of  the redblack tree
                      4            
		         /         \         
		      <2>           <6>      
		     /   \         /   \    
		    1     3       5     7  
     1-2) delete 5 , 5 has no child and 5 is black.delete it will break grammatical of the redblack tree.
          so we give black color to 5,that 5'color will be double black;
          if 5's sib 7's color is black, and 7 right node's color is red;
          parent is 6 and right child node is 8
          set 7's color to 6's color
          set 6's and 8's color to black
          left rotate with 6 
          at least delete 5
                      4            
		         /         \         
		      <2>           <7>     
		     /   \         /   \    
		    1     3       6     8 
     2-1) delete 7,7 has one child and 7 is black. 8 is 7's successor and 8 is red; 
          let 7's node value is 8;
          give one black color to 8;
          8's color will be red and black;
          set value of {8} is null, red black color ,remove the red color . 
               4                  4            
			 /    \            /       \         
		   <2>   <6>   ->    <2>       <6>      
		   /  \  /  \       /   \     /   \    
		  1   3 5    8     1     3   5     8  
			          \                           
			          {8}                      
     3-1) delete 6, 6 is red node; 
                      4            
		         /         \         
		      <2>           <6>      
		     /   \         /   \    
		    1     3       5     7  
		                         \ 
		                         <8>
		   7 is 6's successor.  set 6's value to 7
		             4            
		         /         \         
		      <2>           <7>      
		     /   \         /   \    
		    1     3       5     7  
		                         \ 
		                         <8>
		   delete 7 , circle use 1 and 2 grammatical check 7
		   7 has one child <8> , set 7 value to 8, give one black to <8>
		             4            
		         /         \         
		      <2>           <7>      
		     /   \         /   \    
		    1     3       5     8  
		                         \ 
		                         {8}
		   now <8> change to {8},color is red black; set {8} value to null, remove the red color
		             4            
		         /         \         
		      <2>           <7>      
		     /   \         /   \    
		    1     3       5     8  
     3-2) delete 2, 2 is red node; 
                      4            
		         /         \         
		      <2>           <6>      
		     /   \         /   \    
		    1     3       5     7  
		                         \ 
		                         <8>
		   3 is 2's successor.  set 2's value to 3
		             4            
		         /         \         
		      <3>           <7>      
		     /   \         /   \    
		    1     3       5     7  
		                         \ 
		                         <8>
		   delete 3 , circle use 1 and 2 grammatical check 3
		   3 has no child  ,  give one black to 3
		             4            
		         /         \         
		      <3>           <6>      
		     /   \         /   \    
		    1    [3]      5     7  
		                         \ 
		                         <8>
		   3's sibbing 1 has two black node,that 1 and 3 use one black color to give to it's parent 2
		   set [3] value to null
		             4            
		         /         \         
		      {3}           <7>      
		      /   \         /   \    
		    <1>  [null]      5     8  
		   {3} is red black node ,so remove one red color
		            4            
		         /     \         
		        3      <7>      
		      /       /   \    
		    <1>      5     8  
     3-3) delete 4, 4 is root node; 
                      4            
		         /         \         
		      <2>           <6>      
		     /   \         /   \    
		    1     3       5     7  
		                         \ 
		                         <8>
		   5 is 4's successor.  set 4's value to 5
		            (5)            
		         /       \         
		      <2>        <6>      
		     /   \      /   \    
		    1     3    5     7  
		                      \ 
		                      <8>
		   delete 5 , circle use 1 and 2 grammatical check 3
		   5 has no child  ,  give one black to 5
		             (5)            
		         /         \         
		      <2>           <6>      
		     /   \         /   \    
		    1    3       [5]    7  
		                         \ 
		                         <8>
		   [5]’s sibbing node is 7 and 7's right node<8> is red;
		   set 7's color to <6>'s color and set <6> and <8> color to black
		             (5)            
		         /         \         
		      <2>            6      
		     /   \         /   \    
		    1    3       [5]    <7>  
		                          \ 
		                           8
		    left rotate with 6, set [5]'s value to null and delete it 
		             (5)             
		         /         \         
		      <2>           <7>     
		     /   \         /   \    
		    1    3        6      8 
		                 /          
		                [null] 
		             5            
		         /       \         
		      <2>        <7>     
		     /   \      /   \    
		    1     3    6     8 

rotate grammar

在上部分的第一二种情况中,若删除的节点X为黑色的,这时,为了保持黑高不被破坏,我们可以将替代X的节点S再加一种再额外增加一种黑色。此时节点S可能是双黑或者红黑,若为红黑则只需删除其中的红色保留黑色即可,若为双黑色则需要做进一步的调整,最终要删掉双黑色节点

在上部分第三种情况中,若移动的节点S是黑色的,则将代替节点S的节点SR额外增加一种黑色,使得树的黑高相等。同样,此时的SR节点可能为双重黑色或红黑色,若是红黑色则只需保留黑色即可,若为双黑色则需要对树进行调整。

当节点X具有双重黑色的特性时需要对其进行调整。这时有两种情况,分别为:X是左孩子节点和X是右孩子节点。由于这两种情况是对称的,因而在此只对X是左孩子节点的情况进行讨论。
当X为左孩子节点时,这时又有四种情况。

1)x的兄弟节点w是红色的。这种情况不好处理,因为x的兄弟节点w为红色,不能将黑色直接上移。所以对其做一些变换,首先将w变成黑色,x的父节点变成红色,然后以x.p为中心进行左旋。旋转后的x的兄弟节点为原来w的左孩子,此节点必为黑色,因为w为红色。通过这一步,我们就能确保x的兄弟节点为黑色,为下一步调整做好准备。

2)x的兄弟节点w是黑色的,并且w的两个子节点也是黑色的。此种情况比较好办,我们可以通过将x和w中的一个黑色上移到x的父节点,使得x的父节点变成新的x节点。在将x上的一个黑色上移后还剩下了一个黑色,将w的黑色上移后w只剩下了红色。若新的x为红黑色,则将新的x变成黑色,调整结束;若新的x为双黑色时,并且此时的x不为根节点,则继续进行调整,否则结束。

3)x的兄弟节点w是黑色的,并且w的左孩子为红色,右孩子为黑色。这种情况不能提取x和w的黑色上移,因为w的孩子有红色节点。此种情况w的左孩子为红色,右孩子为黑色。我们首先以w为中心进行右旋,并且将w的左孩子的颜色修改为黑色,将w修改为红色。这时将此种情况转化成了第四种情况,即x的兄弟节点w的右孩子为红色节点。

4)x的兄弟节点w是黑色的,并且w的右孩子为红色,左孩子颜色任意。这时,首先将w的颜色改为其父节点的颜色,x的父节点和w的右孩子修改为黑色。然后以x的父节点为中心进行左旋,最后将x指向其根节点。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值