0

This is a grade 12 assignment. One of the questions asks us to write a method in BTree class that takes either a BNode or an integer, and removes the node from the tree.

Here's what I've tried:

public void delete(BNode b){
    if(b==null){
        return;
    }
    if(b.getLeft()==null && b.getRight()==null){

        b = null;
    }
    else{
        //System.out.println("boiboi");
        BNode tmp = b;
        b = null;
        add(tmp.getLeft());
        add(tmp.getRight());
        tmp = null;
    }
}
public void delete(int v){
    //System.out.println("gord");
    delete(find(v));
}

Here's the add and find method which i think are correct:

public BNode find(int v){
    return find(v, root);
}
public BNode find(int v, BNode branch){
    if(branch == null || branch.getVal() == v){
        return branch;
    }
    if(v<branch.getVal()){
        return find(v, branch.getLeft());
    }
    else{//else if(v>branch.getVal())
        return find(v, branch.getRight());
    }
}
public void add(int v){
    if(root == null){
        root = new BNode(v);
    }
    else{
        add(v, root);
    }
}
public void add(int v, BNode branch){
    if(v == branch.getVal()){
        return;
    }
    if(v<branch.getVal()){
        if(branch.getLeft() == null){
            branch.setLeft(new BNode(v));
        }
        else{
            add(v, branch.getLeft());
        }
    }
    else{
        if(branch.getRight() == null){
            branch.setRight(new BNode(v));
        }
        else{
            add(v, branch.getRight());
        }
    }
}
public void add(BNode n){
    if(n==null){
        return;
    }
    add(n.getVal());
}

Here's my testing class:

    BTree bTree = new BTree();
    bTree.add(50);
    bTree.add(60);
    bTree.add(40);
    bTree.add(35);
    bTree.add(55);
    bTree.add(45);
    bTree.add(51);
    bTree.delete(60);
    bTree.display();

the output is still everything i've added: 35 40 45 50 51 55 60 even if i tried to delete 51 which is the simplest case, still same output. Any help or suggestions would be appreciated, thank you.

2
  • 1
    BNode b is a REFERENCE. b=null won't modify The VALUE of your node, just put a null reference to b. You have modify Parent's left/right reference. If it is root, you have modify root reference. Commented Mar 27, 2013 at 3:52
  • thanks, i think i get what you are saying. Commented Mar 27, 2013 at 3:58

2 Answers 2

2

There are three cases that you need to take care of when deleting a node from a BST.

  1. If its a leaf, just go ahead and delete.

  2. If a node has just 1 child , simply connect its child to its parent. [you will obviously need some helper methods to getParentOfNode etc]

  3. If a node has 2 children, find the smallest element in the right subtree.And put its value in the current node and delete that node.

More info here. http://www.cs.sunysb.edu/~skiena/373/notes/lecture6/lecture6.html

Sign up to request clarification or add additional context in comments.

Comments

0
/* Algorithm to delete a record from a B-tree of order n.
The field used indicates how many keys in the node are being used. */
    q = NULL;
    p = tree;
    while (p) {
        i = nodesearch(p, key);
        q = p;
        if (i < used(p) -1 && key == k(p,i)) {
            found = TRUE;
            position = i;
            break;
        }
        p = son(p,i);
    }
    if (!found)
    /* error - item not found */
    else if (subtree(p)) {
        /* node is not a leaf */
        if (used(p) > ((n-1)/2)+1)
            /* no underflow */
            delkey (p, position, key);
        else {
            /* Statements to replace key to be deleted */
            /* with successor in father node. */
            replace (p, position, fsucc(p));
            p0 r1 p1 r2 p2 r3 ……. pn-1 rn-1 pn
            q = &fsucc(p);
            qpos = index of fsucc;
            if (used(rbrother(p)) > ((n-1)/2)+1)
                replace (q, qpos, sonsucc(q));
            else
                while (q && used(q) < (n-1)/2) {
                    concatenate(q, brother(q));
                    q = father(q);
                }
        }
    }
    else /* is a leaf */
    delkey(p, position, key);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.