BinaryTreeExpression { ExpressionNode root = new ExpressionNode("XXX"); // it will simply hold the tree int nodes = 0, parents = 0; // counter int height = -1; public BinaryTreeExpression()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Binomial Heap
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education