0

i got a problem with the class of binary search tree, in simple cases it works fine.. but with heavy ones it doesn't work well i mean, it messes up the tree by setting the deleted one over someone else.. i think the problem is in the deletion method

my code:

 public void delete(int data) {
        root = delete(root, data);
    }

private BSTnode delete(BSTnode t, int data) {
        BSTnode 2delete;
BSTnode  node2delete;
BSTnode  parent;
        int value;


    2delete = getNode(t, data);

    if (2delete == null) {
        return root;
    }

    parent = parent(t, 2delete);


    if (isLeaf(2delete)) {
        if (parent == null) {
            return null; 
        }
        if (data< parent.getdata()) {
            parent.setLeft(null);
        } 
        else {
            parent.setRight(null);
        }

        return t;
    }

    if (hasOnlyLeftChild(2delete)) {
        if (parent == null) {
            return 2delete.getLeft();
        }



        if (data < parent.getdata()) {
            parent.setLeft(parent.getLeft().getLeft());
        } 
        else {
            parent.setRight(parent.getRight().getLeft());
        }

        return t;
    }

    if (hasOnlyRightChild(2delete)) {
        if (parent == null) {
            return 2delete.getRight();
        }


        if (data < parent.getdata()) {
            parent.setLeft(parent.getLeft().getRight());
        } 
        else {
            parent.setRight(parent.getRight().getRight());
        }

        return t;
    }



    node2delete = minNode(2delete.getRight());
    value= node2delete.getdata();

    t = delete(t, value);


    2delete.setdata(value);

    return t;
}
  • the delete method shouldn't change the root node of the tree in every case, so I think there is an error in the public version of your method. – dataNinja124 Dec 18 '13 at 17:04
  • What is `MACnumber` and why do you use it to determine whether the left node should be set to null? – MAV Dec 18 '13 at 17:05
  • i've edited that mistake, and i tried to remove the public version before.. but that didn't work out – user2373804 Dec 18 '13 at 20:26

0 Answers0