Пример #1
0
def parse_number(number):
    root = Node(-1)
    current = root

    for char in number:
        if char == '[':
            current.left = Node(-1)
            current = current.left
        elif char == ']':
            current = get_parent(root, current)
        elif char == ',':
            current = get_parent(root, current)
            current.right = Node(-1)
            current = current.right
        else:
            current.value = int(char)

    return root
Пример #2
0
def test_get_parent():
    root = Node(0)
    root.left = Node(1)
    root.right = Node(2)
    root.left.left = Node(3)
    root.right.right = Node(4)

    assert get_parent(root, root.left.left) == root.left
    assert get_parent(root, root.left) == root
    assert get_parent(root, root) is None
    assert get_parent(root, root.right.right) == root.right
    assert get_parent(root, root.right) == root
    assert get_parent(root, Node(5)) is None
    assert get_parent(None, root.left) is None
    assert get_parent(root, None) is None
Пример #3
0
def backup(node, terminalValue, height, backdown):
    node.tNode = node.tNode + terminalValue
    node.numberVisits = node.numberVisits + 1
    if node.height == height:
        return node, backdown
    else:
        parent = bt.get_parent(root, node)
        # Saves path so path root -> this node remembered, for multiple rollouts
        if parent.left == node:
            #TODO implemente FILO structure
            backdown.insert(0, 'left')
        else:
            backdown.insert(0, 'right')
        return backup(parent, terminalValue, height, backdown)
Пример #4
0
Файл: 18a.py Проект: df7cb/aoc
def explode(root):
    for node in root.inorder:
        if node.value >= 0 and leaf_depth(root, node) > 4:
            parent = get_parent(root, node)
            parent.value = 0
            ln = left_neighbor(root, parent.left)
            if ln:
                ln.value += parent.left.value
            rn = right_neighbor(root, parent.right)
            if rn:
                rn.value += parent.right.value
            parent.left = None
            parent.right = None
            return True
    return False
Пример #5
0
Файл: 18a.py Проект: df7cb/aoc
def leaf_depth(root, node):
    if root == node:
        return 0
    return 1 + leaf_depth(root, get_parent(root, node))