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 BinaryTreeString input throws
InvalidSyntaxException
input input.trim;
if input.startsWithinput.endsWith
throw new InvalidSyntaxExceptionInput must
start with and end with ;
String inner input.substring input.length
trim;
String tokens inner.splitls;
root makeTreetokens new int;
Constructor to create a balanced BST from an
ArrayList of integers
public BinaryTreeArrayList
TreeNode node new
TreeNodeIntegerparselnttoken;
Process left child
if index tokens.length &&
tokensindexequals
index; Consume
node.left makeTreetokens index;
if index tokens.length
tokensindexequals
throw new InvalidSyntaxExceptionExpected
closing parenthesis for left child";
Process right child
if index tokens.length &&
tokensindexequals
index; Consume
node.right makeTreetokens index;
if index tokens.length
tokensindexequals
throw new InvalidSyntaxExceptionExpected
closing parenthesis for right child";
return node;
private TreeNode
constructBalancedBSTArrayList
public void printIndented
printIndentedroot;
private void printIndentedTreeNode node, int depth
if node null
return;
printIndentednoderight, depth ;
System.out.printlnrepeatdepth node.data;
spaces per level
printIndentednodeleft, depth ;
public boolean isBinarySearchTree
return isBSTroot Integer.MINVALUE,
Integer.MAXVALUE;
private boolean isBSTTreeNode node, int min, int
max
if node null return true;
if nodedata min node.data max return false;
return isBSTnodeleft, min, node.data &&
isBSTnoderight, node.data max;
public boolean isBalanced
return checkHeightroot;
private int checkHeightTreeNode node
if node null return ;
int leftHeight checkHeightnodeleft;
if leftHeight return ;
int rightHeight checkHeightnoderight;
if rightHeight return ;
if MathabsleftHeight rightHeight return ;
return Math.maxleftHeight rightHeight;
public int getHeight
return getHeightroot;
private int getHeightTreeNode node
if node null return ;
return Math.maxgetHeightnodeleft
getHeightnoderight;
public ArrayList
try
BinaryTree tree new BinaryTreeinput;
System.out.printlnIndented representation:";
tree.printIndented;
if treeisBinarySearchTree
if treeisBalanced
System.out.printlnIt is a balanced binary
search tree";
else
System.out.printlnIt is a binary search
tree but it is not balanced";
ArrayList
if choice.equalslgnoreCaseY
break;
scanner.close;
Input
Indented representation:
It is a balanced binary search tree
When it should say
Indented representation:
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
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:
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
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
