码迷,mamicode.com
首页 > 其他好文 > 详细

红黑树插入操作

时间:2016-01-17 13:28:35      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

通过具体分析每一步的操作来理解插入过程,将每一步操作之后的树结构打印出来可以帮助我们理解,在图中"#"表示黑色,"~"表示红色,空结点(NULL)都为黑色,但是没有被打印出来,看图像的时候注意一下就可以了。

下面将整个过程贴出来,所有的步骤都是通过程序实现的。

 

Inserting 61 -----------------------------------------------------
case 1 <61> (若当前结点是根结点,则将其颜色设置为黑色) : 
~61~ 

result:
#61# 

Inserting 9 -----------------------------------------------------
case 1 <9> (若当前结点是根结点,则将其颜色设置为黑色) : 
  _#61# 
  |     
~9~     

case 2 <9> (若该结点的父结点是黑色,则依然有效并返回) : 
  _#61# 
  |     
~9~     

result:
  _#61# 
  |     
~9~     

Inserting 267 -----------------------------------------------------
case 1 <267> (若当前结点是根结点,则将其颜色设置为黑色) : 
  _#61#_     
  |    |     
~9~    ~267~ 

case 2 <267> (若该结点的父结点是黑色,则依然有效并返回) : 
  _#61#_     
  |    |     
~9~    ~267~ 

result:
  _#61#_     
  |    |     
~9~    ~267~ 

Inserting 107 -----------------------------------------------------
case 1 <107> (若当前结点是根结点,则将其颜色设置为黑色) : 
  _#61#______     
  |         |     
~9~        _~267~ 
           |      
       ~107~      

case 2 <107> (若该结点的父结点是黑色,则依然有效并返回) : 
  _#61#______     
  |         |     
~9~        _~267~ 
           |      
       ~107~      

case 3 <107> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
  _#61#______     
  |         |     
~9~        _~267~ 
           |      
       ~107~      

case 1 <61> (若当前结点是根结点,则将其颜色设置为黑色) : 
  _~61~______     
  |         |     
#9#        _#267# 
           |      
       ~107~      

result:
  _#61#______     
  |         |     
#9#        _#267# 
           |      
       ~107~      

Inserting 301 -----------------------------------------------------
case 1 <301> (若当前结点是根结点,则将其颜色设置为黑色) : 
  _#61#______          
  |         |          
#9#        _#267#_     
           |     |     
       ~107~     ~301~ 

case 2 <301> (若该结点的父结点是黑色,则依然有效并返回) : 
  _#61#______          
  |         |          
#9#        _#267#_     
           |     |     
       ~107~     ~301~ 

result:
  _#61#______          
  |         |          
#9#        _#267#_     
           |     |     
       ~107~     ~301~ 

Inserting 173 -----------------------------------------------------
case 1 <173> (若当前结点是根结点,则将其颜色设置为黑色) : 
  _#61#___________          
  |              |          
#9#        ______#267#_     
           |          |     
       ~107~_         ~301~ 
            |               
            ~173~           

case 2 <173> (若该结点的父结点是黑色,则依然有效并返回) : 
  _#61#___________          
  |              |          
#9#        ______#267#_     
           |          |     
       ~107~_         ~301~ 
            |               
            ~173~           

case 3 <173> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
  _#61#___________          
  |              |          
#9#        ______#267#_     
           |          |     
       ~107~_         ~301~ 
            |               
            ~173~           

case 1 <267> (若当前结点是根结点,则将其颜色设置为黑色) : 
  _#61#___________          
  |              |          
#9#        ______~267~_     
           |          |     
       #107#_         #301# 
            |               
            ~173~           

case 2 <267> (若该结点的父结点是黑色,则依然有效并返回) : 
  _#61#___________          
  |              |          
#9#        ______~267~_     
           |          |     
       #107#_         #301# 
            |               
            ~173~           

result:
  _#61#___________          
  |              |          
#9#        ______~267~_     
           |          |     
       #107#_         #301# 
            |               
            ~173~           

Inserting 129 -----------------------------------------------------
case 1 <129> (若当前结点是根结点,则将其颜色设置为黑色) : 
  _#61#________________          
  |                   |          
#9#        ___________~267~_     
           |               |     
       #107#______         #301# 
                 |               
                _~173~           
                |                
            ~129~                

case 2 <129> (若该结点的父结点是黑色,则依然有效并返回) : 
  _#61#________________          
  |                   |          
#9#        ___________~267~_     
           |               |     
       #107#______         #301# 
                 |               
                _~173~           
                |                
            ~129~                

case 3 <129> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
  _#61#________________          
  |                   |          
#9#        ___________~267~_     
           |               |     
       #107#______         #301# 
                 |               
                _~173~           
                |                
            ~129~                

case 4 <129> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置不同:) : 
  _#61#________________          
  |                   |          
#9#        ___________~267~_     
           |               |     
       #107#______         #301# 
                 |               
                _~173~           
                |                
            ~129~                

case 4 handle1: 若当前结点是左子结点,父结点是右子结点,则右旋父结点,下次处理旋转后右子结点:
右旋: 173
  _#61#________________          
  |                   |          
#9#        ___________~267~_     
           |               |     
       #107#_              #301# 
            |                    
            ~129~_               
                 |               
                 ~173~           

case 5 <173> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置相同: ) : 
  _#61#________________          
  |                   |          
#9#        ___________~267~_     
           |               |     
       #107#_              #301# 
            |                    
            ~129~_               
                 |               
                 ~173~           

case 5 handle1: 将父结点设置为黑色,祖父设为红色:
  _#61#________________          
  |                   |          
#9#        ___________~267~_     
           |               |     
       ~107~_              #301# 
            |                    
            #129#_               
                 |               
                 ~173~           

case 5 handle2: 若当前结点和父结点是右子结点,则左旋祖父结点:
左旋: 107
  _#61#________________          
  |                   |          
#9#             ______~267~_     
                |          |     
           _#129#_         #301# 
           |     |               
       ~107~     ~173~           

result:
  _#61#________________          
  |                   |          
#9#             ______~267~_     
                |          |     
           _#129#_         #301# 
           |     |               
       ~107~     ~173~           

Inserting 232 -----------------------------------------------------
case 1 <232> (若当前结点是根结点,则将其颜色设置为黑色) : 
  _#61#_____________________          
  |                        |          
#9#             ___________~267~_     
                |               |     
           _#129#_              #301# 
           |     |                    
       ~107~     ~173~_               
                      |               
                      ~232~           

case 2 <232> (若该结点的父结点是黑色,则依然有效并返回) : 
  _#61#_____________________          
  |                        |          
#9#             ___________~267~_     
                |               |     
           _#129#_              #301# 
           |     |                    
       ~107~     ~173~_               
                      |               
                      ~232~           

case 3 <232> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
  _#61#_____________________          
  |                        |          
#9#             ___________~267~_     
                |               |     
           _#129#_              #301# 
           |     |                    
       ~107~     ~173~_               
                      |               
                      ~232~           

case 1 <129> (若当前结点是根结点,则将其颜色设置为黑色) : 
  _#61#_____________________          
  |                        |          
#9#             ___________~267~_     
                |               |     
           _~129~_              #301# 
           |     |                    
       #107#     #173#_               
                      |               
                      ~232~           

case 2 <129> (若该结点的父结点是黑色,则依然有效并返回) : 
  _#61#_____________________          
  |                        |          
#9#             ___________~267~_     
                |               |     
           _~129~_              #301# 
           |     |                    
       #107#     #173#_               
                      |               
                      ~232~           

case 3 <129> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
  _#61#_____________________          
  |                        |          
#9#             ___________~267~_     
                |               |     
           _~129~_              #301# 
           |     |                    
       #107#     #173#_               
                      |               
                      ~232~           

case 4 <129> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置不同:) : 
  _#61#_____________________          
  |                        |          
#9#             ___________~267~_     
                |               |     
           _~129~_              #301# 
           |     |                    
       #107#     #173#_               
                      |               
                      ~232~           

case 4 handle1: 若当前结点是左子结点,父结点是右子结点,则右旋父结点,下次处理旋转后右子结点:
右旋: 267
  _#61#______                         
  |         |                         
#9#        _~129~___________          
           |               |          
       #107#         ______~267~_     
                     |          |     
                 #173#_         #301# 
                      |               
                      ~232~           

case 5 <267> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置相同: ) : 
  _#61#______                         
  |         |                         
#9#        _~129~___________          
           |               |          
       #107#         ______~267~_     
                     |          |     
                 #173#_         #301# 
                      |               
                      ~232~           

case 5 handle1: 将父结点设置为黑色,祖父设为红色:
  _~61~______                         
  |         |                         
#9#        _#129#___________          
           |               |          
       #107#         ______~267~_     
                     |          |     
                 #173#_         #301# 
                      |               
                      ~232~           

case 5 handle2: 若当前结点和父结点是右子结点,则左旋祖父结点:
左旋: 61
      ______#129#___________          
      |                    |          
  _~61~_             ______~267~_     
  |    |             |          |     
#9#    #107#     #173#_         #301# 
                      |               
                      ~232~           

result:
      ______#129#___________          
      |                    |          
  _~61~_             ______~267~_     
  |    |             |          |     
#9#    #107#     #173#_         #301# 
                      |               
                      ~232~           

Inserting 211 -----------------------------------------------------
case 1 <211> (若当前结点是根结点,则将其颜色设置为黑色) : 
      ______#129#________________          
      |                         |          
  _~61~_             ___________~267~_     
  |    |             |               |     
#9#    #107#     #173#______         #301# 
                           |               
                          _~232~           
                          |                
                      ~211~                

case 2 <211> (若该结点的父结点是黑色,则依然有效并返回) : 
      ______#129#________________          
      |                         |          
  _~61~_             ___________~267~_     
  |    |             |               |     
#9#    #107#     #173#______         #301# 
                           |               
                          _~232~           
                          |                
                      ~211~                

case 3 <211> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
      ______#129#________________          
      |                         |          
  _~61~_             ___________~267~_     
  |    |             |               |     
#9#    #107#     #173#______         #301# 
                           |               
                          _~232~           
                          |                
                      ~211~                

case 4 <211> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置不同:) : 
      ______#129#________________          
      |                         |          
  _~61~_             ___________~267~_     
  |    |             |               |     
#9#    #107#     #173#______         #301# 
                           |               
                          _~232~           
                          |                
                      ~211~                

case 4 handle1: 若当前结点是左子结点,父结点是右子结点,则右旋父结点,下次处理旋转后右子结点:
右旋: 232
      ______#129#________________          
      |                         |          
  _~61~_             ___________~267~_     
  |    |             |               |     
#9#    #107#     #173#_              #301# 
                      |                    
                      ~211~_               
                           |               
                           ~232~           

case 5 <232> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置相同: ) : 
      ______#129#________________          
      |                         |          
  _~61~_             ___________~267~_     
  |    |             |               |     
#9#    #107#     #173#_              #301# 
                      |                    
                      ~211~_               
                           |               
                           ~232~           

case 5 handle1: 将父结点设置为黑色,祖父设为红色:
      ______#129#________________          
      |                         |          
  _~61~_             ___________~267~_     
  |    |             |               |     
#9#    #107#     ~173~_              #301# 
                      |                    
                      #211#_               
                           |               
                           ~232~           

case 5 handle2: 若当前结点和父结点是右子结点,则左旋祖父结点:
左旋: 173
      ______#129#________________          
      |                         |          
  _~61~_                  ______~267~_     
  |    |                  |          |     
#9#    #107#         _#211#_         #301# 
                     |     |               
                 ~173~     ~232~           

result:
      ______#129#________________          
      |                         |          
  _~61~_                  ______~267~_     
  |    |                  |          |     
#9#    #107#         _#211#_         #301# 
                     |     |               
                 ~173~     ~232~           

Inserting 122 -----------------------------------------------------
case 1 <122> (若当前结点是根结点,则将其颜色设置为黑色) : 
      ___________#129#________________          
      |                              |          
  _~61~_                       ______~267~_     
  |    |                       |          |     
#9#    #107#_             _#211#_         #301# 
            |             |     |               
            ~122~     ~173~     ~232~           

case 2 <122> (若该结点的父结点是黑色,则依然有效并返回) : 
      ___________#129#________________          
      |                              |          
  _~61~_                       ______~267~_     
  |    |                       |          |     
#9#    #107#_             _#211#_         #301# 
            |             |     |               
            ~122~     ~173~     ~232~           

result:
      ___________#129#________________          
      |                              |          
  _~61~_                       ______~267~_     
  |    |                       |          |     
#9#    #107#_             _#211#_         #301# 
            |             |     |               
            ~122~     ~173~     ~232~           

Inserting 28 -----------------------------------------------------
case 1 <28> (若当前结点是根结点,则将其颜色设置为黑色) : 
          ___________#129#________________          
          |                              |          
  _____~61~_                       ______~267~_     
  |        |                       |          |     
#9#_       #107#_             _#211#_         #301# 
   |            |             |     |               
   ~28~         ~122~     ~173~     ~232~           

case 2 <28> (若该结点的父结点是黑色,则依然有效并返回) : 
          ___________#129#________________          
          |                              |          
  _____~61~_                       ______~267~_     
  |        |                       |          |     
#9#_       #107#_             _#211#_         #301# 
   |            |             |     |               
   ~28~         ~122~     ~173~     ~232~           

result:
          ___________#129#________________          
          |                              |          
  _____~61~_                       ______~267~_     
  |        |                       |          |     
#9#_       #107#_             _#211#_         #301# 
   |            |             |     |               
   ~28~         ~122~     ~173~     ~232~           

Inserting 82 -----------------------------------------------------
case 1 <82> (若当前结点是根结点,则将其颜色设置为黑色) : 
          _______________#129#________________          
          |                                  |          
  _____~61~_____                       ______~267~_     
  |            |                       |          |     
#9#_          _#107#_             _#211#_         #301# 
   |          |     |             |     |               
   ~28~    ~82~     ~122~     ~173~     ~232~           

case 2 <82> (若该结点的父结点是黑色,则依然有效并返回) : 
          _______________#129#________________          
          |                                  |          
  _____~61~_____                       ______~267~_     
  |            |                       |          |     
#9#_          _#107#_             _#211#_         #301# 
   |          |     |             |     |               
   ~28~    ~82~     ~122~     ~173~     ~232~           

result:
          _______________#129#________________          
          |                                  |          
  _____~61~_____                       ______~267~_     
  |            |                       |          |     
#9#_          _#107#_             _#211#_         #301# 
   |          |     |             |     |               
   ~28~    ~82~     ~122~     ~173~     ~232~           

Inserting 287 -----------------------------------------------------
case 1 <287> (若当前结点是根结点,则将其颜色设置为黑色) : 
          _______________#129#________________               
          |                                  |               
  _____~61~_____                       ______~267~______     
  |            |                       |               |     
#9#_          _#107#_             _#211#_             _#301# 
   |          |     |             |     |             |      
   ~28~    ~82~     ~122~     ~173~     ~232~     ~287~      

case 2 <287> (若该结点的父结点是黑色,则依然有效并返回) : 
          _______________#129#________________               
          |                                  |               
  _____~61~_____                       ______~267~______     
  |            |                       |               |     
#9#_          _#107#_             _#211#_             _#301# 
   |          |     |             |     |             |      
   ~28~    ~82~     ~122~     ~173~     ~232~     ~287~      

result:
          _______________#129#________________               
          |                                  |               
  _____~61~_____                       ______~267~______     
  |            |                       |               |     
#9#_          _#107#_             _#211#_             _#301# 
   |          |     |             |     |             |      
   ~28~    ~82~     ~122~     ~173~     ~232~     ~287~      

Inserting 54 -----------------------------------------------------
case 1 <54> (若当前结点是根结点,则将其颜色设置为黑色) : 
              _______________#129#________________               
              |                                  |               
  _________~61~_____                       ______~267~______     
  |                |                       |               |     
#9#_              _#107#_             _#211#_             _#301# 
   |              |     |             |     |             |      
   ~28~_       ~82~     ~122~     ~173~     ~232~     ~287~      
       |                                                         
       ~54~                                                      

case 2 <54> (若该结点的父结点是黑色,则依然有效并返回) : 
              _______________#129#________________               
              |                                  |               
  _________~61~_____                       ______~267~______     
  |                |                       |               |     
#9#_              _#107#_             _#211#_             _#301# 
   |              |     |             |     |             |      
   ~28~_       ~82~     ~122~     ~173~     ~232~     ~287~      
       |                                                         
       ~54~                                                      

case 3 <54> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
              _______________#129#________________               
              |                                  |               
  _________~61~_____                       ______~267~______     
  |                |                       |               |     
#9#_              _#107#_             _#211#_             _#301# 
   |              |     |             |     |             |      
   ~28~_       ~82~     ~122~     ~173~     ~232~     ~287~      
       |                                                         
       ~54~                                                      

case 4 <54> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置不同:) : 
              _______________#129#________________               
              |                                  |               
  _________~61~_____                       ______~267~______     
  |                |                       |               |     
#9#_              _#107#_             _#211#_             _#301# 
   |              |     |             |     |             |      
   ~28~_       ~82~     ~122~     ~173~     ~232~     ~287~      
       |                                                         
       ~54~                                                      

case 5 <54> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置相同: ) : 
              _______________#129#________________               
              |                                  |               
  _________~61~_____                       ______~267~______     
  |                |                       |               |     
#9#_              _#107#_             _#211#_             _#301# 
   |              |     |             |     |             |      
   ~28~_       ~82~     ~122~     ~173~     ~232~     ~287~      
       |                                                         
       ~54~                                                      

case 5 handle1: 将父结点设置为黑色,祖父设为红色:
              _______________#129#________________               
              |                                  |               
  _________~61~_____                       ______~267~______     
  |                |                       |               |     
~9~_              _#107#_             _#211#_             _#301# 
   |              |     |             |     |             |      
   #28#_       ~82~     ~122~     ~173~     ~232~     ~287~      
       |                                                         
       ~54~                                                      

case 5 handle2: 若当前结点和父结点是右子结点,则左旋祖父结点:
左旋: 9
              _______________#129#________________               
              |                                  |               
      _____~61~_____                       ______~267~______     
      |            |                       |               |     
  _#28#_          _#107#_             _#211#_             _#301# 
  |    |          |     |             |     |             |      
~9~    ~54~    ~82~     ~122~     ~173~     ~232~     ~287~      

result:
              _______________#129#________________               
              |                                  |               
      _____~61~_____                       ______~267~______     
      |            |                       |               |     
  _#28#_          _#107#_             _#211#_             _#301# 
  |    |          |     |             |     |             |      
~9~    ~54~    ~82~     ~122~     ~173~     ~232~     ~287~      

Inserting 95 -----------------------------------------------------
case 1 <95> (若当前结点是根结点,则将其颜色设置为黑色) : 
              ___________________#129#________________               
              |                                      |               
      _____~61~_________                       ______~267~______     
      |                |                       |               |     
  _#28#_          _____#107#_             _#211#_             _#301# 
  |    |          |         |             |     |             |      
~9~    ~54~    ~82~_        ~122~     ~173~     ~232~     ~287~      
                   |                                                 
                   ~95~                                              

case 2 <95> (若该结点的父结点是黑色,则依然有效并返回) : 
              ___________________#129#________________               
              |                                      |               
      _____~61~_________                       ______~267~______     
      |                |                       |               |     
  _#28#_          _____#107#_             _#211#_             _#301# 
  |    |          |         |             |     |             |      
~9~    ~54~    ~82~_        ~122~     ~173~     ~232~     ~287~      
                   |                                                 
                   ~95~                                              

case 3 <95> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
              ___________________#129#________________               
              |                                      |               
      _____~61~_________                       ______~267~______     
      |                |                       |               |     
  _#28#_          _____#107#_             _#211#_             _#301# 
  |    |          |         |             |     |             |      
~9~    ~54~    ~82~_        ~122~     ~173~     ~232~     ~287~      
                   |                                                 
                   ~95~                                              

case 1 <107> (若当前结点是根结点,则将其颜色设置为黑色) : 
              ___________________#129#________________               
              |                                      |               
      _____~61~_________                       ______~267~______     
      |                |                       |               |     
  _#28#_          _____~107~_             _#211#_             _#301# 
  |    |          |         |             |     |             |      
~9~    ~54~    #82#_        #122#     ~173~     ~232~     ~287~      
                   |                                                 
                   ~95~                                              

case 2 <107> (若该结点的父结点是黑色,则依然有效并返回) : 
              ___________________#129#________________               
              |                                      |               
      _____~61~_________                       ______~267~______     
      |                |                       |               |     
  _#28#_          _____~107~_             _#211#_             _#301# 
  |    |          |         |             |     |             |      
~9~    ~54~    #82#_        #122#     ~173~     ~232~     ~287~      
                   |                                                 
                   ~95~                                              

case 3 <107> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
              ___________________#129#________________               
              |                                      |               
      _____~61~_________                       ______~267~______     
      |                |                       |               |     
  _#28#_          _____~107~_             _#211#_             _#301# 
  |    |          |         |             |     |             |      
~9~    ~54~    #82#_        #122#     ~173~     ~232~     ~287~      
                   |                                                 
                   ~95~                                              

case 1 <129> (若当前结点是根结点,则将其颜色设置为黑色) : 
              ___________________~129~________________               
              |                                      |               
      _____#61#_________                       ______#267#______     
      |                |                       |               |     
  _#28#_          _____~107~_             _#211#_             _#301# 
  |    |          |         |             |     |             |      
~9~    ~54~    #82#_        #122#     ~173~     ~232~     ~287~      
                   |                                                 
                   ~95~                                              

result:
              ___________________#129#________________               
              |                                      |               
      _____#61#_________                       ______#267#______     
      |                |                       |               |     
  _#28#_          _____~107~_             _#211#_             _#301# 
  |    |          |         |             |     |             |      
~9~    ~54~    #82#_        #122#     ~173~     ~232~     ~287~      
                   |                                                 
                   ~95~                                              

Inserting 116 -----------------------------------------------------
case 1 <116> (若当前结点是根结点,则将其颜色设置为黑色) : 
              ________________________#129#________________               
              |                                           |               
      _____#61#_________                            ______#267#______     
      |                |                            |               |     
  _#28#_          _____~107~______             _#211#_             _#301# 
  |    |          |              |             |     |             |      
~9~    ~54~    #82#_            _#122#     ~173~     ~232~     ~287~      
                   |            |                                         
                   ~95~     ~116~                                         

case 2 <116> (若该结点的父结点是黑色,则依然有效并返回) : 
              ________________________#129#________________               
              |                                           |               
      _____#61#_________                            ______#267#______     
      |                |                            |               |     
  _#28#_          _____~107~______             _#211#_             _#301# 
  |    |          |              |             |     |             |      
~9~    ~54~    #82#_            _#122#     ~173~     ~232~     ~287~      
                   |            |                                         
                   ~95~     ~116~                                         

result:
              ________________________#129#________________               
              |                                           |               
      _____#61#_________                            ______#267#______     
      |                |                            |               |     
  _#28#_          _____~107~______             _#211#_             _#301# 
  |    |          |              |             |     |             |      
~9~    ~54~    #82#_            _#122#     ~173~     ~232~     ~287~      
                   |            |                                         
                   ~95~     ~116~                                         

Inserting 281 -----------------------------------------------------
case 1 <281> (若当前结点是根结点,则将其颜色设置为黑色) : 
              ________________________#129#________________                    
              |                                           |                    
      _____#61#_________                            ______#267#___________     
      |                |                            |                    |     
  _#28#_          _____~107~______             _#211#_                  _#301# 
  |    |          |              |             |     |                  |      
~9~    ~54~    #82#_            _#122#     ~173~     ~232~         _~287~      
                   |            |                                  |           
                   ~95~     ~116~                              ~281~           

case 2 <281> (若该结点的父结点是黑色,则依然有效并返回) : 
              ________________________#129#________________                    
              |                                           |                    
      _____#61#_________                            ______#267#___________     
      |                |                            |                    |     
  _#28#_          _____~107~______             _#211#_                  _#301# 
  |    |          |              |             |     |                  |      
~9~    ~54~    #82#_            _#122#     ~173~     ~232~         _~287~      
                   |            |                                  |           
                   ~95~     ~116~                              ~281~           

case 3 <281> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
              ________________________#129#________________                    
              |                                           |                    
      _____#61#_________                            ______#267#___________     
      |                |                            |                    |     
  _#28#_          _____~107~______             _#211#_                  _#301# 
  |    |          |              |             |     |                  |      
~9~    ~54~    #82#_            _#122#     ~173~     ~232~         _~287~      
                   |            |                                  |           
                   ~95~     ~116~                              ~281~           

case 4 <281> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置不同:) : 
              ________________________#129#________________                    
              |                                           |                    
      _____#61#_________                            ______#267#___________     
      |                |                            |                    |     
  _#28#_          _____~107~______             _#211#_                  _#301# 
  |    |          |              |             |     |                  |      
~9~    ~54~    #82#_            _#122#     ~173~     ~232~         _~287~      
                   |            |                                  |           
                   ~95~     ~116~                              ~281~           

case 5 <281> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置相同: ) : 
              ________________________#129#________________                    
              |                                           |                    
      _____#61#_________                            ______#267#___________     
      |                |                            |                    |     
  _#28#_          _____~107~______             _#211#_                  _#301# 
  |    |          |              |             |     |                  |      
~9~    ~54~    #82#_            _#122#     ~173~     ~232~         _~287~      
                   |            |                                  |           
                   ~95~     ~116~                              ~281~           

case 5 handle1: 将父结点设置为黑色,祖父设为红色:
              ________________________#129#________________                    
              |                                           |                    
      _____#61#_________                            ______#267#___________     
      |                |                            |                    |     
  _#28#_          _____~107~______             _#211#_                  _~301~ 
  |    |          |              |             |     |                  |      
~9~    ~54~    #82#_            _#122#     ~173~     ~232~         _#287#      
                   |            |                                  |           
                   ~95~     ~116~                              ~281~           

case 5 handle2: 若当前结点和父结点是左子结点,则右旋祖父结点:
右旋: 301
              ________________________#129#________________                    
              |                                           |                    
      _____#61#_________                            ______#267#______          
      |                |                            |               |          
  _#28#_          _____~107~______             _#211#_             _#287#_     
  |    |          |              |             |     |             |     |     
~9~    ~54~    #82#_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                   |            |                                              
                   ~95~     ~116~                                              

result:
              ________________________#129#________________                    
              |                                           |                    
      _____#61#_________                            ______#267#______          
      |                |                            |               |          
  _#28#_          _____~107~______             _#211#_             _#287#_     
  |    |          |              |             |     |             |     |     
~9~    ~54~    #82#_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                   |            |                                              
                   ~95~     ~116~                                              

Inserting 93 -----------------------------------------------------
case 1 <93> (若当前结点是根结点,则将其颜色设置为黑色) : 
              ____________________________#129#________________                    
              |                                               |                    
      _____#61#_____________                            ______#267#______          
      |                    |                            |               |          
  _#28#_          _________~107~______             _#211#_             _#287#_     
  |    |          |                  |             |     |             |     |     
~9~    ~54~    #82#_____            _#122#     ~173~     ~232~     ~281~     ~301~ 
                       |            |                                              
                      _~95~     ~116~                                              
                      |                                                            
                   ~93~                                                            

case 2 <93> (若该结点的父结点是黑色,则依然有效并返回) : 
              ____________________________#129#________________                    
              |                                               |                    
      _____#61#_____________                            ______#267#______          
      |                    |                            |               |          
  _#28#_          _________~107~______             _#211#_             _#287#_     
  |    |          |                  |             |     |             |     |     
~9~    ~54~    #82#_____            _#122#     ~173~     ~232~     ~281~     ~301~ 
                       |            |                                              
                      _~95~     ~116~                                              
                      |                                                            
                   ~93~                                                            

case 3 <93> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
              ____________________________#129#________________                    
              |                                               |                    
      _____#61#_____________                            ______#267#______          
      |                    |                            |               |          
  _#28#_          _________~107~______             _#211#_             _#287#_     
  |    |          |                  |             |     |             |     |     
~9~    ~54~    #82#_____            _#122#     ~173~     ~232~     ~281~     ~301~ 
                       |            |                                              
                      _~95~     ~116~                                              
                      |                                                            
                   ~93~                                                            

case 4 <93> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置不同:) : 
              ____________________________#129#________________                    
              |                                               |                    
      _____#61#_____________                            ______#267#______          
      |                    |                            |               |          
  _#28#_          _________~107~______             _#211#_             _#287#_     
  |    |          |                  |             |     |             |     |     
~9~    ~54~    #82#_____            _#122#     ~173~     ~232~     ~281~     ~301~ 
                       |            |                                              
                      _~95~     ~116~                                              
                      |                                                            
                   ~93~                                                            

case 4 handle1: 若当前结点是左子结点,父结点是右子结点,则右旋父结点,下次处理旋转后右子结点:
右旋: 95
              ____________________________#129#________________                    
              |                                               |                    
      _____#61#_____________                            ______#267#______          
      |                    |                            |               |          
  _#28#_          _________~107~______             _#211#_             _#287#_     
  |    |          |                  |             |     |             |     |     
~9~    ~54~    #82#_                _#122#     ~173~     ~232~     ~281~     ~301~ 
                   |                |                                              
                   ~93~_        ~116~                                              
                       |                                                           
                       ~95~                                                        

case 5 <95> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置相同: ) : 
              ____________________________#129#________________                    
              |                                               |                    
      _____#61#_____________                            ______#267#______          
      |                    |                            |               |          
  _#28#_          _________~107~______             _#211#_             _#287#_     
  |    |          |                  |             |     |             |     |     
~9~    ~54~    #82#_                _#122#     ~173~     ~232~     ~281~     ~301~ 
                   |                |                                              
                   ~93~_        ~116~                                              
                       |                                                           
                       ~95~                                                        

case 5 handle1: 将父结点设置为黑色,祖父设为红色:
              ____________________________#129#________________                    
              |                                               |                    
      _____#61#_____________                            ______#267#______          
      |                    |                            |               |          
  _#28#_          _________~107~______             _#211#_             _#287#_     
  |    |          |                  |             |     |             |     |     
~9~    ~54~    ~82~_                _#122#     ~173~     ~232~     ~281~     ~301~ 
                   |                |                                              
                   #93#_        ~116~                                              
                       |                                                           
                       ~95~                                                        

case 5 handle2: 若当前结点和父结点是右子结点,则左旋祖父结点:
左旋: 82
              ____________________________#129#________________                    
              |                                               |                    
      _____#61#_____________                            ______#267#______          
      |                    |                            |               |          
  _#28#_              _____~107~______             _#211#_             _#287#_     
  |    |              |              |             |     |             |     |     
~9~    ~54~       _#93#_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                  |    |            |                                              
               ~82~    ~95~     ~116~                                              

result:
              ____________________________#129#________________                    
              |                                               |                    
      _____#61#_____________                            ______#267#______          
      |                    |                            |               |          
  _#28#_              _____~107~______             _#211#_             _#287#_     
  |    |              |              |             |     |             |     |     
~9~    ~54~       _#93#_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                  |    |            |                                              
               ~82~    ~95~     ~116~                                              

Inserting 122 -----------------------------------------------------
result:
              ____________________________#129#________________                    
              |                                               |                    
      _____#61#_____________                            ______#267#______          
      |                    |                            |               |          
  _#28#_              _____~107~______             _#211#_             _#287#_     
  |    |              |              |             |     |             |     |     
~9~    ~54~       _#93#_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                  |    |            |                                              
               ~82~    ~95~     ~116~                                              

Inserting 90 -----------------------------------------------------
case 1 <90> (若当前结点是根结点,则将其颜色设置为黑色) : 
              ________________________________#129#________________                    
              |                                                   |                    
      _____#61#_________________                            ______#267#______          
      |                        |                            |               |          
  _#28#_                  _____~107~______             _#211#_             _#287#_     
  |    |                  |              |             |     |             |     |     
~9~    ~54~       _____#93#_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                  |        |            |                                              
               ~82~_       ~95~     ~116~                                              
                   |                                                                   
                   ~90~                                                                

case 2 <90> (若该结点的父结点是黑色,则依然有效并返回) : 
              ________________________________#129#________________                    
              |                                                   |                    
      _____#61#_________________                            ______#267#______          
      |                        |                            |               |          
  _#28#_                  _____~107~______             _#211#_             _#287#_     
  |    |                  |              |             |     |             |     |     
~9~    ~54~       _____#93#_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                  |        |            |                                              
               ~82~_       ~95~     ~116~                                              
                   |                                                                   
                   ~90~                                                                

case 3 <90> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
              ________________________________#129#________________                    
              |                                                   |                    
      _____#61#_________________                            ______#267#______          
      |                        |                            |               |          
  _#28#_                  _____~107~______             _#211#_             _#287#_     
  |    |                  |              |             |     |             |     |     
~9~    ~54~       _____#93#_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                  |        |            |                                              
               ~82~_       ~95~     ~116~                                              
                   |                                                                   
                   ~90~                                                                

case 1 <93> (若当前结点是根结点,则将其颜色设置为黑色) : 
              ________________________________#129#________________                    
              |                                                   |                    
      _____#61#_________________                            ______#267#______          
      |                        |                            |               |          
  _#28#_                  _____~107~______             _#211#_             _#287#_     
  |    |                  |              |             |     |             |     |     
~9~    ~54~       _____~93~_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                  |        |            |                                              
               #82#_       #95#     ~116~                                              
                   |                                                                   
                   ~90~                                                                

case 2 <93> (若该结点的父结点是黑色,则依然有效并返回) : 
              ________________________________#129#________________                    
              |                                                   |                    
      _____#61#_________________                            ______#267#______          
      |                        |                            |               |          
  _#28#_                  _____~107~______             _#211#_             _#287#_     
  |    |                  |              |             |     |             |     |     
~9~    ~54~       _____~93~_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                  |        |            |                                              
               #82#_       #95#     ~116~                                              
                   |                                                                   
                   ~90~                                                                

case 3 <93> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
              ________________________________#129#________________                    
              |                                                   |                    
      _____#61#_________________                            ______#267#______          
      |                        |                            |               |          
  _#28#_                  _____~107~______             _#211#_             _#287#_     
  |    |                  |              |             |     |             |     |     
~9~    ~54~       _____~93~_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                  |        |            |                                              
               #82#_       #95#     ~116~                                              
                   |                                                                   
                   ~90~                                                                

case 4 <93> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置不同:) : 
              ________________________________#129#________________                    
              |                                                   |                    
      _____#61#_________________                            ______#267#______          
      |                        |                            |               |          
  _#28#_                  _____~107~______             _#211#_             _#287#_     
  |    |                  |              |             |     |             |     |     
~9~    ~54~       _____~93~_            _#122#     ~173~     ~232~     ~281~     ~301~ 
                  |        |            |                                              
               #82#_       #95#     ~116~                                              
                   |                                                                   
                   ~90~                                                                

case 4 handle1: 若当前结点是左子结点,父结点是右子结点,则右旋父结点,下次处理旋转后右子结点:
右旋: 107
              ________________________________#129#________________                    
              |                                                   |                    
      _____#61#_________                                    ______#267#______          
      |                |                                    |               |          
  _#28#_          _____~93~_____                       _#211#_             _#287#_     
  |    |          |            |                       |     |             |     |     
~9~    ~54~    #82#_          _~107~______         ~173~     ~232~     ~281~     ~301~ 
                   |          |          |                                             
                   ~90~    #95#         _#122#                                         
                                        |                                              
                                    ~116~                                              

case 5 <107> (若父亲是红色,叔父是黑色,当前结点和父结点的左右位置相同: ) : 
              ________________________________#129#________________                    
              |                                                   |                    
      _____#61#_________                                    ______#267#______          
      |                |                                    |               |          
  _#28#_          _____~93~_____                       _#211#_             _#287#_     
  |    |          |            |                       |     |             |     |     
~9~    ~54~    #82#_          _~107~______         ~173~     ~232~     ~281~     ~301~ 
                   |          |          |                                             
                   ~90~    #95#         _#122#                                         
                                        |                                              
                                    ~116~                                              

case 5 handle1: 将父结点设置为黑色,祖父设为红色:
              ________________________________#129#________________                    
              |                                                   |                    
      _____~61~_________                                    ______#267#______          
      |                |                                    |               |          
  _#28#_          _____#93#_____                       _#211#_             _#287#_     
  |    |          |            |                       |     |             |     |     
~9~    ~54~    #82#_          _~107~______         ~173~     ~232~     ~281~     ~301~ 
                   |          |          |                                             
                   ~90~    #95#         _#122#                                         
                                        |                                              
                                    ~116~                                              

case 5 handle2: 若当前结点和父结点是右子结点,则左旋祖父结点:
左旋: 61
                          ____________________#129#________________                    
                          |                                       |                    
              _________#93#_____                            ______#267#______          
              |                |                            |               |          
      _____~61~_              _~107~______             _#211#_             _#287#_     
      |        |              |          |             |     |             |     |     
  _#28#_       #82#_       #95#         _#122#     ~173~     ~232~     ~281~     ~301~ 
  |    |           |                    |                                              
~9~    ~54~        ~90~             ~116~                                              

result:
                          ____________________#129#________________                    
                          |                                       |                    
              _________#93#_____                            ______#267#______          
              |                |                            |               |          
      _____~61~_              _~107~______             _#211#_             _#287#_     
      |        |              |          |             |     |             |     |     
  _#28#_       #82#_       #95#         _#122#     ~173~     ~232~     ~281~     ~301~ 
  |    |           |                    |                                              
~9~    ~54~        ~90~             ~116~                                              

Inserting 274 -----------------------------------------------------
case 1 <274> (若当前结点是根结点,则将其颜色设置为黑色) : 
                          ____________________#129#________________                         
                          |                                       |                         
              _________#93#_____                            ______#267#___________          
              |                |                            |                    |          
      _____~61~_              _~107~______             _#211#_                  _#287#_     
      |        |              |          |             |     |                  |     |     
  _#28#_       #82#_       #95#         _#122#     ~173~     ~232~         _~281~     ~301~ 
  |    |           |                    |                                  |                
~9~    ~54~        ~90~             ~116~                              ~274~                

case 2 <274> (若该结点的父结点是黑色,则依然有效并返回) : 
                          ____________________#129#________________                         
                          |                                       |                         
              _________#93#_____                            ______#267#___________          
              |                |                            |                    |          
      _____~61~_              _~107~______             _#211#_                  _#287#_     
      |        |              |          |             |     |                  |     |     
  _#28#_       #82#_       #95#         _#122#     ~173~     ~232~         _~281~     ~301~ 
  |    |           |                    |                                  |                
~9~    ~54~        ~90~             ~116~                              ~274~                

case 3 <274> (若结点的叔父结点(和父亲)是红色,则将叔父和父亲都变成黑色,将祖父变成红色并转到case 1) : 
                          ____________________#129#________________                         
                          |                                       |                         
              _________#93#_____                            ______#267#___________          
              |                |                            |                    |          
      _____~61~_              _~107~______             _#211#_                  _#287#_     
      |        |              |          |             |     |                  |     |     
  _#28#_       #82#_       #95#         _#122#     ~173~     ~232~         _~281~     ~301~ 
  |    |           |                    |                                  |                
~9~    ~54~        ~90~             ~116~                              ~274~                

case 1 <287> (若当前结点是根结点,则将其颜色设置为黑色) : 
                          ____________________#129#________________                         
                          |                                       |                         
              _________#93#_____                            ______#267#___________          
              |                |                            |                    |          
      _____~61~_              _~107~______             _#211#_                  _~287~_     
      |        |              |          |             |     |                  |     |     
  _#28#_       #82#_       #95#         _#122#     ~173~     ~232~         _#281#     #301# 
  |    |           |                    |                                  |                
~9~    ~54~        ~90~             ~116~                              ~274~                

case 2 <287> (若该结点的父结点是黑色,则依然有效并返回) : 
                          ____________________#129#________________                         
                          |                                       |                         
              _________#93#_____                            ______#267#___________          
              |                |                            |                    |          
      _____~61~_              _~107~______             _#211#_                  _~287~_     
      |        |              |          |             |     |                  |     |     
  _#28#_       #82#_       #95#         _#122#     ~173~     ~232~         _#281#     #301# 
  |    |           |                    |                                  |                
~9~    ~54~        ~90~             ~116~                              ~274~                

result:
                          ____________________#129#________________                         
                          |                                       |                         
              _________#93#_____                            ______#267#___________          
              |                |                            |                    |          
      _____~61~_              _~107~______             _#211#_                  _~287~_     
      |        |              |          |             |     |                  |     |     
  _#28#_       #82#_       #95#         _#122#     ~173~     ~232~         _#281#     #301# 
  |    |           |                    |                                  |                
~9~    ~54~        ~90~             ~116~                              ~274~                

 

红黑树插入操作

标签:

原文地址:http://www.cnblogs.com/lengyue365/p/5137044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!