Example #1
0
def should_rotate_left():
    '''
    
                                         
                                         
                                                                         
               4                                          4              
               |                    =>                    |              
             -------                                    -------          
           /         \                                /         \        
        2              6                           3              6      
        |              |                          /               |      
      -----          -----                       2              -----    
     /     \        /     \                     /             /     \   
   1        3      5       7                  1              5       7  
                                        

    '''
    tree = sample_tree()
    before_inorder = inorder(tree)
    rotate_left(tree.left)
    assert before_inorder == inorder(tree)
    assert 3 == tree.left.value
    assert 2 == tree.left.left.value
    assert 1 == tree.left.left.left.value
    rotate_right(tree.left)
    assert sample_tree().equivalent(tree)
Example #2
0
def should_rotate_left():
    '''
    
                                         
                                         
                                                                         
               4                                          4              
               |                    =>                    |              
             -------                                    -------          
           /         \                                /         \        
        2              6                           3              6      
        |              |                          /               |      
      -----          -----                       2              -----    
     /     \        /     \                     /             /     \   
   1        3      5       7                  1              5       7  
                                        

    '''
    tree = sample_tree()
    before_inorder = inorder(tree)
    rotate_left(tree.left)
    assert before_inorder == inorder(tree)
    assert 3 == tree.left.value
    assert 2 == tree.left.left.value
    assert 1 == tree.left.left.left.value
    rotate_right(tree.left)
    assert sample_tree().equivalent(tree)
Example #3
0
def should_rotate_right():
    '''
    
    
               4                                      2                 
               |                    =>                |                 
             -------                                -------             
           /         \                            /         \               
        2              6                        1            4
        |              |                                   /   \
      -----          -----                                3     6         
     /     \        /     \                                     |         
   1        3      5       7                                   -----       
                                                              /     \      
                                                             5       7

    '''
    four = sample_tree()
    rotate_right(four)
    assert [3, 4, 5, 6, 7] == inorder(four)
    assert 3 == four.left.value
    assert four.equivalent(four.left.parent)
    two = four.parent
    assert 2 == two.value
    assert not two.parent # 2 is root
    original_tree = sample_tree()
    assert inorder(original_tree) == inorder(two)
    rotate_left(two)
    assert four.equivalent(original_tree)
    assert not four.parent # 4 is root again
    assert 2 == four.left.value
    assert 6 == four.right.value

    three = four.left.right
    assert 3 == three.value
    assert 2 == three.parent.value
    assert 4 == three.parent.parent.value
Example #4
0
def should_rotate_right():
    '''
    
    
               4                                      2                 
               |                    =>                |                 
             -------                                -------             
           /         \                            /         \               
        2              6                        1            4
        |              |                                   /   \
      -----          -----                                3     6         
     /     \        /     \                                     |         
   1        3      5       7                                   -----       
                                                              /     \      
                                                             5       7

    '''
    four = sample_tree()
    rotate_right(four)
    assert [3, 4, 5, 6, 7] == inorder(four)
    assert 3 == four.left.value
    assert four.equivalent(four.left.parent)
    two = four.parent
    assert 2 == two.value
    assert not two.parent  # 2 is root
    original_tree = sample_tree()
    assert inorder(original_tree) == inorder(two)
    rotate_left(two)
    assert four.equivalent(original_tree)
    assert not four.parent  # 4 is root again
    assert 2 == four.left.value
    assert 6 == four.right.value

    three = four.left.right
    assert 3 == three.value
    assert 2 == three.parent.value
    assert 4 == three.parent.parent.value