Exemplo n.º 1
0
 def test_add_mixed_numbers(self):
     bst = BinarySearchTree()
     bst.add(1)
     bst.add(8)
     bst.add(7.5)
     bst.add(5.3)
     self.assertEqual(list(bst.list()), [1, 5.3, 7.5, 8])
Exemplo n.º 2
0
 def test_add_duplicated_numbers(self):
     bst = BinarySearchTree()
     bst.add(1)
     bst.add(1)
     bst.add(7.5)
     bst.add(5.3)
     self.assertEqual(list(bst.list()), [1, 1, 5.3, 7.5])
 def test_add(self):
     bst = BinarySearchTree()
     vals = [10, 5, 3, 7, 15, 13, 17]
     for val in vals:
         bst.add(val)
     for i, (actual, expected) in enumerate(zip(bst, sorted(vals))):
         self.assertEqual(actual, expected)
     self.assertEqual(len(vals) - 1, i)
Exemplo n.º 4
0
def test_bst_contains_for_existing_values():
    bst = BinarySearchTree()
    bst.add(5)
    bst.add(6)
    bst.add(3)
    bst.add(4)
    assert 4 in bst
    assert 3 in bst
    assert 5 in bst
    assert 6 in bst
 def test_post_order(self):
     bst = BinarySearchTree()
     vals = [10, 5, 3, 7, 15, 13, 17]
     ordered = [3, 7, 5, 13, 17, 15, 10]
     for val in vals:
         bst.add(val)
     for i, (actual,
             expected) in enumerate(zip(bst.post_order_iterator(),
                                        ordered)):
         self.assertEqual(actual, expected)
     self.assertEqual(len(vals) - 1, i)
Exemplo n.º 6
0
def test_bst_contains_for_inexisting_values():
    bst = BinarySearchTree()
    bst.add(3)
    bst.add(1)
    bst.add(2)
    bst.add(4)
    assert 5 not in bst
    assert 6 not in bst
    assert 99 not in bst
    assert 0 not in bst
    assert -1 not in bst
 def test_add_multiple_values_in_tree(self):
     tree = BinarySearchTree()
     self.assertEqual(tree.add(-1), True)
     self.assertEqual(tree.add(50), True)
     self.assertEqual(tree.add(-73), True)
     self.assertEqual(tree.add(20), True)
     self.assertEqual(tree.size(), 4)
     self.assertEqual(tree.contains(-1), True)
     self.assertEqual(tree.contains(50), True)
     self.assertEqual(tree.contains(-73), True)
     self.assertEqual(tree.contains(20), True)
Exemplo n.º 8
0
def test_bst_height():
    bst = BinarySearchTree()
    assert bst.height() == None
    bst.add(5)
    assert bst.height() == 0
    bst.add(2)
    bst.add(7)
    assert bst.height() == 1
    bst.add(8)
    assert bst.height() == 2
    bst.add(9)
    assert bst.height() == 3
Exemplo n.º 9
0
 def test_multiple_large_numbers_should_be_nested_to_right(self):
     bst = BinarySearchTree()
     bst.add(1)
     bst.add(2)
     bst.add(3)
     bst.add(4)
     bst.add(5)
     self.assertEqual(bst.root.preorder_traversal(), [1, 2, 3, 4, 5])
Exemplo n.º 10
0
 def test_add_float_numbers(self):
     bst = BinarySearchTree()
     bst.add(7.5)
     bst.add(5.3)
     bst.add(5.5)
     bst.add(6.0)
     bst.add(7.7)
     self.assertEqual(list(bst.list()), [5.3, 5.5, 6.0, 7.5, 7.7])
Exemplo n.º 11
0
 def test_multiple_small_numbers_should_be_nested_to_left(self):
     bst = BinarySearchTree()
     bst.add(5)
     bst.add(4)
     bst.add(3)
     bst.add(2)
     bst.add(1)
     self.assertEqual(bst.root.preorder_traversal(), [5, 4, 3, 2, 1])
Exemplo n.º 12
0
 def test_add_integer_numbers(self):
     bst = BinarySearchTree()
     bst.add(1)
     bst.add(8)
     bst.add(3)
     bst.add(5)
     bst.add(2)
     self.assertEqual(list(bst.list()), [1, 2, 3, 5, 8])
Exemplo n.º 13
0
def test_bst_traversals():
    bst = BinarySearchTree()
    bst.add(4)
    bst.add(2)
    bst.add(5)
    bst.add(1)
    bst.add(3)
    inorder = bst.inorder()
    preorder = bst.preorder()
    postorder = bst.postorder()
    assert [1, 2, 3, 4, 5] == list(inorder)
    assert [4, 2, 1, 3, 5] == list(preorder)
    assert [1, 3, 2, 5, 4] == list(postorder)
Exemplo n.º 14
0
def test_bst_remove():
    bst = BinarySearchTree()
    bst.add(5)
    bst.add(6)
    bst.add(1)
    bst.add(4)
    bst.add(3)
    bst.remove(5)
    assert 5 not in bst
    assert bst.root.value == 4
    bst.remove(3)
    assert 3 not in bst
    bst.remove(6)
    assert 6 not in bst
    bst.remove(1)
    assert 1 not in bst
    bst.remove(4)
    assert 4 not in bst
    assert bst.root is None
Exemplo n.º 15
0
 def test_add_repeated_values_in_tree(self):
     tree = BinarySearchTree()
     self.assertEqual(tree.add(2), True)
     self.assertEqual(tree.add(3), True)
     self.assertEqual(tree.add(4), True)
     self.assertEqual(tree.add(2), False)
     self.assertEqual(tree.add(1), True)
     self.assertEqual(tree.add(1), False)
     self.assertEqual(tree.add(1), False)
     self.assertEqual(tree.add(2), False)
     self.assertEqual(tree.add(3), False)
     self.assertEqual(tree.add(4), False)
     self.assertEqual(tree.add(5), True)
     self.assertEqual(tree.size(), 5)
     self.assertEqual(tree.contains(1), True)
     self.assertEqual(tree.contains(2), True)
     self.assertEqual(tree.contains(3), True)
     self.assertEqual(tree.contains(4), True)
     self.assertEqual(tree.contains(5), True)
Exemplo n.º 16
0
 def test_remove_multiple_values_in_tree(self):
     tree = BinarySearchTree()
     tree.add(3)
     tree.add(1)
     tree.add(2)
     tree.add(5)
     tree.add(4)
     self.assertEqual(tree.remove(3), True)
     self.assertEqual(tree.remove(1), True)
     self.assertEqual(tree.remove(5), True)
     self.assertEqual(tree.remove(6), False)
     self.assertEqual(tree.remove(7), False)
     self.assertEqual(tree.size(), 2)
     self.assertEqual(tree.contains(1), False)
     self.assertEqual(tree.contains(2), True)
     self.assertEqual(tree.contains(3), False)
     self.assertEqual(tree.contains(4), True)
     self.assertEqual(tree.contains(5), False)
     self.assertEqual(tree.contains(6), False)
     self.assertEqual(tree.contains(7), False)
Exemplo n.º 17
0
 def test_add_one_value_in_tree(self):
     tree = BinarySearchTree()
     self.assertEqual(tree.add(3), True)
     self.assertEqual(tree.size(), 1)
     self.assertEqual(tree.contains(3), True)
Exemplo n.º 18
0
  bt.add(6)
  bt.add(9)
  bt.add(15)
  bt.add(12)
  bt.add(11)
  bt.add(14)
  bt.add(17)
  bt.add(16)
  bt.add(19)

# small tree

if False:

  bt = BinarySearchTree()
  bt.add('a')
  bt.add('d')
  bt.add('e')
  bt.add('f')
  bt.add('b')
  bt.add('c')

  bt.show_all()
  bt.show_all_visual()

  for e in ['a','e','j']:
    check_for(bt,e)

  print

# deleting
Exemplo n.º 19
0
from binary_search_tree import BinarySearchTree

tree = BinarySearchTree((1, 2, 4, 5))

# Test add()
tree.add(0)
tree.add(3)
tree.add(6)
tree.print_tree()
print("Expected: 0 1 2 3 4 5 6")
print("-----")

# Test remove()
tree.remove(0)
tree.remove(6)
tree.remove(3)
tree.print_tree()
print("Expected: 1 2 4 5")
print("-----")

# Test get_first()
print(tree.get_first())
print("Expected: 1")
print("-----")

# Test get_last()
print(tree.get_last())
print("Expected: 5")
print("-----")

# Test contains()
Exemplo n.º 20
0
 def test_contains_value_in_tree_with_multiple_operations(self):
     tree = BinarySearchTree()
     tree.remove(1)
     self.assertEqual(tree.contains(1), False)
     tree.add(1)
     self.assertEqual(tree.contains(1), True)
     tree.remove(2)
     self.assertEqual(tree.contains(2), False)
     tree.add(3)
     self.assertEqual(tree.contains(3), True)
     tree.add(2)
     self.assertEqual(tree.contains(2), True)
     tree.add(4)
     self.assertEqual(tree.contains(4), True)
     tree.remove(1)
     self.assertEqual(tree.contains(1), False)
     tree.remove(1)
     self.assertEqual(tree.contains(1), False)
     tree.add(2)
     self.assertEqual(tree.contains(2), True)
     tree.add(5)
     self.assertEqual(tree.contains(5), True)
     tree.remove(2)
     self.assertEqual(tree.contains(1), False)
     self.assertEqual(tree.contains(2), False)
     self.assertEqual(tree.contains(3), True)
     self.assertEqual(tree.contains(4), True)
     self.assertEqual(tree.contains(5), True)
Exemplo n.º 21
0
    bt.add(9)
    bt.add(15)
    bt.add(12)
    bt.add(11)
    bt.add(14)
    bt.add(17)
    bt.add(16)
    bt.add(19)


# small tree

if False:

    bt = BinarySearchTree()
    bt.add('a')
    bt.add('d')
    bt.add('e')
    bt.add('f')
    bt.add('b')
    bt.add('c')

    bt.show_all()
    bt.show_all_visual()

    for e in ['a', 'e', 'j']:
        check_for(bt, e)

    print

# deleting
Exemplo n.º 22
0
 def test_find_an_existent_value(self):
     #          5
     #      2        9
     #    1   3    7   10
     #  0        6   8
     #
     #
     #
     bst = BinarySearchTree()
     bst.add(5)
     bst.add(2)
     bst.add(1)
     bst.add(3)
     bst.add(9)
     bst.add(10)
     bst.add(7)
     bst.add(6)
     bst.add(8)
     bst.add(0)
     self.assertEqual(bst.find(5), 0)
     self.assertEqual(bst.find(9), 1)
     self.assertEqual(bst.find(7), 2)
     self.assertEqual(bst.find(2), 1)
     self.assertEqual(bst.find(3), 2)
     self.assertEqual(bst.find(0), 3)
     self.assertEqual(bst.find(8), 3)
Exemplo n.º 23
0
 def test_assign_first_added_node_to_root(self):
     bst = BinarySearchTree()
     bst.add(5)
     self.assertEqual(bst.root.data, 5)
Exemplo n.º 24
0
 def test_remove_value_in_tree_with_one_element(self):
     tree = BinarySearchTree()
     tree.add(0)
     self.assertEqual(tree.remove(0), True)
     self.assertEqual(tree.size(), 0)
     self.assertEqual(tree.contains(0), False)
Exemplo n.º 25
0
 def test_find_value_that_does_not_exist(self):
     bst = BinarySearchTree()
     bst.add(5)
     self.assertEqual(bst.find(100), -1)
Exemplo n.º 26
0
 def test_larger_number_should_be_added_to_the_right_side_of_tree(self):
     bst = BinarySearchTree()
     bst.add(5)
     bst.add(6)
     self.assertEqual(bst.root.preorder_traversal(), [5, 6])
Exemplo n.º 27
0
 def test_add_a_new_node_to_tree_when_the_root_is_not_none(self):
     bst = BinarySearchTree()
     bst.add(5)
     bst.add(4)
     self.assertEqual(bst.root.data, 5)
Exemplo n.º 28
0
 def test_search_existent_numbers(self):
     bst = BinarySearchTree()
     bst.add(1)
     bst.add(7.5)
     self.assertEqual(bst.search(1).value, 1)
     self.assertEqual(bst.search(7.5).value, 7.5)
Exemplo n.º 29
0
 def test_search_nonexistent_numbers(self):
     bst = BinarySearchTree()
     bst.add(1)
     bst.add(7.5)
     self.assertIs(bst.search(6), None)
     self.assertIs(bst.search(8.8), None)
Exemplo n.º 30
0
 def test_smaller_number_should_be_added_to_the_left_side_of_tree(self):
     bst = BinarySearchTree()
     bst.add(5)
     bst.add(4)
     self.assertEqual(bst.root.preorder_traversal(), [5, 4])
Exemplo n.º 31
0
 def test_mixed_numbers_should_be_in_correct_order_in_the_tree(self):
     #          5
     #      2        9
     #    1   3    7   10
     #  0        6   8
     #
     #
     #
     bst = BinarySearchTree()
     bst.add(5)
     bst.add(2)
     bst.add(1)
     bst.add(3)
     bst.add(9)
     bst.add(10)
     bst.add(7)
     bst.add(6)
     bst.add(8)
     bst.add(0)
     self.assertEqual(bst.root.preorder_traversal(),
                      [5, 2, 1, 0, 3, 9, 7, 6, 8, 10])