1

我正在处理要删除的节点是节点的情况。我不确定是否需要跟踪父节点,以便在找到要删除的节点时将其父节点指针设置为空。但是那我怎么知道要删除的节点是哪个子节点呢?我需要更多 if 语句吗?

任何帮助表示赞赏,我觉得它并不太复杂,但我只是对如何真正摆脱节点感到困惑。

这是我到目前为止所拥有的:

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

}
4

2 回答 2

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

注意:如果你有parent节点,Node<E>那么它会很容易。

于 2013-03-02T06:40:29.990 回答
0

如果您不想更改节点结构,请在遍历树时跟踪父节点。

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

}
于 2013-03-02T06:44:56.437 回答