# Display List Code Below: def height(node): if (node == None): return 0 return 1 + max(height(node.left), height(node.right)) def draw_node(output, link_above, node, level, p, link_char): if (node == None): return out = "[" h = len(output) SP = " " if (p < 0): for s in output: if (s): s = " "*(-1*p) + s for s in link_above: if (s): s = " "*(-1*p) + s if level < h - 1: p = max(p, len(output[level + 1])) if (level > 0): p = max(p, len(output[level - 1])) p = max(p, len(output[level])) # Fill in to left if (node.left): leftData = SP + str(node.left.data) + SP draw_node(output, link_above, node.left, level + 1, p - len(leftData),'L') p = max(p, len(output[level + 1])) # Enter this data space = p - len(output[level]) if (space > 0): output[level] += (' ' * space) node_data = SP + str(node.data) + SP output[level] += node_data # Add vertical link above space = p + len(SP) - len(link_above[level]) if (space > 0): link_above[level] += (' ' * space) link_above[level] += link_char # Fill in to right if (node.right): draw_node(output, link_above, node.right, level + 1, len(output[level]),'R') def display_tree(root): if (root == None): print("\tNone") h = height(root) output = [] link_above = [] for i in range(0,h): output.append("") link_above.append("") draw_node(output, link_above, root, 0, 5, ' ') # Create link lines for i in range(h): for j in range(len(link_above[i])): if (link_above[i][j] != ' '): size = len(output[i - 1]) if (size < j + 1): output[i - 1] += " " *(j + 1 - size) jj = j if (link_above[i][j] == 'L'): while (output[i - 1][jj] == ' '): jj += 1 for k in range(j+1, jj-1): str1 = output[i - 1] list1 = list(str1) list1[k] = '_' output[i - 1] = ''.join(list1) elif(link_above[i][j] == 'R'): while (output[i - 1][jj] == ' '): jj-=1 # k = j-1 for k in range(j-1,jj+1,-1): temp = output[i - 1] list1 = list(temp) list1[k] = '_' output[i - 1] = ''.join(list1) k = k - 1 str1 = link_above[i] list1 = list(str1) list1[j] = '|' link_above[i] = ''.join(list1) # Output for i in range(h): if (i): print("\t" , link_above[i]) print("\t" , output[i]) from TreeNode import * from BinaryTree import * from collections import deque def find_max_depth(root): # Check if the root is None if not root: return 0 # Initialize a nodes_stack with the root node and its depth nodes_stack = deque([(root, 1)]) # Initialize max_depth to 0 max_depth = 0 # Loop while the nodes_stack is not empty while nodes_stack: # Pop the top item from the nodes_stack node, depth = nodes_stack.pop() # If the node has a left child, add it to the nodes_stack with its depth if node.left: nodes_stack.append((node.left, depth + 1)) # If the node has a right child, add it to the nodes_stack with its depth if node.right: nodes_stack.append((node.right, depth + 1)) # If the node is a leaf node, update max_depth if necessary if not node.left and not node.right: max_depth = max(max_depth, depth) # Return the maximum depth return max_depth # Driver code def main(): input = [ [TreeNode(1), TreeNode(2), TreeNode(3), None, None, TreeNode(4), TreeNode(5)], [TreeNode(1), TreeNode(2), None, TreeNode(3), None, TreeNode(4)], [TreeNode(1), TreeNode(2), TreeNode(3), TreeNode(4), TreeNode(5), TreeNode(6), TreeNode(7), TreeNode(8)], [TreeNode(1), TreeNode(2), TreeNode(3), TreeNode(4), TreeNode(5), TreeNode(6), None, TreeNode(8)], [TreeNode(1), TreeNode(2), None, TreeNode(3), TreeNode(4)] ] # Create the binary trees using the BinaryTree class input_trees = [] for list_of_nodes in input: tree = BinaryTree(list_of_nodes) input_trees.append(tree) for i in range(len(input_trees)): root = input_trees[i].root print(str(i + 1) + ". Binary tree:") display_tree(root) print("\n Maximum Depth: " + str(find_max_depth(root))) print("-" * 100, "\n", sep="") if __name__ == "__main__": main()