我正在尝试为我的BST类实现删除功能。我正在尝试遵循一些标准教程,并且除了我的删除功能之外我很好。我没有收到任何错误,但它没有删除预期的节点。我递归地到达预期的Node,如果它只有一个子节点则返回对方节点,否则我从n.right返回最小值。
任何帮助将不胜感激 - 谢谢!
public class BinaryTree {
private Node root = null;
private class Node {
private Node left;
private Node right;
private int data;
public Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
public boolean add(int data) {
root = add(root, data);
return true;
}
private Node add(Node node, int data) {
if (node == null) {
node = new Node(data, null, null);
}
else {
if (data < node.data) {
node.left = add(node.left, data);
}
else {
node.right = add(node.right, data);
}
}
return node;
}
// Helper function
private Node findMin(Node node) {
while (node.left != null) {
node = node.left;
}
return node;
}
private Node findMax(Node node) {
while (node.right != null) {
node = node.right;
}
return node;
}
private void inOrderTraversal(Node n) {
if (n == null) return;
else {
inOrderTraversal(n.left);
System.out.println(n.data);
inOrderTraversal(n.right);
}
}
public void inOrderTraversal() {
inOrderTraversal(root);
}
private Node deleteKey(Node n, int data) {
if (n == null) return n;
if (data < n.data) deleteKey(n.left, data);
else if (data > n.data) deleteKey(n.right, data);
else {
if (n.left == null) return n.right;
else if (n.right == null) return n.left;
n.data = findMin(n.right).data;
n.right = deleteKey(n.right, n.data);
}
return n;
}
public void deleteKey(int data) {
root = deleteKey(root, data);
}
public static void main(String[] args) {
BinaryTree bst = new BinaryTree();
bst.add(2);
bst.add(8);
bst.add(1);
bst.add(7);
bst.add(3);
bst.add(11);
bst.add(1);
bst.add(21);
bst.add(10);
bst.add(12);
bst.inOrderTraversal();
System.out.println("----------------");
bst.deleteKey(3);
bst.inOrderTraversal();
}
}
答案 0 :(得分:2)
我认为问题出在您的private Node deleteKey(Node n, int data) {
if (n == null) return n;
if (data < n.data)
n.left = deleteKey(n.left, data); // must reassign child here
else if (data > n.data)
n.right = deleteKey(n.right, data); // must reassign child here
else {
if (n.left == null) return n.right;
else if (n.right == null) return n.left;
n.data = findMin(n.right).data;
n.right = deleteKey(n.right, n.data); // this was correct
}
return n;
}
函数中:
data < n.data
当data > n.data
或 LOGINID DATE TIME
mickey 1/1/2018 0 days 08:54:00.000000000
mickey 1/1/2018 0 days 08:57:00.000000000
mickey 1/1/2018 0 days 09:01:00.000000000
mickey 1/1/2018 0 days 09:19:00.000000000
mickey 1/1/2018 0 days 09:26:00.000000000
mickey 1/1/2018 0 days 09:41:00.000000000
mickey 1/1/2018 0 days 10:03:00.000000000
mickey 1/1/2018 0 days 11:35:00.000000000
mickey 1/1/2018 0 days 11:36:00.000000000
mickey 1/2/2018 0 days 09:54:00.000000000
mickey 1/2/2018 0 days 10:21:00.000000000
minnie 1/1/2018 0 days 09:00:00.000000000
minnie 1/1/2018 0 days 10:20:00.000000000
minnie 1/1/2018 0 days 10:14:00.000000000
minnie 1/1/2018 0 days 10:51:00.000000000
minnie 1/1/2018 0 days 11:05:00.000000000
minnie 1/1/2018 0 days 11:24:00.000000000
minnie 1/2/2018 0 days 02:20:00.000000000
minnie 1/2/2018 0 days 02:25:00.000000000
minnie 1/2/2018 0 days 02:39:00.000000000
This is what I want:
LOGINID DATE TIME intense_cnt
mickey 1/1/2018 0 days 08:54:00.000000000 4
mickey 1/1/2018 0 days 08:57:00.000000000 4
mickey 1/1/2018 0 days 09:01:00.000000000 3
mickey 1/1/2018 0 days 09:19:00.000000000 3
mickey 1/1/2018 0 days 09:26:00.000000000 2
mickey 1/1/2018 0 days 09:41:00.000000000 2
mickey 1/1/2018 0 days 10:03:00.000000000 1
mickey 1/1/2018 0 days 11:35:00.000000000 2
mickey 1/1/2018 0 days 11:36:00.000000000 2
mickey 1/2/2018 0 days 09:54:00.000000000 2
mickey 1/2/2018 0 days 10:21:00.000000000 1
minnie 1/1/2018 0 days 09:00:00.000000000 1
minnie 1/1/2018 0 days 10:20:00.000000000 2
minnie 1/1/2018 0 days 10:14:00.000000000 2
minnie 1/1/2018 0 days 10:51:00.000000000 2
minnie 1/1/2018 0 days 11:05:00.000000000 2
minnie 1/1/2018 0 days 11:24:00.000000000 1
minnie 1/2/2018 0 days 02:20:00.000000000 3
minnie 1/2/2018 0 days 02:25:00.000000000 3
minnie 1/2/2018 0 days 02:39:00.000000000 3
时,您不会更新当前节点的离开。所以这是找到要删除的相应节点,但是它不能在从递归的备份路径上正确地重建节点分支。