Guest User

Untitled

a guest
Jan 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. package com.tree;
  2.  
  3.  
  4. import java.util.HashSet;
  5. import javax.swing.tree.DefaultMutableTreeNode;
  6.  
  7. /*
  8. * To change this template, choose Tools | Templates
  9. * and open the template in the editor.
  10. */
  11. /**
  12. *
  13. * @author adrian
  14. */
  15. public class Tree {
  16.  
  17. final String body;
  18. final char[] hierarchy = {'=', '+', '-', '*', '/', '^'};
  19.  
  20. public Tree(String body) {
  21. this.body = body;
  22. }
  23.  
  24. public DefaultMutableTreeNode makeTree() {
  25.  
  26. DefaultMutableTreeNode father = new DefaultMutableTreeNode(this.body);
  27.  
  28. divideTree(father);
  29.  
  30. return father;
  31. }
  32.  
  33. public void divideTree(DefaultMutableTreeNode father) {
  34. DefaultMutableTreeNode leftSon, rightSon;
  35. String fatherBody, leftSonBody, rightSonBody;
  36. int position;
  37. fatherBody = father.toString();
  38.  
  39. position = calculatePosition(fatherBody);
  40.  
  41. leftSonBody = fatherBody.substring(0, position);
  42.  
  43. rightSonBody = fatherBody.substring(position+1);
  44.  
  45.  
  46. if (haveBorders(leftSonBody)) {
  47. leftSonBody = removeBorders(leftSonBody);
  48. }
  49. if (haveBorders(rightSonBody)) {
  50. rightSonBody = removeBorders(rightSonBody);
  51.  
  52. }
  53.  
  54. leftSon = new DefaultMutableTreeNode(leftSonBody);
  55. rightSon = new DefaultMutableTreeNode(rightSonBody);
  56.  
  57. father.add(leftSon);
  58. father.add(rightSon);
  59.  
  60. if (haveSigns(leftSon.toString())) {
  61. divideTree(leftSon);
  62. }
  63. if (haveSigns(rightSon.toString())) {
  64. divideTree(rightSon);
  65. }
  66. }
  67.  
  68. public boolean haveSigns(String body) {
  69.  
  70. for (int i = 0; i < hierarchy.length; i++) {
  71. if (body.indexOf(hierarchy[i]) != -1) {
  72. return true;
  73. }
  74.  
  75. }
  76. return false;
  77. }
  78.  
  79. public boolean haveBorders(String body) {
  80.  
  81. if (calculateInvalidPositions(body).size() == body.length()) {
  82. return true;
  83. }
  84. return false;
  85.  
  86. }
  87.  
  88. public int calculatePosition(String body) {
  89. HashSet<Integer> invalidPositions = calculateInvalidPositions(body);
  90. int position = 0;
  91. for (int i = 0; i < hierarchy.length; i++) {
  92.  
  93. for (int j = 0; j < body.length(); j++) {
  94.  
  95. if (body.charAt(j) == hierarchy[i]) {
  96. position = j;
  97. }
  98.  
  99. if (invalidPositions.contains(position)==false && position!=0) {
  100. return position;
  101. }
  102.  
  103.  
  104. }
  105.  
  106. }
  107.  
  108.  
  109.  
  110. return position;
  111. }
  112.  
  113. public HashSet calculateInvalidPositions(String body) {
  114. HashSet<Integer> invalids = new HashSet<Integer>();
  115. char[] openHierarchy = {'{', '[', '('};
  116. char[] closeHierarchy = {'}', ']', ')'};
  117. boolean activation = false;
  118. for (int i = 0; i < openHierarchy.length; i++, activation = false) {
  119. for (int j = 0; j < body.length(); j++) {
  120. if (body.charAt(j) == openHierarchy[i]) {
  121. activation = true;
  122. }
  123. if (activation) {
  124. invalids.add(j);
  125. }
  126. if (body.charAt(j) == closeHierarchy[i]) {
  127. activation = false;
  128. }
  129.  
  130.  
  131. }
  132.  
  133. }
  134.  
  135.  
  136.  
  137.  
  138. return invalids;
  139. }
  140.  
  141. public String removeBorders(String body) {
  142. int f = (body.length() - 1);
  143.  
  144. if ((body.charAt(0) == '{') && (body.charAt(f) == '}')) {
  145. body = body.substring(1, (f));
  146. return body;
  147. }
  148.  
  149. if ((body.charAt(0) == '[') && (body.charAt(f) == ']')) {
  150. body = body.substring(1, (f));
  151. return body;
  152. }
  153.  
  154. if ((body.charAt(0) == '(') && (body.charAt(f) == ')')) {
  155. body = body.substring(1, (f));
  156. return body;
  157. }
  158.  
  159.  
  160. return body;
  161.  
  162. }
  163. }
Add Comment
Please, Sign In to add comment