Advertisement
MarijaDzingova

Untitled

Dec 16th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.TreeSet;
  4.  
  5. public class BinaryTreeSetTest {
  6.  
  7. public static void main(String[] args) {
  8. Scanner jin = new Scanner(System.in);
  9. int t = jin.nextInt();
  10. if ( t == 0 ) {
  11. BinaryTreeSet<Integer> ts = new BinaryTreeSet<Integer>();
  12. while ( jin.hasNextInt() ) {
  13. ts.addElement(jin.nextInt());
  14. }
  15. System.out.println(ts);
  16. }
  17. if ( t == 1 ) {
  18. BinaryTreeSet<String> ts = new BinaryTreeSet<String>();
  19. while ( true ) {
  20. String next = jin.next();
  21. if ( next.equals("stop") ) break;
  22. ts.addElement(next);
  23. }
  24. System.out.println(ts);
  25. }
  26. if ( t == 2 ) {
  27. BinaryTreeSet<Long> ts = new BinaryTreeSet<Long>();
  28. while ( jin.hasNextLong() ) {
  29. ts.addElement(jin.nextLong());
  30. }
  31. jin.next();
  32. System.out.println(ts);
  33. while ( jin.hasNextLong() ) {
  34. System.out.println(ts.contains(jin.nextLong()));
  35. }
  36. System.out.println(ts);
  37. }
  38. if ( t == 3 ) {
  39. BinaryTreeSet<String> ts = new BinaryTreeSet<String>();
  40. int counter = 0;
  41. while ( true ) {
  42. if ( counter % 20 == 0 ) System.out.println(ts);
  43. ++counter;
  44. String next = jin.next();
  45. if ( next.equals("stop") ) break;
  46. if ( next.equals("add") ) {
  47. ts.addElement(jin.next());
  48. }
  49. if ( next.equals("remove") ) {
  50. ts.removeElement(jin.next());
  51. }
  52. if ( next.equals("query") ) {
  53. System.out.println(ts.contains(jin.next()));
  54. }
  55. }
  56. System.out.println(ts);
  57. }
  58. if ( t == 4 ) {
  59. BinaryTreeSet<Long> ts = new BinaryTreeSet<Long>();
  60. TreeSet<Long> control_set = new TreeSet<Long>();
  61. ArrayList<Long> all = new ArrayList<Long>();
  62. all.add(5L);
  63. int n = jin.nextInt();
  64. boolean exact = true;
  65. for ( int i = 0 ; exact&&i < n ; ++i ) {
  66. if ( Math.random() < 0.4 ) {
  67. if ( Math.random() < 0.6 ) {
  68. long to_add = (long)(Math.random()*98746516548964156L);
  69. ts.addElement(to_add);
  70. control_set.add(to_add);
  71. all.add(to_add);
  72. }
  73. else {
  74. int add_idx = (int)(Math.random()*all.size());
  75. long to_add = all.get(add_idx);
  76. ts.addElement(to_add);
  77. control_set.add(to_add);
  78. }
  79. }
  80. else {
  81. if ( Math.random() < 0.4 ) {
  82. if ( Math.random() < 0.1 ) {
  83. long to_remove = (long)(Math.random()*98746516548964156L);
  84. ts.removeElement(to_remove);
  85. control_set.remove(to_remove);
  86. }
  87. else {
  88. int remove_idx = (int)(Math.random()*all.size());
  89. long to_remove = all.get(remove_idx);
  90. ts.removeElement(to_remove);
  91. control_set.remove(to_remove);
  92. }
  93. }
  94. else {
  95. if ( Math.random() < 0.3 ) {
  96. long to_query = (long)(Math.random()*98746516548964156L);
  97. exact &= ts.contains(to_query)==control_set.contains(to_query);
  98. }
  99. else {
  100. int query_idx = (int)(Math.random()*all.size());
  101. long to_query = all.get(query_idx);
  102. exact &= ts.contains(to_query)==control_set.contains(to_query);
  103. }
  104. }
  105. }
  106. }
  107. System.out.println(exact);
  108. }
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement