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 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 code should prompt and execute exactly like the
examples below.
Enter a binary tree: (53(28(11**)(41*
*)*))
53
28
11
41
83
67
It is a balanced binary search tree
More trees? Y or N: Y
Enter a binary tree: (63(51(20(13**)*)
*)*)
63
51
20
13
It is a binary search tree but it is not
balanced
20
13
51
63
Original tree has height 3
Balanced tree has height 2
More trees? Y or N: Y
Enter a binary tree: (13(53**)(11(59**)
*))
13
53
11
59
It is not a binary search tree
13
11
53
59
Original tree has height 2
Balanced tree has height 2
More trees? Y or N: N
Need help with java code import

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!