删除方法二叉搜索树

时间:2013-11-09 00:06:39

标签: java binary-search-tree

我正在尝试为我一直在研究的BST结构实现一个remove方法。以下是包含find,insert和remove方法的代码:

public class BST {
    BSTNode root = new BSTNode("root");

    public void insert(BSTNode root, String title){
        if(root.title!=null){

            if(title==root.title){
                //return already in the catalog
            }
            else if(title.compareTo(root.title)<0){

                if(root.leftChild==null){
                    root.leftChild = new BSTNode(title);
                }
                else{
                    insert(root.leftChild,title);
                }
            }
            else if(title.compareTo(root.title)>0){
                if(root.rightChild==null){
                    root.rightChild = new BSTNode(title);
                }
                else{
                    insert(root.rightChild,title);
                }
            }
        }
    }

    public void find(BSTNode root, String title){
        if(root!= null){
            if(title==root.title){
                //return(true);
            }
            else if(title.compareTo(root.title)<0){
                find(root.leftChild, title);
            }
            else{
                find(root.rightChild, title);
            }
        }
        else{
            //return false;
        }
    }

    public void remove(BSTNode root, String title){
        if(root==null){
            return false;
        }
        if(title==root.title){
            if(root.leftChild==null){
                root = root.rightChild;
            }
            else if(root.rightChild==null){
                root = root.leftChild;
            }
            else{
                //code if 2 chlidren remove
            }
        }
        else if(title.compareTo(root.title)<0){
            remove(root.leftChild, title);
        }
        else{
            remove(root.rightChild, title);
        }
    }
}

我被告知我可以使用insert方法来帮助我使用remove方法,但我只是没有看到我如何抓取最小/最大元素,然后用该值替换我要删除的元素,然后以递归方式删除我获取替换值的节点,同时仍保持O(logn)复杂度。任何人都有我错过的任何想法或公然漏洞,或者其他任何有用的东西,因为我对这个问题感到头疼?

编辑: 我使用了答案的想法来提出这个,我相信它会起作用,但我得到一个错误,我的方法(不只是删除)必须返回字符串,这里是代码的样子,我认为这是返回语句??

public String remove(BSTNode root, String title){
            if(root==null){
                return("empty root");
            }
            if(title==root.title){
                    if(root.leftChild==null){
                            if(root.rightChild==null){
                                    root.title = null;
                                    return(title+ "was removed");
                            }
                            else{
                            root = root.rightChild;
                            return(title+ "was removed");
                            }
                    }
                    else if(root.rightChild==null){
                            root = root.leftChild;
                            return(title+ "was removed");
                    }
                    else{
                            String minTitle = minTitle(root);
                            root.title = minTitle;
                            remove(root.leftChild,minTitle);
                            return(title+ "was removed");
                    }
            }
            else if(title.compareTo(root.title)<0){
                    remove(root.leftChild, title);
            }
            else{
                    remove(root.rightChild, title);
            }
    }

6 个答案:

答案 0 :(得分:6)

public void remove (String key, BSTNode pos)
    {
        if (pos == null) return;
        if (key.compareTo(pos.key)<0)
            remove (key, pos.leftChild);
        else if (key.compareTo(pos.key)>0)
            remove (key, pos.rightChild);
        else {
            if (pos.leftChild != null && pos.rightChild != null)
            {
                /* pos has two children */
                BSTNode maxFromLeft = findMax (pos.leftChild); //need to make a findMax helper 
                //"Replacing "  pos.key " with " maxFromLeft.key
                pos.key = maxFromLeft.key;
                remove (maxFromLeft.key, pos.leftChild);
            }
            else if(pos.leftChild != null) {
                /* node pointed by pos has at most one child */
                BSTNode trash = pos;
                //"Promoting " pos.leftChild.key " to replace " pos.key
                pos = pos.leftChild;
                trash = null;
            }
            else if(pos.rightChild != null) {
                /* node pointed by pos has at most one child */
                BSTNode trash = pos;
                /* "Promoting " pos.rightChild.key" to replace " pos.key */
                pos = pos.rightChild;
                trash = null;
            }
            else {
                pos = null;
            }
        }
    }
  

这是对不平衡树的删除。我有C ++代码,所以我很快翻译了。可能会有一些小错误。您编码的树是否必须平衡?如果需要,我也有平衡删除。根据你问题的措辞,我不太确定。还要确保为findMax()

添加私有帮助函数

答案 1 :(得分:2)

void deleteTreeNode(int data){
    root = deleteTreeNode(root ,data);
}

private TreeNode deleteTreeNode(TreeNode root, int data) {
    TreeNode cur = root;
    if(cur == null){
        return cur;
    }
    if(cur.data > data){            
        cur.left = deleteTreeNode(cur.left, data);
    }else if(cur.data < data){
        cur.right = deleteTreeNode(cur.right, data);
    }else{          
        if(cur.left == null && cur.right == null){
            cur = null;
        }else if(cur.right == null){
            cur = cur.left;
        }else if(cur.left == null){
            cur = cur.right;
        }else{
            TreeNode temp  = findMinFromRight(cur.right);
            cur.data = temp.data;
            cur.right = deleteTreeNode(cur.right, temp.data);
        }
    }
    return cur;
}

private TreeNode findMinFromRight(TreeNode node) {
    while(node.left != null){
        node = node.left;
    }
    return node;
}

答案 2 :(得分:0)

要比较java中的对象,请使用.equals()方法而不是“==”运算符

  if(title==root.title)
          ^______see here

你需要像这样使用

 if(title.equals(root.title)) 

或如果您有兴趣忽略该案例,请按照以下代码

 if(title.equalsIgnoreCase(root.title))

答案 3 :(得分:0)

private void deleteNode(Node temp, int n) {
    if (temp == null)
        return;
    if (temp.number == n) {
        if (temp.left == null || temp.right == null) {
            Node current = temp.left == null ? temp.right : temp.left;
            if (getParent(temp.number, root).left == temp)
                getParent(temp.number, root).left = current;
            else
                getParent(temp.number, root).right = current;
        } else {
            Node successor = findMax(temp.left);
            int data = successor.number;
            deleteNode(temp.left, data);
            temp.number = data;
        }
    } else if (temp.number > n) {
        deleteNode(temp.left, n);
    } else {
        deleteNode(temp.right, n);
    }
}

答案 4 :(得分:0)

我知道这是一个非常古老的问题,但无论如何...接受的答案的实现取自c ++,所以指针的想法仍然存在,应该改变,因为Java中没有指针。

因此,每当您将节点更改为null或其他内容时,该节点的实例都会更改,但不会更改原始实例

此实现取自其中一个课程关于算法。

public TreeNode deleteBSTNode(int value,TreeNode node)
{
    if(node==null)
    {
        System.out.println("the value " + value + " is not found");
        return null;
    }
    //delete
    if(node.data>value) node.left = deleteBSTNode(value,node.left);
    else if(node.data<value) node.right = deleteBSTNode(value,node.right);
    else{
        if(node.isLeaf())
            return null;
        if(node.right==null)
            return node.left;
        if(node.left==null)
            return node.right;

        TreeNode successor = findMax(node.left);
        int data = successor.data;
        deleteBSTNode(data, node.left);
        node.data = data;


    }
    return node;
}

使用递归的返回值来关联节点之间的所有链接。

答案 5 :(得分:0)

对于深度优先后订单遍历和删除,请使用:

/*
 * 
 *  Remove uses
 *  depth-first Post-order traversal.
 *  
 * The Depth First Post-order traversal follows:
 * Left_Child -> Right-Child -> Node convention
 * 
 * Partial Logic was implemented from this source:
 * https://stackoverflow.com/questions/19870680/remove-method-binary-search-tree
 * by: sanjay
 */
@SuppressWarnings("unchecked")
public BinarySearchTreeVertex<E> remove(BinarySearchTreeVertex<E> rootParameter, E eParameter)  {
    BinarySearchTreeVertex<E> deleteNode = rootParameter;
    
    if ( deleteNode == null )   {
        return deleteNode;  }
    
    if ( deleteNode.compareTo(eParameter) == 1 )    {
        deleteNode.left_child = remove(deleteNode.left_child, eParameter);  }
    
    else if ( deleteNode.compareTo(eParameter) == -1 )  {
        deleteNode.right_child = remove(deleteNode.right_child, eParameter);    }
    
    else    {
        if ( deleteNode.left_child == null && deleteNode.right_child == null )  {
            deleteNode = null;
            }
        
        else if ( deleteNode.right_child == null )  {
            deleteNode = deleteNode.left_child; }
        
        else if ( deleteNode.left_child == null )   {
            deleteNode = deleteNode.right_child;    }
        
        else    {
            BinarySearchTreeVertex<E> interNode = findMaxLeftBranch( deleteNode.left_child );
            deleteNode.e = interNode.e;
            deleteNode.left_child = remove(deleteNode.left_child, interNode.e);
            }
    }   return deleteNode;  }   // End of remove(E e)

/*
 * Checking right branch for the swap value
 */
@SuppressWarnings("rawtypes")
public BinarySearchTreeVertex findMaxLeftBranch( BinarySearchTreeVertex vertexParameter )   {
    while (vertexParameter.right_child != null )    {
        vertexParameter = vertexParameter.right_child;  }
    return vertexParameter; }   // End of findMinRightBranch