Advertisement
UniQuet0p1

Untitled

Nov 3rd, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. //https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java/29331473
  2. //https://gist.github.com/Gaurav-Jayswal/9228965
  3. //https://www.101computing.net/reverse-polish-notation/
  4. //https://www.tabnine.com/code/java/methods/net.sf.saxon.dom.DOMNodeList/%3Cinit%3E
  5.  
  6. import java.util.*;
  7.  
  8. public class Tnode {
  9.  
  10. private String name;
  11. private Tnode firstChild;
  12. private Tnode nextSibling;
  13.  
  14. private static boolean isDigit(Tnode node) { //https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java/29331473
  15. try {
  16. Integer.parseInt(node.name); //Возвращает true, если все символы в строке являются цифрами и есть хотя бы один символ, иначе false.
  17. return true;
  18. } catch (RuntimeException e) {
  19. return false;
  20. }
  21. }
  22.  
  23. private static List<Tnode> newList(List<String> stringList) { //https://www.tabnine.com/code/java/methods/net.sf.saxon.dom.DOMNodeList/%3Cinit%3E
  24. List<Tnode> tList = new ArrayList<>(); //Создает пустой список для узлов дерева.
  25. for (String str : stringList) { //Для каждой строки из параметра stringList создает узел дерева с текстом из этой строки и помещает созданный узел в список узлов.
  26. tList.add(new Tnode(str, null, null));
  27. }
  28. return tList; //Возвращает полученный список узлов в качестве результата туда, где метод был вызван.
  29. }
  30.  
  31.  
  32. private static List<Tnode> expressions(String pol) {
  33.  
  34. List<String> symbolList = Arrays.asList(pol.split(" "));
  35. List<Tnode> tList = newList(symbolList);
  36.  
  37. if (tList.size() == 1 && !isDigit(tList.get(0))) {
  38. throw new RuntimeException(String.format("Invalid %s symbol ", pol));
  39. }
  40.  
  41. for (Tnode tnode : tList) {
  42. if (!isDigit(tnode) && !isSymbol(tnode)) {
  43. throw new RuntimeException(String.format("Statement %s has invalid symbol ", pol));
  44. }
  45. }
  46. return tList;
  47. }
  48.  
  49.  
  50. @Override
  51. public String toString() {
  52. StringBuffer b = new StringBuffer();
  53. List<Tnode> usedList = new ArrayList<>();
  54. Tnode current = this;
  55.  
  56. do {
  57. if (!usedList.contains(current)) {
  58. b.append(current.getName());
  59. usedList.add(current);
  60. }
  61.  
  62. Tnode child = current.getFirstChild();
  63. Tnode sibling = current.getNextSibling();
  64.  
  65. if (child != null) {
  66. if (child.getNextSibling() != null || child.getFirstChild() != null) {
  67. current = current.getFirstChild();
  68. } else {
  69. current.setFirstChild(null);
  70. current = this;
  71. }
  72. if (!usedList.contains(current)) {
  73. b.append("(");
  74. }
  75. } else if (null != sibling) {
  76. if (sibling.getFirstChild() != null) {
  77. current = current.getNextSibling();
  78. } else {
  79. current.setNextSibling(null);
  80. if (!usedList.contains(sibling)) b.append(",").append(sibling.getName());
  81. current = this;
  82. b.append(")");
  83. }
  84. if (!usedList.contains(current)) {
  85. b.append(",");
  86. }
  87. }
  88. } while (this.getFirstChild() != null);
  89. return b.toString();
  90. }
  91.  
  92. private Tnode(String n, Tnode down, Tnode right) {
  93. name = n;
  94. firstChild = down;
  95. nextSibling = right;
  96. }
  97.  
  98. private String getName() {
  99. return name;
  100. }
  101.  
  102. private Tnode getFirstChild() {
  103. return firstChild;
  104. }
  105.  
  106. private void setFirstChild(Tnode down) {
  107. firstChild = down;
  108. }
  109.  
  110. private Tnode getNextSibling() {
  111. return nextSibling;
  112. }
  113.  
  114. private void setNextSibling(Tnode right) {
  115. nextSibling = right;
  116. }
  117.  
  118. private static boolean isSymbol(Tnode node) {
  119. return (Arrays.asList("*", "/", "+", "-").contains(node.name));
  120. }
  121.  
  122. public static Tnode buildFromRPN (String pol) {
  123. List<Tnode> tList = expressions(pol);
  124. int counter = 0;
  125.  
  126. while (true) {
  127. if (tList.size() <= 1)
  128. break;
  129. if (tList.size() < 3) {
  130. throw new RuntimeException(String.format("There are not enough elements in the stack to complete %s", pol));
  131. }
  132. if (tList.size() > 9) {
  133. throw new RuntimeException(String.format("There are too much elements in the stack to complete %s", pol));
  134. }
  135. if (pol.contains("[!@#$%^&?,.]+")) {
  136. throw new RuntimeException(String.format("%s contains illegal arguments", pol));
  137. } else if (pol.contains("( )")) {
  138. throw new RuntimeException(String.format("empty subtree %s", pol));
  139. }
  140.  
  141. if (isDigit(tList.get(counter)) || tList.get(counter).getFirstChild() != null) {
  142. if (isDigit(tList.get(counter + 1)) || tList.get(counter + 1).getFirstChild() != null) {
  143. if (!isDigit(tList.get(counter + 2)) && tList.get(counter + 2).getFirstChild() == null) {
  144. Tnode top = tList.get(counter + 2);
  145. Tnode left = tList.get(counter);
  146. Tnode right = tList.get(counter + 1);
  147. left.setNextSibling(right);
  148. top.setFirstChild(left);
  149. tList.remove(counter + 1);
  150. tList.remove(counter);
  151. counter = 0;
  152. continue;
  153. }
  154. }
  155. }
  156. counter = counter + 1;
  157. }
  158. return tList.get(0);
  159. }
  160.  
  161.  
  162.  
  163. public static void main (String[] param) {
  164. String rpn = "1 2 +";
  165. System.out.println ("RPN: " + rpn);
  166. Tnode res = buildFromRPN (rpn);
  167. System.out.println ("Tree: " + res);
  168. }
  169. }
  170.  
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement