java—将printwriter存储在方法中以打印驱动程序中的void方法

ryoqjall  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(206)

对于我当前的程序,我有两个递归方法,它们对binarytree类中的树进行排序。驱动程序将通过binarysearchtree类调用它们。我已经完成了所有的工作,除了叫司机来。我把他们叫到司机那里去了(看看最后几句话)。
司机:

package driver;

import java.io.*;
import java.util.*;

public class Driver {

    private static Scanner file;
    static PrintWriter outputFilePrinter;
    static Scanner inputFileScanner;

    public static void main(String[] args) throws FileNotFoundException {

        Scanner inFile; //Declaring scanner obj
        PrintWriter printWriter = new PrintWriter("output.txt"); //Text file where results are printed to
        inFile = new Scanner(new File("input.txt")); //Text file where student names and associated number are read from

        BinarySearchTreeInterface<String> aTree = new BinarySearchTree<>() {}; //Giving access to BinarySearchTree methods
        BinaryTreeInterface<String> aTree2 = new BinaryTree<>() {};

        while (inFile.hasNext()) { //Scans input file
            String name = inFile.nextLine(); //Assigns contents to string obj
            aTree.add(name); //Add string to a tree
        } //End while

        Iterator<String> traverse = aTree.getPreorderIterator(); //Using iterator to print tree in an "postorder" fashion   
            while (traverse.hasNext())
        printWriter.println(traverse.next()); //Prints tree

        printWriter.println("Postorder Recrusive: ");
        aTree.postorderTraverse(???);
        printWriter.println("");
        printWriter.println("Inorder Recursive: ");
        aTree.inorderTraverse(???);

        printWriter.close(); //Closes file

    } //End Main

} //End Driver

二进制树中的递归方法:

public void inorderTraverse(PrintWriter printWriter) {
    inorderTraverse(root, printWriter);
    }

    public void inorderTraverse(BinaryNode<T> node, PrintWriter printWriter) {
        if (node != null) {
            inorderTraverse(node.getLeftChild(), printWriter);
            printWriter.println(node.getData());
            inorderTraverse(node.getRightChild(), printWriter);
        }
    }

    public void postorderTraverse(PrintWriter printWriter) {
        postorderTraverse(root, printWriter);
    }

    public void postorderTraverse(BinaryNode<T> node, PrintWriter printWriter) {
        if (node != null) {  
        postorderTraverse(node.getLeftChild(), printWriter);
        postorderTraverse(node.getRightChild(), printWriter);
        printWriter.println(node.getData());
        }
    }

binarysearchtree生成可通过binarysearchtree访问的binarytree方法:

public void inorderTraverse (PrintWriter printWriter) {  
    super.inorderTraverse(printWriter);
}

public void postorderTraverse(PrintWriter printWriter) {
    super.postorderTraverse(printWriter);
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题