Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 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.             headSetRecursive(headSet, root, element);
  17.         }
  18.  
  19.         return headSet;
  20.     }
  21.  
  22.     /**
  23.      * Grąžinamas aibės poaibis nuo elemento element1 iki element2.
  24.      *
  25.      * @param element1 - pradinis aibės poaibio elementas.
  26.      * @param element2 - galinis aibės poaibio elementas.
  27.      * @return Grąžinamas aibės poaibis nuo elemento element1 iki element2.
  28.      */
  29.     @Override
  30.     public SetADT<E> subSet(E element1, E element2) {
  31.         if (element1 == null || element2 == null) {
  32.             throw new IllegalArgumentException("Element is null in headSet(E element)");
  33.         }
  34.  
  35.         BstSetKTU<E> subSet = new BstSetKTU<>();
  36.  
  37.         if (contains(element1) && contains(element2)) {
  38.             BstNode<E> n = getNode(element2);
  39.             subSetRecursive(subSet, n, element1);
  40.         }
  41.  
  42.         return subSet;
  43.     }
  44.  
  45.     /**
  46.      * Grąžinamas aibės poaibis nuo elemento.
  47.      *
  48.      * @param element - Aibės elementas.
  49.      * @return Grąžinamas aibės poaibis nuo elemento.
  50.      */
  51.     @Override
  52.     public SetADT<E> tailSet(E element) {
  53.         if (element == null) {
  54.             throw new IllegalArgumentException("Element is null in headSet(E element)");
  55.         }
  56.  
  57.         BstSetKTU<E> tailSet = new BstSetKTU<>();
  58.  
  59.         if (contains(element)) {
  60.             tailSet = (BstSetKTU<E>) subSetRecursive(tailSet, root, element);
  61.         }
  62.  
  63.         return tailSet;
  64.     }
  65.  
  66.     private SetADT<E> headSetRecursive(SetADT<E> headSet, BstNode<E> n, E element) {
  67.         if (n != null) {
  68.             int cmp = (c == null) ? n.element.compareTo(element) : c.compare(n.element, element);
  69.  
  70.             if (cmp <= 0) {
  71.                 headSet.add(n.element);
  72.             }
  73.  
  74.             headSet = headSetRecursive(headSet, n.left, element);
  75.             headSet = headSetRecursive(headSet, n.right, element);
  76.         }
  77.         return headSet;
  78.     }
  79.  
  80.     private SetADT<E> subSetRecursive(SetADT<E> subSet, BstNode<E> n, E element) {
  81.         if (n != null) {
  82.             int cmp = (c == null) ? n.element.compareTo(element) : c.compare(n.element, element);
  83.  
  84.             if (cmp >= 0) {
  85.                 subSet.add(n.element);
  86.             }
  87.  
  88.             subSet = subSetRecursive(subSet, n.left, element);
  89.             subSet = subSetRecursive(subSet, n.right, element);
  90.         }
  91.         return subSet;
  92.     }
  93.  
  94.     private BstNode<E> getNode(E element) {
  95.         if (element == null) {
  96.             throw new IllegalArgumentException("Element is null in get(E element)");
  97.         }
  98.  
  99.         BstNode<E> node = root;
  100.         while (node != null) {
  101.             int cmp = c.compare(element, node.element);
  102.  
  103.             if (cmp < 0) {
  104.                 node = node.left;
  105.             } else if (cmp > 0) {
  106.                 node = node.right;
  107.             } else {
  108.                 return node;
  109.             }
  110.         }
  111.  
  112.         return null;
  113.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement