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

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

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
stack overflow Building a tree in java using input from a text file
原文答案

答案:

作者头像

I'm not sure if this will behave as you expect it to:

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

With this if statement, you're checking the reference values; not the string values. To compare strings, you should be using the equals() operator. It should look more like this:

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

Your approach

For me, I would have a nested loop. The structure would be something like this:

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.
}

Notice this is in pseudocode.