Advertisement
UniQuet0p1

Untitled

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