Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. package com.wixpress.test.tree;
  2.  
  3. import java.util.HashSet;
  4.  
  5. public class BinTree {
  6. private static final Character LEFT_DELIMITER = '{';
  7. private static final Character RIGHT_DELIMITER = '}';
  8. private static final String SINGLE_SPACE = " ";
  9. private String value;
  10. private BinTree left;
  11. private BinTree right;
  12.  
  13. public BinTree(String value) {
  14. this(value, null, null);
  15. }
  16.  
  17. public BinTree(String value, BinTree left, BinTree right) {
  18. this.value = value;
  19. this.left = left;
  20. this.right = right;
  21. }
  22.  
  23. public String getValue() {
  24. return value;
  25. }
  26.  
  27. public void setValue(String value) {
  28. this.value = value;
  29. }
  30.  
  31. public BinTree getLeft() {
  32. return left;
  33. }
  34.  
  35. public void setLeft(BinTree left) {
  36. this.left = left;
  37. }
  38.  
  39. public BinTree getRight() {
  40. return right;
  41. }
  42.  
  43. public void setRight(BinTree right) {
  44. this.right = right;
  45. }
  46.  
  47. public String toString() {
  48. try {
  49. return serialize();
  50. } catch (BinTreeSerializationException e) {
  51. return "failed to represent the tree as a string cause of " + e.getMessage();
  52. }
  53. }
  54.  
  55. @Override
  56. public boolean equals(Object o) {
  57. if (this == o) return true;
  58. if (o == null || getClass() != o.getClass()) return false;
  59.  
  60. BinTree binTree = (BinTree) o;
  61.  
  62. if (left != null ? !left.equals(binTree.left) : binTree.left != null) return false;
  63. if (right != null ? !right.equals(binTree.right) : binTree.right != null) return false;
  64. if (value != null ? !value.equals(binTree.value) : binTree.value != null) return false;
  65.  
  66. return true;
  67. }
  68.  
  69. ///////////////////////////
  70. // //
  71. // Implement: //
  72. // //
  73. ///////////////////////////
  74.  
  75. public String serialize() throws BinTreeSerializationException {
  76. HashSet<BinTree> visitedBinTrees = new HashSet<BinTree>();
  77. return serialize(visitedBinTrees);
  78. }
  79.  
  80. private String serialize(HashSet<BinTree> visitedBinTrees) throws BinTreeSerializationException {
  81. if (visitedBinTrees.contains(this)) {
  82. throw new BinTreeSerializationException("The input is cyclic");
  83. }
  84. visitedBinTrees.add(this);
  85. String serialized = LEFT_DELIMITER
  86. + SINGLE_SPACE
  87. + this.getValue()
  88. + serializeWrapper(this.getLeft(), visitedBinTrees)
  89. + SINGLE_SPACE
  90. + serializeWrapper(this.getRight(), visitedBinTrees)
  91. + RIGHT_DELIMITER;
  92. return serialized;
  93. }
  94.  
  95. private String serializeWrapper(BinTree binTree, HashSet<BinTree> visitedBinTrees) throws BinTreeSerializationException {
  96. return (binTree!= null) ? binTree.serialize(visitedBinTrees) : " {} ";
  97. }
  98.  
  99. public static BinTree deserialize(String serialized) throws BinTreeSerializationException {
  100.  
  101. if (serialized == null || serialized.isEmpty()) {
  102. throw new BinTreeSerializationException();
  103. }
  104.  
  105. if ((!serialized.contains(LEFT_DELIMITER + ""))
  106. || (!serialized.contains(RIGHT_DELIMITER + ""))){
  107. throw new BinTreeSerializationException();
  108. }
  109. String peeledSerialized = peelOuterBrackets(serialized);
  110. if (peeledSerialized.isEmpty()) {
  111. return null;
  112. }
  113. String nodeValue = peeledSerialized.substring(0, peeledSerialized.indexOf(LEFT_DELIMITER)).trim();
  114. System.out.println("node Value: " + nodeValue);
  115. String unparsedInputLeftAndRight = peeledSerialized.substring(nodeValue.length()).trim();
  116. String left = findLeftComponent(unparsedInputLeftAndRight.substring(unparsedInputLeftAndRight.indexOf(LEFT_DELIMITER))).trim();
  117. BinTree leftBinTree = BinTree.deserialize(left);
  118. if (left.length() == unparsedInputLeftAndRight.length()) {
  119. throw new BinTreeSerializationException();
  120. }
  121. String right = unparsedInputLeftAndRight.substring(left.length(), unparsedInputLeftAndRight.length()).trim();
  122. BinTree rightBinTree = BinTree.deserialize(right);
  123. return new BinTree(nodeValue, leftBinTree, rightBinTree);
  124. }
  125.  
  126. static String peelOuterBrackets(String serialized) throws BinTreeSerializationException {
  127. String trimmedSerialized = serialized.trim();
  128. if ((trimmedSerialized.length() < 2)
  129. || (trimmedSerialized.indexOf(LEFT_DELIMITER) == -1)
  130. || (trimmedSerialized.indexOf(RIGHT_DELIMITER) == -1)
  131. || (trimmedSerialized.charAt(0) != LEFT_DELIMITER)
  132. || (trimmedSerialized.charAt(trimmedSerialized.length()-1) != RIGHT_DELIMITER)){
  133. throw new BinTreeSerializationException();
  134. }
  135. return trimmedSerialized.substring(1, trimmedSerialized.length()-1).trim();
  136. }
  137.  
  138. static String findLeftComponent(String unparsedInputLeftAndRight) throws BinTreeSerializationException {
  139. String trimmed = unparsedInputLeftAndRight.trim();
  140. int balance = 0;
  141. for (int i = 0; i < trimmed.length(); i++) {
  142. if (trimmed.charAt(i) == LEFT_DELIMITER) {
  143. balance++;
  144. } else if (trimmed.charAt(i) == RIGHT_DELIMITER) {
  145. balance--;
  146. }
  147. if (balance == 0) {
  148. if (trimmed.charAt(i) != RIGHT_DELIMITER) {
  149. throw new BinTreeSerializationException("Cannot get BinTree component");
  150. } else {
  151. return trimmed.substring(0, i+1);
  152. }
  153. }
  154. }
  155. throw new BinTreeSerializationException("Cannot get BinTree component");
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement