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))
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)
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))
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)
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)
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))
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
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
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))
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)
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))
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)
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))