Advertisement
UniQuet0p1

Untitled

Nov 14th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 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. @Override
  33. public String toString() {
  34. StringBuffer b = new StringBuffer();
  35. List<Tnode> usedList = new ArrayList<>();
  36. Tnode current = this;
  37.  
  38. do {
  39. if (!usedList.contains(current)) {
  40. b.append(current.getName());
  41. usedList.add(current);
  42. }
  43.  
  44. Tnode child = current.getFirstChild();
  45. Tnode sibling = current.getNextSibling();
  46.  
  47. if (child != null) {
  48. if (child.getNextSibling() != null || child.getFirstChild() != null) {
  49. current = current.getFirstChild();
  50. } else {
  51. current.setFirstChild(null);
  52. current = this;
  53. }
  54. if (!usedList.contains(current)) {
  55. b.append("(");
  56. }
  57. } else if (null != sibling) {
  58. if (sibling.getFirstChild() != null) {
  59. current = current.getNextSibling();
  60. } else {
  61. current.setNextSibling(null);
  62. if (!usedList.contains(sibling)) b.append(",").append(sibling.getName());
  63. current = this;
  64. b.append(")");
  65. }
  66. if (!usedList.contains(current)) {
  67. b.append(",");
  68. }
  69. }
  70. } while (this.getFirstChild() != null);
  71. return b.toString();
  72. }
  73.  
  74. private Tnode(String n, Tnode down, Tnode right) {
  75. name = n;
  76. firstChild = down;
  77. nextSibling = right;
  78. }
  79.  
  80. private String getName() {
  81. return name;
  82. }
  83.  
  84. private Tnode getFirstChild() {
  85. return firstChild;
  86. }
  87.  
  88. private void setFirstChild(Tnode down) {
  89. firstChild = down;
  90. }
  91.  
  92. private Tnode getNextSibling() {
  93. return nextSibling;
  94. }
  95.  
  96. private void setNextSibling(Tnode right) {
  97. nextSibling = right;
  98. }
  99.  
  100. private static boolean isSymbol(Tnode node) {
  101. return (Arrays.asList("*", "/", "+", "-").contains(node.name));
  102. }
  103.  
  104. public static Tnode buildFromRPN (String pol) {
  105. if (pol.equals("")) {
  106. throw new RuntimeException(String.format("empty subtree %s", pol));
  107. }
  108. List<String> symbolList = Arrays.asList(pol.split(" "));
  109. List<Tnode> tList = newList(symbolList);
  110. int counter = 0;
  111. if (tList.size() == 1 && !isDigit(tList.get(0))) {
  112. throw new RuntimeException(String.format("Invalid %s symbol ", pol));
  113. }
  114.  
  115. for (Tnode tnode : tList) {
  116. if (!isDigit(tnode) && !isSymbol(tnode)) {
  117. throw new RuntimeException(String.format("Statement %s has invalid symbol ", pol));
  118. }
  119. }
  120.  
  121. while (true) {
  122. if (tList.size() <= 1)
  123. break;
  124. if (tList.size() < 3) {
  125. throw new RuntimeException(String.format("There are not enough elements in the stack to complete %s", pol));
  126. }
  127. if (tList.size() > 9) {
  128. throw new RuntimeException(String.format("There are too much elements in the stack to complete %s", pol));
  129. }
  130. if (pol.contains("[!@#$%^&?,.]+") || (pol.contains("[A-Za-z]+"))) {
  131. throw new RuntimeException(String.format("%s contains illegal arguments", pol));
  132. }
  133.  
  134.  
  135. if (isDigit(tList.get(counter)) || tList.get(counter).getFirstChild() != null) {
  136. if (isDigit(tList.get(counter + 1)) || tList.get(counter + 1).getFirstChild() != null) {
  137. if (!isDigit(tList.get(counter + 2)) && tList.get(counter + 2).getFirstChild() == null) {
  138. Tnode top = tList.get(counter + 2);
  139. Tnode left = tList.get(counter);
  140. Tnode right = tList.get(counter + 1);
  141. left.setNextSibling(right);
  142. top.setFirstChild(left);
  143. tList.remove(counter + 1);
  144. tList.remove(counter);
  145. counter = 0;
  146. continue;
  147. }
  148. }
  149. }
  150. counter = counter + 1;
  151. }
  152. return tList.get(0);
  153. }
  154.  
  155.  
  156.  
  157. public static void main (String[] param) {
  158. String rpn = "1 2 +";
  159. System.out.println ("RPN: " + rpn);
  160. Tnode res = buildFromRPN (rpn);
  161. System.out.println ("Tree: " + res);
  162. }
  163. }
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement