コード例 #1
0
ファイル: FHthreadedBST.java プロジェクト: seanxtr/JAVA3_Lab1
  // 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;
  }