二叉搜索树_Deletion节点(右子树的最小值)

时间:2015-09-18 15:31:37

标签: java methods binary-search-tree

我喜欢删除方法可以删除节点并替换它 右子树的最小值。我不想制作setSrting方法 所以当我替换值(最小权利子树)时我需要做一个卑鄙的事。 我尝试了insert("b")insert("d")insert("c")insert("a")delete("b")。 它会删除"c"而不是"b",我真的不明白为什么它会以这种方式运作。请帮我修复Delete方法的错误。谢谢。

class StringNode {

    private String word;

    private StringNode left, right;

    // constructor, setter for word
    public StringNode(String w) {
        word = w;
        left = right = null;
    }

    // getter for Left
    public StringNode getLeft() {
        return left;
    }

    // setter for Left
    public void setLeft(StringNode pt) {
        left = pt;
    }

    // getter for Right
    public StringNode getRight() {
        return right;
    }

    // setter for Right
    public void setRight(StringNode pt) {
        right = pt;
    }

    // getter for word
    public String getString() {
        return word;
    }

}

class BSTStrings {

    // member variable pointing to the root of the BST
    private StringNode root;

    // default constructor
    public BSTStrings() {
        root = null;
    }

    /*
    //copy constructor
    public BSTStrings(BSTStrings t) {
        root = copyTree(root); //"root = t" does not make a copy
    }

    //copy the base Node for root
    private static StringNode copyTree(StringNode l) {
        return l;
    }
    */

    // for output purposes -- override Object version
    public String toString() {
        return toString(root, 0);
    }

    private static String toString(StringNode l, int x) {
        String s = "";
        if (l == null)
            ;// nothing to output
        else {
            if (l.getLeft() != null) // don't output empty subtrees
                s = '(' + toString(l.getLeft(), x + 1) + ')';
            s = s + l.getString() + "-" + x;
            if (l.getRight() != null) // don't output empty subtrees
                s = s + '(' + toString(l.getRight(), x + 1) + ')';
        }
        return s;
    }

    /************************************************
     *
     * Search
     *
     *************************************************/

    // find the string
    public StringNode search(String s) {
        return search(root, s);
    }

    private StringNode search(StringNode x, String s) {

        // the comparison result define as an integer
        int cmp = s.compareTo(x.getString());

        // the desired string found
        if (cmp == 0)
            return x;
        // if the s lexicographically precedes root, search(lf subtree)
        else if (cmp < 0)
            return search(x.getLeft(), s);
        // if the s lexicographically follows root, search(rt subtree)
        else
            return search(x.getRight(), s);

    }

    /************************************************
     *
     * Insert
     *
     *************************************************/

    // insert s into this tree. If s is already in the tree, do nothing.
    public void insert(String s) {
        if (s == null)
            ;
        root = insert(root, s);
    }

    /*
     * inserts String value into binary search tree of which StringNode is the root
     */
    private StringNode insert(StringNode x, String s) {

        if (x == null) {
            // position if insertion found; insert after leaf
            // create new node
            x = new StringNode(s);
        } // end if
          // search for the insertion position
        int cmp = s.compareTo(x.getString());
        // search left tree
        if (cmp < 0)
            x.setLeft(insert(x.getLeft(), s));
        // doing nothing, if already there
        else if (cmp == 0)
            ;
        // search the right subtree
        else
            x.setRight(insert(x.getRight(), s));
        return x;
    }

    /************************************************
     *
     * Delete
     *
     *************************************************/

    // Deletes val from BST - does nothing if val is not in the tree.
    // Deletion must be done so that the replacement node is the smallest node
    // from the right subtree.
    public void delete(String val) {
        if (val == null)
            ;
        delete(root, val);
    }

    private StringNode delete(StringNode x, String val) {

        int cmp = val.compareTo(x.getString());
        if (x == null)
            ;
        else if (cmp < 0)
            x.setLeft(delete(x.getLeft(), val));
        else if (cmp > 0)
            x.setRight(delete(x.getRight(), val));
        else { // find it!
            if (x.getLeft() == null)
                return x.getRight();
            if (x.getRight() == null)
                return x.getLeft();
            StringNode t = x;
            // replace current node which need to delete
            // with min of right sub tree
            x = min(t.getRight());
            // delete the smallest value of right subtree
            x.setRight(deleteMin(t.getRight()));
            x.setLeft(t.getLeft());
        }
        return x;

    }

    // delete the smallest
    private StringNode deleteMin(StringNode x) {
        if (x.getLeft() == null)
            return x.getRight();
        x.setLeft(deleteMin(x.getLeft()));
        return x;
    }

    // returns smallest
    private StringNode min(StringNode x) {
        if (x.getLeft() == null)
            return x;
        else
            return min(x.getLeft());
    }

    /************************************************
     *
     * Height
     *
     *************************************************/
    // return height of the tree
    public int height() {
        return height(root);
    }

    private int height(StringNode x) {
        if (x == null)
            return -1;
        return 1 + Math.max(height(x.getLeft()), height(x.getRight()));
    }

    /************************************************
     *
     * Closeness
     *
     *************************************************/

}

0 个答案:

没有答案
相关问题