Advertisement
Guest User

Untitled

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