Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Stack;
  3. import java.util.StringTokenizer;
  4.  
  5. class ArithTree
  6. {
  7. public String Treebuild;
  8. public ArithTree Tree1;
  9. public ArithTree Tree2;
  10.  
  11. ArithTree()
  12. {
  13. this.Treebuild = null;
  14. this.Tree2 = null;
  15. this.Tree1 = null;
  16. }
  17.  
  18. public ArithTree (String buildtree)
  19. {
  20. Treebuild = buildtree;
  21. this.Tree2 = null;
  22. this.Tree1 = null;
  23. }
  24.  
  25. public String toString()
  26. {
  27. return "" + Treebuild;
  28. }
  29. }
  30.  
  31. class BuildArithTree
  32. {
  33. private ArithTree base;
  34. public BuildArithTree()
  35. {
  36. base = null;
  37. }
  38.  
  39. public BuildArithTree(String theString)
  40. {
  41. Stack<ArithTree> BuildStack = new Stack<ArithTree>();
  42. Stack<ArithTree> FinishStack = new Stack<ArithTree>();
  43. StringTokenizer tokenizer = new StringTokenizer(theString, " +-*/()",true);
  44. while(tokenizer.hasMoreTokens())
  45. {
  46.  
  47. String str = tokenizer.nextToken().trim();
  48.  
  49. if(str.length() == 0)
  50. {
  51. int ignoreWhiteSpace = 0;
  52. }
  53. else if(str.equals("+"))
  54. {
  55. BuildStack.push(new ArithTree(str));
  56. }
  57. else if(str.equals("-"))
  58. {
  59. BuildStack.push(new ArithTree(str));
  60. }
  61. else if(str.equals("/"))
  62. {
  63. BuildStack.push(new ArithTree(str));
  64. }
  65. else if(str.equals("*"))
  66. {
  67. BuildStack.push(new ArithTree(str));
  68. }
  69. else if(str.equals("%"))
  70. {
  71. BuildStack.push(new ArithTree(str));
  72. }
  73. else if(str.equals(")"))
  74. {
  75. ArithTree operand1Removed = FinishStack.pop();
  76. ArithTree operand2Removed = FinishStack.pop();
  77. ArithTree operator1Removed = BuildStack.pop();
  78. operator1Removed.Tree1 = operand2Removed;
  79. operator1Removed.Tree2 = operand1Removed;
  80. FinishStack.push(operator1Removed);
  81. }
  82.  
  83. else if(str.equals("("))
  84. {
  85. int emptyStatement = 0;
  86. }
  87.  
  88. else
  89. {
  90. FinishStack.push(new ArithTree(str));
  91. }
  92. }
  93.  
  94. ArithTree fullBuildArithTree = FinishStack.pop();
  95. base = fullBuildArithTree;
  96. }
  97.  
  98.  
  99. public String asPostfix()
  100. {
  101. if (base == null)
  102. return "No Exp<b></b>ression";
  103. else
  104. return postOrder(base);
  105. }
  106. private String postOrder(ArithTree subTree)
  107. {
  108. if (subTree == null)
  109. return "";
  110. else
  111. return postOrder(subTree.Tree1) + " " + postOrder(subTree.Tree2) + " " + subTree;
  112. }
  113.  
  114. public void printTree()
  115. {
  116. if (base == null)
  117. {
  118. System.out.println("Sorry! The tree is empty");
  119. }
  120. else
  121. printTree(base, 0);
  122. }
  123.  
  124. private void printTree(ArithTree subTree, int indents)
  125. {
  126.  
  127. if (subTree.Tree2 != null)
  128. printTree(subTree.Tree2, indents+1);
  129.  
  130. System.out.println("");
  131.  
  132. for (int i=0; i<indents; i++)
  133. System.out.print("\t");
  134.  
  135. System.out.println(subTree);
  136.  
  137. if (subTree.Tree1 != null)
  138. printTree(subTree.Tree1, indents+1);
  139.  
  140. }
  141.  
  142. public String toString()
  143. {
  144. printTree();
  145. return "";
  146. }
  147. }
  148.  
  149. public class ArithmeticTree {
  150.  
  151. public static void main(String[] args) {
  152.  
  153. Scanner key = new Scanner (System.in);
  154.  
  155. System.out.println();
  156. System.out.println("Enter arithmetic expression (Please apply parenthesis correctly): ");
  157. //Test with ((3*(7-4))+6)
  158. String eq = key.next();
  159.  
  160. BuildArithTree t = new BuildArithTree(eq);
  161. t.asPostfix();
  162. t.printTree();
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement