Dépôt du be graphe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BinarySearchTree.java 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package org.insa.graphs.algorithm.utils;
  2. import java.util.SortedSet;
  3. import java.util.TreeSet;
  4. public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<E> {
  5. // Underlying implementation
  6. private final SortedSet<E> sortedSet;
  7. /**
  8. * Create a new empty binary search tree.
  9. */
  10. public BinarySearchTree() {
  11. this.sortedSet = new TreeSet<>();
  12. }
  13. /**
  14. * Create a copy of the given binary search tree.
  15. *
  16. * @param bst Binary search tree to copy.
  17. */
  18. public BinarySearchTree(BinarySearchTree<E> bst) {
  19. this.sortedSet = new TreeSet<>(bst.sortedSet);
  20. }
  21. @Override
  22. public boolean isEmpty() {
  23. return sortedSet.isEmpty();
  24. }
  25. @Override
  26. public int size() {
  27. return sortedSet.size();
  28. }
  29. @Override
  30. public void insert(E x) {
  31. sortedSet.add(x);
  32. }
  33. @Override
  34. public void remove(E x) throws ElementNotFoundException {
  35. if (!sortedSet.remove(x)) {
  36. throw new ElementNotFoundException(x);
  37. }
  38. }
  39. @Override
  40. public E findMin() throws EmptyPriorityQueueException {
  41. if (isEmpty()) {
  42. throw new EmptyPriorityQueueException();
  43. }
  44. return sortedSet.first();
  45. }
  46. @Override
  47. public E deleteMin() throws EmptyPriorityQueueException {
  48. E min = findMin();
  49. remove(min);
  50. return min;
  51. }
  52. }