예제 #1
0
def simple_tree():
    #build a binary tree of three nodes in two levels
    st = BinaryTree()
    curr_root = TreeNode(3)
    root_left = TreeNode(1)
    root_right = TreeNode(7)
    curr_root.left_child = root_left
    curr_root.right_child = root_right
    st.root_node = curr_root
    return st
예제 #2
0
def unbalanced_tree():
    #build a binary tree of seven nodes in four levels
    ut = BinaryTree()
    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)

    ut.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 ut
예제 #3
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
예제 #4
0
def test_empty_tree_instantiates():
    tree = BinaryTree()
    expected = None
    actual = tree.root_node
    assert actual == expected
예제 #5
0
def test_non_empty_binary_tree_fails():
    fbt = BinaryTree()
    root = TreeNode(7)
    fbt.root_node = root
    assert fbt.root_node.value != None
예제 #6
0
def test_non_empty_binary_tree_passes():
    pbt = BinaryTree()
    root = TreeNode(3)
    pbt.root_node = root
    assert pbt.root_node.value == 3
예제 #7
0
def test_empty_binary_tree_fails():
    febt = BinaryTree()
    assert febt != 7
예제 #8
0
def test_empty_binary_tree_instantiates():
    et = BinaryTree()
    assert et.root_node == None