Пример #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 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
Пример #4
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