Step 1: 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 2: 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. A violating node will 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 field referencing an ancestor 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.
Run the program to tell me there is an error,
BSTChecker.java:3: error: duplicate class: Node class Node { ^ BSTChecker.java:63: error: class LabProgram is public, should be declared in a file named LabProgram.java public class LabProgram { ^ 2 errors
This is my program, please fix it.
import java.util.*;
class Node {
int key;
Node left, right;
public Node(int key) {
this.key = key;
left = right = null;
}
}
class BSTChecker {
public static Node checkBSTValidity(Node root) {
return checkBSTValidityHelper(root, null, null);
}
private static Node checkBSTValidityHelper(Node node, Integer min, Integer max) {
if (node == null) {
return null;
}
if ((min != null && node.key <= min) || (max != null && node.key >= max)) {
return node;
}
Node leftResult = checkBSTValidityHelper(node.left, min, node.key);
if (leftResult != null) {
return leftResult;
}
Node rightResult = checkBSTValidityHelper(node.right, node.key, max);
if (rightResult != null) {
return rightResult;
}
return null;
}
}
public class LabProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.next();
Node root = buildTree(input);
Node result = BSTChecker.checkBSTValidity(root);
if (result == null) {
System.out.println("No violation");
}
else
{
System.out.println(result.key);
}
}
public static Node buildTree(String input) {
input = input.substring(1, input.length() - 1); // remove outermost parentheses
String[] tokens = input.split(",");
if (tokens.length == 1) {
return new Node(Integer.parseInt(tokens[0].trim()));
}
Node root = new Node(Integer.parseInt(tokens[0].trim()));
Queue<Node> queue = new LinkedList<>();
queue.add(root);
int i = 1;
while (!queue.isEmpty() && i < tokens.length) {
Node curr = queue.poll();
if (!tokens[i].trim().equals("None")) {
curr.left = new Node(Integer.parseInt(tokens[i].trim()));
queue.add(curr.left);
}
i++;
if (i >= tokens.length) {
break;
}
if (!tokens[i].trim().equals("None")) {
curr.right = new Node(Integer.parseInt(tokens[i].trim()));
queue.add(curr.right);
}
i++;
}
return root;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at LabProgram.main(LabProgram.java:7)