Java program to implement a binary tree for an given express

Java program to implement a binary tree for an given expression using preorder and then solving the expression.

Solution

package snippet;

import java.util.Scanner;
class BST
{
  
class TreeNode
{
char data;
TreeNode left, right;

  
public TreeNode(char data)
{
this.data = data;
this.left = null;
this.right = null;
}
}

  
class StackNode
{
TreeNode treeNode;
StackNode next;

  
public StackNode(TreeNode treeNode)
{
this.treeNode = treeNode;
next = null;
}
}

private static StackNode topRoot;

  
public BST()
{
topRoot = null;
}

  
public void clear()
{
topRoot = null;
}

  
private void InsertNode(TreeNode ptr)
{
if (topRoot == null)
topRoot = new StackNode(ptr);
else
{
StackNode temp = new StackNode(ptr);
temp.next = topRoot;
topRoot = temp;
}
}

  
private TreeNode RemoveNode()
{
if (topRoot == null)
throw new RuntimeException(\"Underflow\");
else
{
TreeNode ptr = topRoot.treeNode;
topRoot = topRoot.next;
return ptr;
}
}

  
private TreeNode getRoot()
{
return topRoot.treeNode;
}


private void insert(char val)
{
try
{
if (isDigit(val))
{
TreeNode temp = new TreeNode(val);
InsertNode(temp);
}
else if (isOperator(val))
{
TreeNode temp = new TreeNode(val);
temp.left = RemoveNode();
temp.right = RemoveNode();
InsertNode(temp);
}
}
catch (Exception e)
{
System.out.println(\"Invalid Expression\");
}
}

  
private boolean isDigit(char ch)
{
return ch >= \'0\' && ch <= \'9\';
}

  
private boolean isOperator(char ch)
{
return ch == \'+\' || ch == \'-\' || ch == \'*\' || ch == \'/\';
}

  
private int toDigit(char ch)
{
return ch - \'0\';
}

  
public void createTree(String eqn)
{
for (int i = eqn.length() - 1; i >= 0; i--)
insert(eqn.charAt(i));
}

  
public double eval()
{
return eval(getRoot());
}

  
public double eval(TreeNode ptr)
{
if (ptr.left == null && ptr.right == null)
return toDigit(ptr.data);
else
{
double result = 0.0;
double left = eval(ptr.left);
double right = eval(ptr.right);
char operator = ptr.data;

switch (operator)
{
case \'+\' : result = left + right; break;
case \'-\' : result = left - right; break;
case \'*\' : result = left * right; break;
case \'/\' : result = left / right; break;
default : result = left + right; break;
}
return result;
}
}

  
public void print_postfix()
{
postOrder(getRoot());
}


private void postOrder(TreeNode ptr)
{
if (ptr != null)
{
postOrder(ptr.left);
postOrder(ptr.right);
System.out.print(ptr.data);
}
}

  
public void print_infix()
{
inorderTraversal(getRoot());
}


private void inorderTraversal(TreeNode ptr)
{
if (ptr != null)
{
inorderTraversal(ptr.left);
System.out.print(ptr.data);
inorderTraversal(ptr.right);
}
}

  
public void print_Prefix()
{
PreorderTraversal(getRoot());
}


private void PreorderTraversal(TreeNode ptr)
{
if (ptr != null)
{
System.out.print(ptr.data);
PreorderTraversal(ptr.left);
PreorderTraversal(ptr.right);
}
}
}

public class PrefixEvaluation
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

BST obj = new BST();

System.out.println(\"\ Enter equation in print_Prefix form\");
obj.createTree(scan.next());

System.out.print(\"\ Prefix Expression : \");
obj.print_Prefix();
System.out.print(\"\ \ Infix Expression : \");
obj.print_infix();
System.out.print(\"\ \ Postfix Expression: \");
obj.print_postfix();
System.out.println(\"\ \ Result : \"+ obj.eval());
}
}

===================================================

Enter equation in print_Prefix form

+35

Prefix Expression : +35

Infix Expression : 3+5

Postfix Expression: 35+

Result : 8.0

Java program to implement a binary tree for an given expression using preorder and then solving the expression.Solutionpackage snippet; import java.util.Scanner
Java program to implement a binary tree for an given expression using preorder and then solving the expression.Solutionpackage snippet; import java.util.Scanner
Java program to implement a binary tree for an given expression using preorder and then solving the expression.Solutionpackage snippet; import java.util.Scanner
Java program to implement a binary tree for an given expression using preorder and then solving the expression.Solutionpackage snippet; import java.util.Scanner
Java program to implement a binary tree for an given expression using preorder and then solving the expression.Solutionpackage snippet; import java.util.Scanner

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site