刪除節點,我認爲我得到了大多數情況下,除了2的情況下工作(刪除節點只有一個子樹)。我的測試用例是下面沒有1,2,3和4節點的樹:從BST
這個程序告訴我從樹中刪除了6個,但是當我嘗試打印樹時,它仍然顯示6是在那個樹。
public class RandomNode {
static int size;
public static class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
left = null;
right = null;
size++;
}
}
// delete node in right subtree
public Node delete(Node root, int data) {
// base case - if tree is empty
if (root == null)
return root;
// search for deletion node
else if (data < root.data)
root.left = delete(root.left, data);
else if (data > root.data) {
root.right = delete(root.right, data);
} else {
// case 1: deletion node has no subtrees
if (root.left == null && root.right == null) {
root = null;
size--;
System.out.println(data + " successfully deleted from tree (case1)");
// case 2: deletion node has only one subtree
} else if (root.left != null && root.right == null) {
root = root.left;
size--;
System.out.println(data + " successfully deleted from tree (case2a)");
} else if (root.left == null && root.right != null) {
root = root.left;
size--;
System.out.println(data + " successfully deleted from tree (case2b)");
// case 3: deletion node has two subtrees
// *find min value in right subtree
// *replace deletion node with min value
// *remove the min value from right subtree or else there'll be
// a duplicate
} else if (root.left != null && root.right != null) {
Node temp;
if (root.right.right == null && root.right.left == null)
temp = findMinNode(root.left);
else
temp = findMinNode(root.right);
System.out.println(root.data + " replaced with " + temp.data);
root.data = temp.data;
if (root.right.right == null || root.left.left == null)
root.left = delete(root.left, root.data);
else
root.right = delete(root.right, root.data);
size--;
System.out.println(temp.data + " succesfuly deleted from tree (case3)");
}
}
return root;
}
// find min value in tree
public Node findMinNode(Node root) {
while (root.left != null)
root = root.left;
System.out.println("min value: " + root.data);
return root;
}
public void preOrderTraversal(Node root) {
if (root == null)
return;
preOrderTraversal(root.left);
System.out.println(root.data);
preOrderTraversal(root.right);
}
public static void main(String[] args) {
RandomNode r = new RandomNode();
Node root = new Node(6);
//root.left = new Node(2);
root.right = new Node(9);
//root.left.left = new Node(1);
//root.left.right = new Node(5);
//root.left.right.left = new Node(4);
//root.left.right.left.left = new Node(3);
root.right.left = new Node(8);
root.right.right = new Node(13);
root.right.left.left = new Node(7);
root.right.right.left = new Node(11);
root.right.right.right = new Node(18);
r.delete(root, 6);
r.preOrderTraversal(root);
}
}
工作!我只是認爲我不需要設置root = r.delete(root,someValue),因爲在我的刪除方法中,我使用root.left或root.right設置了root的值。你知道爲什麼這不起作用嗎? –
Java是通過價值傳遞的。所以,你的主循環有一個名爲'root'的變量。然後它調用'Node.delete(root,6)'。通過調用它,它將'root'(它是對某個包含對象數據的內存的引用)的值複製到'Node.delete()'方法中'root'參數的值。因此,無論main.root和node.root是參考文獻,都指同值到節點的節點6 刪除則其工作,和刪除的根變量被修改爲引用不同的節點(節點(7))。但主節點的根仍然指向節點(6)。刪除之後,主要方法打印出來的是root –
對我來說終於有道理了。謝謝Koos! –