Ejemplo n.º 1
0
    private E findStartingNodeForDeletion(E element) {
      if (root == null) return null;

      TreeNode<E> parent = null;
      TreeNode<E> current = root;
      while (current != null) {
        if (element.compareTo(current.element) < 0) {
          parent = current;
          current = current.left;
        } else if (element.compareTo(current.element) > 0) {
          parent = current;
          current = current.right;
        } else break;
      }

      if (current == null) return null;

      if (current.left == null) {
        if (parent == null) {
          return null;
        } else {
          return parent.element;
        }
      } else {
        TreeNode<E> parentOfRightMost = current;
        TreeNode<E> rightMost = current.left;

        while (rightMost.right != null) {
          parentOfRightMost = rightMost;
          rightMost = rightMost.right;
        }
        return parentOfRightMost.element;
      }
    }
Ejemplo n.º 2
0
 void moveUp(int pos, E node) {
   while (pos > 0) {
     int parent = (pos - 1) >> 1;
     E parentNode = h.get(parent);
     if (node.compareTo(parentNode) >= 0) {
       break;
     }
     h.set(pos, parentNode);
     pos = parent;
   }
   h.set(pos, node);
 }
Ejemplo n.º 3
0
 /**
  * Inserts the specified element into this delay queue.
  *
  * @param e the element to add
  * @return <tt>true</tt>
  * @throws NullPointerException if the specified element is null
  */
 public boolean offer(E e) {
   final ReentrantLock lock = this.lock;
   lock.lock();
   try {
     E first = q.peek();
     q.offer(e);
     if (first == null || e.compareTo(first) < 0) available.signalAll();
     return true;
   } finally {
     lock.unlock();
   }
 }
Ejemplo n.º 4
0
    public boolean delete(E element) {
      if (root == null) return false;
      TreeNode<E> parent = null;
      TreeNode<E> current = root;
      while (current != null) {
        if (element.compareTo(current.element) < 0) {
          parent = current;
          current = current.left;
        } else if (element.compareTo(current.element) > 0) {
          parent = current;
          current = current.right;
        } else break;
      }
      if (current == null) return false;
      if (current.left == null) {
        if (parent == null) {
          root = current.right;
        } else {
          if (element.compareTo(parent.element) < 0) parent.left = current.right;
          else parent.right = current.right;
          balancePath(parent.element);
        }
      } else {
        TreeNode<E> parentOfRightMost = current;
        TreeNode<E> rightMost = current.left;

        while (rightMost.right != null) {
          parentOfRightMost = rightMost;
          rightMost = rightMost.right;
        }
        current.element = rightMost.element;
        if (parentOfRightMost.right == rightMost) parentOfRightMost.right = rightMost.left;
        else parentOfRightMost.left = rightMost.left;
        balancePath(parentOfRightMost.element);
      }
      size--;
      return true;
    }
Ejemplo n.º 5
0
 void moveDown(int pos, E node) {
   int half = h.size() >> 1;
   while (pos < half) {
     int child = 2 * pos + 1;
     if (child < h.size() - 1 && h.get(child).compareTo(h.get(child + 1)) > 0) {
       ++child;
     }
     if (node.compareTo(h.get(child)) <= 0) {
       break;
     }
     h.set(pos, h.get(child));
     pos = child;
   }
   h.set(pos, node);
 }
Ejemplo n.º 6
0
  protected FHthreadedNode<E> find(FHthreadedNode<E> root, E x) {
    int compareResult;

    if (root == null) return null;

    while (true) {
      if (root == null) return null;

      compareResult = x.compareTo(root.data);
      if (compareResult < 0) {
        if (root.lftThread) return null;
        root = root.lftChild;
      } else if (compareResult < 0) {
        if (root.rtThread) return null;
        root = root.rtChild;
      } else break;
    }
    return root; // found
  }
Ejemplo n.º 7
0
  public boolean insert(E x) {
    int compareResult;

    if (mRoot == null) {
      mRoot = new FHthreadedNode<E>(x, null, null, true, true, 0);
      mSize++;
      return true;
    }

    FHthreadedNode<E> newNode, parent;
    parent = mRoot;

    while (true) {
      compareResult = x.compareTo(parent.data);
      if (compareResult < 0) {
        if (!(parent.lftThread)) parent = parent.lftChild;
        else {
          // place as new left child
          newNode = new FHthreadedNode<E>(x, parent.lftChild, parent, true, true, 0);
          parent.lftChild = newNode;
          parent.lftThread = false;
          break;
        }
      } else if (compareResult > 0) {
        if (!(parent.rtThread)) parent = parent.rtChild;
        else {
          // place as new right child
          newNode = new FHthreadedNode<E>(x, parent, parent.rtChild, true, true, 0);
          parent.rtChild = newNode;
          parent.rtThread = false;
          break;
        }
      } else return false; // duplicate
    }

    mSize++;
    return true;
  }
Ejemplo n.º 8
0
  // very hard to remove recursion, so only adjust pred/succ links
  protected FHthreadedNode<E> remove(FHthreadedNode<E> root, E x) {
    int compareResult; // avoid multiple calls to compareTo()
    FHthreadedNode<E> tempRoot;

    if (root == null) return null;

    compareResult = x.compareTo(root.data);
    if (compareResult < 0) {
      if (!root.lftThread) root.lftChild = remove(root.lftChild, x);
    } else if (compareResult > 0) {
      if (!root.rtThread) root.rtChild = remove(root.rtChild, x);
    }

    // found the node
    else if (!(root.lftThread) && !(root.rtThread)) {
      // two real children
      root.data = findMin(root.rtChild).data;
      root.rtChild = remove(root.rtChild, root.data);
    } else {
      // one or two "fake" children => at least one thread
      redirectThreadsPointingToMe(root);

      // if a full leaf, we have to modify one of parent's thread flags
      if (root.lftThread && root.rtThread) {
        tempRoot = adjustParentThreadFlagsAndUnlink(root);

        // in case this was final node in tree
        if (root.lftChild == null && root.rtChild == null) mRoot = null;

        root = tempRoot;
      } else
        // at least one real child, so we copy to parent
        root = (!(root.lftThread)) ? root.lftChild : root.rtChild;

      mSize--;
    }
    return root;
  }
 public int compare(E e1, E e2) {
   return e1.compareTo(e2);
 }