Java 链接二叉树和删除叶子

Java 链接二叉树和删除叶子,java,algorithm,binary-tree,Java,Algorithm,Binary Tree,目前我正在做一个项目,似乎我无法超越逻辑 删除叶子的最佳方式是什么?现在我陷入了一个NullPointerException,不知道为什么。任何帮助都将不胜感激 @Override public E removeLeaf(Position<E> p){ BinaryTreeNode<E> node = ((BinaryTreeNode<E>) p); E element = node.element();

目前我正在做一个项目,似乎我无法超越逻辑

删除叶子的最佳方式是什么?现在我陷入了一个NullPointerException,不知道为什么。任何帮助都将不胜感激

@Override
    public E removeLeaf(Position<E> p){
        BinaryTreeNode<E> node = ((BinaryTreeNode<E>) p);
        E element = node.element();
        if(node.left != null && node.right != null) throw new InvalidPositionException();
        node.parent.left = null;
        node.parent.right = null;
        return element;
    }
@覆盖
公共电子设备拆卸区(位置p){
二元树节点=((二元树节点)p);
E element=node.element();
如果(node.left!=null&&node.right!=null)抛出新的InvalidPositionException();
node.parent.left=null;
node.parent.right=null;
返回元素;
}
下面的代码是我的测试类

public class BinaryTreeTester {

    public static void main(String[] args) {

        LinkedBinaryTree<Integer> numTree = new LinkedBinaryTree<>();
        Position<Integer> nroot = numTree.addRoot(1);
        System.out.println(numTree.toString());
        Position<Integer> nchild1 = numTree.insertChild(nroot, 6);
        System.out.println(numTree.toString());
        Position<Integer> nchild2 = numTree.insertChild(nroot, 7);
        Position<Integer> nchild11 = numTree.insertChild(nchild1, 8);
        Position<Integer> nchild12 = numTree.insertChild(nchild1, 23);
        Position<Integer> nchild20 = numTree.insertChild(nchild12, 11);
        Position<Integer> nchild21 = numTree.insertChild(nchild12, 21);
        System.out.println(numTree.toString());

        numTree.removeLeaf(nchild20);

        System.out.println(numTree.toString());
    }
}

公共类BinaryTreeTester{
公共静态void main(字符串[]args){
LinkedBinaryTree numTree=新建LinkedBinaryTree();
位置nroot=numTree.addRoot(1);
System.out.println(numTree.toString());
位置nchild1=numTree.insertChild(nroot,6);
System.out.println(numTree.toString());
位置nchild2=numTree.insertChild(nroot,7);
位置nchild11=numTree.insertChild(nchild1,8);
位置nchild12=numTree.insertChild(nchild1,23);
位置nchild20=numTree.insertChild(nchild12,11);
位置nchild21=numTree.insertChild(nchild12,21);
System.out.println(numTree.toString());
numTree.removeLeaf(nchild20);
System.out.println(numTree.toString());
}
}

我在下面的代码片段中看到两个问题:

if(node.left != null && node.right != null) throw new InvalidPositionException();
node.parent.left = null;
node.parent.right = null;
第一行似乎测试节点是否真的是一个叶子,但逻辑是错误的。它当前测试节点是否有两个子节点,但它甚至应该拒绝有一个子节点的情况。所以
&&
应该是
|

另外两行清除了带有两个子节点的链接,而它应该只分离当前节点,而不分离其同级节点。因此,您需要确定当前节点是其父节点的左子节点还是右子节点,并且只清除该链接:

if (node.left != null || node.right != null) throw new InvalidPositionException();
if (node.parent.left == node) {
    node.parent.left = null;
} else {
    node.parent.right = null;
}
这仍然不能很好地处理边界情况,其中节点是根本身(树中的最后一个节点)。在这种情况下,
parent
很可能是
null
(这取决于您的实现)。因此,您需要为此添加一个条件。如果对树的根节点有一个引用
root
,则应清除该
root

if (node.left != null || node.right != null) throw new InvalidPositionException();
if (node.parent == null) {
    root = null; // this assumes you have such a variable...
} else if (node.parent.left == node) {
    node.parent.left = null;
} else {
    node.parent.right = null;
}

请在您的帖子中添加一个异常stacktrace。这将有助于回答这个问题。