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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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;

}

}

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.
Transcribed Image Text: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.
Ex:
10
If the input is:
20
29
30
No violation
31
(20, (10), (30, (29), (31)))
which corresponds to the tree above, then the output is:
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.
Transcribed Image Text:Ex: 10 If the input is: 20 29 30 No violation 31 (20, (10), (30, (29), (31))) which corresponds to the tree above, then the output is: 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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

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)

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Threads in linked list
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education