_Deletion Node binary search tree (minimum value of the right subtype)

I like that the delete method can remove the node and replace it with the minimum value of the right subtree. I do not want the setSrting method so I need to make a mistake when I replace the value (minimum right subtree). I tried to insert("b"), insert("d"), insert("c"), insert("a")and delete("b"). it removes "c"instead "b", I really don't understand why it works this way. Please help me fix the Delete method error. Thanks.

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
     *
     *************************************************/

}
+4
source share

Source: https://habr.com/ru/post/1608047/


All Articles