使用java在二叉搜索树中保留root的值

使用java在二叉搜索树中保留root的值,java,recursion,tree,root,binary-search-tree,Java,Recursion,Tree,Root,Binary Search Tree,我试图用java编写一个二叉搜索树,目前遇到了调用某些方法后无法保留root值的问题 现在我正在编写add()和height()方法,如下所示: public boolean add(E x) { BinaryNode<E> temp = null; if (root == null) { root = new BinaryNode<>(x); temp = root; return true; }

我试图用java编写一个二叉搜索树,目前遇到了调用某些方法后无法保留root值的问题

现在我正在编写add()和height()方法,如下所示:

public boolean add(E x) {
    BinaryNode<E> temp = null;
    if (root == null) {
        root = new BinaryNode<>(x);
        temp = root;
        return true;
    } else {


        BinaryNode<E> node = root;

        if(root.equals(x)){
            root = temp;
            return false;
        }

        if (node.element.compareTo(x) < 0) {
            if (node.left == null) {
                node.left = new BinaryNode<E>(x);
                return true;
            }
            root = node.left;
            add(x);
        }
        if (node.element.compareTo(x) > 0) {
            if (node.right == null) {
                node.right = new BinaryNode<E>(x);
                return true;
            }
            root = node.right;
            add(x);
        }
        root = temp;
    }
    return false;
}
public int height() {

    if(root == null){
        return 0;           
    } else {

        int lefth = 0;
        int righth = 0;

        BinaryNode<E> node = root;
        if(root.left != null){
            root = root.left;
            lefth = height();
        }
        if(root.right != null){
            root = root.right;
            righth = height();
        }

        root = node;

        if(lefth > righth){
            return lefth+1;
        } else {
            return righth+1;
        }
    }
}
因此,看起来,我的add(ex)功能正常,因为我不允许添加重复项。然而,我真的不知道如何正确地编写height()代码


任何帮助都将不胜感激

我还没有详细阅读代码,但看起来/听起来您在混合临时/局部变量和类变量

调用
height
时,您正在修改根节点,这实际上更改了全局评分的根节点

相反,您应该在方法中使用一个局部变量来存储当前根并使用它进行搜索,而不是修改全局变量

i、 e


现在,您可以使用searchNode进行所有处理,而不会损坏根节点的全局记录。

Hey Tim!我确实尝试过这样做,但我当时遇到的问题是add(ex)和height()会导致堆栈溢出,因为它们实际上没有更改全局根属性。在方法本身中调用该方法的事实似乎使它不得不更改全局属性,因为我没有使用输入参数。我只需要找出一种方法,让root在方法调用前后具有相同的值。然而,在该方法中,似乎必须修改root才能实现正确的递归;对于临时根-这本质上就是我遇到的问题。
public int height() {

    if(root == null){
        return 0;           
    } else {

        int lefth = 0;
        int righth = 0;

        BinaryNode<E> node = root;
        if(root.left != null){
            root = root.left;
            lefth = height();
        }
        if(root.right != null){
            root = root.right;
            righth = height();
        }

        root = node;

        if(lefth > righth){
            return lefth+1;
        } else {
            return righth+1;
        }
    }
}
    public static void main(String[] args) {
    BinarySearchTree<Integer> tree = new BinarySearchTree<>();

    int a = 10;
    int b = 11;
    int c = 9;
    int d = 9;
    int e = 5;
    int f = 8;

    System.out.println("Adding " + a + " " + tree.add(a) + " \tHeight: " + tree.height());
    System.out.println("Adding " + b + " " + tree.add(b) + " \tHeight: " + tree.height());
    System.out.println("Adding " + c + " " + tree.add(c) + " \tHeight: " + tree.height());
    System.out.println("Adding " + d + " " + tree.add(d) + " \tHeight: " + tree.height());
    System.out.println("Adding " + e + " " + tree.add(e) + " \tHeight: " + tree.height());
    System.out.println("Adding " + f + " " + tree.add(f) + " \tHeight: " + tree.height());

}
Adding 10 true  Height: 1
Adding 11 true  Height: 2
Adding 9 true   Height: 2
Adding 9 false  Height: 0
Adding 5 true   Height: 1
Adding 8 true   Height: 2
public int height() {
    Node searchNode = root;
    if(searchNode == null){