Example #1
0
def should_delete_double_black():
    '''
   delete 3
    2         2
   / \  =>   /
  1   3    (1)

                   8
                   |
               ---------
            /             \
          4                12
         |                  |
       ----                ----          
     /      \            /      \        
    2        6          10       14       
  /   \    /  \       /   \     /  \      
 1     3  5    7     9     11  13   15     
 
    
    '''
    tree1 = N(2, N(1), N(3))
    tree1 = delete(3, tree1)

    assert is_valid(tree1)
    assert [1, 2] == inorder(tree1)
    tree2 = N(8, N(4, N(2, N(1), N(3)), N(6, N(5), N(7))),
              N(12, N(10, N(9), N(11)), N(14, N(13), N(15))))
    for i in range(1, 16):
        print strify(tree2)
        assert is_valid(tree2)
        assert range(i, 16) == inorder(tree2)
        print "remove %s from %s" % (i, strify(tree2))
        tree2 = delete(i, tree2)
    assert not tree2
Example #2
0
def should_delete_double_black():
    '''
   delete 3
    2         2
   / \  =>   /
  1   3    (1)

                   8
                   |
               ---------
            /             \
          4                12
         |                  |
       ----                ----          
     /      \            /      \        
    2        6          10       14       
  /   \    /  \       /   \     /  \      
 1     3  5    7     9     11  13   15     
 
    
    '''
    tree1 = N(2, N(1), N(3))
    tree1 = delete(3, tree1)
    
    assert is_valid(tree1)
    assert [1, 2] == inorder(tree1)
    tree2 =N(8,
             N(4,
               N(2, N(1), N(3)),
               N(6, N(5), N(7))),
             N(12,
               N(10, N(9), N(11)),
               N(14, N(13), N(15))))
    for i in range(1, 16):
        print strify(tree2)
        assert is_valid(tree2)
        assert range(i, 16) == inorder(tree2)
        print "remove %s from %s" % (i, strify(tree2))
        tree2 = delete(i, tree2)
    assert not tree2
Example #3
0
def should_delete_corner_cases():
    '''
 delete 4 in this tree

 Example 1:
    
      3                       3            
    /    \                  /     \         
   2     (5)       =>      2      (7)       
  /     /   \             /      /   \      
(1)    4     7          (1)     5     8     
            /  \              X   \  
         (6)    (8)          (4)  (6) 


 Example 2:
 sibling black with single red child.
 Rotate to romve zig zag (if any) and rotate left.
    
         3                            3                        3          
       /    \                       /    \                   /    \       
      2     (5)    rotate right    2     (5)     left       2     (6)     
     /     /   \      =>          /     /   \     =>       /     /   \    
   (1)    4     7               (1)    4     6           (1)    5     7   
               /                              \                X
            (6)                               (7)            (4)


  Ex 3:
  black node, black parent but one of sibling or sibling's children is red
    
                                         3 
                                         |                    
            3                          -------
        /       \                   /          \               
       1         5                 1             7                
    /    \    /     \    =>     /    \        /     \         
  1       2  4      (7)        1       2     5       8           
                   /  \                    X   \         
                  6    8                 (4)   (6)       


                                         3                              3               
                                         |                              |                 
            3                          -------                        -------           
        /       \                   /          \        =>         /          \         
       1         5                 1             5                1             6        
    /    \    /     \    =>     /    \        /    \           /    \        /     \     
  1       2  4       7        1       2      4       6        1       2     5       7       
                   /                                  \                   X             
                 (6)                                  (7)               (4)             

    
    '''
    for tree in [
    make_node(3, color=B,
              left=make_node(2, color=B, left=make_node(1)), 
              right = make_node(5,
                                left=make_node(4, color=B),
                                right = make_node(7, color=B,
                                                  left=make_node(6),
                                                  right = make_node(8)))), 

    make_node(3, color=B,
              left=make_node(1, color=B, left=make_node(1)), 
              right = make_node(5,
                                left=make_node(4, color=B),
                                right = make_node(7, color=B, left=make_node(6)))), 
    make_node(3, color=B,
              left=make_node(1, color=B,                                     
                             left=make_node(1, color=B),
                             right = make_node(2, color =B)), 
              right = make_node(5, color=B, 
                                left=make_node(4, color=B),
                                right = make_node(7, color=B,
                                                  left=make_node(6),
                                                  right = make_node(8)))), 
    make_node(3, color=B,
              left=make_node(1, color=B,                                     
                             left=make_node(1, color=B),
                             right = make_node(2, color =B)), 
              right = make_node(5, color=B, 
                                left=make_node(4, color=B),
                                right = make_node(7, color=B,
                                                  left=make_node(6))))]:
        tree = assign_parents(tree)
        assert is_valid(tree) # ensure test data is valid
        vals =  inorder(tree)
        print strify(tree)
        tree = delete(4, tree)
        print strify(tree)
        assert is_valid(tree)
        vals.remove(4)
        assert vals == inorder(tree)
Example #4
0
def should_delete_corner_cases():
    '''
 delete 4 in this tree

 Example 1:
    
      3                       3            
    /    \                  /     \         
   2     (5)       =>      2      (7)       
  /     /   \             /      /   \      
(1)    4     7          (1)     5     8     
            /  \              X   \  
         (6)    (8)          (4)  (6) 


 Example 2:
 sibling black with single red child.
 Rotate to romve zig zag (if any) and rotate left.
    
         3                            3                        3          
       /    \                       /    \                   /    \       
      2     (5)    rotate right    2     (5)     left       2     (6)     
     /     /   \      =>          /     /   \     =>       /     /   \    
   (1)    4     7               (1)    4     6           (1)    5     7   
               /                              \                X
            (6)                               (7)            (4)


  Ex 3:
  black node, black parent but one of sibling or sibling's children is red
    
                                         3 
                                         |                    
            3                          -------
        /       \                   /          \               
       1         5                 1             7                
    /    \    /     \    =>     /    \        /     \         
  1       2  4      (7)        1       2     5       8           
                   /  \                    X   \         
                  6    8                 (4)   (6)       


                                         3                              3               
                                         |                              |                 
            3                          -------                        -------           
        /       \                   /          \        =>         /          \         
       1         5                 1             5                1             6        
    /    \    /     \    =>     /    \        /    \           /    \        /     \     
  1       2  4       7        1       2      4       6        1       2     5       7       
                   /                                  \                   X             
                 (6)                                  (7)               (4)             

    
    '''
    for tree in [
            make_node(3,
                      color=B,
                      left=make_node(2, color=B, left=make_node(1)),
                      right=make_node(5,
                                      left=make_node(4, color=B),
                                      right=make_node(7,
                                                      color=B,
                                                      left=make_node(6),
                                                      right=make_node(8)))),
            make_node(3,
                      color=B,
                      left=make_node(1, color=B, left=make_node(1)),
                      right=make_node(5,
                                      left=make_node(4, color=B),
                                      right=make_node(7,
                                                      color=B,
                                                      left=make_node(6)))),
            make_node(3,
                      color=B,
                      left=make_node(1,
                                     color=B,
                                     left=make_node(1, color=B),
                                     right=make_node(2, color=B)),
                      right=make_node(5,
                                      color=B,
                                      left=make_node(4, color=B),
                                      right=make_node(7,
                                                      color=B,
                                                      left=make_node(6),
                                                      right=make_node(8)))),
            make_node(3,
                      color=B,
                      left=make_node(1,
                                     color=B,
                                     left=make_node(1, color=B),
                                     right=make_node(2, color=B)),
                      right=make_node(5,
                                      color=B,
                                      left=make_node(4, color=B),
                                      right=make_node(7,
                                                      color=B,
                                                      left=make_node(6))))
    ]:
        tree = assign_parents(tree)
        assert is_valid(tree)  # ensure test data is valid
        vals = inorder(tree)
        print strify(tree)
        tree = delete(4, tree)
        print strify(tree)
        assert is_valid(tree)
        vals.remove(4)
        assert vals == inorder(tree)