SashkoKlincharov

[Java][АПС] - InorderSuccesor (на еден јазел)

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