fork download
  1. interface Tree
  2. {
  3. public Tree add(String word);
  4.  
  5. public boolean contains(String word);
  6.  
  7. public int size();
  8. }
  9.  
  10. class EmptyTree implements Tree
  11. {
  12. public Tree add(String word)
  13. {
  14. return new Leaf(word);
  15. }
  16.  
  17. public boolean contains(String word)
  18. {
  19. return false;
  20. }
  21.  
  22. public int size()
  23. {
  24. return 0;
  25. }
  26.  
  27. public String toString()
  28. {
  29. return "()";
  30. }
  31. }
  32.  
  33. class Leaf implements Tree
  34. {
  35. private final String _word;
  36.  
  37. public Leaf(String word)
  38. {
  39. _word = word;
  40. }
  41.  
  42. public Tree add(String word)
  43. {
  44. int order = word.compareTo(_word);
  45. if (order < 0) return new LeftTree(new Leaf(word), _word);
  46. if (order > 0) return new RightTree(_word, new Leaf(word));
  47. return this;
  48. }
  49.  
  50. public boolean contains(String word)
  51. {
  52. return word.compareTo(_word) == 0;
  53. }
  54.  
  55. public int size()
  56. {
  57. return 1;
  58. }
  59.  
  60. public String toString()
  61. {
  62. return "(" + _word + ")";
  63. }
  64. }
  65.  
  66. class LeftTree implements Tree
  67. {
  68. private final Tree _left;
  69. private final String _word;
  70.  
  71. public LeftTree(Tree left, String word)
  72. {
  73. _left = left;
  74. _word = word;
  75. }
  76.  
  77. public Tree add(String word)
  78. {
  79. int order = word.compareTo(_word);
  80. if (order < 0) return new LeftTree(_left.add(word), _word);
  81. if (order > 0) return new BinaryTree(_left, _word, new Leaf(word));
  82. return this;
  83. }
  84.  
  85. public boolean contains(String word)
  86. {
  87. int order = word.compareTo(_word);
  88. return (order == 0) || ((order < 0) && _left.contains(word));
  89. }
  90.  
  91. public int size()
  92. {
  93. return 1 + _left.size();
  94. }
  95.  
  96. public String toString()
  97. {
  98. return "(" + _left.toString() + " " + _word + ")";
  99. }
  100. }
  101.  
  102. class RightTree implements Tree
  103. {
  104. private final String _word;
  105. private final Tree _right;
  106.  
  107. public RightTree(String word, Tree right)
  108. {
  109. _word = word;
  110. _right = right;
  111. }
  112.  
  113. public Tree add(String word)
  114. {
  115. int order = word.compareTo(_word);
  116. if (order < 0) return new BinaryTree(new Leaf(word), _word, _right);
  117. if (order > 0) return new RightTree(_word,_right.add(word));
  118. return this;
  119. }
  120.  
  121. public boolean contains(String word)
  122. {
  123. int order = word.compareTo(_word);
  124. return (order == 0) || ((order > 0) && _right.contains(word));
  125. }
  126.  
  127. public int size()
  128. {
  129. return 1 + _right.size();
  130. }
  131.  
  132. public String toString()
  133. {
  134. return "(" + _word + " " + _right.toString() + ")";
  135. }
  136. }
  137.  
  138. class BinaryTree implements Tree
  139. {
  140. private final Tree _left;
  141. private final String _word;
  142. private final Tree _right;
  143.  
  144. public BinaryTree(Tree left, String word, Tree right)
  145. {
  146. _left = left;
  147. _word = word;
  148. _right = right;
  149. }
  150.  
  151. public Tree add(String word)
  152. {
  153. int order = word.compareTo(_word);
  154. if (order < 0) return new BinaryTree(_left.add(word), _word, _right);
  155. if (order > 0) return new BinaryTree(_left, _word, _right.add(word));
  156. return this;
  157. }
  158.  
  159. public boolean contains(String word)
  160. {
  161. int order = word.compareTo(_word);
  162. return (order == 0) || ((order < 0) && _left.contains(word)) || ((order > 0) && _right.contains(word));
  163. }
  164.  
  165. public int size()
  166. {
  167. return 1 + _left.size() + _right.size();
  168. }
  169.  
  170. public String toString()
  171. {
  172. return "(" + _left.toString() + " " + _word + " " + _right.toString() + ")";
  173. }
  174. }
  175.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
spoj: The program compiled successfully, but main class was not found.
      Main class should contain method: public static void main (String[] args).
stdout
Standard output is empty