예제 #1
0
def test_right_left_conversion_six_nodes():
    """
    Test that given six nodes in a left-right state converts to left-left.
    """
    from bst import Bst, Node
    new_bst = Bst()
    node1 = Node(10)
    node2 = Node(5)
    node3 = Node(15)
    node4 = Node(20)
    node5 = Node(14)
    node6 = Node(13)
    new_bst.head = node1
    node1.left = node2
    node2.parent = node1
    node1.right = node3
    node3.parent = node1
    node3.right = node4
    node4.parent = node3
    node3.left = node5
    node5.parent = node3
    node5.left = node6
    node6.parent = node5
    new_bst.head.right_left_conversion()
    assert new_bst.head.right == node5
    assert new_bst.head.right.right == node3
    assert new_bst.head.right.right.right == node4
예제 #2
0
def test_left_rotation_three_nodes():
    """Test that a right-right becomes balanced."""
    from bst import Bst, Node
    new_bst = Bst()
    node1 = Node(10)
    node2 = Node(15)
    node3 = Node(20)
    new_bst.head = node1
    node1.right = node2
    node2.parent = node1
    node2.right = node3
    node3.parent = node2
    new_bst.head.left_rotation()
    assert node2.parent is None
    assert node2.left == node1
    assert node2.right == node3
    assert node2.left.parent == node2
예제 #3
0
def test_right_left_conversion_three_nodes():
    """
    Test that given three nodes in a right-left state converts to right-right.
    """
    from bst import Bst, Node
    new_bst = Bst()
    node1 = Node(10)
    node2 = Node(15)
    node3 = Node(12)
    new_bst.head = node1
    node1.right = node2
    node2.parent = node1
    node2.left = node3
    node3.parent = node2
    new_bst.head.right_left_conversion()
    assert new_bst.head.right == node3
    assert new_bst.head.right.right == node2
    assert new_bst.head.right.right.parent == node3
    assert new_bst.head.right.parent == node1