SashkoKlincharov

[Java][АПС] - Бинарно пребарувачко дрво

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