Example #1
0
def test_delete_single_rotation(insert, delete, expected):
    """Tests deletion resulting in a single rotation."""
    tree = AVLTree(insert)
    tree.delete(delete)
    assert delete not in tree
    assert pre_order(tree) == expected
    assert_avl_invariants(tree.root)
Example #2
0
def test_delete_reattach():
    """Removing the root from a 2-depth V should reattach nodes correctly."""
    tree = AVLTree([3, 2, 4, 1, 5])
    tree.delete(3)
    assert 3 not in tree
    assert pre_order(tree) == [2, 1, 4, 5]
    assert_avl_invariants(tree.root)
Example #3
0
def test_delete_trivial(insert, delete, expected):
    """Tests deletion of root/leaf nodes requiring no rotation."""
    tree = AVLTree(insert)
    tree.delete(delete)
    assert delete not in tree
    assert pre_order(tree) == expected
    assert_avl_invariants(tree.root)
Example #4
0
def test_delete_rotation_under_root(insert, delete):
    tree = AVLTree(insert)
    tree.delete(delete)
    assert delete not in tree
    expected = sorted(insert)
    expected.remove(delete)
    assert in_order(tree) == expected
    assert_avl_invariants(tree.root)
Example #5
0
def test_delete_attach_without_rotation(insert, deletes, expected):
    """Tests selective pruning of nodes and reattaching them to parents."""
    tree = AVLTree(insert)
    for key in deletes:
        tree.delete(key)
    assert key not in tree
    assert pre_order(tree) == expected
    assert_avl_invariants(tree.root)
Example #6
0
def test_two_way_rotations_with_subtree_at_pivot(insert, delete):
    """Deletion causes a LR or RL rotation and the new subtree root has children."""
    tree = AVLTree(insert)
    tree.delete(delete)
    assert delete not in tree
    expected = sorted(insert)
    expected.remove(delete)
    assert in_order(tree) == expected
    assert_avl_invariants(tree.root)
Example #7
0
def test_delete_double_rotation():
    """Tests deletion resulting in a double rotation."""
    tree = AVLTree([16, 8, 24, 4, 12, 20, 28, 2, 6, 10, 32, 1, 3])
    tree.delete(20)
    assert 20 not in tree
    assert tree.root.value == 8
    assert tree.root.left.value == 4
    assert tree.root.right.value == 16
    assert_avl_invariants(tree.root)
Example #8
0
def test_delete_root():
    """Tests deletion of singular root node."""
    tree = AVLTree([1])
    tree.delete(1)
    assert 1 not in tree
    assert tree.root is None
Example #9
0
def test_delete_nonexisting():
    """Tests deletion of nonexisting key raises KeyError."""
    tree = AVLTree()
    with pytest.raises(KeyError):
        tree.delete(2)