예제 #1
0
def test_in_order_traversal():
    for nodes, expected_result in [
        (
            (
                8,
                [(4,
                  [2, 6]),
                 (10,
                  [None, 20])]
            ),
            "2->4->6->8->10->20"
        ),
        (
            (1, ), "1"
        ),

        (
            (1, [2]), "2->1"
        ),

        (
            (1, [None, 2]), "1->2"
        ),
    ]:
        result = []
        def visit(node: Node) -> None:
            result.append(node.val)

        bt = BinaryTree(BinaryTree.build(nodes))
        bt.in_order(visit)

        result = "->".join([str(val) for val in result])
        assert result == expected_result, "{} != {}".format(result, expected_result)
def test_Can_successfully_return_a_collection_from_an_inorder_traversal():
    a = Node("A")  # this assigns value to the node
    b = Node("B")
    c = Node("C")
    d = Node("D")
    e = Node("E")
    tree = BinaryTree()  # this makes the binary tree
    tree.root = a
    a.left = b
    a.right = c
    a.left.left = d
    a.left.right = e
    actual = tree.in_order()
    expected = ["D", "B", "E", "A", "C"]
    assert actual == expected