2013-03-02 73 views
1

我正在處理要刪除的節點是節點的情況。我不確定是否需要跟蹤父項,以便當我找到要刪除的節點時,可以將它的父項指針設置爲null。但是,我怎麼知道要刪除的節點是哪個孩子?我是否需要更多的陳述?Java二進制搜索樹沒有子項的刪除節點

任何幫助表示讚賞,我覺得它不是太複雜,但我只是困惑如何實際擺脫節點。

這是我到目前爲止有:

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

} 
+0

「但是我怎麼知道哪個子節點被刪除?」回答:通過使用if(parent.getRight()。equals(this))...' – alfasin 2013-03-02 05:17:49

+0

所以然後如果初始化父對象在頂部爲null並且當前對T然後當我使遞歸調用向下移動樹時我必須進行兩個遞歸調用(一個用於父級,一個用於當前)? – alexthefourth 2013-03-02 05:21:45

+0

我不明白你的意見。但是我也不明白爲什麼你在這裏發佈這個問題:在Web上的Java的BST實現是否存在短缺?只是谷歌它,看看它是如何完成的。 – alfasin 2013-03-02 05:33:54

回答

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

注:如果你想在Node<E>parent節點那就已經很不容易。

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

}