示例#1
0
def test_delete_root():
    tree = BinaryTree()
    tree.insert(5)
    tree.insert(1)
    tree.insert(10) # 1 - (5) - 10
    tree.delete(5) # 1 - (10)
    assert tree.root.value == 10
    assert tree.root.right is None
    tree.delete(10) # (1)
    assert tree.root.value == 1
    assert tree.root.left is None
示例#2
0
def test_find_bigger_and_delete():
    tree = BinaryTree()
    tree.insert(5)
    tree.insert(10)
    assert tree.root.right.value == 10
    tree.delete(10)
    assert tree.root.right is None
    assert tree.find(10) == False
示例#3
0
def test_delete_left_has_children():
    tree = BinaryTree()
    tree.insert(5)
    # left
    tree.insert(3)
    tree.insert(2)
    tree.insert(4) # 2 - (3) - 4

    tree.delete(3) # 2 - (4)
    assert tree.root.left.value == 4
    assert tree.root.left.left.value == 2
    assert tree.root.left.right is None
示例#4
0
def test_find_smaller_and_delete():
    tree = BinaryTree()
    tree.insert(5)
    tree.insert(1)
    assert tree.root.left.value == 1
    tree.delete(1)
    assert tree.root.left is None
    assert tree.find(1) == False
示例#5
0
def test_delete_not_root_has_children_min_value_node_is_not_leaf():
    tree = BinaryTree()
    tree.insert(10)
    tree.insert(5)

    tree.insert(8)
    tree.insert(6)
    tree.insert(7)
    tree.insert(9) # x - (8) - 9, x -> (6) - 7

    tree.delete(5) # 7 - (8) - 9
    assert tree.root.left.value == 6
    assert tree.root.left.left is None
    assert tree.root.left.right.value == 8
    assert tree.root.left.right.left.value == 7
示例#6
0
def test_delete_not_root_has_children_min_value_node_is_leaf():
    tree = BinaryTree()
    tree.insert(10)
    tree.insert(5)

    tree.insert(7)
    tree.insert(6)
    tree.insert(8) # 6 - (7) - 8

    tree.delete(5) # (6) - 7 - 8
    assert tree.root.left.value == 6
    assert tree.root.left.left is None
    assert tree.root.left.right.value == 7
示例#7
0
def test_find():
    tree = BinaryTree()
    tree.insert(0)
    assert tree.empty() == False
    assert tree.find(0) == True
示例#8
0
def test_delete_right_has_children():
    tree = BinaryTree()
    tree.insert(5)
    # right
    tree.insert(10)
    tree.insert(9)
    tree.insert(11) # 9 - (10) - 11

    tree.delete(10) # 9 - (11)
    assert tree.root.right.value == 11
    assert tree.root.right.left.value == 9
    assert tree.root.right.right is None
示例#9
0
def test_empty():
    tree = BinaryTree()
    assert tree.empty() == True
    assert tree.find(0) == False
示例#10
0
def test_find_after_deleted():
    tree = BinaryTree()
    tree.insert(0)
    tree.delete(0)
    assert tree.empty() == True
    assert tree.find(0) == False