在java上从输入文件构建树
我遇到了以下问题: 我有这些输入文件,我必须从中恢复树模式。我是java新手,我读了很多书,在这项任务中遇到了严重的困难。请给我一些想法来帮助我。 提前谢谢 现在是以下文件:在java上从输入文件构建树,java,file,tree,treenode,Java,File,Tree,Treenode,我遇到了以下问题: 我有这些输入文件,我必须从中恢复树模式。我是java新手,我读了很多书,在这项任务中遇到了严重的困难。请给我一些想法来帮助我。 提前谢谢 现在是以下文件: 再次感谢你们 从该文件中,我可以说数据表示逗号分隔的值id、parantId、value 从第一眼看,我认为第三个值是一个标志“hasChild”,但事实并非如此。 所以我决定依靠id和parentId 依我看,你需要做下一步 解析文件并获取所有层次结构节点 创建映射父对象到子对象 打印树 最后 import java.i
再次感谢你们 从该文件中,我可以说数据表示逗号分隔的值id、parantId、value 从第一眼看,我认为第三个值是一个标志“hasChild”,但事实并非如此。 所以我决定依靠id和parentId 依我看,你需要做下一步
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;
}
}
}
import java.io.*;
导入java.util.*;
公共类显示层次结构{
公共静态最终void main(字符串[]args){
ShowHierarchy=新的ShowHierarchy();
//1) 解析文件并获取所有层次结构节点
//它们都添加到HashMap Allitem中
parseFile(“C://tree.txt”);
//2) 创建映射父对象到子对象HashMap parentToChildMap
hierarchy.mapParentChild();
//3) 打印层次结构
printHierarchy();
}
HashMap allItems=新的HashMap();
HashMap parentToChildMap=新HashMap();
ArrayList rootElements=新的ArrayList();
公共ShowHierarchy(){
}
公共无效解析文件(字符串文件路径){
文件层次结构文件=新文件(文件路径);
allItems.clear();
//检查文件是否存在
if(hierarchyFile.exists()&&hierarchyFile.isFile()){
//解析文件并检索层次结构项
allItems=getHierarchyItemsFromFile(hierarchyFile);
}否则{
System.out.println(“找不到文件\”“+文件路径+”\”);
}
}
私有void mapParentChild(){
parentToChildMap.clear();
rootElements.clear();
//将孩子映射到父母
对于(整数id:allItems.keySet()){
HierarchyItem HierarchyItem=allItems.get(id);
如果(!allItems.containsKey(hierarchyItem.getParentId())){
add(hierarchyItem.getId());
}
//将项作为叶添加到数组中
如果(!parentToChildMap.containsKey(hierarchyItem.getParentId())){
ArrayList childs=新的ArrayList();
add(hierarchyItem.getId());
parentToChildMap.put(hierarchyItem.getParentId(),childs);
}否则{
获取(hierarchyItem.getParentId()).add(hierarchyItem.getId());
}
}
}
私有HashMap getHierarchyItemsFromFile(文件hierarchyFile){
HashMap hierarchyItems=新HashMap();
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(hierarchyFile));
弦线;
而((line=br.readLine())!=null){
字符串[]值=行。拆分(“,”);
整数id=Integer.valueOf(值[0]);
整数parentId=Integer.valueOf(值[1]);
整数值=整数.valueOf(值[2]);
put(id,新的HierarchyItem(id,parentId,value));
if(hierarchyItems.size()>1000){
打破
}
}
br.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回层次项目;
}
public-void-printHierarchy(){
for(整数rootItem:rootElements){
PrintTreeFortheSiElement(根项,1);
}
}
私有void printTreeforthiseElement(整数项,整数级别){
HierarchyItem HierarchyItem=allItems.get(item);
hierarchyItem.setLevel(级别);
System.out.println(getPrintLine(hierarchyItem));
if(亲子地图包含(项目)){
for(整数子级:parentToChildMap.get(项)){
PrintTreeFortheSiElement(子级,级别+1);
}
}
}
私有字符串getPrintLine(层次结构项){
字符串文本=”;
对于(int i=0;i
可以做得更好,但这完全取决于您的需要。预期的输出是什么?您尝试过什么代码?请细化问题,以便其他人能够理解您必须解决的问题。文件的格式是什么?您的解决方案的预期输出是什么?一点也不清楚。。。