Build tree from input file on java -
Build tree from input file on java -
i stuck @ next problem: have these input file, , have restore tree schema it. i'm new in java, had read lot , have difficulties task. please give me ideas help. in advance!
and next file: http://universumerp.com/sites/default/files/treedata.txt
and 1 time 1 time again give thanks you!
from file can info represent comma separated values id,parantid,value
from first thought 3rd value flag 'haschild' not. decide count on id , parentid.
on sentiment need next steps
parse file , hierarchy nodes create map parent kid print treefinally
import java.io.*; import java.util.*; public class showhierarchy { public static final void main(string[] args) { showhierarchy hierarchy = new showhierarchy(); //1)parse file , hierarchy nodes // added hashmap<integer, hierarchyitem> allitems hierarchy.parsefile("c://tree.txt"); //2) create map parent 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 file exist if (hierarchyfile.exists() && hierarchyfile.isfile()) { //parse file , retrive hierarchy items allitems = gethierarchyitemsfromfile(hierarchyfile); } else { system.out.println("cannot find file \"" + filepath + "\""); } } private void mapparentchild() { parenttochildmap.clear(); rootelements.clear(); //map children parents (integer id : allitems.keyset()) { hierarchyitem hierarchyitem = allitems.get(id); if(!allitems.containskey(hierarchyitem.getparentid())){ rootelements.add(hierarchyitem.getid()); } //add item array 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>(); seek { 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(); } grab (filenotfoundexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } homecoming hierarchyitems; } public void printhierarchy() { (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)) { (integer kid : parenttochildmap.get(item)) { printtreeforthiselement(child,level+1); } } } private string getprintline(hierarchyitem item) { string text = ""; (int = 0; < item.getlevel(); i++) { text += " "; } text += "l" + item.getlevel() + " id=" + item.getid() + " pid=" + item.getparentid() + " value=" + item.getvalue(); homecoming 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() { homecoming id; } public integer getparentid() { homecoming parentid; } public integer getlevel() { homecoming level; } public integer getvalue() { homecoming value; } public void setlevel(integer level) { this.level = level; } } }
it done improve depends on needs.
java file tree treenode
Comments
Post a Comment