需要有关删除二叉搜索树节点的帮助

时间:2013-03-27 03:41:50

标签: java

这是12年级的作业。其中一个问题要求我们在BTree类中编写一个采用BNode或整数的方法,并从树中删除该节点。

这是我尝试过的:

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));
}

这是我认为正确的添加和查找方法:

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());
}

这是我的测试课程:

    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();

输出仍然是我添加的所有内容:35 40 45 50 51 55 60 即使我试图删除51这是最简单的情况,仍然相同的输出。 任何帮助或建议将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:2)

从BST删除节点时,需要注意三种情况。

  1. 如果是一片叶子,请继续删除。

  2. 如果一个节点只有一个孩子,只需将其子节点连接到其父节点即可。 [你显然需要一些帮助方法     getParentOfNode等]

  3. 如果节点有2个子节点,请找到右侧的最小元素 subtree.And将其值放在当前节点中并删除该节点。

  4. 此处有更多信息。 http://www.cs.sunysb.edu/~skiena/373/notes/lecture6/lecture6.html

答案 1 :(得分: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);
}
相关问题