Build tree from input file on java

1k views Asked by At

I stuck at the following problem: I have these input file, and I have to restore the tree schema from it. I'm new in java, I had read a lot and I have seriously difficulties with this task. Please give me some ideas to help. Thanks in advance!

And now the following file: http://universumerp.com/sites/default/files/treedata.txt

And once again THANK YOU!

1

There are 1 answers

0
Vadim Svv On

From the file I can say that data represent comma separated values id,parantId,value

From the first look I thought that third value is a flag 'hasChild' but it is not. So I decide count on id and parentId.

On my opinion you need to do the next steps

  1. Parse the file and get all Hierarchy nodes
  2. Create map parent to child
  3. Print tree

Finally

import java.io.*;
import java.util.*;

public class ShowHierarchy {

public static final void main(String[] args) {
    ShowHierarchy hierarchy = new ShowHierarchy();

    //1)Parse the file and get all Hierarchy nodes
    //  they all added to HashMap<Integer, HierarchyItem> allItems
    hierarchy.parseFile("C://tree.txt");

    //2) Create map parent to children HashMap<Integer, ArrayList<Integer>> parentToChildMap
    hierarchy.mapParentChild();

    //3) Print hierarchy
    hierarchy.printHierarchy();
}

HashMap<Integer, HierarchyItem> allItems = new HashMap<Integer, HierarchyItem>();
HashMap<Integer, ArrayList<Integer>> parentToChildMap = new HashMap<Integer, ArrayList<Integer>>();
ArrayList<Integer> rootElements = new ArrayList<Integer>();


public ShowHierarchy() {
}

public void parseFile(String filePath){
    File hierarchyFile = new File(filePath);
    allItems.clear();
    //check is file exist
    if (hierarchyFile.exists() && hierarchyFile.isFile()) {
        //parse file and retrive Hierarchy items
        allItems = getHierarchyItemsFromFile(hierarchyFile);
    } else {
        System.out.println("Cannot find the file \"" + filePath + "\"");
    }

}

private void mapParentChild() {
    parentToChildMap.clear();
    rootElements.clear();

    //map children to parents
    for (Integer id : allItems.keySet()) {
        HierarchyItem hierarchyItem = allItems.get(id);
        if(!allItems.containsKey(hierarchyItem.getParentId())){
            rootElements.add(hierarchyItem.getId());
        }
        //add item to array as leaf
        if(!parentToChildMap.containsKey(hierarchyItem.getParentId())){
            ArrayList<Integer> childs = new ArrayList<Integer>();
            childs.add(hierarchyItem.getId());
            parentToChildMap.put(hierarchyItem.getParentId(),childs);
        } else {
            parentToChildMap.get(hierarchyItem.getParentId()).add(hierarchyItem.getId());
        }
    }

}

private HashMap<Integer, HierarchyItem> getHierarchyItemsFromFile(File hierarchyFile) {
    HashMap<Integer, HierarchyItem> hierarchyItems = new HashMap<Integer, HierarchyItem>();
    try {
        BufferedReader br = new BufferedReader(new FileReader(hierarchyFile));
        String line;
        while ((line = br.readLine()) != null) {
            String[] values = line.split(",");
            Integer id = Integer.valueOf(values[0]);
            Integer parentId = Integer.valueOf(values[1]);
            Integer value = Integer.valueOf(values[2]);
            hierarchyItems.put(id, new HierarchyItem(id, parentId, value));
            if (hierarchyItems.size() > 1000) {
                break;
            }
        }
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return hierarchyItems;
}

public void printHierarchy() {
    for (Integer rootItem : rootElements) {
        printTreeForThisElement(rootItem,1);
    }
}

private void printTreeForThisElement(Integer item,int level) {
    HierarchyItem hierarchyItem = allItems.get(item);
    hierarchyItem.setLevel(level);
    System.out.println(getPrintLine(hierarchyItem));
    if (parentToChildMap.containsKey(item)) {
        for (Integer child : parentToChildMap.get(item)) {
            printTreeForThisElement(child,level+1);
        }
    }

}

private String getPrintLine(HierarchyItem item) {
    String text = "";
    for (int i = 0; i < item.getLevel(); i++) {
        text += " ";
    }

    text += "L" + item.getLevel() + " id=" + item.getId() + " pId=" + item.getParentId() + " value=" + item.getValue();

    return text;
}


private class HierarchyItem {
    private Integer id;
    private Integer parentId;
    private Integer value;
    private Integer level;

    private HierarchyItem(Integer id, Integer parentId, Integer value) {
        this.id = id;
        this.parentId = parentId;
        this.value = value;
    }

    public Integer getId() {
        return id;
    }

    public Integer getParentId() {
        return parentId;
    }

    public Integer getLevel() {
        return level;
    }

    public Integer getValue() {
        return value;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

}
}

It is could be done better but this is all depends on your needs.