我需要在一个名为 Dictionary 的类中创建一个方法,该方法在 java 中递归地逐行读取 txt 文件的内容,然后将每一行作为节点添加到树中。这是文件内容的示例:
ourselves
out
over
own
same
shan't
she
all
am
a
about
above
after
again
against
aren't
should
shouldn't
so
some
such
than
that
that's
the
their
它持续了四倍多的长度。我已经设置了二叉树,它包括以下方法:
getKey() //returns the value of the current node
setKey() //sets the value of the current node
getLeftChild() //gets the value of the left child of the node inside the parentheses
getRightChild() //does the same as the above, but with the right child
setLeftChild(BinaryTreeNode node) //sets the left child
setRightChild(BinaryTreeNode node) //sets the right child
最后,树应该将 txt 文件中的单独文本行作为树中的单独节点。我在递归方面遇到了很多麻烦,我不确定如何让程序正确读取文件的内容。我真的很感激任何建议或帮助。
如果它有帮助,这是我迄今为止完成的已完成 1/4 的方法:
public Dictionary(String filePath) throws IOException{ // the file path in my system for
//dictionary.txt
//I read from this file and build my tree.
BufferedReader br = new
BufferedReader(new FileReader("Dictionary.txt"));
String word;
if ((word = br.readLine()) == null)
System.out.println("Your tree is empty.");
while ((word = br.readLine()) != null) {
//now I need to tell it to add word as a node.
}
br.close();
}
- -编辑 - -
对于如何设置树没有其他要求。Dictionary.txt 文件中的每一行文本都必须是一个单独的节点。也就是说,“我们自己”和“外面”需要是单独的节点,等等。