SashkoKlincharov

[Java][АПС] - InorderSuccesor

Feb 6th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3.  
  4. class BNode<E extends Comparable<E>> {
  5.  
  6. public E info;
  7. public BNode<E> left;
  8. public BNode<E> right;
  9.  
  10. public BNode(E info) {
  11. this.info = info;
  12. left = null;
  13. right = null;
  14. }
  15. public BNode() {
  16. this.info = null;
  17. left = null;
  18. right = null;
  19. }
  20.  
  21.  
  22. public BNode(E info, BNode<E> left, BNode<E> right) {
  23. this.info = info;
  24. this.left = left;
  25. this.right = right;
  26. }
  27.  
  28. }
  29.  
  30. class BinarySearchTree<E extends Comparable<E>> {
  31.  
  32. /** The tree root. */
  33. private BNode<E> root;
  34.  
  35. /**
  36. * Construct the tree.
  37. */
  38. public BinarySearchTree() {
  39. root = null;
  40. }
  41.  
  42. /**
  43. * Insert into the tree; duplicates are ignored.
  44. * @param x the item to insert.
  45. */
  46. public void insert(E x) {
  47. root = insert(x, root);
  48. }
  49.  
  50. /**
  51. * Remove from the tree. Nothing is done if x is not found.
  52. * @param x the item to remove.
  53. */
  54. public void remove(E x) {
  55. root = remove(x, root);
  56. }
  57.  
  58. /**
  59. * Find the smallest item in the tree.
  60. * @return smallest item or null if empty.
  61. */
  62. public E findMin() {
  63. return elementAt(findMin(root));
  64. }
  65.  
  66. /**
  67. * Find the largest item in the tree.
  68. * @return the largest item of null if empty.
  69. */
  70. public E findMax() {
  71. return elementAt(findMax(root));
  72. }
  73.  
  74. /**
  75. * Find an item in the tree.
  76. * @param x the item to search for.
  77. * @return the matching item or null if not found.
  78. */
  79. public BNode<E> find(E x) {
  80. return find(x, root);
  81. }
  82.  
  83. /**
  84. * Make the tree logically empty.
  85. */
  86. public void makeEmpty() {
  87. root = null;
  88. }
  89.  
  90. /**
  91. * Test if the tree is logically empty.
  92. * @return true if empty, false otherwise.
  93. */
  94. public boolean isEmpty() {
  95. return root == null;
  96. }
  97.  
  98. /**
  99. * Print the tree contents in sorted order.
  100. */
  101. public void printTree() {
  102. if (isEmpty()) {
  103. System.out.println("Empty tree");
  104. } else {
  105. printTree(root);
  106. }
  107. }
  108.  
  109. /**
  110. * Internal method to get element field.
  111. * @param t the node.
  112. * @return the element field or null if t is null.
  113. */
  114. private E elementAt(BNode<E> t) {
  115. if (t == null)
  116. return null;
  117. return t.info;
  118. }
  119.  
  120. /**
  121. * Internal method to insert into a subtree.
  122. * @param x the item to insert.
  123. * @param t the node that roots the tree.
  124. * @return the new root.
  125. */
  126. private BNode<E> insert(E x, BNode<E> t) {
  127. if (t == null) {
  128. t = new BNode<E>(x, null, null);
  129. } else if (x.compareTo(t.info) < 0) {
  130. t.left = insert(x, t.left);
  131. } else if (x.compareTo(t.info) > 0) {
  132. t.right = insert(x, t.right);
  133. } else; // Duplicate; do nothing
  134. return t;
  135. }
  136.  
  137. /**
  138. * Internal method to remove from a subtree.
  139. * @param x the item to remove.
  140. * @param t the node that roots the tree.
  141. * @return the new root.
  142. */
  143. private BNode<E> remove(Comparable x, BNode<E> t) {
  144. if (t == null)
  145. return t; // Item not found; do nothing
  146.  
  147. if (x.compareTo(t.info) < 0) {
  148. t.left = remove(x, t.left);
  149. } else if (x.compareTo(t.info) > 0) {
  150. t.right = remove(x, t.right);
  151. } else if (t.left != null && t.right != null) { // Two children
  152. t.info = findMin(t.right).info;
  153. t.right = remove(t.info, t.right);
  154. } else {
  155. if (t.left != null)
  156. return t.left;
  157. else
  158. return t.right;
  159. }
  160. return t;
  161. }
  162.  
  163. /**
  164. * Internal method to find the smallest item in a subtree.
  165. * @param t the node that roots the tree.
  166. * @return node containing the smallest item.
  167. */
  168. private BNode<E> findMin(BNode<E> t) {
  169. if (t == null) {
  170. return null;
  171. } else if (t.left == null) {
  172. return t;
  173. }
  174. return findMin(t.left);
  175. }
  176.  
  177. /**
  178. * Internal method to find the largest item in a subtree.
  179. * @param t the node that roots the tree.
  180. * @return node containing the largest item.
  181. */
  182. private BNode<E> findMax(BNode<E> t) {
  183. if (t == null) {
  184. return null;
  185. } else if (t.right == null) {
  186. return t;
  187. }
  188. return findMax(t.right);
  189. }
  190.  
  191. /**
  192. * Internal method to find an item in a subtree.
  193. * @param x is item to search for.
  194. * @param t the node that roots the tree.
  195. * @return node containing the matched item.
  196. */
  197. private BNode<E> find(E x, BNode<E> t) {
  198. if (t == null)
  199. return null;
  200.  
  201. if (x.compareTo(t.info) < 0) {
  202. return find(x, t.left);
  203. } else if (x.compareTo(t.info) > 0) {
  204. return find(x, t.right);
  205. } else {
  206. return t; // Match
  207. }
  208. }
  209.  
  210. /**
  211. * Internal method to print a subtree in sorted order.
  212. * @param t the node that roots the tree.
  213. */
  214. private void printTree(BNode<E> t) {
  215. if (t != null) {
  216. printTree(t.left);
  217. System.out.println(t.info);
  218. printTree(t.right);
  219. }
  220. }
  221.  
  222. public BNode<E> getSuccessor(BNode<E> root, BNode<E> node) {
  223. if(node.right!=null) {
  224. return findMin(node.right);
  225. }
  226. else {
  227. BNode<E> store = new BNode<E>();
  228. while(root!=node) {
  229. if(root.info.compareTo((E)node.info)>0) {
  230. store = root;
  231. root = root.left;
  232. }
  233. else {
  234. root = root.right;
  235. }
  236. }
  237. return store;
  238. }
  239.  
  240. }
  241.  
  242. public BNode<E> getSuccessor(E x) {
  243. return getSuccessor(root, find(x));
  244. }
  245.  
  246. }
  247.  
  248. public class InorderSuccessor {
  249.  
  250. public static void main(String[] args) throws Exception {
  251. int i,j,k;
  252.  
  253. BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
  254.  
  255. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  256. int N = Integer.parseInt(br.readLine());
  257.  
  258. for (i=0;i<N;i++) {
  259. int num = Integer.parseInt(br.readLine());
  260. tree.insert(num);
  261. }
  262.  
  263. br.close();
  264.  
  265. int prev = tree.findMin();
  266. System.out.println(prev);
  267.  
  268. for (i=1;i<N;i++) {
  269. int tmp = tree.getSuccessor(prev).info;
  270. System.out.println(tmp);
  271. prev = tmp;
  272. }
  273.  
  274. }
  275.  
  276. }
Add Comment
Please, Sign In to add comment