Beispiel #1
0
        iter_node = iter_node.parent

    node.locked = True
    iter_node = node.parent
    while iter_node:
        iter_node.num_desc_locked += 1
        iter_node = iter_node.parent
    return node.locked


def get_input(case=0):
    src = [['A'], ['B', 'I'], ['C', 'F', 'J', 'O'],
           ['D', 'E', None, 'G', None, 'K', None, 'P']]
    src.append([None] * 6 + ['H'] + [None] * 3 + ['L', 'N'] + [None] * 4)
    src.append([None] * 21 + ['M'] + [None] * 10)
    root = gen_binary_tree_breadth_first(src, need_parent=True)
    return root


def main():
    root = get_input()
    print('Test node "{}"'.format(root.left.right.data))
    print('Test success' if set_locked(root.left.right) ==
          True else 'Test failure')
    print('Test success' if is_locked(root.left.right) ==
          True else 'Test failure')
    print('Test success' if root.num_desc_locked == 1 else 'Test failure')
    print('============')

    print('Test node "{}"'.format(root.left.right.right.left.data))
    print('Test success' if set_locked(root.left.right.right.left) ==
    res = []
    node = ls.head
    while node:
        res.append(node.data.data)  # node.data is a tree node
        node = node.nxt

    return ','.join(map(lambda x: str(x), res))


def get_input(case=0):
    src = [['A'], ['B', 'I'], ['C', 'F', 'J', 'O'],
           ['D', 'E', None, 'G', None, 'K', None, 'P']]
    src.append([None] * 6 + ['H'] + [None] * 3 + ['L', 'N'] + [None] * 4)
    src.append([None] * 21 + ['M'] + [None] * 10)
    if case == 0:
        root = gen_binary_tree_breadth_first(src)
        # order = 'inorder'
        ans = 'D,E,H,M,N,P'
        return root, ans


def main():
    for case in xrange(1):
        print('--- case {} ---'.format(case))
        root, ans = get_input(case)
        res = get_leaves_as_linked_list(root)
        res_str = get_list_node_str(res)
        print('res =', res)
        print('res_str =', res_str)
        print('ans =', ans)
        print('Test success' if res_str == ans else 'Test failure')
Beispiel #3
0
        }, None, {
            'data': 'P',
            'total': 1
        }]]
 src.append([None] * 6 + [{
     'data': 'H',
     'total': 1
 }] + [None] * 3 + [{
     'data': 'L',
     'total': 2
 }, {
     'data': 'N',
     'total': 1
 }] + [None] * 4)
 src.append([None] * 21 + [{'data': 'M', 'total': 1}] + [None] * 10)
 root = gen_binary_tree_breadth_first(src, is_dict_input=True)
 # order = 'inorder'
 # The inorder sequence is 'D,C,E,B,F,H,G,A,J,L,M,K,N,I,O,P'
 k = None
 ans = None
 if case == 0:
     k = 1
     ans = root.left.left.left  # the 'D' node
 elif case == 1:
     k = 5
     ans = root.left.right  # the 'F' node
 elif case == 2:
     k = 16
     ans = root.right.right.right  # the 'P' node
 elif case == 3:
     k = 11