Esempio n. 1
0
def N(val, left=None, right=None, color=B):
    node = make_node(val, left=left, right=right, color=color)
    if left:
        left.parent = node
    if right:
        right.parent = node
    return node
Esempio n. 2
0
def N(val, left=None, right=None, color=B):
    node = make_node(val, left=left, right=right, color=color)
    if left:
        left.parent = node
    if right:
        right.parent = node
    return node
Esempio n. 3
0
def sample_tree():
    '''
    Tree for rotation - the pivot is 4
               4
               |
             -------
           /         \
        2             (6)
        |              |
      -----          -----
     /     \        /     \ 
   (1)     (3)     5       7    

    '''
    return  assign_parents(make_node(4,color=B, 
                                     left = make_node(2, color=B,
                                                      left=make_node(1),
                                                      right = make_node(3)),
                                     right = make_node(6,
                                                       left=make_node(5, color=B),
                                                       right=make_node(7, color=B))))
Esempio n. 4
0
def sample_tree():
    '''
    Tree for rotation - the pivot is 4
               4
               |
             -------
           /         \
        2             (6)
        |              |
      -----          -----
     /     \        /     \ 
   (1)     (3)     5       7    

    '''
    return assign_parents(
        make_node(4,
                  color=B,
                  left=make_node(2,
                                 color=B,
                                 left=make_node(1),
                                 right=make_node(3)),
                  right=make_node(6,
                                  left=make_node(5, color=B),
                                  right=make_node(7, color=B))))
Esempio n. 5
0
def valid_trunk_2_children():
    return assign_parents(make_node(2, color=B, left=make_node(1), right = make_node(3)))
Esempio n. 6
0
def valid_trunk_1_child():
    return assign_parents(make_node(2, color=B, left=make_node(1)))
Esempio n. 7
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)
Esempio n. 8
0
def should_satisfy_invariant():
    assert is_valid(valid_trunk_2_children())
    assert is_valid(valid_trunk_1_child())

    tree = valid_trunk_2_children()
    tree.left.left = make_node(-1, R)
    assert not is_valid(tree)
    tree.left.left = make_node(-1, B)
    
    tree = valid_trunk_2_children()
    tree.left.value = tree.value + 1
    assert not is_valid(tree)

    tree = valid_trunk_2_children()
    tree.right.value = tree.value - 1
    assert not is_valid(tree)

    tree = assign_parents(make_node(4, color = B,
                     left = make_node(2,
                                      left=make_node(1, color=B), 
                                      right=make_node(3, color=B)), 
                     right = make_node(6, color = B,
                                       left = make_node(5), 
                                       right = make_node(7))))
    assert is_valid(tree)
    tree.left.right.value = tree.value + 1
    assert not is_valid(tree)

    tree = assign_parents(make_node(8, color = B,
                     left = make_node(4,
                                      left=make_node(2, color=B, 
                                                 left = make_node(1, color=B),
                                                 right = make_node(3, color=B)), 
                                      right=make_node(6, color=B,
                                                  left = make_node(5, color=B),
                                                  right = make_node(7, color=B))), 
                     right = make_node(10, color = B,
                                       left = make_node(9, color = B), 
                                       right = make_node(11, color = B))))
    assert is_valid(tree)
Esempio n. 9
0
def valid_trunk_2_children():
    return assign_parents(
        make_node(2, color=B, left=make_node(1), right=make_node(3)))
Esempio n. 10
0
def valid_trunk_1_child():
    return assign_parents(make_node(2, color=B, left=make_node(1)))
Esempio n. 11
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)
Esempio n. 12
0
def should_satisfy_invariant():
    assert is_valid(valid_trunk_2_children())
    assert is_valid(valid_trunk_1_child())

    tree = valid_trunk_2_children()
    tree.left.left = make_node(-1, R)
    assert not is_valid(tree)
    tree.left.left = make_node(-1, B)

    tree = valid_trunk_2_children()
    tree.left.value = tree.value + 1
    assert not is_valid(tree)

    tree = valid_trunk_2_children()
    tree.right.value = tree.value - 1
    assert not is_valid(tree)

    tree = assign_parents(
        make_node(4,
                  color=B,
                  left=make_node(2,
                                 left=make_node(1, color=B),
                                 right=make_node(3, color=B)),
                  right=make_node(6,
                                  color=B,
                                  left=make_node(5),
                                  right=make_node(7))))
    assert is_valid(tree)
    tree.left.right.value = tree.value + 1
    assert not is_valid(tree)

    tree = assign_parents(
        make_node(8,
                  color=B,
                  left=make_node(4,
                                 left=make_node(2,
                                                color=B,
                                                left=make_node(1, color=B),
                                                right=make_node(3, color=B)),
                                 right=make_node(6,
                                                 color=B,
                                                 left=make_node(5, color=B),
                                                 right=make_node(7, color=B))),
                  right=make_node(10,
                                  color=B,
                                  left=make_node(9, color=B),
                                  right=make_node(11, color=B))))
    assert is_valid(tree)