我需要输出到一个txt文件..我能够输出我的单词但不能输出我的BST.I'我试图将我的二进制树按顺序,预订和后序输出到我的txt文件中输出。我该怎么做呢??我的代码如下:
import java.io.FileReader;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.FileNotFoundException;
public class Project3 {
public Node root;
public Project3 insert(int number) {
Node node = new Node(number);
if (root == null) {
root = node;
return this;
}
insertValue(root, node);
return this;
}
public class Node {
public int number;
public Node left;
public Node right;
public Node(int number) {
this.number = number;
}
}
private void insertValue(Node newRoot, Node node) {
if (newRoot.number > node.number) {
if (newRoot.left == null) {
newRoot.left = node;
return;
} else {
insertValue(newRoot.left, node);
}
} else {
if (newRoot.right == null) {
newRoot.right = node;
return;
} else {
insertValue(newRoot.right, node);
}
}
}
public void printInorder() {
printInOrderImplem(root);
System.out.println("");
}
public void printPreorder() {
PreOrderImplem(root);
System.out.println("");
}
public void printPostorder() {
printPostOrderImplem(root);
System.out.println("");
}
private void printInOrderImplem(Node currRoot) {
if (currRoot == null) {
return;
}
printInOrderImplem(currRoot.left);
System.out.print(currRoot.number + ", ");
printInOrderImplem(currRoot.right);
}
private void PreOrderImplem(Node currRoot) {
if (currRoot == null) {
return;
}
System.out.print(currRoot.number + ", ");
PreOrderImplem(currRoot.left);
PreOrderImplem(currRoot.right);
}
private void printPostOrderImplem(Node currRoot) {
if (currRoot == null) {
return;
}
printPostOrderImplem(currRoot.left);
printPostOrderImplem(currRoot.right);
System.out.print(currRoot.number + ", ");
}
public static void main(String[] args) throws FileNotFoundException {
PrintStream out = new PrintStream(new FileOutputStream("/Users/Desktop/output.txt"));
Project3 BST = new Project3();
// numbers to insert 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
BST.insert(14);
BST.insert(15);
BST.insert(4);
BST.insert(9);
BST.insert(7);
BST.insert(18);
BST.insert(3);
BST.insert(5);
BST.insert(16);
BST.insert(4);
BST.insert(20);
BST.insert(17);
BST.insert(9);
BST.insert(14);
BST.insert(5);
System.out.println("Inorder traversal is- ");
out.println("Inorder traversal is- ");
BST.printInorder();
System.out.println("Preorder traversal is- ");
out.println("Preorder Traversal is- ");
BST.printPreorder();
System.out.println("Postorder traversal is- ");
out.println("Postorder Traversal is- ");
BST.printPostorder();
}
}
答案 0 :(得分:0)
以下是如何写入txt文件的示例
For 128.138.243.100/26
Address: 128.138.243.100 10000000.10001010.11110011.01 100100
Netmask: 255.255.255.192 = 26 11111111.11111111.11111111.11 000000
Wildcard: 0.0.0.63 00000000.00000000.00000000.00 111111
=>
Network: 128.138.243.64/26 **10**000000.10001010.11110011.01 000000 (Class B)
Broadcast: 128.138.243.127 10000000.10001010.11110011.01 111111
HostMin: 128.138.243.65 10000000.10001010.11110011.01 000001
HostMax: 128.138.243.126 10000000.10001010.11110011.01 111110
Hosts/Net: 62
请注意.txt(或您编写的任何文件)与.java位于同一目录中。如果希望它位于不同的目录中,则必须在文件名前输入目录。 (即" filename / example.txt")
答案 1 :(得分:0)
您的printXorder()
方法始终打印到System.out
,因此您永远不会在磁盘上的文件中看到它。你有几个选择,我将解释主要的两个:
printXorder()
方法,请使用toStringXorder()
,它返回String
,然后您可以打印到您喜欢的任何流。如果您执行此操作,则应在StringBuilder
构建阶段使用StringBuffer
(或String
,如果您的代码是可重入的),并将其转换为String
最后一步。此外,StringBuilder
允许您轻松删除您在实现中获得的令人讨厌的最后一个逗号(如果您选择删除逗号,请注意toString
空树。)printXorder()
方法获取PrintStream
参数并将其打印到PrintStream
。如果你的树不会长得太多,第一个解决方案就是OK(通常更灵活)。如果树可能变得非常大(因此生成的String
会导致难以适应内存),那么您将需要第二个。