Guest User

Untitled

a guest
Oct 24th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. package exercise1;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5.  
  6. /**
  7. * A linked implementation of a binary search tree.
  8. * Duplicates of elements are not allowed in the tree.
  9. *
  10. * @author mlch
  11. */
  12. public class LinkedBinarySearchTree<E extends Comparable<E>> implements BinarySearchTree<E>,
  13. Iterable<E>
  14. {
  15. private BinaryTree<E> tree = new LinkedBinaryTree<E>();
  16. private int internalCount; //count of internal nodes in the binary search tree
  17.  
  18. /**
  19. * Creates a new empty LinkedBinarySearchTree
  20. * (that is, a tree with one external node).
  21. */
  22. public LinkedBinarySearchTree()
  23. {
  24. tree.addRoot(null);
  25. internalCount = 0;
  26. }
  27.  
  28. /**
  29. * Returns the count of elements in the binary search tree
  30. * (that is, the count of internal nodes in the tree).
  31. */
  32. @Override
  33. public int size()
  34. {
  35. return internalCount;
  36. }
  37.  
  38. /**
  39. * Returns whether the binary search tree is empty
  40. * (that is, whether it has any internal nodes).
  41. */
  42. @Override
  43. public boolean isEmpty()
  44. {
  45. return internalCount == 0;
  46. }
  47.  
  48. //Returns the position of the element in the (sub)tree rooted at the specified position.
  49. //If the element is not found, the external position, where the element should be
  50. //inserted in the binary search tree, is returned.
  51. //Requires: The position is valid.
  52. private Position<E> treeSearch(E e, Position<E> p)
  53. {
  54. if (tree.isExternal(p))
  55. return p;
  56.  
  57. if (p.element().compareTo(e) == 0)
  58. return p;
  59. else if (p.element().compareTo(e) < 0)
  60. return treeSearch(e, tree.right(p));
  61. else
  62. return treeSearch(e, tree.left(p));
  63. }
  64.  
  65. /**
  66. * Inserts the element in the binary search tree
  67. * (but only if the element is not in the tree already).
  68. * @param e - the element to be inserted
  69. */
  70. @Override
  71. public void insert(E e)
  72. {
  73. Position<E> p = treeSearch(e, tree.root());
  74. if (tree.isExternal(p)) {
  75. tree.replace(p, e);
  76. tree.insertLeft(p, null);
  77. tree.insertRight(p, null);
  78. internalCount++;
  79. }
  80. }
  81.  
  82. /**
  83. * Removes the element from the binary search tree
  84. * (if the element is in the tree).
  85. * @param e - the element to be removed
  86. */
  87. @Override
  88. public void remove(E e)
  89. {
  90. Position<E> remPos = treeSearch(e, tree.root());
  91. if (tree.isInternal(remPos)) {
  92. if (tree.isExternal(tree.left(remPos))) {
  93. remPos = tree.left(remPos);
  94. }
  95. else if (tree.isExternal(tree.right(remPos))) {
  96. remPos = tree.right(remPos);
  97. }
  98. else {
  99. Position<E> swapPos = remPos;
  100. remPos = tree.right(remPos);
  101. while (tree.isInternal(tree.left(remPos))) {
  102. remPos = tree.left(remPos);
  103. }
  104. tree.replace(swapPos, remPos.element());
  105. remPos = tree.left(remPos);
  106. }
  107. removeExternal(remPos);
  108. }
  109. }
  110.  
  111. /**
  112. * Returns the smallest element in the binary search tree.
  113. * If the tree is empty, null is returned
  114. */
  115. public E findMin() {
  116. if (isEmpty()) {
  117. return null;
  118. }
  119.  
  120. Position<E> min = tree.root();
  121.  
  122. while (tree.hasLeft(tree.left(min))) {
  123. min = tree.left(min);
  124. }
  125.  
  126. return min.element();
  127. }
  128.  
  129. /**
  130. * Removes and returns the smallest element in the binary search tree.
  131. * If the tree is empty, null is returned.
  132. */
  133. public E removeMin() {
  134. E e = findMin();
  135. remove(e);
  136. return e;
  137. }
  138.  
  139. //Removes the node and the parent above the node,
  140. //replacing the parent with the node's sibling.
  141. //Requires: The node is an external node.
  142. private void removeExternal(Position<E> p)
  143. {
  144. if (!tree.isExternal(p))
  145. throw new RuntimeException("The node is not external.");
  146.  
  147. Position<E> parent = tree.parent(p);
  148. tree.remove(p);
  149. tree.remove(parent);
  150. internalCount--;
  151. }
  152.  
  153. /**
  154. * Returns the position of the element in the binary search tree.
  155. * If the element is not found, null is returned.
  156. * @param e - the element to find the position of
  157. */
  158. @Override
  159. public Position<E> search(E e)
  160. {
  161. Position<E> p = treeSearch(e, tree.root());
  162. if (tree.isInternal(p))
  163. return p;
  164. else
  165. return null;
  166. }
  167.  
  168. /**
  169. * Returns an iterator on the binary search tree.
  170. */
  171. @Override
  172. public Iterator<E> iterator()
  173. {
  174. ArrayList<E> list = new ArrayList<E>();
  175. for (E e : tree) {
  176. if (e != null)
  177. list.add(e);
  178. }
  179. return list.iterator();
  180. }
  181.  
  182. }
Add Comment
Please, Sign In to add comment