Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 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. if (x.compareTo(t.info) < 0) {
  236. E tmp = findMax(t.left).info;
  237. if (tmp.equals(x)) {
  238. return t.info;
  239. } else {
  240. return getSuccessor(t.left, x);
  241. }
  242. } else if (x.compareTo(t.info) > 0) {
  243. return getSuccessor(t.right, x);
  244. } else {
  245. return findMin(t.right).info;
  246. }
  247. }
  248.  
  249. public E getSuccessor(E x) {
  250. return getSuccessor(root, x);
  251. }
  252.  
  253. public int broj_na_teminja_na_dlabocina_n_pom(BNode<E> root, int k) {
  254. //vasiot kod tuka
  255. if (k == 0) {
  256. if (root != null) {
  257. return 1;
  258. } else {
  259. return 0;
  260. }
  261. } else if (root == null) {
  262. return 0;
  263. } else {
  264. k--;
  265. return broj_na_teminja_na_dlabocina_n_pom(root.left, k) + broj_na_teminja_na_dlabocina_n_pom(root.right, k);
  266. }
  267. }
  268.  
  269. public int broj_na_teminja_na_dlabocina_n(int k) {
  270. return broj_na_teminja_na_dlabocina_n_pom(this.root, k);
  271. }
  272.  
  273. public int calculateHeight(BNode<E> t) {
  274. //vasiot kod tuka
  275. if (t == null) {
  276. return 0;
  277. }
  278. if (t.left == null && t.right == null) {
  279. return 1;
  280. }
  281. return 1 + Math.max(calculateHeight(t.left), calculateHeight(t.right));
  282. }
  283.  
  284. }
  285.  
  286. public class BinarnoDrvo {
  287.  
  288. public static void main(String[] args) throws Exception {
  289. int i, j, k;
  290.  
  291. BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
  292.  
  293. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  294. int N = Integer.parseInt(br.readLine());
  295.  
  296. for (i = 0; i < N; i++) {
  297. int num = Integer.parseInt(br.readLine());
  298. tree.insert(new Integer(num));
  299. }
  300.  
  301. int x = Integer.parseInt(br.readLine());
  302.  
  303. BNode<Integer> t = tree.find(x);
  304. int rez = tree.calculateHeight(t);
  305. System.out.println(rez);
  306.  
  307. int kraj = tree.broj_na_teminja_na_dlabocina_n(rez);
  308. System.out.println(kraj);
  309. br.close();
  310. }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement