Advertisement
Guest User

Untitled

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