I need to add the height method but I am not understanding how it works.... an e
ID: 3728970 • Letter: I
Question
I need to add the height method but I am not understanding how it works.... an example of my desired output from a tester class is below
Sample Output:
input: 1 2 +
inorder: (1.0 + 2.0)
height: 1
Code:
import java.util.Stack;
public class PostfixTree
{
private final String add = "+";
private final String subtract = "-";
private final String multiply = "*";
private final String divide = "/";
private class TreeNode
{
double value;
String oper;
TreeNode Left;
TreeNode Right;
boolean isOp;
boolean isRoot;
}
//private TreeNode root;
Stack numberStack = new Stack();
public double evaluate() //should return the numeric value associated with the tree. This must operate recursively on the expression tree.
{
TreeNode root = numberStack.peek();
return evaluate(root);
}
private double evaluate(TreeNode root)
{
double result = 0;
if (root.isOp == false)
{
return root.value;
}
else
{
double L = evaluate(root.Left);
double R = evaluate(root.Right);
switch (root.oper)
{
case "+":
return result = L + R;
case "-":
return result = L - R;
case "/":
return result = L / R;
case "*":
return result = L * R;
}
}
return result;
}
public void pushNumber(double d) //should push a node containing a number to the internal stack.
{
TreeNode n = new TreeNode(); //create a new node
n.value = d;
n.isOp = false;
numberStack.push(n);
}
public void pushAdd () //should push a node representing the corresponding operation.
{
TreeNode d = new TreeNode();
d.isOp = true;
d.oper = add;
d.isRoot = true;
if (numberStack.empty() == true)
{
System.out.println("invalid input order");
}
else
{
d.Left = numberStack.pop();
d.Right = numberStack.pop();
numberStack.push(d);
}
}
public void pushMultiply () //should push a node representing the corresponding operation.
{
TreeNode d = new TreeNode();
d.isOp = true;
d.oper = multiply;
if (numberStack.empty() == true)
{
System.out.println("invalid input order");
}
else
{
d.Left = numberStack.pop();
d.Right = numberStack.pop();
numberStack.push(d);
}
}
public void pushSubtract () //should push a node representing the corresponding operation.
{
TreeNode d = new TreeNode();
d.isOp = true;
d.oper = subtract;
if (numberStack.empty() == true)
{
System.out.println("invalid input order");
}
else
{
d.Right = numberStack.pop();
d.Left = numberStack.pop();
numberStack.push(d);
}
}
public void pushDivide () //should push a node representing the corresponding operation.
{
TreeNode d = new TreeNode();
d.isOp = true;
d.oper = divide;
if (numberStack.empty() == true)
{
System.out.println("invalid input order");
}
else
{
d.Left = numberStack.pop();
d.Right = numberStack.pop();
numberStack.push(d);
}
}
public String inorder () //should return a String containing a parenthesized inorder traversal of the expression tree.
{
TreeNode root = numberStack.peek();
return toInFix(root);
}
private String toInFix(TreeNode root) //algorithm based on in-class example on 02/21/18
{
if (root.isOp == false)
{
return root.value + " ";
}
else
{
String S = " ( " + toInFix(root.Left);
S += root.oper;
S += toInFix(root.Right) + " ) ";
return S;
}
}
public int height() //should return the height of the expression tree (distance in edges between the root and the farthest leaf).
{
TreeNode root = numberStack.peek();
return getHeight(root);
}
private int getHeight(TreeNode root){
}
Explanation / Answer
//Try this code in the above program.You will get the height of the binary tree:
private int getHeight(TreeNode root){
if(root==null)
return 0;
else
{
int lh=getHeight(root.Left);
int rh=getHeight(root.Right);
return Math.max(lh,rh)+1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.