Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. /**
  2.      * Grąžinamas aibės poaibis iki elemento.
  3.      *
  4.      * @param element - Aibės elementas.
  5.      * @return Grąžinamas aibės poaibis iki elemento.
  6.      */
  7.     @Override
  8.     public SetADT<E> headSet(E element) {
  9.         if (element == null) {
  10.             throw new IllegalArgumentException("Element is null in headSet(E element)");
  11.         }
  12.  
  13.         BstSetKTU<E> headSet = new BstSetKTU<>();
  14.  
  15.         if (contains(element)) {
  16.             for (Iterator<E> it = iterator(); it.hasNext();) {
  17.                 E e = it.next();
  18.                 int cmp = (c == null) ? e.compareTo(element) : c.compare(e, element);
  19.                 if (cmp <= 0) {
  20.                     headSet.add(e);
  21.                 }
  22.             }
  23.         }
  24.  
  25.         return headSet;
  26.     }
  27.  
  28.     /**
  29.      * Grąžinamas aibės poaibis nuo elemento element1 iki element2.
  30.      *
  31.      * @param element1 - pradinis aibės poaibio elementas.
  32.      * @param element2 - galinis aibės poaibio elementas.
  33.      * @return Grąžinamas aibės poaibis nuo elemento element1 iki element2.
  34.      */
  35.     @Override
  36.     public SetADT<E> subSet(E element1, E element2) {
  37.         if (element1 == null || element2 == null) {
  38.             throw new IllegalArgumentException("Element is null in headSet(E element)");
  39.         }
  40.  
  41.         BstSetKTU<E> subSet = new BstSetKTU<>();
  42.  
  43.         if (contains(element1) && contains(element2)) {
  44.             for (Iterator<E> it = iterator(); it.hasNext();) {
  45.                 E e = it.next();
  46.                 int cmp1 = (c == null) ? e.compareTo(element1) : c.compare(e, element1);
  47.                 int cmp2 = (c == null) ? e.compareTo(element2) : c.compare(e, element2);
  48.                 if (cmp1 >= 0 && 0 >= cmp2) {
  49.                     subSet.add(e);
  50.                 }
  51.             }
  52.         }
  53.  
  54.         return subSet;
  55.     }
  56.  
  57.     /**
  58.      * Grąžinamas aibės poaibis nuo elemento.
  59.      *
  60.      * @param element - Aibės elementas.
  61.      * @return Grąžinamas aibės poaibis nuo elemento.
  62.      */
  63.     @Override
  64.     public SetADT<E> tailSet(E element) {
  65.         if (element == null) {
  66.             throw new IllegalArgumentException("Element is null in headSet(E element)");
  67.         }
  68.  
  69.         BstSetKTU<E> tailSet = new BstSetKTU<>();
  70.  
  71.         if (contains(element)) {
  72.             for (Iterator<E> it = iterator(); it.hasNext();) {
  73.                 E e = it.next();
  74.                 int cmp = (c == null) ? e.compareTo(element) : c.compare(e, element);
  75.                 if (cmp >= 0) {
  76.                     tailSet.add(e);
  77.                 }
  78.             }
  79.         }
  80.  
  81.         return tailSet;
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement