예제 #1
0
def construct_tree(root, arr, index):
    if index == len(arr):
        return index
    if arr[index] is None:
        index += 1
    else:
        root.left_node = BiNode(data=arr[index])
        index = construct_tree(root.left_node, arr, index + 1)

    if arr[index] is None:
        index += 1
    else:
        root.right_node = BiNode(data=arr[index])
        index = construct_tree(root.right_node, arr, index + 1)
    return index
예제 #2
0
def test():
  result = []
  weave(result, [], [1,2], 0, [3,4,5], 0)
  print('PASS' if result == [
    [1,2,3,4,5],
    [1,3,2,4,5],
    [1,3,4,2,5],
    [1,3,4,5,2],
    [3,1,2,4,5],
    [3,1,4,2,5],
    [3,1,4,5,2],
    [3,4,1,2,5],
    [3,4,1,5,2],
    [3,4,5,1,2],
  ] else 'FAIL')

  root = BiNode(
    data=8,
    left_node=BiNode(data=2),
    right_node=BiNode(data=10),
  )
  print('PASS' if BST_sequences_helper(root) == [[8,2,10], [8,10,2]] else 'FAIL')
  
  root = BiNode(
    data=8,
    left_node=BiNode(data=6),
    right_node=BiNode(
      data=12,
      left_node=BiNode(data=10),
      right_node=BiNode(
        data=14,
        right_node=BiNode(data=16),
      ),
    ),
  )
  print('PASS' if BST_sequences_helper(root) == [
    [8,6,12,10,14,16],
    [8,12,6,10,14,16],
    [8,12,10,6,14,16],
    [8,12,10,14,6,16],
    [8,12,10,14,16,6],
    [8,6,12,14,10,16],
    [8,12,6,14,10,16],
    [8,12,14,6,10,16],
    [8,12,14,10,6,16],
    [8,12,14,10,16,6],
    [8,6,12,14,16,10],
    [8,12,6,14,16,10],
    [8,12,14,6,16,10],
    [8,12,14,16,6,10],
    [8,12,14,16,10,6],
  ] else 'FAIL')
예제 #3
0
def test():
    arr = [1]
    print('PASS' if is_identical(reverse_preorder_traversal(arr), BiNode(
        data=1)) else 'FAIL')

    arr = [1, 5, 6, None, None, None, 4, None, 7, None, None]
    T = BiNode(data=1,
               left_node=BiNode(
                   data=5,
                   left_node=BiNode(data=6),
               ),
               right_node=BiNode(
                   data=4,
                   right_node=BiNode(data=7),
               ))
    print(
        'PASS' if is_identical(reverse_preorder_traversal(arr), T) else 'FAIL')
예제 #4
0
def reverse_preorder_traversal(arr):
    if not arr:
        return None
    root = BiNode(data=arr[0])
    construct_tree(root, arr, 1)
    return root
예제 #5
0
def test():
    t = None
    print('PASS' if check_balanced(t) else 'FAIL')

    t = BiNode(data=6)
    print('PASS' if check_balanced(t) else 'FAIL')

    t = BiNode(data=6,
               left_node=BiNode(data=7),
               right_node=BiNode(data=8, right_node=BiNode(data=9)))
    print('PASS' if check_balanced(t) else 'FAIL')

    t = BiNode(data=6,
               left_node=BiNode(data=7),
               right_node=BiNode(data=9,
                                 left_node=BiNode(data=10,
                                                  right_node=BiNode(data=11)),
                                 right_node=BiNode(data=12)))
    print('PASS' if not check_balanced(t) else 'FAIL')

    t = BiNode(data=6,
               left_node=BiNode(data=7),
               right_node=BiNode(data=9,
                                 left_node=BiNode(data=10),
                                 right_node=BiNode(
                                     data=12,
                                     left_node=BiNode(
                                         data=15, left_node=BiNode(data=11)))))
    print('PASS' if not check_balanced(t) else 'FAIL')
def test():
    root = None
    print('PASS' if paths_with_sum(root, 0) == 0 else 'FAIL')

    root = BiNode(data=10,
                  left_node=BiNode(
                      data=5,
                      left_node=BiNode(
                          data=0,
                          left_node=BiNode(data=5),
                          right_node=BiNode(data=5),
                      ),
                      right_node=BiNode(data=5),
                  ),
                  right_node=BiNode(
                      data=-4,
                      left_node=BiNode(
                          data=20,
                          left_node=BiNode(
                              data=-6,
                              right_node=BiNode(data=8),
                          ),
                          right_node=BiNode(data=10),
                      ),
                      right_node=BiNode(data=5),
                  ))
    print('PASS' if paths_with_sum(root, 10) == 6 else 'FAIL')

    root = BiNode(data=5,
                  left_node=BiNode(
                      data=5,
                      left_node=BiNode(
                          data=0,
                          left_node=BiNode(
                              data=-1,
                              left_node=BiNode(
                                  data=1,
                                  right_node=BiNode(data=3),
                              ),
                          ),
                          right_node=BiNode(data=-5),
                      ),
                      right_node=BiNode(data=5),
                  ),
                  right_node=BiNode(
                      data=10,
                      left_node=BiNode(
                          data=0,
                          right_node=BiNode(data=2),
                      ),
                      right_node=BiNode(data=-5),
                  ))
    print('PASS' if paths_with_sum(root, 10) == 7 else 'FAIL')
예제 #7
0
def test():
    T1 = BiNode(data=5,
                left_node=BiNode(
                    data=9,
                    left_node=BiNode(
                        data=2,
                        left_node=BiNode(data=1),
                    ),
                    right_node=BiNode(data=11),
                ),
                right_node=BiNode(
                    data=8,
                    left_node=BiNode(data=7),
                    right_node=BiNode(
                        data=10,
                        right_node=BiNode(data=0),
                    ),
                ))
    print('PASS' if check_subtree(T1, T1) else 'FAIL')

    T2 = BiNode(
        data=9,
        left_node=BiNode(
            data=2,
            left_node=BiNode(data=1),
        ),
        right_node=BiNode(data=11),
    )
    print('PASS' if check_subtree(T1, T2) else 'FAIL')

    T2 = BiNode(
        data=8,
        left_node=BiNode(data=7),
        right_node=BiNode(
            data=10,
            right_node=BiNode(data=11),
        ),
    )
    print('PASS' if not check_subtree(T1, T2) else 'FAIL')

    T2 = BiNode(
        data=8,
        left_node=BiNode(data=7),
        right_node=BiNode(data=10),
    )
    print('PASS' if not check_subtree(T1, T2) else 'FAIL')

    T2 = BiNode(data=0)
    print('PASS' if check_subtree(T1, T2) else 'FAIL')

    T2 = BiNode(data=10)
    print('PASS' if not check_subtree(T1, T2) else 'FAIL')
예제 #8
0
def is_identical_test():
    T1 = BiNode(data=5,
                left_node=BiNode(data=9),
                right_node=BiNode(
                    data=8,
                    left_node=BiNode(data=7),
                ))
    print('PASS' if is_identical(T1, T1) else 'FAIL')

    T2 = BiNode(data=5,
                left_node=BiNode(data=9),
                right_node=BiNode(
                    data=8,
                    left_node=BiNode(data=10),
                ))
    print('PASS' if not is_identical(T1, T2) else 'FAIL')

    T2 = BiNode(data=5,
                left_node=BiNode(data=9),
                right_node=BiNode(
                    data=8,
                    right_node=BiNode(data=10),
                ))
    print('PASS' if not is_identical(T1, T2) else 'FAIL')

    T2 = BiNode(data=5,
                left_node=BiNode(data=9),
                right_node=BiNode(
                    data=8,
                    left_node=BiNode(
                        data=7,
                        left_node=BiNode(data=7),
                    ),
                ))
    print('PASS' if not is_identical(T1, T2) else 'FAIL')
예제 #9
0
def test():
    t = None
    print('PASS' if validate_BST(t) else 'FAIL')
    print('PASS' if validate_BST_alternative(t) else 'FAIL')

    t = BiNode(data=6)
    print('PASS' if validate_BST(t) else 'FAIL')
    print('PASS' if validate_BST_alternative(t) else 'FAIL')

    t = BiNode(data=6,
               left_node=BiNode(data=7),
               right_node=BiNode(data=8, right_node=BiNode(data=7)))
    print('PASS' if not validate_BST(t) else 'FAIL')
    print('PASS' if not validate_BST_alternative(t) else 'FAIL')

    t = BiNode(data=6,
               left_node=BiNode(data=7),
               right_node=BiNode(data=9,
                                 left_node=BiNode(data=5,
                                                  right_node=BiNode(data=10)),
                                 right_node=BiNode(data=12)))
    print('PASS' if not validate_BST(t) else 'FAIL')
    print('PASS' if not validate_BST_alternative(t) else 'FAIL')

    t = BiNode(data=10,
               left_node=BiNode(data=2),
               right_node=BiNode(data=15,
                                 left_node=BiNode(data=13),
                                 right_node=BiNode(data=17,
                                                   left_node=BiNode(data=16))))
    print('PASS' if validate_BST(t) else 'FAIL')
    print('PASS' if validate_BST_alternative(t) else 'FAIL')
def test():
    node1, node2 = BiNode(data=4), BiNode(data=4)
    print('PASS'
          if first_common_ancestor(None, node1, node2) is None else 'FAIL')
    print(
        'PASS' if first_common_ancestor(node1, None, None) is None else 'FAIL')
    print('PASS'
          if first_common_ancestor(node1, None, node2) is node2 else 'FAIL')
    print('PASS'
          if first_common_ancestor(node1, node1, None) is node1 else 'FAIL')

    result = BiNode(
        data=8,
        left_node=node1,
        right_node=node2,
    )
    root = BiNode(data=6,
                  left_node=result,
                  right_node=BiNode(
                      data=10,
                      left_node=BiNode(data=5),
                      right_node=BiNode(data=5),
                  ))
    print('PASS'
          if first_common_ancestor(root, node1, node2) is result else 'FAIL')

    root = BiNode(data=6,
                  left_node=BiNode(
                      data=11,
                      left_node=node1,
                      right_node=BiNode(data=10),
                  ),
                  right_node=BiNode(
                      data=10,
                      left_node=BiNode(data=5),
                      right_node=node2,
                  ))
    print('PASS'
          if first_common_ancestor(root, node1, node2) is root else 'FAIL')

    node1 = BiNode(data=4)
    node2 = BiNode(
        data=4,
        left_node=node1,
    )
    root = BiNode(data=6,
                  left_node=node2,
                  right_node=BiNode(
                      data=10,
                      left_node=BiNode(data=5),
                      right_node=BiNode(data=5),
                  ))
    print('PASS'
          if first_common_ancestor(root, node1, node2) is node2 else 'FAIL')
    print('PASS' if first_common_ancestor(root, node1, BiNode(
        data=8)) is None else 'FAIL')
    print('PASS' if first_common_ancestor(root, BiNode(data=2), BiNode(
        data=8)) is None else 'FAIL')
def test():
    t = None
    print('PASS' if in_order_successor(t) is None else 'FAIL')

    t = BiNode(data=6)
    print('PASS' if in_order_successor(t) is None else 'FAIL')

    result_node = BiNode(data=1)
    t = BiNode(data=6,
               right_node=BiNode(
                   data=3,
                   left_node=BiNode(
                       data=2,
                       left_node=result_node,
                   ),
               ))
    print('PASS' if in_order_successor(t) is result_node else 'FAIL')

    grandparent = BiNode(data=5)
    parent = BiNode(data=2, parent=grandparent)
    son = BiNode(data=1, parent=parent)
    print('PASS' if in_order_successor(son) is parent else 'FAIL')

    grandparent = BiNode(data=2)
    parent = BiNode(data=5, parent=grandparent)
    son = BiNode(data=3, parent=parent)
    print('PASS' if in_order_successor(son) is parent else 'FAIL')

    result_node = BiNode(data=1)
    parent = BiNode(data=2, parent=result_node)
    son = BiNode(data=3, parent=parent)
    print('PASS' if in_order_successor(son) is None else 'FAIL')

    result_node = BiNode(data=5)
    parent = BiNode(data=2, parent=result_node)
    son = BiNode(data=3, parent=parent)
    print('PASS' if in_order_successor(son) is result_node else 'FAIL')