BinaryTreeExpression { ExpressionNode root = new ExpressionNode("XXX"); // it will simply hold the tree int nodes = 0, parents = 0; // counter int height = -1; public BinaryTreeExpression()
Im missing parts, as in getting method to throw InvalidSyntaxExpression and I dont think its printing in order it should! See image for full assignment details. Help :'-(
BinaryTreeExpression.java
public class BinaryTreeExpression {
ExpressionNode root = new ExpressionNode("XXX"); // it will simply hold the tree
int nodes = 0, parents = 0; // counter
int height = -1;
public BinaryTreeExpression() {
root.pflag = true;
}
void makeTree() {
// makeTree tree
ExpressionNode node = root;
StringBuilder t = new StringBuilder(); // expression node value
int s = "(A(C(k)(2))(n(24)))".length();
// assumed first last will open and closed brackets
for(int i = 1; i < s - 1; i++) {
char x = "(A(C(k)(2))(n(24)))".charAt(i);
if(x == '(' || x == ')') {
String val = t.toString();
if(t.length() > 0) {
if(!node.pflag) {
// not marked yet
node.pflag = true;
parents++;
}
if(node.left == null) {
// left first
node.left = new ExpressionNode(val);
node.left.parent = node;
node = node.left;
} else {
// then right
node.right = new ExpressionNode(val);
node.right.parent = node;
node = node.right;
}
nodes++;
}
height++; // move downward
if(x == ')') {
// back to immediate parent
node = node.parent;
height--; // move back
}
t = new StringBuilder(); // reset
} else {
// node value
t.append(x);
}
}
}
int size() {
return nodes;
}
int totalParents() {
return parents;
}
int totalLeaves() {
return nodes - parents;
}
int getHeight() {
return height;
}
// recursive approach
int height(ExpressionNode node) {
if(node == null) {
// base case
return 0;
}
return 1 + Math.max(height(node.left), height(node.right));
}
boolean balanced(ExpressionNode node) {
if(node == null) {
// base case
return true;
}
boolean ok = balanced(node.left) && balanced(node.right);
int h1 = height(node.left);
int h2 = height(node.right);
return ok && (Math.abs(h1 - h2) <= 1);
}
boolean isBalanced() {
return balanced(root.left); // root.left is our original root node
}
boolean full(ExpressionNode node) {
if(node.pflag) {
// if it's leaf then return true, otherwise
if(node.left == null || node.right == null) {
// it's not full
return false;
}
// recursive call
return full(node.left) && full(node.right);
} else {
// leaf
return true;
}
}
boolean isFull() {
// root.left is our original root node
return full(root.left);
}
void inorder(ExpressionNode node) {
if(node == null) {
return;
}
inorder(node.left);
System.out.print(node.node + " ");
inorder(node.right);
}
void recursiveInorder() {
System.out.print("[");
inorder(root.left);
System.out.println("]");
}
// driver program to test
public static void main(String[] args) {
BinaryTreeExpression tree = new BinaryTreeExpression();
// try with some other test case like (A(C(K)(2))(N(24(10(12(13))))))
tree.makeTree();
System.out.println("Size: " + tree.size());
System.out.println("Height: " + tree.getHeight());
System.out.println("Parents: " + tree.totalParents());
System.out.println("Leaves: " + tree.totalLeaves());
System.out.println("Balanced: " + tree.isBalanced());
System.out.println("Full:" + tree.isFull());
System.out.println("In-Order: ");
tree.recursiveInorder();
}
}
ExpressionNode.java
class ExpressionNode {
String node;
ExpressionNode left, right;
ExpressionNode parent;
boolean pflag = false;
public ExpressionNode(String node) {
this.node = node;
left = right = null;
}
}
![Description and Objective
The purpose of this lab is to enhance programming skills using binary trees and stacks to process parenthesized prefix expressions.
The program will request to the computer user to enter a parenthesized prefix expression and then:
• create and validate the prefix expression (see Part I)
•construct a binary tree based on the expression rules (see Part II)
• perform operations in the tree (see Part III)
Part I: Tree Construction and Validation
The program shall have a method called makeTree(), that will take a String as a parameter, representing the prefix expression provided
by the computer user. This method will:
• validate if the given expression is valid. The method will throw a Invalid SyntaxExpression' in case the expression contains blank
spaces or the parenthesis are not matching (i.e., open and closing parenthesis).
•create a binary tree based on the given expression. The rules for the tree can be found on Part
II.
The method shall return a Node representing the root of this tree.
Part II: Prefix Expressions
The expression given by the computer user represents a binary tree, where
• each node of the tree contains a single alphanumeric character.
.no blank spaces are allowed
• Each tree is enclosed in parentheses.
• Inside those parentheses, after the single character are either 0, 1, or 3 subtrees also enclosed in parentheses.
•In the case that only one subtree is present, it is the left subtree and when two are present, they represent the left and right
subtrees.
'You are responsible to create the InvalidSyntax Expression class and handle the potential exception
For example, if the expression looks like this: (A(C(k)(2))(n(24)))
The corresponding tree shall look like this:
Part III: Methods
Create a program called BinaryTreeExpression.java, where the following methods are imple-
mented:
1. getHeight(): The height of a tree is the maximum level of all of its nodes, i.e., the largest
path from the root to the largest leaf
2.size(): it will return an int representing the total number of nodes in the tree
3. totalParents(): it will return an int representing the total number of parents in the tree.
A parent is a node having one or two nodes as children
4. totalLeaves (): it will return an int representing the total number of leaves in the tree. A
node is considered to be a leaf if it has no dependencies, thus no children
5. isBalanced (): returns true if the binary tree is balanced, meaning that for each node in the
binary tree, the height from the left and right subtree is at most one level. Otherwise the
method shall return false
6. isFull(): A full binary tree is full if each node is either a leaf or possesses exactly two child
nodes
7. get Inorder (): it will return a String representing the inorder transversal algorithm](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F8295a52d-4a2c-4cdc-ad96-938dc7903dc9%2F3d219547-ba7d-4925-9724-3bbddf1675e4%2Fvh7fgea_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)