Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java中的文本填充树_Java_Recursion_Tree_Binary Search Tree - Fatal编程技术网

用Java中的文本填充树

用Java中的文本填充树,java,recursion,tree,binary-search-tree,Java,Recursion,Tree,Binary Search Tree,我需要在一个名为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 public Node readFile(Bu

我需要在一个名为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
 public Node readFile(BufferedReader reader){
     String word = reader.readLine();         
     if(word!=null){
         return new Node(word, readFile(reader), readFile(reader));            
     }else{
         return new Node() ;  // empty node or null?
     }      
 }
它的长度是这个长度的四倍多。我已经建立了二叉树,它包括以下方法:

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文件中的每一行文本都必须是一个单独的节点。也就是说,“我们”和“我们”需要是分开的节点,等等

假设您有一个可以这样构造的
节点(您现在不需要getter和setter):

首先,我将设置一个方法,该方法实际递归读取文件:

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
 public Node readFile(BufferedReader reader){
     String word = reader.readLine();         
     if(word!=null){
         return new Node(word, readFile(reader), readFile(reader));            
     }else{
         return new Node() ;  // empty node or null?
     }      
 }
编辑

然后,您可以按如下方式初始化词典:

   private Node dictionaryTree;   // this is the tree within your dictionary class

   public Dictionary(String filePath) throws IOException{
         BufferedReader br = new BufferedReader(new FileReader("Dictionary.txt"));
         this.dictionaryTree = readFile(br);
         br.close();
   }

我不是100%确定它是否会像您希望的那样工作,但它遵循递归原则。之后,
root
就是完整的树,您可以通过
getLeftChild()
getRightChild()
方法在树中迭代。

请告诉我们更多要求(家庭作业问题)。。这棵树(二叉树)需要按照单词的字母顺序来构建,还是只按照随机顺序就可以了?看起来像是一个家庭作业问题。我还怀疑它必须被分类……我相信我可以帮你解决这部分问题:“……不知道如何让程序正确读取文件的内容。”与其使用
缓冲读取器
,不如像这样使用
Scanner
类:
Scanner input=new Scanner(new file(“Dictionary.txt”)。只需确保抛出
FileNotFoundException`而不是IOException,并导入
java.util.Scanner
java.io.File
java.io.FileNotFoundException
如果必须使用递归读取文件,则不能使用带有while循环的扫描仪,他必须对他的读取函数进行递归调用。这是如何连接回Dictionary.txt文件的?是的,我忘记了:在您开始使用
Node root=readFile(bufferedReader)创建树之前应该初始化BufferedReader。读卡器将连接到文本文件,它将始终知道下一行是哪一行。很抱歉,我刚刚意识到我确实调用了它
节点
,而不是像您那样调用了
二进制树节点
(可能是我太懒了,没有写;)