algorithmtutorprograms icon indicating copy to clipboard operation
algorithmtutorprograms copied to clipboard

RB Tree is leaning right

Open SirJohnFalstaff opened this issue 4 years ago • 0 comments

That's not a bug, I am just curious why it is the case.

int main() {
  RBTree bst;
  for (int i = 0; i < 31; i++) {
      bst.insert(i);
  }
  bst.prettyPrint();

  return 0;
}

produces a tree without any violations but at the other time it isn't really balanced.

R----7(BLACK)                                  
     L----3(BLACK)                             
     |    L----1(BLACK)                        
     |    |    L----0(BLACK)                   
     |    |    R----2(BLACK)                   
     |    R----5(BLACK)                        
     |         L----4(BLACK)                   
     |         R----6(BLACK)                   
     R----15(RED)                              
          L----11(BLACK)                       
          |    L----9(BLACK)                   
          |    |    L----8(BLACK)              
          |    |    R----10(BLACK)             
          |    R----13(BLACK)                  
          |         L----12(BLACK)             
          |         R----14(BLACK)             
          R----19(BLACK)                       
               L----17(BLACK)                  
               |    L----16(BLACK)             
               |    R----18(BLACK)             
               R----23(RED)                    
                    L----21(BLACK)             
                    |    L----20(BLACK)        
                    |    R----22(BLACK)        
                    R----25(BLACK)             
                         L----24(BLACK)        
                         R----27(RED)          
                              L----26(BLACK)   
                              R----29(BLACK)   
                                   L----28(RED)
                                   R----30(RED)

I stumbled upon playing with my own implementation and just looked around for something else to compare with.

SirJohnFalstaff avatar Jan 04 '22 23:01 SirJohnFalstaff