خرید بک لینک

Vote count: 0

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.

example txt file:

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.

EXPECTED:

20160101

12:00

Here

There

20160102

13:00

Here

There

20160103

12:00

Here

13:00

Here

There

ACTUAL:

20160101

13:00

There

20160102

13:00

20160103

13:00

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(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();
        Scaer scanFile = new Scaer(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();

            dateNode = new Tree(line.split(",")[0], parentNode);
            timeNode = new Tree(line.split(",")[1], dateNode);
            locationNode = new Tree(line.split(",")[2], timeNode);

            timeNode.addChild(locationNode);
            dateNode.addChild(timeNode);
            parentNode.addChild(dateNode);
        }
        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");
    }
}

asked 18 secs ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 26 ارديبهشت 1395 ساعت: 14:48

صفحه بندی