简体   繁体   English

Java将txt文件读取到树数据结构中

[英]Java reading txt file into tree data structure

I need help in implementing tree structure, I created the add child function that adds data to tree but there seems to be a problem while adding the child. 我在实现树形结构方面需要帮助,我创建了add child函数,该函数将数据添加到tree中,但是在添加child时似乎存在问题。

What I want the tree to look like: 我希望树看起来像什么:

                 Date
            /      |      \
           /       |        \
         /         |         \
       /           |           \
  20160101     20160102     20160103
    /               |       |        \
 12:00           13:00    12:00      13:00
 /    \           /   \     |       /    \
Here  There     Here There Here Here     There

example txt file: txt文件示例:

Date,Time,Location
20160101,12:00,Here
20160101,12:00,There
20160102,13:00,Here
20160102,13:00,There
20160103,12:00, Here
20160103,13:00, Here
20160103,13:00, There

The output for the date seems fine, it shows 2 dates since I don't want the same date to show up twice but the time and location is wrong. 该日期的输出似乎很好,它显示2个日期,因为我不希望同一日期出现两次,但是时间和位置错误。

EXPECTED: 预期:

20160101
12:00
Here
There
20160102
13:00
Here
There
20160103
12:00
Here
13:00
Here
There

ACTUAL: 实际:

20160101
12:00
Here
There
13:00
Here
There
20160102
12:00
Here
There
13:00
Here
There
20160103
12:00
Here
There
13:00
Here
There

I appreciate any help or feedback for my code. 感谢您对我的代码的任何帮助或反馈。

public class Tree {
    List<Tree> children = new ArrayList<Tree>();
    Tree parent = null;
    String data = null;

    public Tree(String data) {
        this.data = data;
    }

    public Tree(String data, Tree parent){
        this.data = data;
        this.parent = parent;
    }

    public void addChild(String data) {
        Tree child = new Tree(data);
        child.parent = this;
        Boolean match = false;
        for (int i = 0; i < this.children.size(); i++) {
            if (this.children.get(i).data.equals(child.data)) {
                match = true;
                break;
            }
        }
        if (!match) {
            this.children.add(child);
        }
    }

    public void addChild(Tree child) {
        Boolean match = false;
        for (int i = 0; i < this.children.size(); i++) {
            if (this.children.get(i).data.equals(child.data)) {
                match = true;
                break;
            }
        }
        if (!match) {
            this.children.add(child);
        }
    }

    public static void main(String[] args) throws IOException {
        long startTime = System.nanoTime();
        Scanner scanFile = new Scanner(new File("example.txt"));
        String line = "";
        line = scanFile.nextLine();
        Tree parentNode = new Tree(line.split(",")[0]);
        Tree dateNode = new Tree(null, parentNode);
        Tree timeNode = new Tree(null, dateNode);
        Tree locationNode = new Tree(null, timeNode);
        System.out.println(parentNode.data);

        while(scanFile.hasNext()) {
            line = scanFile.nextLine();

            timeNode.addChild(line.split(",")[2]);
            dateNode.addChild(line.split(",")[1]);
            parentNode.addChild(line.split(",")[0]);
        }
        scanFile.close();



        for(int i =0; i < parentNode.children.size(); i++) {
            System.out.println(parentNode.children.get(i).data);
            for(int j = 0; j < dateNode.children.size(); j++) {
                System.out.println(dateNode.children.get(j).data);
                for(int k = 0; k < timeNode.children.size(); k++) {
                    System.out.println(timeNode.children.get(k).data);
            }
    }

    long endTime = System.nanoTime();
    System.out.println("Time taken: " + (endTime - startTime) / 1E9 + "s");
    }
}

Try these: 试试这些:

List<String> lines = new ArrayList<>();
Map<String,Map<String,List<String>>> tree = new TreeMap<String,Map<String,List<String>>>();

    for(int i=0;i<lines.size();i++){

        String[] parts = lines.get(i).split(",");

        if(!tree.containsKey(parts[0])){
            tree.put(parts[0],new TreeMap<String,List<String>>());
        }

        Map<String,List<String>> tree2 = tree.get(parts[0]);

        if(!tree.containsKey(parts[1])){
            tree2.put(parts[1],new ArrayList<String>());
        }

        List<String> tree3 = tree2.get(parts[1]);

        if(!tree3.contains(parts[2])){
            tree3.add(parts[2]);
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM