Java将txt文件读入树数据结构

时间:2016-05-15 07:35:07

标签: java

我需要帮助实现树结构,我创建了add子函数,它将数据添加到树中,但是在添加子项时似乎存在问题。

我希望树看起来像什么:

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

示例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

日期的输出似乎很好,它显示了2个日期,因为我不希望同一日期出现两次,但时间和地点是错误的。

预期:

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

我感谢您对我的代码提供任何帮助或反馈。

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");
    }
}

1 个答案:

答案 0 :(得分:-1)

试试这些:

v=10; 
teta=20; % angle of the projectile motion
vx=v*cos(teta); % velocity in x axis
vy=v*sin(teta); % velocity in y axis
x=0:20;
y=zeros(size(x));

y=vy.*(x./vx)-(0.5*9.81*(x./vx).^2); % here I calculate the height of the ball in y axis
plot(x,y)
set(gca,'ylim',[0,5])