Esempio n. 1
0
def test__BST_remove_list2(xs, ys):
    xs = list(set(xs))
    bst = BST(xs)
    bst.remove_list(ys)
    for y in ys:
        if y in xs:
            xs.remove(y)
    assert bst.to_list('inorder') == sorted(xs)
Esempio n. 2
0
def test__BST_is_bst_satisified5():
    bst = BST()
    bst.root = Node(0)
    bst.root.left = Node(-2)
    bst.root.left.left = Node(-3)
    bst.root.left.right = Node(-1)
    bst.root.right = Node(2)
    bst.root.right.left = Node(1)
    bst.root.right.right = Node(3)
    assert bst.is_bst_satisfied()
Esempio n. 3
0
def test__BST_remove2(xs):
    '''
    If we remove something from the BST that is not in the BST,
    then the BST should remain unchanged.
    '''
    xs = list(set(xs))
    bst = BST(xs)
    y = 0
    while y in xs:
        y += 1
    bst.remove(y)
    assert bst.to_list('inorder') == sorted(xs)
Esempio n. 4
0
def test__BST___contains__1(xs):
    '''
    Checks that if a value is in the bst then __contains__ returns True
    '''
    xs = list(set(xs))
    if len(xs) > 0:
        x = random.choice(xs)
        bst = BST(xs)
        assert x in bst
Esempio n. 5
0
def test__BST_remove_and_insert1(xs, ys):
    '''
    This test performs a mixture of both insertions and removals.
    This ensures that there are no weird interactions between inserting and removing.
    '''
    xs = list(set(xs))
    bst = BST(xs)
    for y in ys:
        bst.insert(y)
        x = random.choice(bst.to_list('inorder'))
        bst.remove(x)
        assert bst.is_bst_satisfied()
Esempio n. 6
0
def test__BST_remove_and_insert3(xs, ys):
    '''
    This test performs a mixture of both insertions and removals.
    This ensures that there are no weird interactions between inserting and removing.
    '''
    xs = list(set(xs))
    bst = BST(xs)
    for y in ys:
        bst.insert(y)
        x = bst.find_smallest()
        bst.remove(x)
        assert bst.is_bst_satisfied()
Esempio n. 7
0
def test__BST_insert(xs):
    xs = list(set(xs))
    bst = BST()
    for x in xs:
        bst.insert(x)
        assert x in bst.to_list('inorder')
        assert bst.is_bst_satisfied()
Esempio n. 8
0
def test__BST___contains__2(xs):
    '''
    Checks that if a value is NOT in the bst then __contains__ returns False
    '''
    xs = list(set(xs))
    if len(xs) > 0:
        while True:
            x = random.uniform(min(xs) - 1, max(xs) + 1)
            if x not in xs:
                break
    else:
        x = 10
    bst = BST(xs)
    assert x not in bst
Esempio n. 9
0
def test__BST_remove1(xs):
    '''
    This tests the remove function.
    In order to test the remove function, we must be able to generate valid BSTs.
    Therefore, you must have all the insert functionality completed before this test can pass.
    '''
    xs = list(set(xs))
    bst = BST(xs)
    while len(xs) > 0:
        x = random.choice(xs)
        xs.remove(x)
        assert x in bst
        bst.remove(x)
        assert x not in bst
        assert bst.to_list('inorder') == sorted(xs)
        assert bst.is_bst_satisfied()
Esempio n. 10
0
def test__BST_inorder_property(xs):
    '''
    The order we insert objects into a BST can affect the structure of the tree,
    but it should NOT affect the list we get out from an inorder traversal.
    (Recall that the inorder traversal of a BST should always be a sorted list.)
    This test randomly shuffles the input list two different ways
    and checks that both shufflings give the same output list.
    This tests both the insertion functions and the traversal functions
    to ensure that there are no bad interactions between theses functions.
    '''
    xs = list(set(xs))

    xs1 = copy.copy(xs)
    random.shuffle(xs1)
    bst1 = BST(xs1)

    xs2 = copy.copy(xs)
    random.shuffle(xs2)
    bst2 = BST(xs2)

    assert bst1.to_list('inorder') == bst2.to_list('inorder')
Esempio n. 11
0
def test__BST_is_bst_satisified4():
    bst = BST()
    bst.root = Node(0)
    bst.root.left = Node(-1)
    assert bst.is_bst_satisfied()
Esempio n. 12
0
def test__BST_is_bst_satisified3():
    bst = BST()
    assert bst.is_bst_satisfied()
Esempio n. 13
0
def test__BST_super():
    x = BST()
    assert isinstance(x, BinaryTree)
Esempio n. 14
0
def test__BST_is_bst_satisfied2():
    bst = BST()
    bst.root = Node(-2)
    bst.root.left = Node(-3)
    bst.root.right = Node(-4)
    assert not bst.is_bst_satisfied()
Esempio n. 15
0
def test__BST_find_smallest(xs):
    xs = list(set(xs))
    if len(xs) > 0:
        x = min(xs)
        bst = BST(xs)
        assert x == bst.find_smallest()
Esempio n. 16
0
def test__BST_insert_list(xs):
    xs = list(set(xs))
    bst = BST()
    bst.insert_list(xs)
    assert bst.is_bst_satisfied()
Esempio n. 17
0
def test__BST_find_largest(xs):
    xs = list(set(xs))
    if len(xs) > 0:
        x = max(xs)
        bst = BST(xs)
        assert x == bst.find_largest()
Esempio n. 18
0
def test__BST_remove_list1(xs, ys):
    xs = list(set(xs))
    bst = BST(xs)
    bst.remove_list(ys)
    for y in ys:
        assert y not in bst
Esempio n. 19
0
def test__BST___init__(xs):
    xs = list(set(xs))
    bst = BST(xs)
    assert bst.is_bst_satisfied()
Esempio n. 20
0
from Basic.List import List
from Trees.BST import BST
from Trees.BST import BSTNode
from Trees.RBT import RBTNode
from Trees.RBT import RBT
import random

tree = BST()
tree.add(5)
tree.add(1)
tree.add(12)
tree.add(7)
tree.add(8)
tree.add(6)
tree.add(4)
tree.add(3)
tree.add(10)
tree.draw()