Example #1
0
 public int compareTo(T child, T otherChild) {
   if (max) {
     return child.compareTo(otherChild);
   } else {
     return -1 * (child.compareTo(otherChild));
   }
 }
Example #2
0
  /**
   * Partition the table so that values from first to pivIndex are less than or equal to the pivot
   * value, and values from pivIndex to last are greater than the pivot value.
   *
   * @param table The table to be partitioned
   * @param first The index of the low bound
   * @param last The index of the high bound
   * @return The location of the pivot value
   */
  @Override
  protected <T extends Comparable<T>> int partition(T[] table, int first, int last) {
    // Select the first item as the pivot value.
    T pivot = table[first];
    int up = first;
    int down = last;
    do {
      /* Invariant:
        All items in table[first . . . up - 1] <= pivot
        All items in table[down + 1 . . . last] > pivot
      */
      while ((up < last) && (pivot.compareTo(table[up]) >= 0)) {
        up++;
      }
      // assert: up equals last or table[up] > pivot.
      while (pivot.compareTo(table[down]) < 0) {
        down--;
      }
      // assert: down equals first or table[down] <= pivot.
      if (up < down) { // if up is to the left of down.
        // Exchange table[up] and table[down].
        swap(table, up, down);
      }
    } while (up < down); // Repeat while up is left of down.

    // Exchange table[first] and table[down] thus putting the
    // pivot value where it belongs.
    swap(table, first, down);

    // Return the index of the pivot value.
    return down;
  }
Example #3
0
  protected AVLNode<T> insert(T data, AVLNode<T> t) throws Exception {
    if (t == null) t = new AVLNode<T>(data);
    else if (data.compareTo(t.data) < 0) {
      t.left = insert(data, t.left);

      if (height(t.left) - height(t.right) == 2) {
        if (data.compareTo(t.left.data) < 0) {
          t = rotateWithLeftChild(t);
          countSingleRotations++;
        } else {
          t = doubleRotateWithleftChild(t);
          countDoubleRotations++;
        }
      }

    } else if (data.compareTo(t.data) > 0) {
      t.right = insert(data, t.right);

      if (height(t.right) - height(t.left) == 2) {
        if (data.compareTo(t.right.data) > 0) {
          t = rotateWithRightChild(t);
          countSingleRotations++;
        } else {
          t = doubleRotateWithRightChild(t);
          countDoubleRotations++;
        }
      }

    } else {
      throw new Exception("Attempting to insert to duplicate value");
    }

    t.height = max(height(t.left), height(t.right)) + 1;
    return t;
  }
 public T eliminar(T elemento) {
   NodoLista<T> actual = primero;
   if (elemento == null || longitud == 0) {
     return null;
   } else if (elemento.compareTo(actual.darElemento()) == 0) {
     primero = actual.darSiguiente();
     if (primero != null) {
       primero.cambiarAnterior(null);
     }
     longitud--;
     return elemento;
   } else {
     actual = actual.darSiguiente();
     while (actual != null) {
       if (elemento.compareTo(actual.darElemento()) == 0) {
         NodoLista<T> anterior = actual.darAnterior();
         NodoLista<T> siguiente = actual.darSiguiente();
         anterior.cambiarSiguiente(siguiente);
         if (siguiente != null) {
           siguiente.cambiarAnterior(anterior);
         }
         longitud--;
         return elemento;
       } else {
         actual = actual.darSiguiente();
       }
     }
   }
   return null;
 }
  protected void verifyTransitiveComparison(T x, T y, T z) {
    assertNotNull(x);
    assertNotNull(y);
    assertNotNull(z);
    int x_y = x.compareTo(y);
    int y_z = y.compareTo(z);
    int x_z = x.compareTo(z);
    checkForMinMaxInt(x_y);
    checkForMinMaxInt(y_z);
    checkForMinMaxInt(x_z);

    if ((x_y > 0) && (y_z > 0)) {
      assertTrue(x_z > 0);
    } else if ((x_y < 0) && (y_z < 0)) {
      assertTrue(x_z < 0);
    } else {
      String msg =
          "Comparison of "
              + x.toString()
              + ", "
              + y.toString()
              + " and "
              + z.toString()
              + " is NOT Transitive. "
              + "Y must be 'between' X and Z.";
      assertTrue(msg, false);
    }
  }
Example #6
0
 // recursive support method for above
 private BSTNode delete(T item, BSTNode current) {
   // if current is null, nothing new to return
   if (current != null) {
     // if item is smaller, go left
     if (item.compareTo(current.element) < 0) {
       current.left = delete(item, current.left);
     }
     // if item is larger, go right
     else if (item.compareTo(current.element) > 0) {
       current.right = delete(item, current.right);
     } else {
       // get smallest of right subtree, then delete this node
       if (current.left != null && current.right != null) {
         current.element = findMinimum(current.right);
         current.right = delete(current.element, current.right);
       }
       // if only left child, replace with this
       else if (current.left != null && current.right == null) {
         current = current.left;
       }
       // if only right child, replace with this
       else if (current.left == null && current.right != null) {
         current = current.right;
       } else {
         // if no children, delete node by setting to null
         current = null;
       }
     }
   }
   return current;
 }
  static <T extends Comparable<? super T>> void intersection(List<T> a, List<T> b, List<T> out) {
    ListIterator<T> aIterator = a.listIterator();
    ListIterator<T> bIterator = b.listIterator();
    ListIterator<T> outIterator = out.listIterator();
    // out=new LinkedList<T>();

    try {
      T aTemp = next(aIterator);
      T bTemp = next(bIterator);

      while (aTemp != null && bTemp != null) {
        if (aTemp.compareTo(bTemp) == 0) {
          // out.add(aTemp);
          outIterator.add(aTemp);
          aTemp = next(aIterator);
          bTemp = next(bIterator);
        } else if (aTemp.compareTo(bTemp) < 0) {
          aTemp = next(aIterator);
        } else {
          bTemp = next(bIterator);
        }
      }

    } catch (NullPointerException ex) {
      System.out.println("The input array(s) is/are empty. Please check!!");
    }
  }
 protected void verifyComparableToVsEquals(T o1, T o2) {
   assertNotNull(o1);
   assertNotNull(o2);
   assertTrue((o1.compareTo(o2) == 0) == (o1.equals(o2)));
   assertTrue((o2.compareTo(o1) == 0) == (o2.equals(o1)));
   assertTrue((o1.compareTo(o2) == 0) == (o2.compareTo(o1) == 0));
 }
Example #9
0
 public int compareTo(Pair<T> pair) {
   int rv = first.compareTo(pair.first);
   if (rv == 0) {
     rv = second.compareTo(pair.second);
   }
   return rv;
 }
  private AANode<T> remove(T element, AANode<T> node) {
    if (node == null) {
      return node;
    } else if (element.compareTo(node.getValue()) > 0) {
      node.setRight(remove(element, node.getRight()));
    } else if (element.compareTo(node.getValue()) < 0) {
      node.setLeft(remove(element, node.getLeft()));
    } else {
      if (node.getLevel() == 1) {
        return null;
      } else if (node.getLeft() == null) {
        AANode<T> left = getSuccessor(node);
        node.setRight(remove(left.getValue(), node.getRight()));
        node.setValue(left.getValue());
      } else {
        AANode<T> left = getPredecessor(node);
        node.setLeft(remove(left.getValue(), node.getLeft()));
        node.setValue(left.getValue());
      }
    }

    node = decreaseLevel(node);
    node = skew(node);
    node.setRight(skew(node.getRight()));
    if (node.getRight() != null) {
      node.getRight().setRight(skew(node.getRight().getRight()));
    }

    node = split(node);
    node.setRight(split(node).getRight());

    return node;
  }
 @Override
 public boolean offer(T data) {
   boolean offered = false;
   if (root == null) {
     root = new Node<T>(data);
     offered = true;
   } else if (data.compareTo(root.getValue()) == -1) {
     if (root.getLeft() == null) {
       root.setLeft(new Node<T>(data));
       offered = true;
     } else {
       offered = offer(data, root.getLeft(), root);
     }
   } else if (data.compareTo(root.getValue()) == 1) {
     if (root.getRight() == null) {
       root.setRight(new Node<T>(data));
       offered = true;
     } else {
       offered = offer(data, root.getRight(), root);
     }
   }
   balance(root, null);
   if (offered) {
     numValues++;
   }
   return offered;
 }
 /**
  * 比較する
  *
  * @param o1
  * @param o2
  * @param order
  * @return
  */
 private <T extends Comparable<? super T>> int compareTo(T o1, T o2, boolean order) {
   if (this.order) {
     return o1.compareTo(o2);
   } else {
     return o2.compareTo(o1);
   }
 }
Example #13
0
 public <T extends Number & Comparable<T>> NumberField(T min, T max, T value, NumberFormatter f) {
   super(f);
   formatter = f;
   f.setMinimum(min);
   f.setMaximum(max);
   if (value != null)
     setValue(value.compareTo(min) < 0 ? min : value.compareTo(max) > 0 ? max : value);
 }
 private T find(T x, ThreadedBinaryNode<T> t) throws ItemNotFound {
   if (x.compareTo(t.element) < 0)
     if (t.isLeftThread) throw new ItemNotFound("Item " + x + "  not found");
     else return find(x, t.left);
   else if (x.compareTo(t.element) > 0)
     if (t.isRightThread) throw new ItemNotFound("Item " + x + " not found");
     else return find(x, t.right);
   else return t.element;
 }
Example #15
0
 public static <T extends Comparable<? super T>> void assertSorted(Iterator<T> i) {
   if (!i.hasNext()) return;
   T last = i.next();
   while (i.hasNext()) {
     T t = i.next();
     assertTrue(last.compareTo(t) <= 0);
     assertTrue(t.compareTo(last) >= 0);
     last = t;
   }
 }
 public static <T extends Comparable<T>> T maximum(T x, T y, T z) {
   T max = x; // assume x is initially the largest
   if (y.compareTo(max) > 0) {
     max = y; // y is the largest so far
   }
   if (z.compareTo(max) > 0) {
     max = z; // z is the largest now
   }
   return max; // returns the largest object
 }
Example #17
0
 /**
  * Gets the minimum and maximum of an array of objects of type T.
  *
  * @param a an array of objects of type T
  * @return a pair with the min and max value, or null if a is null or empty
  */
 public static <T extends Comparable> Pair<T> minmax(T[] a) {
   if (a == null || a.length == 0) return null;
   T min = a[0];
   T max = a[0];
   for (int i = 1; i < a.length; i++) {
     if (min.compareTo(a[i]) > 0) min = a[i];
     if (max.compareTo(a[i]) < 0) max = a[i];
   }
   return new Pair<T>(min, max);
 }
  /**
   * Clamps {@code value} to this range.
   *
   * <p>If the value is within this range, it is returned. Otherwise, if it is {@code <} than the
   * lower endpoint, the lower endpoint is returned, else the upper endpoint is returned.
   * Comparisons are performed using the {@link Comparable} interface.
   *
   * @param value a non-{@code null} {@code T} reference
   * @return {@code value} clamped to this range.
   */
  public T clamp(T value) {
    checkNotNull(value, "value must not be null");

    if (value.compareTo(mLower) < 0) {
      return mLower;
    } else if (value.compareTo(mUpper) > 0) {
      return mUpper;
    } else {
      return value;
    }
  }
Example #19
0
 private T findRec(Node<T> node, T key) {
   if (node == null) {
     return null;
   } else if (key.compareTo(node.key) == 0) {
     return node.key;
   } else if (key.compareTo(node.key) < 0) {
     return findRec(node.left, key);
   } else {
     return findRec(node.right, key);
   }
 }
  private static <T extends Comparable<T>> T max(
      T a,
      T b,
      T c) { // Burada generic metodumuz Comparable sınıfından miras alacağını belirttik T ise
    // dönüş tipidir..
    T büyük = a;

    if (b.compareTo(a) > 0) büyük = b;
    if (c.compareTo(büyük) > 0) büyük = c;
    return büyük;
  }
Example #21
0
 private boolean recContains(T element, BSTNode<T> tree)
       // Returns true if tree contains an element e such that
       // e.compareTo(element) == 0; otherwise, returns false.
     {
   if (tree == null) return false; // element is not found
   else if (element.compareTo(tree.getInfo()) < 0)
     return recContains(element, tree.getLeft()); // Search left subtree
   else if (element.compareTo(tree.getInfo()) > 0)
     return recContains(element, tree.getRight()); // Search right subtree
   else return true; // element is found
 }
Example #22
0
 private boolean toSearch(Node<T> p, T data) {
   if (p == null) {
     return false;
   }
   if (data.compareTo(p.data) == 0) {
     return true;
   }
   if (data.compareTo(p.data) > 0) {
     return toSearch(p.right, data);
   } else return toSearch(p.left, data);
 }
Example #23
0
 private T recGet(T element, BSTNode<T> tree)
       // Returns an element e from tree such that e.compareTo(element) == 0;
       // if no such element exists, returns null.
     {
   if (tree == null) return null; // element is not found
   else if (element.compareTo(tree.getInfo()) < 0)
     return recGet(element, tree.getLeft()); // get from left subtree
   else if (element.compareTo(tree.getInfo()) > 0)
     return recGet(element, tree.getRight()); // get from right subtree
   else return tree.getInfo(); // element is found
 }
Example #24
0
 public boolean contains(T value) {
   if (value.compareTo(data) == 0) {
     return true;
   } else if (value.compareTo(data) > 0 && left != null) {
     return left.contains(value);
   } else if (value.compareTo(data) < 0 && right != null) {
     return right.contains(value);
   } else {
     return false;
   }
 }
Example #25
0
 private Node<T> toInsert(Node<T> p, T data) {
   if (p == null) {
     return new Node<T>(data);
   }
   if (data.compareTo(p.data) == 0) {
     return p;
   }
   if (data.compareTo(p.data) > 0) {
     p.right = toInsert(p.right, data);
   } else p.left = toInsert(p.left, data);
   return p;
 }
Example #26
0
 private BSTNode<T> recRemove(T element, BSTNode<T> tree) {
   if (tree == null) found = false;
   else if (element.compareTo(tree.getInfo()) < 0)
     tree.setLeft(recRemove(element, tree.getLeft()));
   else if (element.compareTo(tree.getInfo()) > 0)
     tree.setRight(recRemove(element, tree.getRight()));
   else {
     tree = removeNode(tree);
     found = true;
   }
   return tree;
 }
 void insert(T x, ThreadedBinaryNode<T> t) throws DuplicateItem {
   if (x.compareTo(t.element) < 0)
     if (t.isLeftThread) {
       t.left = new ThreadedBinaryNode(x, t.left, t);
       t.isLeftThread = false;
     } else insert(x, t.left);
   else if (x.compareTo(t.element) > 0)
     if (t.isRightThread) {
       t.right = new ThreadedBinaryNode(x, t, t.right);
       t.isRightThread = false;
     } else insert(x, t.right);
   else throw new DuplicateItem("attempt to insert duplicateitem");
 }
  static <T extends Comparable<? super T>> void union(List<T> a, List<T> b, List<T> out) {
    ListIterator<T> aIterator = a.listIterator();
    ListIterator<T> bIterator = b.listIterator();
    int aIndex = 0;
    int bIndex = 0;
    try {
      T aTemp = aIterator.next();
      T bTemp = bIterator.next();

      while (aIndex + 1 <= a.size() && bIndex + 1 <= b.size()) {
        {
          if (aTemp.compareTo(bTemp) == 0) {
            out.add(aTemp);
            aIndex++;
            bIndex++;
            if (aIterator.hasNext()) aTemp = aIterator.next();
            if (bIterator.hasNext()) bTemp = bIterator.next();
          } else if (aTemp.compareTo(bTemp) < 0) {
            aIndex++;
            out.add(aTemp);
            if (aIterator.hasNext()) aTemp = aIterator.next();

          } else {
            bIndex++;
            out.add(bTemp);
            if (bIterator.hasNext()) bTemp = bIterator.next();
          }
        }

        if (a.size() >= aIndex + 1 && bIndex + 1 > b.size()) {
          out.add(aTemp);
          aIndex++;
          while (aIterator.hasNext()) {
            aTemp = aIterator.next();
            out.add(aTemp);
          }
        } else if (b.size() >= bIndex + 1 && aIndex + 1 > a.size()) {
          bIndex++;
          out.add(bTemp);
          while (bIterator.hasNext()) {
            bTemp = bIterator.next();
            out.add(bTemp);
          }
        }
      }

    } catch (NoSuchElementException ex) {
      System.out.println("The input array(s) is/are empty. Please check!!");
      ex.printStackTrace();
    }
  }
Example #29
0
  public AVLNode<T> remove(T x, AVLNode<T> t) {
    if (t == null) {
      System.out.println("Sorry but you're mistaken, " + t + " doesn't exist in this tree :)\n");
      return null;
    }
    System.out.println("Remove starts... " + t.data + " and " + x);

    if (x.compareTo(t.data) < 0) {
      t.left = remove(x, t.left);
      int le = t.left != null ? t.left.height : 0;

      if ((t.right != null) && (t.right.height - le >= 2)) {
        int rightHeight = t.right.right != null ? t.right.right.height : 0;
        int leftHeight = t.right.left != null ? t.right.left.height : 0;
        if (rightHeight >= leftHeight) t = rotateWithLeftChild(t);
        else t = doubleRotateWithRightChild(t);
      }

    } else if (x.compareTo(t.data) > 0) {
      t.right = remove(x, t.right);
      int ri = t.right != null ? t.right.height : 0;
      if ((t.left != null) && (t.left.height - ri >= 2)) {
        int leftHeight = t.left.left != null ? t.left.left.height : 0;
        int rightHeight = t.left.right != null ? t.left.right.height : 0;
        if (leftHeight >= rightHeight) t = rotateWithRightChild(t);
        else t = doubleRotateWithleftChild(t);
      }

    } else if (t.left != null) {
      t.data = findMax(t.left).data;
      remove(t.data, t.left);

      if ((t.right != null) && (t.right.height - t.left.height >= 2)) {
        int rightHeight = t.right.right != null ? t.right.right.height : 0;
        int leftHeight = t.right.left != null ? t.right.left.height : 0;
        if (rightHeight >= leftHeight) t = rotateWithLeftChild(t);
        else t = doubleRotateWithRightChild(t);
      }

    } else {
      t = (t.left != null) ? t.left : t.right;
    }

    if (t != null) {
      int leftHeight = (t.left != null) ? t.left.height : 0;
      int rightHeight = (t.right != null) ? t.right.height : 0;
      t.height = max(leftHeight, rightHeight) + 1;
    }

    return t;
  }
Example #30
0
  private T find(T key) {
    Node<T> current = root;

    while (current != null) {
      if (key.compareTo(current.key) < 0) {
        current = current.left;
      } else if (key.compareTo(current.key) > 0) {
        current = current.right;
      } else {
        return current.key;
      }
    }
    return null;
  }