Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.TreeSet;
  4.  
  5. class Node<T extends Comparable<T>>{
  6. T info;
  7. Node<T> left,right;
  8. boolean isDeleted;
  9. public Node(T element,Node<T> l,Node<T> r) {
  10. info = element;
  11. left = l;
  12. right = r;
  13. isDeleted = false;
  14. }
  15.  
  16. }
  17.  
  18. class BinaryTreeSet<T extends Comparable<T>>{
  19. TreeSet<T> tree;
  20. String res;
  21.  
  22. public BinaryTreeSet() {
  23. tree=new TreeSet<T>();
  24. res="";
  25. }
  26. public void addElement(T element){
  27. if(!tree.contains(element))
  28. tree.add(element);
  29. }
  30. public boolean contains(T element){
  31. return tree.contains(element);
  32. }
  33. public boolean removeElement(T element){
  34. return tree.remove(element);
  35. }
  36.  
  37. @Override
  38. public String toString() {
  39. if(tree.isEmpty())return "EMPTY";
  40. String res = tree.toString();
  41. res = res.replace(",","");
  42. res = res.replace("[", "");
  43. res = res.replace("]","");
  44. return res;
  45. }
  46.  
  47. }
  48.  
  49. public class BinaryTreeSetTest {
  50.  
  51. public static void main(String[] args) {
  52. Scanner jin = new Scanner(System.in);
  53. int t = jin.nextInt();
  54. if ( t == 0 ) {
  55. BinaryTreeSet<Integer> ts = new BinaryTreeSet<Integer>();
  56. while ( jin.hasNextInt() ) {
  57. ts.addElement(jin.nextInt());
  58. }
  59. System.out.println(ts);
  60. }
  61. if ( t == 1 ) {
  62. BinaryTreeSet<String> ts = new BinaryTreeSet<String>();
  63. while ( true ) {
  64. String next = jin.next();
  65. if ( next.equals("stop") ) break;
  66. ts.addElement(next);
  67. }
  68. System.out.println(ts);
  69. }
  70. if ( t == 2 ) {
  71. BinaryTreeSet<Long> ts = new BinaryTreeSet<Long>();
  72. while ( jin.hasNextLong() ) {
  73. ts.addElement(jin.nextLong());
  74. }
  75. jin.next();
  76. System.out.println(ts);
  77. while ( jin.hasNextLong() ) {
  78. System.out.println(ts.contains(jin.nextLong()));
  79. }
  80. System.out.println(ts);
  81. }
  82. if ( t == 3 ) {
  83. BinaryTreeSet<String> ts = new BinaryTreeSet<String>();
  84. int counter = 0;
  85. while ( true ) {
  86. if ( counter % 20 == 0 ) System.out.println(ts);
  87. ++counter;
  88. String next = jin.next();
  89. if ( next.equals("stop") ) break;
  90. if ( next.equals("add") ) {
  91. ts.addElement(jin.next());
  92. }
  93. if ( next.equals("remove") ) {
  94. ts.removeElement(jin.next());
  95. }
  96. if ( next.equals("query") ) {
  97. System.out.println(ts.contains(jin.next()));
  98. }
  99. }
  100. System.out.println(ts);
  101. }
  102. if ( t == 4 ) {
  103. BinaryTreeSet<Long> ts = new BinaryTreeSet<Long>();
  104. TreeSet<Long> control_set = new TreeSet<Long>();
  105. ArrayList<Long> all = new ArrayList<Long>();
  106. all.add(5L);
  107. int n = jin.nextInt();
  108. boolean exact = true;
  109. for ( int i = 0 ; exact&&i < n ; ++i ) {
  110. if ( Math.random() < 0.4 ) {
  111. if ( Math.random() < 0.6 ) {
  112. long to_add = (long)(Math.random()*98746516548964156L);
  113. ts.addElement(to_add);
  114. control_set.add(to_add);
  115. all.add(to_add);
  116. }
  117. else {
  118. int add_idx = (int)(Math.random()*all.size());
  119. long to_add = all.get(add_idx);
  120. ts.addElement(to_add);
  121. control_set.add(to_add);
  122. }
  123. }
  124. else {
  125. if ( Math.random() < 0.4 ) {
  126. if ( Math.random() < 0.1 ) {
  127. long to_remove = (long)(Math.random()*98746516548964156L);
  128. ts.removeElement(to_remove);
  129. control_set.remove(to_remove);
  130. }
  131. else {
  132. int remove_idx = (int)(Math.random()*all.size());
  133. long to_remove = all.get(remove_idx);
  134. ts.removeElement(to_remove);
  135. control_set.remove(to_remove);
  136. }
  137. }
  138. else {
  139. if ( Math.random() < 0.3 ) {
  140. long to_query = (long)(Math.random()*98746516548964156L);
  141. exact &= ts.contains(to_query)==control_set.contains(to_query);
  142. }
  143. else {
  144. int query_idx = (int)(Math.random()*all.size());
  145. long to_query = all.get(query_idx);
  146. exact &= ts.contains(to_query)==control_set.contains(to_query);
  147. }
  148. }
  149. }
  150. }
  151. System.out.println(exact);
  152. }
  153. }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement