二叉搜索树实现-搜索函数找不到节点(java)

1tu0hz3e  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(305)

我正在实现一个二进制搜索树 Java . 然而,我的 search 函数在remove函数中似乎工作不正常。总是找不到 Nodes 在树里面,不管它们是否真的在树里面。我认为我的逻辑是正确的,比较节点左右移动取决于 Node 搜索的范围是大还是小,但我在搜索时可能会遇到一些问题 return 价值观。如果添加node类或程序测试会有所帮助,我可以这样做。有什么建议吗?

public class BinarySearchTree {
    private boolean empty = true;
    private int size = 0;
    private Node root;

    public BinarySearchTree(Node root)
    {
        this.root = root;
    }

    public Node[] search(int value)
    {
        Node parent = null;
        Node currentNode = root;

        Node[] returnList = new Node[2];

        while ( currentNode != null )
        {
            if (currentNode.getValue() == value)
            {
                returnList[0] = parent;
                returnList[1] = currentNode;

                return returnList;
            }
            else if (value < currentNode.getValue())
            {
                parent = currentNode;
                currentNode = currentNode.getLeft();
            }
            else if (value > currentNode.getValue())
            {
                parent = currentNode;
                currentNode = currentNode.getRight();
            }
        }

        return returnList;
    }

    public void add(int value)
    {
        Node comparingNode = root;

        while (true)
        {
            if (comparingNode.getValue() == value)
            {
                System.out.println("Tried to add duplicate value of " + value);
                break;
            }
            if (comparingNode.getLeft() == null && comparingNode.getRight() == null)
            {
                if (value > comparingNode.getValue())
                {
                    comparingNode.setRight(new Node(value));
                }
                if (value < comparingNode.getValue())
                {
                    comparingNode.setLeft(new Node(value));
                }
                break;
            }
            else
            {
                if (value < comparingNode.getValue())
                {
                    if (comparingNode.getLeft() == null)
                    {
                        comparingNode.setLeft(new Node(value));
                        break;
                    }
                    else
                    {
                        comparingNode = comparingNode.getLeft();
                    }
                }
                if (value > comparingNode.getValue())
                {
                    if (comparingNode.getRight() == null)
                    {
                        comparingNode.setRight(new Node(value));
                        break;
                    }
                    else
                    {
                        comparingNode = comparingNode.getRight();
                    }
                }
            }
        }
    }

    public void remove(int value)
    {
        Node[] nodesFound = search( value );
        Node parent = nodesFound[0];
        Node child = nodesFound[1];

        boolean fLeft = ( parent.getLeft() == child );

        // child node has no children.
        if (fLeft)
        {
            parent.setLeft(null);
        }
        else
        {
            parent.setRight(null);          
        }

        if( child.getLeft() != null && child.getRight() == null )
        {
            // child node has only left child.
            if( fLeft )
            {
                parent.setLeft(child.getLeft());
            }
            else
            {
                parent.setRight(child.getLeft());
            }
        }
        else if ( child.getRight() != null && child.getLeft() == null )
        {
            // child node has only right child.
            if( fLeft )
            {
                parent.setLeft(child.getRight());
            }
            else
            {
                parent.setRight(child.getRight());
            }
        }
        else
        {
            // child node has both children.
            if( child.getRight().getLeft() == null )
            {
                child.getRight().setLeft( child.getLeft() );
                parent.setRight( child.getRight() );
            }
            else
            {
                Node[] returnList = findLeftMost2Children(child.getRight());
                Node leftMostParent = returnList[0];
                Node leftMostChild = returnList[1];

                leftMostParent.setLeft(null);

                leftMostChild.setLeft(child.getLeft());
                leftMostChild.setRight(child.getRight());
                parent.setRight(leftMostChild);
            }
        }
    }

    public Node getRoot()
    {
        return root;
    }

    public void outputTreeInOrder( Node root )
    {
        if( root == null )
            return;

        // Output the left tree.
        if( root.getLeft() != null )
            outputTreeInOrder( root.getLeft() );

        // Output the current node.
        System.out.print( root.getValue() + " " );

        // Output the right tree.
        if( root.getRight() != null )
            outputTreeInOrder( root.getRight() );       
    }

    private Node[] findLeftMost2Children( Node root )
    {
        Node parent = null;
        Node current = root;

        while (current.getLeft() != null)
        {
            parent = current;
            current = current.getLeft();
        }

        Node[] returnList = new Node[2];
        returnList[0] = parent;
        returnList[1] = current;

        return returnList;
    }
}
vddsk6oq

vddsk6oq1#

我用密码纠正了这个问题。我意识到在删除节点时必须设置适当的指针来移动节点,而不仅仅是更改值。你必须移动 node 用新的 pointers 为了它和它的 parent .

相关问题