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 : 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 : Implement the checkbstvalidity function
Implement the checkbstvalidity 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 Xs key is Ys key
X is in the right subtree of ancestor Y but Xs key is Ys key
Xs 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 leftchild, rightchild where leftchild and rightchild can be nested nodes or None. A leaf node is of the form key After parsing tree input, the checkbstvalidity function is called and the returned node's key or "None" is printed.
# Returns None if rootnode 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:
# A node in the left subtree of an ancestor with a lesser key
# A node in the right subtree of an ancestor with a greater key
# A node with the left or right attribute referencing an ancestor
def checkbstvalidityrootnode:
# Your code here
return rootnode
from Node import Node
from BSTChecker import checkbstvalidity
# Get user input and parse into a binary tree
userinput input
userroot Node.parseuserinput
if userroot None:
printFailed to parse input tree"
else:
badnode checkbstvalidityuserroot
if badnode None:
printstrbadnode.key
else:
printNone
class Node:
def initself key, leftNone, rightNone:
self.key key
self.left left
self.right right
# Counts the number of nodes in this tree
def countself:
leftcount
if self.left None:
leftcount self.left.count
rightcount
if self.right None:
rightcount self.right.count
return leftcount rightcount
# Inserts the new node into the tree.
def insertself node:
currentnode self
while currentnode is not None:
if node.key currentnode.key:
# If there is no left child, add the new
# node here; otherwise repeat from the
# left child.
if currentnode.left is None:
currentnode.left node
currentnode None
else:
currentnode currentnode.left
else:
# If there is no right child, add the new
# node here; otherwise repeat from the
# right child.
if currentnode.right is None:
currentnode.right node
currentnode None
else:
currentnode currentnode.right
def insertallself keys:
for key in keys:
self.insertNodekey
@staticmethod
def parsetreestr:
# 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
treestr treestrstrip
#
# The string must be nonempty, start with and end with
if treestr or treestr or treestr:
return None
# Parse between parentheses
treestr treestr:
# Find nonnested commas
commaindices
parencounter
for i in rangelentreestr:
character treestri
if character :
parencounter
elif character :
parencounter
elif character and parencounter :
commaindices.appendi
# If no commas, treestr is expected to be just the node's key
if lencommaindices:
return Nodeinttreestr
# If number of commas is not then the string's format is invalid
if lencommaindices:
return None
# "Split" on comma
i commaindices
i commaindices
pieces treestr:i treestri:i
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
