Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package pkg1zad;
  7.  
  8. class BNode<E extends Comparable<E>> {
  9.  
  10. public E info;
  11. public BNode<E> left;
  12. public BNode<E> right;
  13.  
  14. public BNode(E info) {
  15. this.info = info;
  16. left = null;
  17. right = null;
  18. }
  19.  
  20. public BNode(E info, BNode<E> left, BNode<E> right) {
  21. this.info = info;
  22. this.left = left;
  23. this.right = right;
  24. }
  25.  
  26. }
  27.  
  28.  
  29. //
  30. // CONSTRUCTION: with no initializer
  31. //
  32. // ******************PUBLIC OPERATIONS*********************
  33. // void insert( x ) --> Insert x
  34. // void remove( x ) --> Remove x
  35. // Comparable find( x ) --> Return item that matches x
  36. // Comparable findMin( ) --> Return smallest item
  37. // Comparable findMax( ) --> Return largest item
  38. // boolean isEmpty( ) --> Return true if empty; else false
  39. // void makeEmpty( ) --> Remove all items
  40. // void printTree( ) --> Print tree in sorted order
  41. /**
  42. * Implements an unbalanced binary search tree.
  43. * Note that all "matching" is based on the compareTo method.
  44. * @author Mark Allen Weiss
  45. */
  46. class BinarySearchTree<E extends Comparable<E>> {
  47.  
  48. /** The tree root. */
  49. private BNode<E> root;
  50.  
  51. /**
  52. * Construct the tree.
  53. */
  54. public BinarySearchTree() {
  55. root = null;
  56. }
  57.  
  58. /**
  59. * Insert into the tree; duplicates are ignored.
  60. * @param x the item to insert.
  61. */
  62. public void insert(E x) {
  63. root = insert(x, root);
  64. }
  65.  
  66. /**
  67. * Remove from the tree. Nothing is done if x is not found.
  68. * @param x the item to remove.
  69. */
  70. public void remove(E x) {
  71. root = remove(x, root);
  72. }
  73.  
  74. /**
  75. * Find the smallest item in the tree.
  76. * @return smallest item or null if empty.
  77. */
  78. public E findMin() {
  79. return elementAt(findMin(root));
  80. }
  81.  
  82. /**
  83. * Find the largest item in the tree.
  84. * @return the largest item of null if empty.
  85. */
  86. public E findMax() {
  87. return elementAt(findMax(root));
  88. }
  89.  
  90. /**
  91. * Find an item in the tree.
  92. * @param x the item to search for.
  93. * @return the matching item or null if not found.
  94. */
  95. public BNode<E> find(E x) {
  96. return find(x, root);
  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. * Internal method to print a subtree in sorted order.
  228. * @param t the node that roots the tree.
  229. */
  230. private void printTree(BNode<E> t) {
  231. if (t != null) {
  232. printTree(t.left);
  233. System.out.println(t.info);
  234. printTree(t.right);
  235. }
  236. }
  237.  
  238. // Test program
  239.  
  240. }
  241. public class Main {
  242.  
  243. /**
  244. * @param args the command line arguments
  245. */
  246. public static void main(String[] args) {
  247. // TODO code application logic here
  248.  
  249. int niza[];
  250. niza = new int[]{23,5,17,32,2,8,4,37,3,5,27,12,15,19,20,6,3,9,29,11};
  251. //smestuvanje na niza vo drvo
  252. BinarySearchTree<Integer> tree=new BinarySearchTree();
  253. for(int i=0;i<niza.length;i++)
  254. {
  255. tree.insert(niza[i]);
  256. }
  257. //izbrisi el 6
  258.  
  259. tree.remove(6);
  260. tree.printTree();
  261.  
  262. }
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement