Expand EquationBinaryTree to include a populate method for prefix and postfix. The populate methods should not convert to the other type before populating the tree nodes. Add Big-O notation to all 3 populate methods.
//ab+ = (a+b)
public void populateFromPostfix(String equation)
{
//does not require recursion to
solve
}
//+ab = (a+b)
public void populateFromPrefix(String equation)
{
//does not require recursion to
solve
}
Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)
import java.util.*;
public class EquationBinaryTree {
private EquationTreeNode root;
private String keyword;
private String infix = "Infix";
private String postfix = "Postfix";
private String prefix = "Prefix";
private String infixFormula, postfixFormula,
prefixFormula;
private Scanner scan = new
Scanner(System.in);
public EquationBinaryTree()
{
root = null;
}
private class EquationTreeNode{
String value;
EquationTreeNode
left;
EquationTreeNode
right;
public
EquationTreeNode(String v)
{
value = v;
left = null;
right = null;
}
public String
toString()
{
return value.toString();
}
}
public void populateFromInfix(String
inf)
{
root =
populateInfix(inf);
}
private EquationTreeNode populateInfix(String
inf)
{
EquationTreeNode
node;
if(inf.length() ==
1)
{
node = new EquationTreeNode(inf);
}
else
{
String[] parts = infixConversion(inf);
node = new EquationTreeNode(parts[2]);
if(!parts[0].equals(""))
node.left = populateInfix(parts[0]);
if(!parts[1].equals(""))
node.right = populateInfix(parts[1]);
}
return node;
}
private String[] infixConversion(String
inf)
{
String[] parts = new
String[3];
inf = inf.substring(1,
inf.length()-1);
int parenCount =
0;
int i = 0;
for(i = 0; i <
inf.length(); i++)
{
if(inf.charAt(i) == '(')
parenCount++;
else if(inf.charAt(i) == ')')
parenCount--;
if(parenCount == 0)
break;
}
parts[0] =
inf.substring(0, i+1);
parts[1] =
inf.substring(i+2);
parts[2] =
""+inf.charAt(i+1);
return parts;
}
public String printInfix()
{
String output =
"";
output +=
printInfixFunction(root);
return output;
}
private String
printInfixFunction(EquationTreeNode node)
{
String output =
"";
if(node.left !=
null)
{
output += "(";
output += printInfixFunction(node.left);
}
output += node;
if(node.right !=
null)
{
output += printInfixFunction(node.right);
output += ")";
}
return output;
}
public String printPostfix()
{
String output =
"";
output +=
printPostFunction(root);
return output;
}
private String
printPostFunction(EquationTreeNode node)
{
String output =
"";
if(node.left !=
null)
{
output += printPostFunction(node.left);
}
if(node.right !=
null)
{
output += printPostFunction(node.right);
}
output += node;
return output;
}
public String printPrefix()
{
String output =
"";
output +=
printPrefixFunction(root);
return output;
}
private String
printPrefixFunction(EquationTreeNode node)
{
String output =
"";
output += node;
if(node.left !=
null)
{
output += printPrefixFunction(node.left);
}
if(node.right !=
null)
{
output += printPrefixFunction(node.right);
}
return output;
}
private boolean isOperator(char ch)
{
if(ch == '+' || ch ==
'-' || ch == '*' || ch == '/' || ch == '^' || ch == '%')
return true;
return false;
}
public void populateFromPostfix(String
postfix)
{
root =
populatePostFunction(postfix);
}
private EquationTreeNode
populatePostFunction(String postfix){
Stack<EquationTreeNode> treeStack = new
Stack<>();
EquationTreeNode
node;
char[] charArray =
postfix.toCharArray();
for(int i = 0; i <
charArray.length; i++){
if(!isOperator(charArray[i])){
String value = String.valueOf(charArray[i]);
node = new EquationTreeNode(value);
treeStack.push(node);
}else
{
String value = String.valueOf(charArray[i]);
node = new EquationTreeNode(value);
node.right = treeStack.pop();
node.left = treeStack.pop();
treeStack.push(node);
}
}
node =
treeStack.pop();
return node;
}
public void populateFromPrefix(String
prefix)
{
root =
PrefixFunction(prefix);
}
private EquationTreeNode
PrefixFunction(String prefix){
Stack<EquationTreeNode> treeStack = new
Stack<>();
EquationTreeNode node,
node1, node2;
char[] charArray =
prefix.toCharArray();
for(int i =
charArray.length -1; i != -1; i--){
if(!isOperator(charArray[i])){
String value = String.valueOf(charArray[i]);
node = new EquationTreeNode(value);
treeStack.push(node);
}else
{
node1 = treeStack.pop();
node2 = treeStack.pop();
String value = String.valueOf(charArray[i]);
node = new EquationTreeNode(value);
node.left = node1;
node.right = node2;
treeStack.push(node);
}
}
node =
treeStack.pop();
return node;
}
public void input(){
System.out.println("Enter the keyword (Infix/Postfix/Prefix) of the
expression you would like to convert:");
keyword =
scan.nextLine();
while(!(keyword.equalsIgnoreCase(infix) ||
keyword.equalsIgnoreCase(postfix) ||
keyword.equalsIgnoreCase(prefix))){
System.out.println("Please enter a valid keyword.");
keyword = scan.nextLine();
}
if(keyword.equalsIgnoreCase(infix)){
System.out.println("Please enter your Infix Expression: (Must
include parentheses)");
infixFormula = scan.nextLine();
populateFromInfix(infixFormula);
printAll();
}else
if(keyword.equalsIgnoreCase(postfix)){
System.out.println("Please enter your Postfix Expression:");
postfixFormula = scan.nextLine();
populateFromPostfix(postfixFormula);
printAll();
}else
if(keyword.equalsIgnoreCase(prefix)){
System.out.println("Please enter your Prefix Expression:");
prefixFormula = scan.nextLine();
populateFromPrefix(prefixFormula);
printAll();
}
}
public void printAll(){
System.out.println("Infix : " + printInfix());
System.out.println("Postfix : " + printPostfix());
System.out.println("Prefix : " + printPrefix());
}
public static void main(String[] args)
{
EquationBinaryTree ebt =
new EquationBinaryTree();
ebt.input();
}
}
Expand EquationBinaryTree to include a populate method for prefix and postfix. The populate methods should not...
Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using namespace std; class List; class Iterator; class Node { public: /* Constructs a node with a given data value. @param s the data to store in this node */ Node(string s); /* Destructor */ ~Node() {} private: string data; Node* previous; Node* next; friend class List; friend class Iterator; }; class List { public: /** Constructs an empty list. */ List(); /* Destructor. Deletes...
In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...
Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...
26-ary tree for spell checker in JAVA You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number...
Complete the following code: You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number 26 indicates that...
Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...
Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...
BST.cpp #include "BST.h" #include "BT-visualize.h" #include <assert.h> #include <stdio.h> #include <string.h> #include <string> // Implement the following five methods // inserts val into BST rooted at x and returns the tree's new root BTnode * insert(BTnode * x, int val) {} // returns true iff target in tree rooted at x bool search(BTnode * x, int target) {} // Find the maximum value of a tree rooted at x int findmax(BTnode * x) {} // Find the manimum value of...
Summary You will write an application to build a tree structure called Trie for a dictionary of English words, and use the Trie to generate completion lists for string searches. Trie Structure A Trie is a general tree, in that each node can have any number of children. It is used to store a dictionary (list) of words that can be searched on, in a manner that allows for efficient generation of completion lists. The word list is originally stored...
Instructions Create a class BettterTree that extends OurTree, to facilitate the following. Update: if extending the class is too much, you can modify class OurTree. In our discussions about OurTree, we focused on nodes and their left and right children. For each node in the tree, is there another node of interest in addition to its children? If yes, how would you extend OurTree for that? How will the behavior of addNode method change? OurTree Code: public class OurTree {...