Concept explainers
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;
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- !!! JAVA ONLY!! Create a class Tree to model a binary tree. This class should contain a single Node which is theroot node of the tree, and should have appropriate getters/setters and toString().Again, test out your class by creating a tree and nodes objects; and by calling various methodson the them in the main method.Create the following tree using instances of your classes: HoareJonesFitzgerald AstarteRoscoeBroadfoot CreeseThis tree captures PhD supervision hierarchy: Jones was supervised by Hoare, etc.Write a method traceAndPrint for your Tree class. It should take as a parameter a stringof L and R characters, and, starting at the root, trace a path through the tree, taking a leftchild when encountering L and a right child when encountering R. Each node passed, startingwith the root, should be printed to the console. If directions given are wrong, i.e. a null isencountered, or any other character is passed to the method, an exception should be thrown.The exception should indicate…arrow_forwardI need an explanation for this program. class Directory { Node head,tail; Directory() { head=null; tail=null; } public void insert(Node node) // method that insers node into list if phone number is new { Node temp=head; while(temp!=null && !temp.phone_number.equals(node.phone_number)) temp=temp.next; if(temp!=null) { System.out.println("Entered phone number already exists"); return; } if(head==null) { head=node; tail=node; } else { tail.next=node; tail=node; } System.out.println("Inserted Successfully"); } public void remove(String ph_no) // method that removes node with given phone number { Node prev=null; Node node=head; while(node!=null && !node.phone_number.equals(ph_no)) { prev=node;…arrow_forwardNeed help with these list questions in Javaarrow_forward
- write this code below as algorithim to determine the leaf node reclusively ? public static void printLeafNodes(TreeNode node) { // base case if (node == null) { return; } if (node.left == null && node.right == null) { System.out.printf("%d ", node.value); } printLeafNodes(node.left); printLeafNodes(node.right); }arrow_forwardI need help with this Java problem to output as it's explained in the image below: public class BST { private Node root; public void insert(int data) { root = insertRec(root, data); } private Node insertRec(Node root, int data) { if (root == null) { root = new Node(data); return root; } if (data < root.data) { root.left = insertRec(root.left, data); } else if (data > root.data) { root.right = insertRec(root.right, data); } return root; } public int getHeight() { return getHeightRec(root); } private int getHeightRec(Node root) { if (root == null) { return 0; } else { int leftHeight = getHeightRec(root.left); int rightHeight = getHeightRec(root.right); return Math.max(leftHeight, rightHeight) + 1; } } public int search(int data) { return searchRec(root, data, 0); }…arrow_forwardPlease use the information in the second screenshot about the BST class and Node class to create a class BSTApp. Please make sure to use the following code as a starting point. import java.util.*; public class BSTTest{public static void main(String[] args) {// perform at least one test on each operation of your BST } private int[] randomArray(int size) {// remove the two linesint[] arr = new int[1];return arr;} // the parameters and return are up to you to define; this method needs to be uncommented// private test() {//// }} Base of class Node public class Node {int key;Node left, right, parent; public Node() {} public Node(int num) {key = num;left = null;right = null;parent = null;}} Base of class BST import java.util.*; class BST {// do not change thisprivate Node root;private ArrayList<Integer> data; // DO NOT MODIFY THIS METHODpublic BST() {root = null;data = new ArrayList<Integer>(0);} // DO NOT MODIFY THIS METHODpublic ArrayList<Integer> getData() {return…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education