Advertisement
Guest User

Untitled

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