Question: Need 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
Need 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 code should prompt and execute exactly like the
examples below.
Enter a binary tree:
It is a balanced binary search tree
More trees? Y or N: Y
Enter a binary tree:
It is a binary search tree but it is not
balanced
Original tree has height
Balanced tree has height
More trees? Y or N: Y
Enter a binary tree:
It is not a binary search tree
Original tree has height
Balanced tree has height
More trees? Y or N: N
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
