java - 将 inOrderTraversal 方法的内容放入文件时遇到问题

标签 java linked-list binary-search-tree printwriter

我一直在尝试获取它,以便该方法返回一个字符串,但它会抛出一堆错误。我需要将 inOrderTraversal 方法内容获取到文件中或以某种方式存储它,以便我可以将其写入文件。如果有人能帮助我,我将非常感激!谢谢!!


public class BinaryTree {

    // Tree: simplest possible binary search tree
    private Node root; // hidden root node

    // inorderTraversal: need because root is hidden
    public void inorderTraversal() {
        inorderT(root);
    }

    // inorderT: recursive function that does the work
    private void inorderT(Node node) {
        if (node != null) {
            inorderT(node.left);
            System.out.println(node.data+" ");
            //node.data = node.data+" ";
            inorderT(node.right);
    }
}
========================================================================================================
public static void main(String ... args) throws IOException {

        System.out.println("Starting");
        File file = new File("C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\unsorteddict.txt");
        Scanner scan = new Scanner(file);

        //Creation of linked list and line variable
        String fileContents = "";
        LinkedList<String> linkedList = new LinkedList<>();
        BinaryTree tree = new BinaryTree();
        int lineNum = 0;

        //Writing contents into a file
        PrintWriter writer = new PrintWriter("C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\sorteddict.txt", "UTF-8");

        while(scan.hasNextLine()){
            fileContents = scan.nextLine();
            lineNum++;
            int x = 0;
            if (linkedList.size() == 0) {
                linkedList.add(0, fileContents);
            }
            else {
                for (int i = 0; i < linkedList.size(); i++) {
                    tree.insert(fileContents);
                }
            }
        }

        tree.inorderTraversal(); //NEED TO PUT THIS LINE INTO A FILE

        System.out.println("Ended");
}

最佳答案

您可以将writer对象传递给inorderTraversal()方法以及inorderT()方法,并将中序遍历节点数据写入文件。

public class BinaryTree {

    // Tree: simplest possible binary search tree
    private Node root; // hidden root node

    // inorderTraversal: need because root is hidden
    public void inorderTraversal(PrintWriter writer) {
        inorderT(root, writer);
    }

    // inorderT: recursive function that does the work
    private void inorderT(Node node, PrintWriter writer) {
        if (node != null) {
            inorderT(node.left, writer);
            System.out.println(node.data+" ");
            //node.data = node.data+" ";
            writer.println(node.data+" ");    // Here, write to the file.
            inorderT(node.right, writer);
    }
}
public static void main(String ... args) throws IOException {

        System.out.println("Starting");
        File file = new File("C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\unsorteddict.txt");
        Scanner scan = new Scanner(file);

        //Creation of linked list and line variable
        String fileContents = "";
        LinkedList<String> linkedList = new LinkedList<>();
        BinaryTree tree = new BinaryTree();
        int lineNum = 0;

        //Writing contents into a file
        PrintWriter writer = new PrintWriter("C:\\Users\\Marlene\\Workspace\\BinaryTree\\src\\com\\company\\sorteddict.txt", "UTF-8");

        while(scan.hasNextLine()){
            fileContents = scan.nextLine();
            lineNum++;
            int x = 0;
            if (linkedList.size() == 0) {
                linkedList.add(0, fileContents);
            }
            else {
                for (int i = 0; i < linkedList.size(); i++) {
                    tree.insert(fileContents);
                }
            }
        }

        tree.inorderTraversal(writer); //NEED TO PUT THIS LINE INTO A FILE

        System.out.println("Ended");
}

关于java - 将 inOrderTraversal 方法的内容放入文件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58577716/

相关文章:

java - 我怎样才能在java中做出暂停选项

java - 如何初始化自定义类对象的二维列表 - Java

C++ 链表核心转储错误

c++ - 清除二叉搜索树

c - 二叉搜索树输出文件不是预期的输出数据

algorithm - 我试图在时间 O(1) 的二叉搜索树中找到一个键的后继

java - 设置 ehcache 复制 - 我需要什么多播设置?

java - 更新图形时 jframe flash

javascript - JavaScript 中的单链表中的 Set 如何与 Get 一起使用?

Java - 创建 3 维数据结构