Java 递归二叉树打印错误

Java 递归二叉树打印错误,java,binary-tree,Java,Binary Tree,我在完成这项任务时遇到了一些问题。我目前正在努力解决这些问题。我可以存储和打印值,但是当我打印值时,它只打印我输入的第一个值。任何帮助都将是惊人的!我真的不太了解递归,它有点伤脑筋 package lab6; import java.util.Scanner; public class node { private int value; static node root; public node leftLink; public node rightLink

我在完成这项任务时遇到了一些问题。我目前正在努力解决这些问题。我可以存储和打印值,但是当我打印值时,它只打印我输入的第一个值。任何帮助都将是惊人的!我真的不太了解递归,它有点伤脑筋

package lab6;

import java.util.Scanner;

public class node {

    private int value;
    static node root;
    public node leftLink;
    public node rightLink;

    public node(int v) {
        this.value = v;
    }

    public int getValue() {
        return value;
    }

    static void traverseShow() {
        if (root.leftLink != null) {
            root = root.leftLink;
            traverseShow();
        }
        System.out.println(root.getValue());
        if (root.rightLink != null) {
            root = root.rightLink;
            traverseShow();

        }

        return;
    }

    static void addNode(node n) {
        if (root == null) {
            root = n;
        } else {
            node tmp = root; // save the current root
            if (root.getValue() > n.getValue()) {
                root = root.leftLink;
                addNode(n);
            } else if (root.getValue() < n.getValue()) {
                root = root.rightLink;
                addNode(n);
            }
            root = tmp; // put the root back to its original value
        }
        return;
    }

    public static void main(String[] args) {
        int val = 0;
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        String command = "";

        while (loop == true) {
            System.out.println("Please enter a command:");
            System.out.println("A = insert a new value");
            System.out.println("B = display all values");
            System.out.println("C = exit program");
            command = sc.next();
            if (command.equalsIgnoreCase("a")) {
                System.out.println("Enter value: ");
                val = sc.nextInt();
                node newNode = new node(val);
                addNode(newNode);
            } else if (command.equalsIgnoreCase("b")) {
                traverseShow();
            } else if (command.equalsIgnoreCase("c")) {
                sc.close();
                System.exit(0);
            } else {
                System.out.println("Invalid command! Please try again.");
            }
        }
    }
}
lab6包装;
导入java.util.Scanner;
公共类节点{
私有int值;
静态节点根;
公共节点左链接;
公共节点右链接;
公共节点(INTV){
该值=v;
}
public int getValue(){
返回值;
}
静态void transverseshow(){
if(root.leftLink!=null){
root=root.leftLink;
traverseShow();
}
System.out.println(root.getValue());
if(root.rightLink!=null){
root=root.rightLink;
traverseShow();
}
返回;
}
静态void addNode(节点n){
if(root==null){
根=n;
}否则{
node tmp=root;//保存当前根目录
if(root.getValue()>n.getValue()){
root=root.leftLink;
addNode(n);
}else if(root.getValue()
我更正了您的代码,并将其分为两类:Main和Node。现在我测试了它,它正在工作。您的主要错误是无法更改根,因为它是访问整个树的唯一引用。相反,您需要告诉子节点为您添加节点(节点n)。这就是递归发生的时候。这同样适用于方法traverseShow()。事实上,在这种情况下,调试将对您有很大帮助

public class Node {

    private int value;
    public Node leftLink;
    public Node rightLink;

    public Node() {

    }

    public Node(int v) {
        this.value = v;
    }

    public int getValue() {
        return value;
    }

    void addNode(Node n) {

        //node tmp = root; // save the current root
        if (getValue() > n.getValue()) {
            if(leftLink == null){
                leftLink = n;
            }else{
                leftLink.addNode(n);
            }
        } else if (getValue() < n.getValue()) {
            if(rightLink == null){
                rightLink = n;
            }else{
                rightLink.addNode(n);
            }
            //root = root.rightLink;
            //addNode(n);
        }
        //root = tmp; // put the root back to its original value

        return;
    }

    void traverseShow() {
        if (leftLink != null) {
            leftLink.traverseShow();
        }
        System.out.println(getValue());
        if (rightLink != null) {
            rightLink.traverseShow();
        }

        return;
    }
}

public class Main {
    public static void main(String[] args) {

        Node rootNode = null;
        int val = 0;
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        String command = "";

        while (loop == true) {
            System.out.println("Please enter a command:");
            System.out.println("A = insert a new value");
            System.out.println("B = display all values");
            System.out.println("C = exit program");
            command = sc.next();
            if (command.equalsIgnoreCase("a")) {
                System.out.println("Enter value: ");
                val = sc.nextInt();
                Node newNode = new Node(val);
                if(rootNode == null){
                    rootNode = new Node(val);
                }else{
                    rootNode.addNode(newNode);
                }
            } else if (command.equalsIgnoreCase("b")) {
                rootNode.traverseShow();
            } else if (command.equalsIgnoreCase("c")) {
                sc.close();
                System.exit(0);
            } else {
                System.out.println("Invalid command! Please try again.");
            }
        }
    }
}
公共类节点{
私有int值;
公共节点左链接;
公共节点右链接;
公共节点(){
}
公共节点(INTV){
该值=v;
}
public int getValue(){
返回值;
}
void addNode(节点n){
//node tmp=root;//保存当前根目录
如果(getValue()>n.getValue()){
if(leftLink==null){
leftLink=n;
}否则{
leftLink.addNode(n);
}
}else if(getValue()
您的
节点
类不应该有
节点根
变量。而且绝对不应该是静态的。此外,目前还不清楚到底是什么不起作用。