# 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 * import math # Function to check if a given binary tree is a valid BST or not def validate_bst(root): # Explicitly using List object to pass prev by reference because # in python, Pass-by-object-reference is used and simple variable is not an object prev = [-math.inf] return validate_bst_helper(root, prev) # Helper function for validate_bst def validate_bst_helper(root, prev): if not root: return True # If left subtree is not a valid BST, return False if not validate_bst_helper(root.left, prev): return False # If current node is less than or equal to previous node, the tree is not a valid BST if root.data <= prev[0]: return False prev[0] = root.data # Checking right subtree return validate_bst_helper(root.right, prev) # Driver code def main(): # Create a list of list of TreeNode objects to represent binary trees list_of_trees = [ [TreeNode(5), TreeNode(3), TreeNode(2), TreeNode(6), TreeNode(4)], [TreeNode(6), TreeNode(2), TreeNode(5), TreeNode(4), TreeNode(7)], [TreeNode(4), TreeNode(2), TreeNode(5), TreeNode(1), TreeNode(3)], [TreeNode(7), TreeNode(2), TreeNode(5), TreeNode(4), TreeNode(8)], [TreeNode(9), TreeNode(5), TreeNode(7), TreeNode(1), TreeNode(3)], [TreeNode(5), TreeNode(3), TreeNode(8), TreeNode(2), TreeNode(4), None, TreeNode(9)] ] # Create the binary trees using the BinaryTree class input_trees = [] for list_of_nodes in list_of_trees: tree = BinaryTree(list_of_nodes) input_trees.append(tree) # Print the input trees x = 1 for tree in input_trees: print(x, ".\tInput Tree:", sep = "") display_tree(tree.root) x += 1 print("\n\tValid BST: ", validate_bst(tree.root), sep = "") print("-" * 100) if __name__ == '__main__': main()