使用文本文件的输入在java中构建树

问题描述 投票:0回答:1

我正在尝试使用文件中的数据构建任意大小的树。在文件中,每个节点都是自己的行,分支由某些关键字分隔。目前,我正在将文件读入列表,逐项阅读,并查找关键字来构建分支。在我开始第一个分支后,我很难弄清楚如何继续下去。以下是我的树类和测试输入文件。我意识到测试输入对于测试来说可能被认为太大,但实际的测试会有很多很多模型。最终目标是让这棵树代表哈雷戴维森自行车阵容,并在每辆自行车完全建成后提供所有可用的选项。例如,分支的一部分如下所示:

Harley(root) -> Model Line -> Model 1 -> color -> c1

每个分支的所有其他关键字依此类推。我的问题是我是否朝着正确的方向前进

populate()
。我能想到的唯一方法是使用一个大型
if...else if...
结构来连续检查每个关键字,并在每个
if...else
中使用一个循环来填充该关键字节点的子节点。即使我这样做了,我也不知道如何跳到下一个分支,而且我知道这是一种非常低效的制作树的方法。有什么建议吗?谢谢你。

树.java

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Tree
{
    private Node root;

    public Tree(String rootData) 
    {
        root = new Node();
        root.data = rootData;
        root.children = new ArrayList<Node>();
    }

    public static class Node
    {
        private String data;
        private Node parent;
        private List<Node> children;

        public Node(){}

        public Node(String newNodeData, Node newNodeParent)
        {
            data = newNodeData;
            parent = newNodeParent;
        }
    }

    public void populate() throws IOException
    {
        //keep track of nodes for jumping up branches quickly
        Node curNode = this.root;
        Node curModelLine;
        Node curModel;

        //get the data
        List<String> fileData = getData();
        int nextDataLine = 0;
        while (!fileData.isEmpty())
        {
            String curLine = fileData.get(nextDataLine);
            if (curLine == "model line")
            {
                curModelLine = new Node(fileData.get(nextDataLine+1), this.root);
                this.root.children.add(curModelLine);    
            }

            /*Not sure where to go from here*/

            nextDataLine++;
        }
    }

    public static List<String> getData() throws IOException
    {
        List<String> filedata = new ArrayList<String>();
        try
        {
            FileInputStream in = new FileInputStream("data.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String line;
            while((line = br.readLine())!= null)
            {
                filedata.add(line);
            }
            br.close();
        }catch(Exception e)
        {
            System.out.println(e);
        }
        return filedata;
    }
}

数据.txt:

harley
model line
linename1
modelname1
color
c1
c2
engine size
es1
es2
windsheild
w1
w2
lights
l1
l2
tire size
t1
t2
radio
r1
r2
abs
a1
a2
alarm
a1
a2
seat
s1
s2
bags
b1
b2
modelname2
color
c1
c2
engine size
es1
es2
windsheild
w1
w2
lights
l1
l2
tire size
t1
t2
radio
r1
r2
abs
a1
a2
alarm
a1
a2
seat
s1
s2
bags
b1
b2
linename2
modelname1
color
c1
c2
engine size
es1
es2
windsheild
w1
w2
lights
l1
l2
tire size
t1
t2
radio
r1
r2
abs
a1
a2
alarm
a1
a2
seat
s1
s2
bags
b1
b2
modelname2
color
c1
c2
engine size
es1
es2
windsheild
w1
w2
lights
l1
l2
tire size
t1
t2
radio
r1
r2
abs
a1
a2
alarm
a1
a2
seat
s1
s2
bags
b1
b2
java file-io tree
1个回答
0
投票

我不确定这是否会如您所期望的那样:

      if (curLine == "model line")
      {
          curModelLine = new Node(fileData.get(nextDataLine+1), this.root);
          this.root.children.add(curModelLine);    
      }

通过这个

if
语句,您可以检查参考值;不是字符串值。要比较字符串,您应该使用
equals()
运算符。它应该看起来更像这样:

    if (curLine.equals("model line"))
    {
        curModelLine = new Node(fileData.get(nextDataLine+1), this.root);
        this.root.children.add(curModelLine);    
    }

你的方法

对于我来说,我会有一个嵌套循环。结构大概是这样的:

while(not at the end of the file) {
    aLine := the next line.
    while(!aLine.equals(the end of a branch delimiter) {
        print out the next item.
        print out a "->"
    }
    print out a new line character.
}

注意这是伪代码。

© www.soinside.com 2019 - 2024. All rights reserved.