Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Stack;
- import java.util.StringTokenizer;
- class ArithTree
- {
- public String Treebuild;
- public ArithTree Tree1;
- public ArithTree Tree2;
- ArithTree()
- {
- this.Treebuild = null;
- this.Tree2 = null;
- this.Tree1 = null;
- }
- public ArithTree (String buildtree)
- {
- Treebuild = buildtree;
- this.Tree2 = null;
- this.Tree1 = null;
- }
- public String toString()
- {
- return "" + Treebuild;
- }
- }
- class BuildArithTree
- {
- private ArithTree base;
- public BuildArithTree()
- {
- base = null;
- }
- public BuildArithTree(String theString)
- {
- Stack<ArithTree> BuildStack = new Stack<ArithTree>();
- Stack<ArithTree> FinishStack = new Stack<ArithTree>();
- StringTokenizer tokenizer = new StringTokenizer(theString, " +-*/()",true);
- while(tokenizer.hasMoreTokens())
- {
- String str = tokenizer.nextToken().trim();
- if(str.length() == 0)
- {
- int ignoreWhiteSpace = 0;
- }
- else if(str.equals("+"))
- {
- BuildStack.push(new ArithTree(str));
- }
- else if(str.equals("-"))
- {
- BuildStack.push(new ArithTree(str));
- }
- else if(str.equals("/"))
- {
- BuildStack.push(new ArithTree(str));
- }
- else if(str.equals("*"))
- {
- BuildStack.push(new ArithTree(str));
- }
- else if(str.equals("%"))
- {
- BuildStack.push(new ArithTree(str));
- }
- else if(str.equals(")"))
- {
- ArithTree operand1Removed = FinishStack.pop();
- ArithTree operand2Removed = FinishStack.pop();
- ArithTree operator1Removed = BuildStack.pop();
- operator1Removed.Tree1 = operand2Removed;
- operator1Removed.Tree2 = operand1Removed;
- FinishStack.push(operator1Removed);
- }
- else if(str.equals("("))
- {
- int emptyStatement = 0;
- }
- else
- {
- FinishStack.push(new ArithTree(str));
- }
- }
- ArithTree fullBuildArithTree = FinishStack.pop();
- base = fullBuildArithTree;
- }
- public String asPostfix()
- {
- if (base == null)
- return "No Exp<b></b>ression";
- else
- return postOrder(base);
- }
- private String postOrder(ArithTree subTree)
- {
- if (subTree == null)
- return "";
- else
- return postOrder(subTree.Tree1) + " " + postOrder(subTree.Tree2) + " " + subTree;
- }
- public void printTree()
- {
- if (base == null)
- {
- System.out.println("Sorry! The tree is empty");
- }
- else
- printTree(base, 0);
- }
- private void printTree(ArithTree subTree, int indents)
- {
- if (subTree.Tree2 != null)
- printTree(subTree.Tree2, indents+1);
- System.out.println("");
- for (int i=0; i<indents; i++)
- System.out.print("\t");
- System.out.println(subTree);
- if (subTree.Tree1 != null)
- printTree(subTree.Tree1, indents+1);
- }
- public String toString()
- {
- printTree();
- return "";
- }
- }
- public class ArithmeticTree {
- public static void main(String[] args) {
- Scanner key = new Scanner (System.in);
- System.out.println();
- System.out.println("Enter arithmetic expression (Please apply parenthesis correctly): ");
- //Test with ((3*(7-4))+6)
- String eq = key.next();
- BuildArithTree t = new BuildArithTree(eq);
- t.asPostfix();
- t.printTree();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement