Ejemplo n.º 1
0
class TestingMyMethods(unittest.TestCase):
    """Now let's test the methods"""
    def setUp(self):
        self.my_bst = BSTree()
        self.filled_tree = BSTree()
        self.filled_tree.insert(5)
        self.filled_tree.insert(3)
        self.filled_tree.insert(6)
        self.filled_tree.insert(8)
        self.filled_tree.insert(11)
        self.filled_tree.insert(2)

    def test_the_contains_method_on_empty_tree(self):
        self.assertEqual(self.my_bst.contains(5), False)

    def test_contains_on_tree_that_doesnt_have_the_val(self):
        self.assertEqual(self.filled_tree.contains(1), False)
        self.assertEqual(self.filled_tree.contains(9), False)

    def test_contains_for_values_it_has(self):
        self.assertEqual(self.filled_tree.contains(8), True)
        self.assertEqual(self.filled_tree.contains(2), True)
        self.assertEqual(self.filled_tree.contains(5), True)
        self.assertEqual(self.filled_tree.contains(11), True)
        self.assertEqual(self.filled_tree.contains(6), True)
        self.assertEqual(self.filled_tree.contains(3), True)

    def test_size(self):
        self.assertEqual(self.my_bst.size(), 0)
        self.my_bst.insert(1)
        self.assertEqual(self.my_bst.size(), 1)
        self.assertEqual(self.filled_tree.size(), 6)
        self.filled_tree.insert(15)
        self.assertEqual(self.filled_tree.size(), 7)

    def test_the_depth_method(self):
        self.assertEqual(self.my_bst.depth(), 0)
        self.assertEqual(self.filled_tree.depth(), 4)
        self.filled_tree.insert(5422)
        self.assertEqual(self.filled_tree.depth(), 5)

    def test_balance(self):
        self.assertEqual(self.my_bst.balance(), 0)
        self.assertEqual(self.filled_tree.balance(), 1)
        self.filled_tree.insert(5422)
        self.assertEqual(self.filled_tree.balance(), 2)
Ejemplo n.º 2
0
class TestingMyMethods(unittest.TestCase):
    """Now let's test the methods"""
    def setUp(self):
        self.my_bst = BSTree()
        self.filled_tree = BSTree()
        self.filled_tree.insert(5)
        self.filled_tree.insert(3)
        self.filled_tree.insert(6)
        self.filled_tree.insert(8)
        self.filled_tree.insert(11)
        self.filled_tree.insert(2)

    def test_the_contains_method_on_empty_tree(self):
        self.assertEqual(self.my_bst.contains(5), False)

    def test_contains_on_tree_that_doesnt_have_the_val(self):
        self.assertEqual(self.filled_tree.contains(1), False)
        self.assertEqual(self.filled_tree.contains(9), False)

    def test_contains_for_values_it_has(self):
        self.assertEqual(self.filled_tree.contains(8), True)
        self.assertEqual(self.filled_tree.contains(2), True)
        self.assertEqual(self.filled_tree.contains(5), True)
        self.assertEqual(self.filled_tree.contains(11), True)
        self.assertEqual(self.filled_tree.contains(6), True)
        self.assertEqual(self.filled_tree.contains(3), True)

    def test_size(self):
        self.assertEqual(self.my_bst.size(), 0)
        self.my_bst.insert(1)
        self.assertEqual(self.my_bst.size(), 1)
        self.assertEqual(self.filled_tree.size(), 6)
        self.filled_tree.insert(15)
        self.assertEqual(self.filled_tree.size(), 7)

    def test_the_depth_method(self):
        self.assertEqual(self.my_bst.depth(), 0)
        self.assertEqual(self.filled_tree.depth(), 4)
        self.filled_tree.insert(5422)
        self.assertEqual(self.filled_tree.depth(), 5)

    def test_balance(self):
        self.assertEqual(self.my_bst.balance(), 0)
        self.assertEqual(self.filled_tree.balance(), 1)
        self.filled_tree.insert(5422)
        self.assertEqual(self.filled_tree.balance(), 2)
Ejemplo n.º 3
0
def test_insert_avl_single_left_branch():
    tree = BSTree()
    for i in xrange(7, 0, -1):
        tree.insert(i)
    actual = [n.key for n in tree.breadth_first()]
    expected = [4, 2, 6, 1, 3, 5, 7]
    assert tree.depth() == 3
    assert tree.balance() == 0
    assert actual == expected
Ejemplo n.º 4
0
def test_rebalance_rotate_right():
    tree = BSTree(3)
    tree.left = BSTree(2, tree)
    tree.left.left = BSTree(1, tree.left)
    tree.rebalance()
    actual = [n.key for n in tree.breadth_first()]
    expected = [2, 1, 3]
    assert tree.depth() == 2
    assert tree.balance() == 0
    assert actual == expected
Ejemplo n.º 5
0
def tree_rebalance_right_right():
    tree = BSTree(2)
    tree.left = BSTree(1, tree)
    tree.right = BSTree(4, tree)
    tree.right.left = BSTree(3, tree.right)
    tree.right.right = BSTree(6, tree.right)
    tree.right.right.left = BSTree(5, tree.right.right)
    tree.right.right.right = BSTree(7, tree.right.right)
    tree.rebalance()
    actual = [n.key for n in tree.breadth_first()]
    expected = [4, 2, 6, 1, 3, 5, 7]
    assert tree.depth() == 3
    assert tree.balance() == 0
    assert actual == expected