def test_contains_but_value_isnt_there():
  tree = BinarySearchTree()
  tree.add(5)
  tree.add(6)
  tree.add(4)
  tree.add(3)
  assert tree.contains(99) == False
def test_add_multiple_small_values():
  tree = BinarySearchTree()
  tree.add(5)
  tree.add(4)
  tree.add(3)
  tree.add(2)
  assert tree.root.left.left.left.value == 2
def test_pre_order_traversal():
  tree = BinarySearchTree()
  tree.add(5)
  tree.add(6)
  tree.add(4)
  tree.add(3)
  assert tree.pre_order_traversal() == [5,4,3,6]
def test_in_order_traversal():
  tree = BinarySearchTree()
  tree.add(5)
  tree.add(6)
  tree.add(4)
  tree.add(3)
  assert tree.in_order_traversal() == [3,4,5,6]
def test_contains():
  tree = BinarySearchTree()
  tree.add(5)
  tree.add(6)
  tree.add(4)
  tree.add(3)
  assert tree.contains(5) == True
def test_add_multiple_right():
  tree = BinarySearchTree()
  tree.add(2)
  tree.add(3)
  tree.add(4)
  tree.add(5)
  assert tree.root.right.right.right.value == 5
Ejemplo n.º 7
0
def test_collisions():
    tree_1 = BinarySearchTree()
    for i in ['mouse', 'cat', 'dog', 'cow']:
        tree_1.add(i)
    tree_2 = BinarySearchTree()
    for i in ['cat', 'hawk', 'god', 'tiger']:
        tree_2.add(i)
    assert find_common_vals(tree_1, tree_2) == ['cat']
Ejemplo n.º 8
0
def test_diff_node_types():
    tree_1 = BinarySearchTree()
    for i in ['mouse', 'cat', 'dog', 'cow']:
        tree_1.add(i)
    tree_2 = BinarySearchTree()
    for i in ['cat', 'hawk', 'dog', 'tiger']:
        tree_2.add(i)
    assert find_common_vals(tree_1, tree_2) == ['cat', 'dog']
Ejemplo n.º 9
0
def test_tree_intersections():
    tree_1 = BinarySearchTree()
    for i in [3, 5, 7, 10, 12, 15, 18]:
        tree_1.add(i)
    tree_2 = BinarySearchTree()
    for i in [2, 5, 10, 17, 18, 19, 22]:
        tree_2.add(i)
    assert find_common_vals(tree_1, tree_2) == [5, 10, 18]
Ejemplo n.º 10
0
def test_find_common_vals_with_sets():
    tree_1 = BinarySearchTree()
    for i in [3, 5, 7, 10, 12, 15, 18]:
        tree_1.add(i)
    tree_2 = BinarySearchTree()
    for i in [2, 5, 10, 17, 18, 19, 22]:
        tree_2.add(i)
    assert set(find_common_vals_with_sets(tree_1, tree_2)) == set([5, 10, 18])
Ejemplo n.º 11
0
def test_add_many():
    t = BinarySearchTree()
    t.add('dog')
    t.add('cat')
    t.add('mouse')
    assert t.root.value == 'dog'
    assert t.root.l_child.value == 'cat'
    assert t.root.r_child.value == 'mouse'
Ejemplo n.º 12
0
def test_adding():
    test_tree = BinarySearchTree(Node(1))
    test_tree.add(2)
    test_tree.add(3)
    test_tree.add(4)
    test_tree.add(5)
    actual = test_tree.inOrder()
    expected = [1, 2, 3, 4, 5]
    assert actual == expected
def test_bst_breadth_first_2():
    bst = BinarySearchTree()
    bst.add(15)
    bst.add(10)
    bst.add(17)
    bst.add(9)
    bst.add(4)
    bst.add(6)
    bst.add(26)
    bst.add(25)
    assert bst.breadth_first() == [15, 10, 17, 9, 26, 4, 25, 6]
def test_post_order_traversal():
  tree = BinarySearchTree()
  tree.add(5)
  tree.add(6)
  tree.add(4)
  tree.add(3)
  
  """
          5
        4    6
      3  
  """ 
  assert tree.post_order_traversal() == [3,4,6,5]
Ejemplo n.º 15
0
def test_order_traversal():
    test_tree = BinarySearchTree(Node(2))
    test_tree.add(1)
    test_tree.add(3)

    actual_pre = test_tree.preOrder()
    expected_pre = [2, 1, 3]
    assert actual_pre == expected_pre

    actual_in = test_tree.inOrder()
    expected_in = [1, 2, 3]
    assert actual_in == expected_in

    actual_post = test_tree.postOrder()
    expected_post = [1, 3, 2]
    assert actual_post == expected_post
def tree():
    tree = BinarySearchTree()
    tree.add('dog')
    tree.add('cat')
    tree.add('mouse')
    tree.add('bird')
    return tree
def test_traverse_inorder():
    tree = BinarySearchTree()
    tree.add('bananas')
    tree.add('apples')
    tree.add('cucumbers')
    items = list(tree.traverse_inorder())
    assert items == ['apples', 'bananas', 'cucumbers']
Ejemplo n.º 18
0
def test_add_method_for_BinarySearch_class():
    bts = BinarySearchTree()
    bts.add(5)
    bts.add(9)
    bts.add(-2)
    bts.add(6)
    bts.add(3)
    bts.add(8)
    bts.add(5)
    assert bts.root.value == 5
    assert bts.root.left.value == -2
    assert bts.root.right.value == 9
    assert bts.root.left.right.value == 3
    assert bts.root.right.left.left.value == 5
def test_left_right_children():
    bst = BinarySearchTree()
    bst.add(10)
    bst.add(9)
    bst.add(11)
    assert bst.root.value == 10
    assert bst.root.left.value == 9
    assert bst.root.right.value == 11
def test_traverse_breadth_first():
    tree = BinarySearchTree()
    tree.add('bananas')
    tree.add('apples')
    tree.add('cucumbers')
    tree.add('cantelope')
    items = list(tree.traverse_breadth_first())
    assert items == ['bananas', 'apples', 'cucumbers', 'cantelope']
def test_traverse_for_loop():
    tree = BinarySearchTree()
    tree.add('bananas')
    tree.add('apples')
    tree.add('cucumbers')
    items = []
    for item in tree.traverse_inorder():
        items.append(item)
    assert items == ['apples', 'bananas', 'cucumbers']
def test_root_has_left():
  tree = BinarySearchTree()
  tree.add(5)
  assert tree.root.left == None
Ejemplo n.º 23
0
def test_add_one():
    t = BinarySearchTree()
    t.add(4)
    assert t.root.value == 4
def test_instantiate_tree_with_single_node():
    bst = BinarySearchTree()
    bst.add(1)
    assert bst
    assert bst.root.value == 1
def test_bst_breadth_first():
    bst = BinarySearchTree()
    bst.add(10)
    bst.add(8)
    bst.add(17)
    bst.add(23)
    bst.add(3)
    bst.add(-1)
    bst.add(50)
    bst.add(34)
    assert bst.breadth_first() == [10, 8, 17, 3, 23, -1, 50, 34]
def test_traverse_preorder():
    bst = BinarySearchTree()
    bst.add(10)
    bst.add(8)
    bst.add(17)
    bst.add(23)
    bst.add(3)
    bst.add(-1)
    bst.add(50)
    bst.add(34)
    assert bst.pre_order() == [10, 8, 3, -1, 17, 23, 50, 34]
def test_traverse_inorder():
    bst = BinarySearchTree()
    bst.add(10)
    bst.add(8)
    bst.add(17)
    bst.add(23)
    bst.add(3)
    bst.add(-1)
    bst.add(50)
    bst.add(34)
    assert bst.in_order() == [-1, 3, 8, 10, 17, 23, 34, 50]
def test_max_value():
    bst = BinarySearchTree()
    bst.add(2)
    bst.add(7)
    bst.add(5)
    bst.add(2)
    bst.add(6)
    bst.add(9)
    bst.add(5)
    bst.add(11)
    bst.add(4)
    assert bst.max_value() == 11
def test_traverse_postorder():
    bst = BinarySearchTree()
    bst.add(10)
    bst.add(8)
    bst.add(17)
    bst.add(23)
    bst.add(3)
    bst.add(-1)
    bst.add(50)
    bst.add(34)
    assert bst.post_order() == [-1, 3, 8, 34, 50, 23, 17, 10]
    assert bst.max_value() == 50
def test_traverse_inorder2():
    bst = BinarySearchTree()
    bst.add(2)
    bst.add(7)
    bst.add(5)
    bst.add(2)
    bst.add(6)
    bst.add(9)
    bst.add(5)
    bst.add(11)
    bst.add(4)
    assert bst.in_order() == [2, 2, 4, 5, 5, 6, 7, 9, 11]