Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. package LigaVerwaltung;
  2.  
  3. public class BinarySearchTree<ContentType extends ComparableContent<ContentType>>
  4. {
  5.  
  6. private class BSTNode<CT extends ComparableContent<CT>>
  7. {
  8.  
  9. private CT content;
  10. private BinarySearchTree<CT> left, right;
  11.  
  12. public BSTNode(CT pContent)
  13. {
  14.  
  15. this.content = pContent;
  16. left = new BinarySearchTree<CT>();
  17. right = new BinarySearchTree<CT>();
  18. }
  19.  
  20. }
  21.  
  22. private BSTNode<ContentType> node;
  23.  
  24. public BinarySearchTree()
  25. {
  26. this.node = null;
  27. }
  28.  
  29. public boolean isEmpty()
  30. {
  31. return this.node == null;
  32. }
  33.  
  34. public void insert(ContentType pContent)
  35. {
  36. if (pContent != null)
  37. {
  38. if (isEmpty())
  39. {
  40. this.node = new BSTNode<ContentType>(pContent);
  41. }
  42. else if (pContent.isLess(this.node.content))
  43. {
  44. this.node.left.insert(pContent);
  45. }
  46. else if (pContent.isGreater(this.node.content))
  47. {
  48. this.node.right.insert(pContent);
  49. }
  50. }
  51. }
  52.  
  53. public BinarySearchTree<ContentType> getLeftTree()
  54. {
  55. if (this.isEmpty())
  56. {
  57. return null;
  58. }
  59. else
  60. {
  61. return this.node.left;
  62. }
  63. }
  64.  
  65. public ContentType getContent()
  66. {
  67. if (this.isEmpty())
  68. {
  69. return null;
  70. }
  71. else
  72. {
  73. return this.node.content;
  74. }
  75. }
  76.  
  77. public BinarySearchTree<ContentType> getRightTree()
  78. {
  79. if (this.isEmpty())
  80. {
  81. return null;
  82. }
  83. else
  84. {
  85. return this.node.right;
  86. }
  87. }
  88.  
  89. public void remove(ContentType pContent)
  90. {
  91. if (isEmpty())
  92. {
  93. return;
  94. }
  95.  
  96. if (pContent.isLess(node.content))
  97. {
  98. node.left.remove(pContent);
  99. }
  100. else if (pContent.isGreater(node.content))
  101. {
  102. node.right.remove(pContent);
  103. }
  104. else
  105. {
  106. if (node.left.isEmpty())
  107. {
  108. if (node.right.isEmpty())
  109. {
  110. node = null;
  111. }
  112. else
  113. {
  114. node = getNodeOfRightSuccessor();
  115. }
  116. }
  117. else if (node.right.isEmpty())
  118. {
  119. node = getNodeOfLeftSuccessor();
  120. }
  121. else
  122. {
  123.  
  124. if (getNodeOfRightSuccessor().left.isEmpty())
  125. {
  126. node.content = getNodeOfRightSuccessor().content;
  127. node.right = getNodeOfRightSuccessor().right;
  128. }
  129. else
  130. {
  131. BinarySearchTree<ContentType> previous = node.right.ancestorOfSmallRight();
  132. BinarySearchTree<ContentType> smallest = previous.node.left;
  133. this.node.content = smallest.node.content;
  134. previous.remove(smallest.node.content);
  135. }
  136. }
  137. }
  138. }
  139.  
  140. public ContentType search(ContentType pContent)
  141. {
  142. if (this.isEmpty() || pContent == null)
  143. {
  144.  
  145. return null;
  146. }
  147. else
  148. {
  149. ContentType content = this.getContent();
  150. if (pContent.isLess(content))
  151. {
  152. return this.getLeftTree().search(pContent);
  153. }
  154. else if (pContent.isGreater(content))
  155. {
  156. return this.getRightTree().search(pContent);
  157. }
  158. else if (pContent.isEqual(content))
  159. {
  160. return content;
  161. }
  162. else
  163. {
  164.  
  165. return null;
  166. }
  167. }
  168. }
  169.  
  170. private BinarySearchTree<ContentType> ancestorOfSmallRight()
  171. {
  172. if (getNodeOfLeftSuccessor().left.isEmpty())
  173. {
  174. return this;
  175. }
  176. else
  177. {
  178. return node.left.ancestorOfSmallRight();
  179. }
  180. }
  181.  
  182. private BSTNode<ContentType> getNodeOfLeftSuccessor()
  183. {
  184. return node.left.node;
  185. }
  186.  
  187. private BSTNode<ContentType> getNodeOfRightSuccessor()
  188. {
  189. return node.right.node;
  190. }
  191.  
  192. }
  193.  
  194.  
  195.  
  196. package LigaVerwaltung;
  197.  
  198.  
  199. public interface ComparableContent<ContentType> {
  200.  
  201. public boolean isGreater(ContentType pContent);
  202.  
  203. public boolean isEqual(ContentType pContent);
  204.  
  205. public boolean isLess(ContentType pContent);
  206.  
  207. boolean isGreater(Verein pContent);
  208.  
  209. }
  210.  
  211.  
  212.  
  213.  
  214. package LigaVerwaltung;
  215.  
  216. public class Verein implements ComparableContent<Verein>
  217. {
  218. private String name;
  219. private int Platz;
  220.  
  221. public Verein(String pName, int pPlatz)
  222. {
  223. name=pName;
  224. Platz=pPlatz;
  225. }
  226.  
  227. public String getName()
  228. {
  229. return name;
  230. }
  231.  
  232. public int getPlatz()
  233. {
  234. return Platz;
  235. }
  236.  
  237. @Override
  238. public boolean isGreater( Verein pContent)
  239. {
  240. if(this.getPlatz()>pContent.getPlatz())
  241. {
  242. return false;
  243. }
  244. else
  245. {
  246. return true;
  247. }
  248. }
  249.  
  250. @Override
  251. public boolean isEqual(Verein pContent)
  252. {
  253.  
  254. return false;
  255. }
  256.  
  257. @Override
  258. public boolean isLess(Verein pContent)
  259. {
  260.  
  261. return false;
  262. }
  263.  
  264.  
  265. public void setName(String name)
  266. {
  267. this.name = name;
  268. }
  269.  
  270. public void setPlatz(int platz)
  271. {
  272. Platz = platz;
  273. }
  274.  
  275.  
  276. }
  277.  
  278.  
  279.  
  280.  
  281. package LigaVerwaltung;
  282.  
  283. public class Ligaverwaltung
  284. {
  285. private BinarySearchTree<Verein> dieLiga;
  286.  
  287. public static void main(String[]args)
  288. {
  289. new Ligaverwaltung();
  290. }
  291.  
  292. public Ligaverwaltung()
  293. {
  294. dieLiga= new BinarySearchTree<Verein>();
  295. dieLiga.insert(new Verein("L.A. Lakers", 10));
  296. dieLiga.insert(new Verein("Chicago Bulls", 1));
  297. dieLiga.insert(new Verein("Cleavland Caveliers", 3));
  298. dieLiga.insert(new Verein("Dallas Mavericks",12));
  299. dieLiga.insert(new Verein("Miami Heats", 9));
  300. dieLiga.insert(new Verein("Minnesota Timberwolves", 4));
  301. inOrder(dieLiga);
  302. }
  303.  
  304. public void inOrder(BinarySearchTree<Verein> baum)
  305. {
  306. if(!baum.getLeftTree().isEmpty()) inOrder(baum.getLeftTree());
  307. System.out.println(baum.getContent().getName() + ","+ baum.getContent().getPlatz());
  308. if(!baum.getRightTree().isEmpty()) inOrder(baum.getRightTree());
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement