Question: Step 1 : Inspect the Node.py file Inspect the class declaration for a BST node in Node.py . Access Node.py by clicking on the orange

Step 1: Inspect the Node.py file
Inspect the class declaration for a BST node in Node.py. Access Node.py by clicking on the orange arrow next to main.py at the top of the coding window. Each node has a key, a left child reference, and a right child reference.
Step 2: Implement the check_bst_validity() function
Implement the check_bst_validity() function in the BSTChecker.py file. The function takes the tree's root node as a parameter and returns the node that violates BST requirements, or None if the tree is a valid BST.
A violating node X will meet one or more of the following conditions:
X is in the left subtree of ancestor Y, but X's key is > Y's key
X is in the right subtree of ancestor Y, but X's key is < Y's key
X's left or right child rererences an ancestor
Note: Other types of BST violations can occur, but are not covered in this lab.
The given code in main.py reads and parses input, and builds the tree for you. Nodes are presented in the form (key, left_child, right_child), where left_child and right_child can be nested nodes or None. A leaf node is of the form (key). After parsing tree input, the check_bst_validity() function is called and the returned node's key (or "None") is printed.
# Returns None if root_node is the root of a valid BST.
# If a BST violation occurs, the node causing the violation is returned. Such a
# node may be one of three things:
# 1. A node in the left subtree of an ancestor with a lesser key
# 2. A node in the right subtree of an ancestor with a greater key
# 3. A node with the left or right attribute referencing an ancestor
def check_bst_validity(root_node):
# Your code here
return root_node
from Node import Node
from BSTChecker import check_bst_validity
# Get user input and parse into a binary tree
user_input = input()
user_root = Node.parse(user_input)
if user_root == None:
print("Failed to parse input tree")
else:
bad_node = check_bst_validity(user_root)
if bad_node != None:
print(str(bad_node.key))
else:
print("None")
class Node:
def __init__(self, key, left=None, right=None):
self.key = key
self.left = left
self.right = right
# Counts the number of nodes in this tree
def count(self):
left_count =0
if self.left != None:
left_count = self.left.count()
right_count =0
if self.right != None:
right_count = self.right.count()
return 1+ left_count + right_count
# Inserts the new node into the tree.
def insert(self, node):
current_node = self
while current_node is not None:
if node.key < current_node.key:
# If there is no left child, add the new
# node here; otherwise repeat from the
# left child.
if current_node.left is None:
current_node.left = node
current_node = None
else:
current_node = current_node.left
else:
# If there is no right child, add the new
# node here; otherwise repeat from the
# right child.
if current_node.right is None:
current_node.right = node
current_node = None
else:
current_node = current_node.right
def insert_all(self, keys):
for key in keys:
self.insert(Node(key))
@staticmethod
def parse(tree_str):
# A node is enclosed in parentheses with a either just a key: (key), or
# a key, left child, and right child triplet: (key, left, right). The
# left and right children, if present, can be either a nested node or
# "None".
#
# Remove whitespace on ends first
tree_str = tree_str.strip()
#
# The string must be non-empty, start with "(", and end with ")"
if tree_str =="" or tree_str[0]!="(" or tree_str[-1]!=")":
return None
# Parse between parentheses
tree_str = tree_str[1:-1]
# Find non-nested commas
comma_indices =[]
paren_counter =0
for i in range(len(tree_str)):
character = tree_str[i]
if character =='(':
paren_counter +=1
elif character ==')':
paren_counter -=1
elif character ==',' and paren_counter ==0:
comma_indices.append(i)
# If no commas, tree_str is expected to be just the node's key
if len(comma_indices)==0:
return Node(int(tree_str))
# If number of commas is not 2, then the string's format is invalid
if len(comma_indices)!=2:
return None
# "Split" on comma
i1= comma_indices[0]
i2= comma_indices[1]
pieces =[tree_str[0:i1], tree_str[i1+1:i2],

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!