Esempio n. 1
0
def test_level_traversal():
    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert [1, 2, 3, 5, 6, 7] == level_traversal(node)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert [100, 5, 6, 7] == level_traversal(node)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] == level_traversal(node)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert [1, 2, 3, 7] == level_traversal(node)

    node = create_tree([])
    assert [] == level_traversal(node)
Esempio n. 2
0
def test_non_recursive_traversal():
    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert [1, 2, 5, 3, 6, 7] == preorder(node)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert [100, 5, 6, 7] == preorder(node)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert [10, 9, 7, 3, 2, 6, 1, 0, 8, 5, 4] == preorder(node)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert [1, 2, 3, 7] == preorder(node)

    node = create_tree([])
    assert [] == preorder(node)
Esempio n. 3
0
def test_maximum_width_size():
    node = create_tree([1, 2, None])
    assert 1 == get_width_size(node)

    node = create_tree([1, 2, 3])
    assert 2 == get_width_size(node)

    node = create_tree([1, 2, 3, 4, None, 5, None, 6])
    assert 3 == get_width_size(node)

    node = create_tree([
        1, 2, 3, None, 4, 5, 6, None, None, 7, None, None, None, None, 8, None,
        None, None, None, 9
    ])
    assert 6 == get_width_size(node)
def test_sample():
    l = [int(x) for x in '2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2'.split()]
    root = create_tree(l)
    print(root)
    for n in root.sons:
        print(n)
        for niece in n.sons:
            print(niece)
def test_leaf_node():
    n = create_tree([0, 1, 99])
    assert n.n_sons == 0
    assert n.n_metadata == 1
    assert len(n.metadata) == 1
    assert n.metadata[0] == 99
    assert n.value() == 99
    print(n)
Esempio n. 6
0
 def fit(self, dataset, labels):
     if len(dataset) == 0 or len(labels) == 0:
         sys.stderr.write("[FATAL] your sample or label is null.\n")
         return False
     if len(dataset[0]) != len(labels) + 1:
         sys.stderr.write(
             "[FATAL] your sample and label have different columns.\n")
         return False
     if len(dataset[0]) < 3:
         sys.stderr.write(
             "[FATAL] please make sure your feature nums are more than 1.\n"
         )
     self.labels = labels
     self.tree = create_tree(dataset, labels[:])
     return True
def test_one_son():
    n = create_tree([1, 1, 0, 1, 99, 2])
    assert n.n_sons == 1
    assert len(n.sons) == 1
    son = n.sons[0]

    assert son.n_sons == 0
    assert son.n_metadata == 1
    assert len(son.metadata) == 1
    assert son.metadata[0] == 99
    assert son.n_sons == 0

    assert len(n.metadata) == 1
    assert n.metadata[0] == 2 

    print(n)
    print(n.sons[0])
def test_reversed_traversal():
    """
    level traverse 를 거꾸로 돌린 값을 리턴한다
    :return:
    """
    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert [4, 5, 6, 7, 2, 3, 1] == reversed_traversal(node)

    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert [5, 6, 7, 2, 3, 1] == reversed_traversal(node)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert [6, 7, 5, 100] == reversed_traversal(node)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert [3, 2, 1, 0, 7, 6, 5, 4, 9, 8, 10] == reversed_traversal(node)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert [7, 2, 3, 1] == reversed_traversal(node)

    node = create_tree([])
    assert [] == reversed_traversal(node)
Esempio n. 9
0
def test_level_traversal():
    """
    각 레벨마다 지그재그로 travasal하는 함수를 만든다.
    최초 root에서 왼쪽에서 우측으로 읽고 (어차피 루트하나)..
    그 다음 레벨에서 우측에서 왼쪽으로 읽고..
    그 다음 레벨에서 좌측에서 우측으로 읽고.. 반복..
    """
    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert [1, 3, 2, 5, 6, 7] == zigzag(node)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert [100, 5, 6, 7] == zigzag(node)

    node = create_tree([100, 4, 5, 3, None, 6, 7])
    assert [100, 5, 4, 3, 6, 7] == zigzag(node)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert [10, 8, 9, 7, 6, 5, 4, 0, 1, 2, 3] == zigzag(node)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert [1, 3, 2, 7] == zigzag(node)

    node = create_tree([])
    assert [] == zigzag(node)
def test_find_sum():
    """
    root에서부터 합을 해서 특정 합이 존재하는지 체크하는 함수를 만든다
    """

    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert find_sum(node, 10)

    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert find_sum(node, 1)

    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert find_sum(node, 4)

    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert find_sum(node, 8)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert find_sum(node, 105)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert find_sum(node, 111)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert find_sum(node, 112)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert not find_sum(node, 110)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert not find_sum(node, 113)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert not find_sum(node, 104)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert not find_sum(node, 0)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert find_sum(node, 10)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert find_sum(node, 19)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert find_sum(node, 18)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert find_sum(node, 26)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert find_sum(node, 29)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert find_sum(node, 23)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert not find_sum(node, 24)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert not find_sum(node, 17)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert find_sum(node, 11)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert not find_sum(node, 12)

    node = create_tree([
        5, 2, 10, None, 9, 6, 22, None, None, None, None, None, None, None, 8,
        None, None, None, None, None, None, None, None, None, None, None, None,
        None, None, 9
    ])
    assert find_sum(node, 54)
Esempio n. 11
0
def test_print_all_ancestors():
    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert [1] == get_ancestors(node, 1)

    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert [1, 3] == get_ancestors(node, 3)

    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert [1, 2] == get_ancestors(node, 2)

    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert [1, 3, 6] == get_ancestors(node, 6)

    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert [1, 3, 7] == get_ancestors(node, 7)

    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert [1, 2, 4] == get_ancestors(node, 4)

    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert [1, 2, 5] == get_ancestors(node, 5)

    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert [1, 2, 5] == get_ancestors(node, 5)

    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert None == get_ancestors(node, 17)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert [100, 5] == get_ancestors(node, 5)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert [100, 5, 7] == get_ancestors(node, 7)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert None == get_ancestors(node, 0)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert [10, 9, 6, 0] == get_ancestors(node, 0)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert [10, 9, 6, 1] == get_ancestors(node, 1)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert [10, 9, 7, 2] == get_ancestors(node, 2)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert [10, 9, 7, 3] == get_ancestors(node, 3)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert [10, 8, 4] == get_ancestors(node, 4)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert [10, 8, 5] == get_ancestors(node, 5)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert None == get_ancestors(node, -1)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert None == get_ancestors(node, 11)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert [1, 3, 7] == get_ancestors(node, 7)

    node = create_tree([
        5, 2, 10, None, 17, 6, 22, None, None, None, None, None, None, None, 8,
        None, None, None, None, None, None, None, None, None, None, None, None,
        None, None, 9
    ])
    assert [5, 10, 22, 8, 9] == get_ancestors(node, 9)

    node = create_tree([
        5, 2, 10, None, 17, 6, 22, None, None, None, None, None, None, None, 8,
        None, None, None, None, None, None, None, None, None, None, None, None,
        None, None, 9
    ])
    assert None == get_ancestors(node, 15)

    node = create_tree([])
    assert None == get_ancestors(node, 0)
def test_least_common_ancestor():
    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert 3 == lca(node, 6, 7)

    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert 1 == lca(node, 2, 7)

    node = create_tree([1, 2, 3, 4, 5, 6, 7])
    assert 2 == lca(node, 4, 5)

    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert 1 == lca(node, 2, 3)

    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert 1 == lca(node, 5, 7)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert 5 == lca(node, 6, 7)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 9 == lca(node, 3, 1)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 9 == lca(node, 3, 0)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 7 == lca(node, 3, 2)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 8 == lca(node, 4, 5)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 10 == lca(node, 2, 5)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 9 == lca(node, 1, 7)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 9 == lca(node, 2, 6)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 9 == lca(node, 7, 0)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 9 == lca(node, 1, 7)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert 1 == lca(node, 2, 7)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert 1 == lca(node, 7, 2)

    node = create_tree([])
    assert lca(node, 3, 0) is None
Esempio n. 13
0
def test_value_index_out_of_limits():
    b = create_tree([1, 1, 0, 1, 99, 2])
    assert b.value() == 0
Esempio n. 14
0
def test_value():
    n = create_tree([0, 3, 10, 11, 12])
    assert n.value() == 33
Esempio n. 15
0
def test_tree_height():
    # With Recursion
    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert 3 == recursive_size(node)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert 3 == recursive_size(node)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 4 == recursive_size(node)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert 3 == recursive_size(node)

    node = create_tree(
        [1, None, 2, None, None, 3, 4, None, None, None, None, None, None, 5])
    assert 4 == recursive_size(node)

    node = create_tree([])
    assert 0 == recursive_size(node)

    # Without Recursion
    node = create_tree([1, 2, 3, None, 5, 6, 7])
    assert 3 == non_recursive_size(node)

    node = create_tree([100, None, 5, None, None, 6, 7])
    assert 3 == non_recursive_size(node)

    node = create_tree([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
    assert 4 == non_recursive_size(node)

    node = create_tree([1, 2, 3, None, None, None, 7])
    assert 3 == non_recursive_size(node)

    node = create_tree(
        [1, None, 2, None, None, 3, 4, None, None, None, None, None, None, 5])
    assert 4 == non_recursive_size(node)

    node = create_tree([])
    assert 0 == non_recursive_size(node)
Esempio n. 16
0
#!/usr/bin/env python3

from tools import read_input, create_tree

root = create_tree(read_input('input.txt'))

solution = root.value()
print('Solution of part 2 in {}'.format(solution))
Esempio n. 17
0
def test_value_sample():
    l = [int(x) for x in '2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2'.split()]
    root = create_tree(l)
    assert root.value() == 66