SashkoKlincharov

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

Feb 6th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. //package binarysearchtree;
  2.  
  3. // BinarySearchTree class
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.util.Random;
  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 E predeCessor(BNode<E> node) {
  207. return predeCessor(root,node);
  208. }
  209. public E predeCessor(BNode<E> root,BNode<E> node) {
  210. if(node.left!=null) {
  211. return findMax(node.left).info;
  212. }
  213. else {
  214. BNode<E> store = new BNode<E>();
  215. while(root!=node) {
  216. if(root.info.compareTo(node.info)<0) {
  217. store = root;
  218. root = root.right;
  219. }
  220. else {
  221. root = root.left;
  222. }
  223. }
  224. return store.info;
  225. }
  226. }
  227.  
  228. /**
  229. * Internal method to print a subtree in sorted order.
  230. * @param t the node that roots the tree.
  231. */
  232. private void printTree(BNode<E> t) {
  233. if (t != null) {
  234. printTree(t.left);
  235. System.out.println(t.info);
  236. printTree(t.right);
  237. }
  238. }
  239.  
  240. // Test program
  241. public static void main(String[] args) {
  242. int i,j,k;
  243.  
  244. Random r = new Random(System.currentTimeMillis());
  245. BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>();
  246.  
  247. for (i=0;i<1000;i++)
  248. bst.insert(r.nextInt(1000000));
  249.  
  250. bst.printTree();
  251.  
  252. }
  253. }
  254.  
  255. class BNode<E> {
  256.  
  257. public E info;
  258. public BNode<E> left;
  259. public BNode<E> right;
  260.  
  261. static int LEFT = 1;
  262. static int RIGHT = 2;
  263.  
  264. public BNode(E info) {
  265. this.info = info;
  266. left = null;
  267. right = null;
  268. }
  269.  
  270. public BNode() {
  271. this.info = null;
  272. left = null;
  273. right = null;
  274. }
  275.  
  276. public BNode(E info, BNode<E> left, BNode<E> right) {
  277. this.info = info;
  278. this.left = left;
  279. this.right = right;
  280. }
  281.  
  282. }
  283.  
  284. public class InOrderPredecessor {
  285.  
  286. public static void main(String[] args) throws Exception, IOException {
  287. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  288. int n = Integer.parseInt(br.readLine());
  289.  
  290. BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
  291. for(int i=0;i<n;i++)
  292. tree.insert(Integer.parseInt(br.readLine()));
  293.  
  294. int sebara = Integer.parseInt(br.readLine());
  295. BNode<Integer> node = tree.find(sebara);
  296. System.out.println(tree.predeCessor(node));
  297. }
  298.  
  299. }
Add Comment
Please, Sign In to add comment