Пример #1
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
Пример #2
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
Пример #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