Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Grąžinamas aibės poaibis iki elemento.
- *
- * @param element - Aibės elementas.
- * @return Grąžinamas aibės poaibis iki elemento.
- */
- @Override
- public SetADT<E> headSet(E element) {
- if (element == null) {
- throw new IllegalArgumentException("Element is null in headSet(E element)");
- }
- BstSetKTU<E> headSet = new BstSetKTU<>();
- // Jei elemento nėra aibėje
- if (!contains(element)) {
- return headSet;
- }
- BstNode<E> node = root;
- while (node != null) {
- int cmp = c.compare(element, node.element);
- if (cmp < 0) {
- headSet.add(node.element);
- node = node.left;
- } else if (cmp > 0) {
- headSet.add(node.element);
- node = node.right;
- } else {
- headSet.add(node.element);
- break;
- }
- }
- return headSet;
- }
- /**
- * Grąžinamas aibės poaibis nuo elemento element1 iki element2.
- *
- * @param element1 - pradinis aibės poaibio elementas.
- * @param element2 - galinis aibės poaibio elementas.
- * @return Grąžinamas aibės poaibis nuo elemento element1 iki element2.
- */
- @Override
- public SetADT<E> subSet(E element1, E element2) {
- if (element1 == null || element2 == null) {
- throw new IllegalArgumentException("Element is null in headSet(E element)");
- }
- BstSetKTU<E> subSet = new BstSetKTU<>();
- // Jei elemento nėra aibėje
- if (!contains(element1) && !contains(element2)) {
- return subSet;
- }
- BstNode<E> nodeStart = root;
- while (nodeStart != null) {
- int cmp = c.compare(element1, nodeStart.element);
- if (cmp < 0) {
- nodeStart = nodeStart.left;
- } else if (cmp > 0) {
- nodeStart = nodeStart.right;
- } else {
- subSet.add(nodeStart.element);
- break;
- }
- }
- while (nodeStart != null) {
- int cmp = c.compare(element2, nodeStart.element);
- if (cmp < 0) {
- nodeStart = nodeStart.left;
- subSet.add(nodeStart.element);
- } else if (cmp > 0) {
- nodeStart = nodeStart.right;
- subSet.add(nodeStart.element);
- } else {
- subSet.add(nodeStart.element);
- break;
- }
- }
- return subSet;
- }
- /**
- * Grąžinamas aibės poaibis nuo elemento.
- *
- * @param element - Aibės elementas.
- * @return Grąžinamas aibės poaibis nuo elemento.
- */
- @Override
- public SetADT<E> tailSet(E element) {
- if (element == null) {
- throw new IllegalArgumentException("Element is null in headSet(E element)");
- }
- BstSetKTU<E> tailSet = new BstSetKTU<>();
- // Jei elemento nėra aibėje
- if (!contains(element)) {
- return tailSet;
- }
- BstNode<E> node = root;
- while (node != null) {
- int cmp = c.compare(element, node.element);
- if (cmp < 0) {
- node = node.left;
- } else if (cmp > 0) {
- node = node.right;
- } else {
- break;
- }
- }
- tailSet.root = this.cloneRecursive(node);
- return tailSet;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement