Java 二叉树调试

Java 二叉树调试,java,algorithm,debugging,binary-tree,Java,Algorithm,Debugging,Binary Tree,我在编写二叉树程序时遇到了一个调试问题。在程序的主方法中,我使用构造函数创建一个名为root的节点,然后使用getKey()方法获取“previous”的键,该键应该指向“root” 这是我的密码: /** * BinaryTreeExample from Internet * @author xinruchen * */ import java.util.*; public class BinaryTreeExample { private static Node root;

我在编写二叉树程序时遇到了一个调试问题。在程序的主方法中,我使用构造函数创建一个名为
root
的节点,然后使用
getKey()
方法获取“
previous
”的键,该键应该指向“root”

这是我的密码:

/**
 * BinaryTreeExample from Internet
 * @author xinruchen
 *
 */
import java.util.*;

public class BinaryTreeExample
{
    private static Node root;




    public BinaryTreeExample(int data)
    {
        root = new Node(data);

    }

    public void add(Node parent,Node child, String orientation)
    {
        if(orientation=="left")
        {
           parent.setLeft(child);
        } 
        else if (orientation=="right")
        {
            parent.setRight(child);
        }

    }

    public static void main(String ar[])
    {

        Scanner sc = new Scanner(System.in);
        int times = sc.nextInt();

        BinaryTreeExample l1=new BinaryTreeExample(3);
        Node previous = root;
        String direction = "";
        System.out.println(previous.getKey());
  }
}

class Node {
    private int key;
    private Node left;
    private Node right;


    Node (int key) {
        this.key = key;
        right = null;
        left = null;

    } // constructor

    public void setKey(int key) {
        this.key = key;
    }

    public int getKey() {
        return key;
    }

    public void setLeft(Node l) {
        if (left == null) {
            this.left = l;
        }
        else {
            left.left = l;
        }
    }

    public Node getLeft() {
        return left;
    }

    public void setRight(Node r ) {
        if (right == null) {
            this.right = r;
        }
        else {
            right.right = r;
        }
    }

    public Node getRight() {
        return right;
    }

}

如果一切按预期进行,它应该输出“3”,但它什么也不输出。我检查了我的代码并遵循了代码流程,但仍然找不到问题所在。请帮帮我,谢谢

启动程序时,它将在指令
int times=sc.nextInt()处等待用户输入


一旦输入,程序将按预期打印
3

您不应该使用扫描仪,因为您已将值硬编码为3,并且您没有使用时间,或者您应该这样使用

        System.out.println("Enter the value");
        Scanner sc = new Scanner(System.in);
        int times = sc.nextInt();

        BinaryTreeExample l1=new BinaryTreeExample(times);

无论何时您请求输入,您都应该给出提示。虽然这不是强制性要求,但可以避免程序等待输入时出现的混乱