有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我一直在尝试获取它,以便该方法返回一个字符串,但它会抛出一系列错误。我需要将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");
}

共 (1) 个答案

  1. # 1 楼答案

    您可以将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");
    }