Example #1
0
def unbalanced_bst():
    #build a binary search tree of seven nodes in four levels
    ubst = BinarySearchTree()
    curr_root = TreeNode(6)
    root_left = TreeNode(4)
    root_left_left = TreeNode(2)
    root_right = TreeNode(16)
    root_right_left = TreeNode(10)
    root_right_right = TreeNode(26)
    root_right_right_right = TreeNode(42)

    ubst.root_node = curr_root
    curr_root.left_child = root_left
    curr_root.left_child.left_child = root_left_left
    curr_root.right_child = root_right
    curr_root.right_child.left_child = root_right_left
    curr_root.right_child.right_child = root_right_right
    curr_root.right_child.right_child.right_child = root_right_right_right

    return ubst
Example #2
0
def balanced_tree():
    #build a binary tree of seven nodes in three levels
    bt = BinaryTree()
    curr_root = TreeNode(7)
    root_left = TreeNode(3)
    root_left_left = TreeNode(1)
    root_left_right = TreeNode(5)
    root_right = TreeNode(11)
    root_right_left = TreeNode(9)
    root_right_right = TreeNode(13)

    bt.root_node = curr_root
    curr_root.left_child = root_left
    curr_root.left_child.left_child = root_left_left
    curr_root.left_child.right_child = root_left_right
    curr_root.right_child = root_right
    curr_root.right_child.left_child = root_right_left
    curr_root.right_child.right_child = root_right_right

    return bt
Example #3
0
def simple_bst():
    #build a binary tree of three nodes in two levels
    sbst = BinarySearchTree()
    curr_root = TreeNode(3)
    root_left = TreeNode(1)
    root_right = TreeNode(7)
    curr_root.left_child = root_left
    curr_root.right_child = root_right
    sbst.root_node = curr_root
    return sbst
Example #4
0
def test_non_empty_binary_tree_fails():
    fbt = BinaryTree()
    root = TreeNode(7)
    fbt.root_node = root
    assert fbt.root_node.value != None
Example #5
0
def test_non_empty_binary_tree_passes():
    pbt = BinaryTree()
    root = TreeNode(3)
    pbt.root_node = root
    assert pbt.root_node.value == 3
Example #6
0
def test_Node_right_value(simple_tree):
    binary_node = TreeNode(7)
    expected = None
    actual = binary_node.right_child
    assert actual == expected
Example #7
0
def test_TreeNode_instantiation():
    binary_node = TreeNode(7)
    expected = 7
    actual = binary_node.value
    assert actual == expected
Example #8
0
def test_non_empty_bst_fails():
    fbst = BinarySearchTree()
    root = TreeNode(7)
    fbst.root_node = root
    assert fbst != None
Example #9
0
def test_non_empty_bst_passes():
    pbst = BinarySearchTree()
    root = TreeNode(3)
    pbst.root_node = root
    assert pbst.root_node.value == 3