예제 #1
0
def create_bst():
    btree = BST()

    n1 = BSTNode(val=1)
    n2 = BSTNode(val=2)
    n3 = BSTNode(val=3)
    n4 = BSTNode(val=4)
    n5 = BSTNode(val=5)
    n6 = BSTNode(val=6)

    btree.root = n4
    btree._size = 6

    n4.left_child = n2
    n4.right_child = n6

    n2.parent = n4
    n6.parent = n4

    n2.left_child = n1
    n2.right_child = n3

    n1.parent = n2
    n3.parent = n2

    n6.left_child = n5

    n5.parent = n6

    return btree
예제 #2
0
def create_r_r_tree():
    n1 = BSTNode(val=1)
    n2 = BSTNode(val=2)
    n3 = BSTNode(val=3)
    n4 = BSTNode(val=4)
    n5 = BSTNode(val=5)
    n6 = BSTNode(val=6)
    n7 = BSTNode(val=7)

    n2.left_child = n1
    n2.right_child = n4
    n4.left_child = n3
    n4.right_child = n6
    n6.left_child = n5
    n6.right_child = n7

    n7.parent = n6
    n5.parent = n6
    n6.parent = n4
    n3.parent = n4
    n4.parent = n2
    n1.parent = n2

    btree = BST()
    btree.root = n2

    return btree
예제 #3
0
def create_bst_2():
    btree = BST()
    vals = [10, 5, 15, 2, 7, 20, 6, 8, 17]

    for val in vals:
        btree.insert(val=val)

    return btree
예제 #4
0
def test_insert_second_value_greater_than_root():
    """When inserting a value larger than the root, it should go in a new
    node as right_child to root in a tree with only root."""

    btree = BST()

    btree.insert(val=3)
    btree.insert(val=10)

    assert btree.root.right_child is not None
    assert btree.root.right_child.val == 10
예제 #5
0
def test_insert_second_value_smaller_than_root():
    """When inserting a value smaller than the root, it should go in a new
    node as left_child to root in a tree with only root."""

    btree = BST()

    btree.insert(val=3)
    btree.insert(val=2)

    assert btree.root.left_child is not None
    assert btree.root.right_child is None
    assert btree.root.left_child.val == 2
예제 #6
0
def test_insert_first_value():
    """First insertion should set root to that node, and that node
    should have no parents or children."""

    btree = BST()

    btree.insert(val=7)
    assert btree.root is not None
    assert btree.root.val == 7
    assert btree.root.parent is None
    assert btree.root.left_child is None
    assert btree.root.right_child is None
예제 #7
0
def test_rotate_left_on_r_r_tree_parentless_and_childless():
    pivot = BSTNode(val=2)
    newroot = BSTNode(val=3)
    n1 = BSTNode(val=1)

    pivot.right_child = newroot
    pivot.left_child = n1
    newroot.parent = pivot
    n1.parent = pivot

    btree = BST()
    btree.root = pivot

    btree._rotate_left(pivot=pivot, newroot=newroot)

    assert btree.root is newroot
    assert newroot.parent is None
    assert newroot.right_child is None
    assert newroot.left_child is pivot
    assert pivot.parent is newroot
    assert pivot.left_child is n1
    assert n1.parent is pivot
예제 #8
0
def test_rotate_right_on_l_l_tree_parentless_and_childless():
    pivot = BSTNode(val=2)
    newroot = BSTNode(val=1)
    n3 = BSTNode(val=3)

    pivot.right_child = n3
    pivot.left_child = newroot
    newroot.parent = pivot
    n3.parent = pivot

    btree = BST()
    btree.root = pivot

    btree._rotate_right(pivot=pivot, newroot=newroot)

    assert btree.root is newroot
    assert newroot.parent is None
    assert newroot.left_child is None
    assert newroot.right_child is pivot
    assert pivot.parent is newroot
    assert pivot.right_child is n3
    assert n3.parent is pivot
예제 #9
0
def test_delete_empty_tree():
    btree = BST()
    assert btree.delete(5) is None
예제 #10
0
def test_balance_when_empty():
    btree = BST()
    assert btree.balance() == 0
예제 #11
0
def test_depth_when_empty():
    btree = BST()
    assert btree.depth() == 0
예제 #12
0
def test_size_when_empty():
    btree = BST()
    assert btree.size() == 0
예제 #13
0
def test_contains_when_empty():
    btree = BST()
    assert btree.contains(3) is False