Question: Help with java code import java.util.ArrayList; import java.util.Scanner; class BinaryTree { private final TreeNode root; / / Constructor to create a tree from preorder representation

Help with java code
import java.util.ArrayList;
import java.util.Scanner;
class BinaryTree {
private final TreeNode root;
// Constructor to create a tree from preorder
representation
public BinaryTree(String input) throws
InvalidSyntaxException {
input = input.trim();
if (!input.startsWith("(")||!input.endsWith(")")){
throw new InvalidSyntaxException("Input must
start with '(' and end with ')"');
}
String inner = input.substring(1, input.length()-
.trim();
String[] tokens = inner.split("(?=\))|(?=\()|\ls+");
root = makeTree(tokens, new int[{{0});
}
// Constructor to create a balanced BST from an
ArrayList of integers
public BinaryTree(ArrayList
TreeNode node = new
TreeNode(Integer.parselnt(token));
// Process left child
if (index[0] tokens.length &&
tokens[index[0]].equals("(")){
index[0]++; // Consume '(
node.left = makeTree(tokens, index);
if (index[0]>= tokens.length ||
!tokens[index[0]++].equals(")")){
throw new InvalidSyntaxException("Expected
closing parenthesis for left child");
}
}
// Process right child
if (index[0] tokens.length &&
tokens[index[0]].equals("(")){
index[0]++; // Consume '(
node.right = makeTree(tokens, index);
if (index[0]>= tokens.length ||
!tokens[index[0]++].equals(")")){
throw new InvalidSyntaxException("Expected
closing parenthesis for right child");
}
}
return node;
}
private TreeNode
constructBalancedBST(ArrayList
public void printIndented(){
printIndented(root,0);
}
private void printIndented(TreeNode node, int depth){
if (node == null){
return;
}
printIndented(node.right, depth +1);
System.out.println("".repeat(depth *4)+ node.data);
//4 spaces per level
printIndented(node.left, depth +1);
}
public boolean isBinarySearchTree(){
return isBST(root, Integer.MIN_VALUE,
Integer.MAX_VALUE);
}
private boolean isBST(TreeNode node, int min, int
max){
if (node == null) return true;
if (node.data min || node.data > max) return false;
return isBST(node.left, min, node.data -1) &&
isBST(node.right, node.data +1, max);
}
public boolean isBalanced(){
return checkHeight(root)!=-1;
}
private int checkHeight(TreeNode node){
if (node == null) return 0;
int leftHeight = checkHeight(node.left);
if (leftHeight ==-1) return -1 ;
int rightHeight = checkHeight(node.right);
if (rightHeight ==-1) return -1 ;
if (Math.abs(leftHeight - rightHeight)>1) return -1;
return Math.max(leftHeight, rightHeight)+1;
}
public int getHeight(){
return getHeight(root);
}
private int getHeight(TreeNode node){
if (node == null) return 0;
return Math.max(getHeight(node.left),
getHeight(node.right))+1;
}
public ArrayList
try {
BinaryTree tree = new BinaryTree(input);
System.out.println("Indented representation:");
tree.printIndented();
if (tree.isBinarySearchTree()){
if (tree.isBalanced()){
System.out.println("It is a balanced binary
search tree");
} else {
System.out.println("It is a binary search
tree but it is not balanced");
ArrayList
if (!choice.equalslgnoreCase("Y")){
break;
}
}
scanner.close();
}
}
Input
(53(28(11**)(41**))(83(67**)*))
Indented representation:
53
It is a balanced binary search tree
When it should say
Indented representation:
53
28
11
41
83
67
It is a balanced binary search tree
The programming project involves writing a program to
read in the preorder representation of a binary tree and
determine whether it is a balanced binary search tree.
Given the binary tree shown below:
Its preorder representation would be (53(28(11**)(41*
*))**)*)).
The asterisks represent null children.
The program should prompt the user for a single binary
tree "Enter a binary tree:". It should then display the tree
using indentation. After returning the results it should
prompt the user if they would like to enter another tree
"More trees? Y or N: " For the above tree, its indented
representation would be as follows:
53
28
11
41
83
67
It should then categorize the binary tree in one of three
ways:
It is not a binary search tree
It is a balanced binary search tree
It is a binary search tree but it is not balanced
If the tree is not a balanced binary search tree, a
balanced binary search tree containing the same set of
values should be constructed and displayed in the
indented format shown above. It is not expected that the
existing tree be rebalanced, just that a new tree with the
same set of values be constructed. Then the height of the
original tree and the balanced binary search tree should
be displayed.
In addition, the program should verify that the tree input
has valid syntax. Each of the following errors should be
detected;
Data is Not an Integer
Extra Characters at the End
Missing Left Parenthesis
Missing Right Parentheses
The program should consist of three classes. The first
class should be the class that defines the binary tree. It
should be an immutable class with the following p
Help with java code import java.util.ArrayList;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!