递归二叉树打印错误

Recursion binary tree print error

我在处理此问题时遇到了一些问题 assignment.I 目前正在努力解决这个问题。我能够存储和打印值,但是当我打印这些值时,它只打印我输入的第一个值。任何帮助都会很棒!我真的不太了解递归,它有点伤脑筋。

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.");
            }
        }
    }
}

我更正了您的代码并将其分成两部分 类: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.");
            }
        }
    }
}