Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util. * ; public class LabProgram { public static void main ( String [ ] args ) { / / Get user input Scanner
import java.util.;
public class LabProgram
public static void mainString args
Get user input
Scanner reader new ScannerSystemin;
String userInput reader.nextLine;
Parse into a binary tree
Node userRoot Node.parseuserInput;
if userRoot null
Node badNode BSTChecker.checkBSTValidityuserRoot;
if badNode null
System.out.printStringvalueOfbadNodekey;
System.out.print
;
else
System.out.printNo violation";
System.out.print
;
else
System.out.printFailed to parse input tree";
System.out.print
;
reader.close;
import java.util.;
public class Node
private static String removeLeadingWhitespaceString str
int i ;
while i int strlength
If the character at index i is not whitespace, then return the
substring that starts at i
if Character.isWhitespacestrcharAti
return strsubstringi;
i;
Completing the loop means the entire string is whitespace
return new String;
public int key;
public Node left;
public Node right;
public Nodeint nodeKey, Node leftChild
thisnodeKey leftChild, null;
public Nodeint nodeKey
thisnodeKey null, null;
public Nodeint nodeKey, Node leftChild, Node rightChild
key nodeKey;
left leftChild;
right rightChild;
public void close
Counts the number of nodes in this tree
public int count
int leftCount ;
if left null
leftCount left.count;
int rightCount ;
if right null
rightCount right.count;
return leftCount rightCount;
Inserts the new node into the tree.
public void insertNode node
Node currentNode this;
while currentNode null
if nodekey currentNode.key
if currentNodeleft null
currentNode currentNode.left;
else
currentNode.left node;
currentNode null;
else
if currentNoderight null
currentNode currentNode.right;
else
currentNode.right node;
currentNode null;
public void insertAllfinal ArrayList keys
for int key : keys
insertnew Nodekey;
public static Node parseString treeString
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
"null".
Remove leading whitespace first
treeString Node.removeLeadingWhitespacetreeString;
The string must be nonempty, start with and end with
if treeString.length treeString.charAt
treeString.charAttreeStringlength
return null;
Parse between parentheses
treeString treeString.substring treeString.length;
Find nonnested commas
ArrayList commaIndices new ArrayList;
int parenCounter ;
for int i ; i int treeString.length; i
char character treeString.charAti;
if character
parenCounter;
else if character
parenCounter;
else if character && parenCounter
commaIndices.addi;
If no commas, treeString is expected to be just the node's key
if commaIndices.size
return new NodeIntegerparseInttreeString;
If number of commas is not then the string's format is invalid
if commaIndices.size
return null;
"Split" on comma
int i commaIndices.get;
int i commaIndices.get;
String piece treeString.substring i;
String piece treeString.substringi i;
String piece treeString.substringi;
Make the node with just the key
Node nodeToReturn new NodeIntegerparseIntpiece;
Recursively parse children
nodeToReturn.left Node.parsepiece;
nodeToReturn.right Node.parsepiece;
return nodeToReturn;
import java.util.;
public class BSTChecker
public static Node checkBSTValidityNode root
return checkBSTValidityroot Integer.MINVALUE, Integer.MAXVALUE;
private static Node checkBSTValidityNode node, int min, int max
if node null
return null;
if nodekey min node.key max
return node;
Node leftInvalidNode checkBSTValiditynodeleft, min, node.key;
Node rightInvalidNode checkBSTValiditynoderight, node.key, max;
return leftInvalidNode null leftInvalidNode : rightInvalidNode;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started