# 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 * def is_symmetric(root): queue = [] queue.append(root.left) queue.append(root.right) while queue: left = queue.pop(0) right = queue.pop(0) if not left and not right: continue if not left or not right: return False if left.data != right.data: return False queue.append(left.left) queue.append(right.right) queue.append(left.right) queue.append(right.left) return True # Driver code def main(): list_of_trees = [[TreeNode(1), TreeNode(2), TreeNode(2), TreeNode(3), TreeNode(4), TreeNode(4), TreeNode(3)], [TreeNode(18), TreeNode(21), TreeNode(21), TreeNode(47), TreeNode(20), TreeNode(21), TreeNode(47)], [TreeNode(25), TreeNode(4), TreeNode(67), TreeNode(2), TreeNode(3), TreeNode(3), TreeNode(2)], [TreeNode(1), TreeNode(2), TreeNode(2), TreeNode(3), None, None, TreeNode(3)], [TreeNode(1), TreeNode(2), TreeNode(2), None, TreeNode(3), TreeNode(3), None, TreeNode(4), TreeNode(5), TreeNode(5), TreeNode(4)]] input_trees = [] for list_of_nodes in list_of_trees: tree = BinaryTree(list_of_nodes) input_trees.append(tree) i = 0 for tree in input_trees: print(i+1,".\tInput Tree: ") display_tree(tree.root) print("\n\tResult:", is_symmetric(tree.root)) print("-"*100) i+=1 if __name__ == '__main__': main()