SashkoKlincharov

[Java][АПС] - Најди го бројот

Feb 6th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class FindTheNum { //copied
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Scanner scan = new Scanner(System.in);
  8.  
  9. int n = scan.nextInt();
  10. int p = scan.nextInt();
  11. BinarySearchTree<Integer> tree = new BinarySearchTree<>();
  12.  
  13. for (int i=0; i<n; ++i)
  14. tree.insert(scan.nextInt());
  15.  
  16.  
  17. System.out.println(tree.pThNode(p,1)-1000);
  18. }
  19.  
  20.  
  21. }
  22.  
  23. class BinarySearchTree<E extends Comparable<E>> {
  24.  
  25. private BNode<E> root;
  26. public BinarySearchTree() {
  27. root = null;
  28. }
  29. public int pThNode(int p,int counter ) {
  30. // TODO Auto-generated method stub
  31. return pThNode(root,p,counter );
  32. }
  33.  
  34. public int pThNode(BNode<E> node, int p,int counter ){
  35. // System.out.println( );
  36. if(p==1) {
  37. return (Integer)node.info;
  38. }
  39. else if(counter==p) {
  40. return (Integer)node.info+1000;
  41. }
  42. else {
  43. counter+=1;
  44. // System.out.println( );
  45. if(node.left!=null&&counter<=p)
  46. counter = pThNode(node.left,p,counter);
  47.  
  48. if(node.right!=null&&counter<=p)
  49. counter = pThNode(node.right,p,counter );
  50. }
  51.  
  52.  
  53.  
  54. return counter;
  55.  
  56.  
  57.  
  58. }
  59. public void insert(E x) {
  60. root = insert(x, root);
  61. }
  62.  
  63. private BNode<E> insert(E x, BNode<E> t) {
  64. if (t == null) {
  65. t = new BNode<E>(x, null, null);
  66. } else if (x.compareTo(t.info) < 0) {
  67. t.left = insert(x, t.left);
  68. } else if (x.compareTo(t.info) > 0) {
  69. t.right = insert(x, t.right);
  70. } else;
  71. return t;
  72. }
  73.  
  74. public BNode<E> find(E x) {
  75. return find(x, root);
  76. }
  77. private BNode<E> find(E x, BNode<E> t) {
  78. if (t == null)
  79. return null;
  80.  
  81. if (x.compareTo(t.info) < 0) {
  82. return find(x, t.left);
  83. } else if (x.compareTo(t.info) > 0) {
  84. return find(x, t.right);
  85. } else {
  86. return t;
  87. }
  88. }
  89. public boolean isEmpty() {
  90. return root == null;
  91. }
  92. public void printTree() {
  93. if(isEmpty())
  94. System.out.println("Empty Tree");
  95. else {
  96. printTree(root);
  97. }
  98. }
  99. public void printTree(BNode<E> node) {
  100. if(node.left!=null) {
  101. printTree(node.left);
  102. }
  103. System.out.println(node.info);
  104. if(node.right!=null) {
  105. printTree(node.right);
  106. }
  107. }
  108. }
  109.  
  110. class BNode<E extends Comparable<E>> {
  111.  
  112. public E info;
  113. public BNode<E> left;
  114. public BNode<E> right;
  115.  
  116. public BNode(E info) {
  117. this.info = info;
  118. left = null;
  119. right = null;
  120. }
  121.  
  122. public BNode(E info, BNode<E> left, BNode<E> right) {
  123. this.info = info;
  124. this.left = left;
  125. this.right = right;
  126. }
  127.  
  128. }
Add Comment
Please, Sign In to add comment