Question: 7 . 1 1 LAB: Modified BST validity checker Step 1 : Inspect the Node.java file Inspect the class declaration for a BST node in
LAB: Modified BST validity checker
Step : Inspect the Node.java file
Inspect the class declaration for a BST node in Node.java. Access Node.java by clicking on the orange arrow next to LabProgram.java at the
top of the coding window. Each node has a key, a left child reference, and a right child reference.
Step : Implement the BSTChecker.checkBSTValidity method
Implement the checkBSTValidity method in the BSTChecker class in the BSTChecker.java file. The method takes the tree's root node as a
parameter and returns the node that violates BST requirements, or null if the tree is a valid BST This method must be implemented non
recursively, and it cannot make any calls to recursive methods.
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 Fxs key
Xs left or right child references an ancestor
Note: Other types of BST violations can occur, but are not covered in this lab. For this lab, only check for the violations listed above and not
any others.
The given code in LabProgram.java 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 BSTChecker.checkBSTValidity method is called and the returned node's key, or No violation", is printed.
Fx
If the input is:
None,
which corresponds to the tree above, then the output is:
because violates BST requirements by being in the left subtree of
Ex:which corresponds to the tree above, then the output is:
because violates BST requirements by being in the left subtree of
c
If the input is:
which corresponds to the tree above, then the output is:
No violation
because all BST requirements are met.
The input format doesn't allow creating a tree with a node's child referencing an ancestor, so unit tests are used to test such cases.
Step : Preorder traversal
Somewhere in your code, you must use a preorder traversal to visit all the nodes in the tree. Do so in a natural and meaningful way, eg do
a preorder traversal of the tree to check if a child of a node X references an ancestor. Have a comment block preceding the code that does
this in which you must briefly explain why your nonrecursive code carries out a preorder traversal.
Step : Ensure that every method has at most lines of code
To satisfy this requirement, you may have to add private helper methods and call them appropriately. However, no methods in this lab can
be recursive. If you do add extra methods, then they must be private. Here is the code what I have right now."import java.util.;
public class BSTChecker
public static Node checkBSTValidityNode rootNode
return checkBSTValidityrootNode null, null;
private static Node checkBSTValidityNode node, Integer min, Integer max
if node null
return null;
if min null && node.key minmax null && node.key max
return node;
Node leftViolation checkBSTValiditynodeleft, min, node.key;
Node rightViolation checkBSTValiditynoderight, node.key, max;
return leftViolation null leftViolation : rightViolation;
public void preOrderTraversalNode root
if root null
return;
Stack stack new Stack;
stack.pushroot;
while stack.isEmpty
Node current stack.pop;
processNodecurrent;
if currentright null
stack.pushcurrentright;
if currentleft null
stack.pushcurrentleft;
private void processNodeNode node
if nodeleft null && node.left.key node.key
System.out.printlnViolation found: Left child of node node.key references an ancestor.";
if noderight null && node.right.key node.key
System.out.printlnViolation found: Right child of node node.key references an ancestor.";
Can only change the BSTChecker.java file. This is where I don't have a pass:Unit test
Invalid tree with right child linking to ancestor
:Unit test
Invalid tree with left child linking to parent
Test feedback
checkBSTValidity returned a node that is not the BST ruleviolating no
:Unit test
Invalid tree with left child linking to ancestor Can anyone please help me with these issues based on my code? Please attach the full changed code. Thank you! Also, the other two java file cannot be changed: LabProgram.java, and Node.java
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
