Java Binary Search Tree Delete node with no children

余生颓废 提交于 2019-12-11 10:31:24

问题


I'm working on the case where the node to be deleted is a node. I'm not sure if I need to keep track of the parent so that when I find the node to delete so I can set its parents pointer to null. But then how would I know which child the node to be deleted is? Do I need more if statements?

Any help is appreciated, I feel its not too complicated but I'm just confused on how to actually get rid of the node.

This is what I have so far:

public void insert(E s) 
{
    root = insert(s, root);
} 

private Node<E> insert(E s, Node<E> T)
{
    //easiest case, empty tree, create new tree
    if(T == null)
    {
        T = new Node<E>(s);
    }
    //easiest case, found s
    else if(s.compareTo(T.getData()) == 0)
    {
        System.out.println("Item already present.");
    }
    //s is greater than T, insert on right subtree
    else if(s.compareTo(T.getData()) > 0)
    {
        T.setRight(insert(s, T.getRight()));
    }
    //s is less than T, insert on left subtree
    else
    {
        T.setLeft(insert(s,T.getLeft()));
    }
    return T;
}

public void delete(E d)
{
    delete( d, root);
}

private void delete( E d, Node<E> T)
{

    if(T == null)
    {

    }
    else if(d.equals(T.getData()))
    {
        System.out.println("it found the node at least");
        if(T.getRight() == null && T.getLeft() == null)
        {

        }
        //code other cases for a node with one child and node with two      children
    }
    else if(d.compareTo(T.getData()) > 0)
    {
        System.out.println("going right");
        delete(d, T.getRight());
    }
    //s is less than T, insert on left subtree
    else
    {System.out.println("going left");
        delete(d,T.getLeft());
    }

}

回答1:


public Node<E> search(Node<E> node, E d)
{
    while(node!=null && ((node.getLeft()!=null && !node.getLeft().getData().equals(d)) || (node.getRight()!=null && !node.getRight().getData().equals(d)))
    {
        if(d.compareTo(node.getData()) < 0)
        {
            node = node.getLeft();
        }
        else
        {
            node = node.getRight();
        }
    }

    return node;
}

private void delete(E d)
{
    // Search the node
    Node parent = search(root, d);

    // parent is the parent node under which the required node is present
    // Now check which child it is - left or right
    if(parent == null)
    {
         System.out.println("Element not found");
         return;
    }

    if(parent.getLeft().getData().equals(d))
    {
        // Left child
        parent.setLeft(null);
    }
    else
    {
        // Right child
        parent.setRight(null);
    }
}

NOTE: If you would had parent node in the Node<E> then it would have been very easy.




回答2:


Track the parent node as you traverse the tree like this if you don't want to change the Node structure.

private Node<E> parent;
public void insert(E s) 
{
    root = insert(s, root);
} 

private Node<E> insert(E s, Node<E> T)
{
    //easiest case, empty tree, create new tree
    if(T == null)
    {
        T = new Node<E>(s);
    }
    //easiest case, found s
    else if(s.compareTo(T.getData()) == 0)
    {
        System.out.println("Item already present.");
    }
    //s is greater than T, insert on right subtree
    else if(s.compareTo(T.getData()) > 0)
    {
        T.setRight(insert(s, T.getRight()));
    }
    //s is less than T, insert on left subtree
    else
    {
        T.setLeft(insert(s,T.getLeft()));
    }
    return T;
}

public void delete(E d)
{
    delete( d, root);
}

private void delete( E d, Node<E> T)
{

    if(T == null)
    {

    }
    else if(d.equals(T.getData()))
    {
        System.out.println("it found the node at least");
        if(T.getRight() == null && T.getLeft() == null)
        {
             if (parent != null){//For the first node, parent will be null
                  if (d.equals(parent.getRight().getData())){//Data matches with the right node of parent
                       parent.setRight(null);
                  }else{//Data matches with the left node of parent
                       parent.setLeft(null);
                  }
                  //Reset parent node
                  parent = null;
             }
        }
        //code other cases for a node with one child and node with two      children
    }
    else if(d.compareTo(T.getData()) > 0)
    {
        parent = T;// Make the current node as parent
        System.out.println("going right");
        delete(d, T.getRight());
    }
    //s is less than T, insert on left subtree
    else
    {
        parent = T;// Make the current node as parent
        System.out.println("going left");
        delete(d,T.getLeft());
    }

}


来源:https://stackoverflow.com/questions/15170999/java-binary-search-tree-delete-node-with-no-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!