コード例 #1
0
def main():
    root = Node(10)
    root.left_child = Node(20)
    root.right_child = Node(30)
    root.right_child.left_child = Node(50)
    root.right_child.right_child = Node(60)
    root.left_child.left_child = Node(40)
    root.left_child.left_child.left_child = Node(80)
    print(max_width(root))
コード例 #2
0
def main():
    root = Node(10)
    root.left_child = Node(6)
    root.right_child = Node(8)
    root.right_child.right_child = Node(7)
    root.right_child.right_child.left_child = Node(11)
    root.right_child.right_child.right_child = Node(12)
    nodes_k(root, 3)
コード例 #3
0
def main():
    root = Node(10)
    root.left_child = Node(50)
    root.right_child = Node(60)
    root.left_child.right_child = Node(20)
    root.left_child.left_child = Node(70)
    root.right_child.left_child = Node(8)
    print(count_nodes(root))
コード例 #4
0
def main():
    root = Node(10)
    root.left_child = Node(20)
    root.right_child = Node(30)
    root.left_child.right_child = Node(50)
    root.left_child.left_child = Node(40)
    root.right_child.left_child = Node(60)
    iter_pre(root)
コード例 #5
0
def main():
    root = Node(10)
    root.left_child = Node(50)
    root.right_child = Node(60)
    root.right_child.right_child = Node(20)
    root.right_child.left_child = Node(70)
    root.right_child.left_child.right_child = Node(8)
    left_view(root)
コード例 #6
0
def main():
    root = Node(10)
    root.left_child = Node(50)
    root.right_child = Node(60)
    root.left_child.right_child = Node(20)
    root.left_child.left_child = Node(70)
    root.right_child.left_child = Node(8)
    arr = [root.value]
    print(serialize(root, arr))
    deserialize(serialize(root, arr))
コード例 #7
0
def construct_tree(in_ord, pr_ord, i_s, i_e):
    if i_s > i_e:
        return None

    # Assign Root as the first element in the pre-order traversal
    root = Node(pr_ord[construct_tree.pre_index])
    construct_tree.pre_index += 1
    # Find where root is present in the in-order traversal and then divide the array into
    # left and right child and then repeat the process
    if i_s == i_e:
        return root
    cut = search(in_ord, i_s, i_e, root.value)
    root.left_child = construct_tree(in_ord, pr_ord, i_s, cut - 1)
    root.right_child = construct_tree(in_ord, pr_ord, cut + 1, i_e)
    return root
コード例 #8
0
def deserialize(arr_in):
    root_n = Node(arr_in[0])
    i = 1
    deserialize.q1.append(root_n)
    print(root_n.value)
    while i < len(arr_in):
        curr_node = deserialize.q1.popleft()
        if arr_in[i] == " ":
            curr_node.left_child = None
        else:
            t_l = Node(arr_in[i])
            curr_node.left_child = t_l
            deserialize.q1.append(t_l)
        i += 1
        if arr_in[i] == " ":
            curr_node.right_child = None
        else:
            t_r = Node(arr_in[i])
            curr_node.right_child = t_r
            deserialize.q1.append(t_r)
        i += 1
    return root_n
コード例 #9
0
def main():
    root = Node(10)
    root.left_child = Node(20)
    root.right_child = Node(30)
    root.right_child.left_child = Node(40)
    root.right_child.right_child = Node(50)
    root.right_child.left_child.left_child = Node(60)
    root.right_child.right_child.left_child = Node(70)
    root.right_child.right_child.right_child = Node(80)
    print(lca_bin(root, 60, 70))
    print(lca_eff(root, 60, 70))
コード例 #10
0
def main():
    root = Node(10)
    root.left_child = Node(20)
    root.right_child = Node(30)
    root.right_child.left_child = Node(40)
    root.right_child.right_child = Node(50)
    root.right_child.left_child.right_child = Node(70)
    root.right_child.left_child.left_child = Node(60)
    root.right_child.right_child.right_child = Node(80)
    print_spiral(root)
コード例 #11
0
def main():
    root = Node(10)
    root.left_child = Node(20)
    root.right_child = Node(30)
    root.right_child.right_child = Node(60)
    root.left_child.left_child = Node(40)
    root.left_child.right_child = Node(50)
    root.right_child.right_child.left_child = Node(70)
    root.right_child.right_child.right_child = Node(80)
    print(size_of_t_rec(root))
コード例 #12
0
def main():
    root = Node(10)
    root.left_child = Node(20)
    root.right_child = Node(30)
    root.right_child.right_child = Node(60)
    root.left_child.left_child = Node(40)
    root.left_child.right_child = Node(50)
    root.right_child.right_child.left_child = Node(70)
    root.right_child.right_child.right_child = Node(80)
    level_oder_trv(root)
コード例 #13
0
def main():
    root = Node(30)
    root.left_child = Node(40)
    root.right_child = Node(80)
    root.right_child.left_child = Node(5)
    root.left_child.left_child = Node(50)
    root.left_child.right_child = Node(70)
    root.left_child.right_child.left_child = Node(20)
    root.left_child.right_child.right_child = Node(10)
    print(is_balanced(root))