十万火急,人家给我出的面试题,求高手 a +---b +----ca +---ca +---b +----d==排序结果==>a +---b +----c +----d +---c 将上面的转换成下面的树装结构,上面的是没有排好序,现在要排序出来,怎么做 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个和展示文件目录类似,你把a当成根目录,找它的child.可以参考一下import java.io.*;public class ListFiles{ public static void main(String[] args){ File f = new File("a"); System.out.println(f.getName()); tree(f,0); } private static void tree(File f,int level){ String preStr = ""; for(int i=0;i<=level;i++){ preStr += " +---"; } File[] childs = f.listFiles(); for(int i=0;i<childs.length;i++){ System.out.println(preStr + childs[i].getName()); if(childs[i].isDirectory()){ tree(childs[i],level + 1); } } }} public class Hierarchy { static class Node { private String name; private List<Node> nodeList; public Node(String name) { this.name = name; nodeList = new ArrayList<Node>(); } public Node getSubNode(String name) { for (Node subNode : nodeList) { if (subNode.getName().equals(name)) return subNode; } return null; } public String getName() { return name; } public void addSubNode(Node subNode) { nodeList.add(subNode); } public void print(){ printNode(this, "+"); } private void printNode(Node node, String prefix){ System.out.println(prefix + node.getName()); prefix += "---"; for(Node subNode : node.nodeList){ printNode(subNode, prefix); } } } private Node root; public Hierarchy() { } public void addNode(String path) { String[] nodeNames = path.split("\r\n"); Node currentNode = null; for(String nodeName : nodeNames){ currentNode = addNodeIfNotExist(nodeName, currentNode); } } public void print(){ if(root != null) root.print(); else System.out.println("empty"); } public Node addNodeIfNotExist(String name, Node parent){ if(parent == null){ if(root == null){ root = new Node(name); return root; }else if(root.getName().equals(name)) return root; else throw new IllegalArgumentException("输入了一个不同的根节点"); }else{ Node targetNode = parent.getSubNode(name); if(targetNode == null){ targetNode = new Node(name); parent.addSubNode(targetNode); } return targetNode; } } public static void main(String[] args) { String input = "a\r\n +---b\r\n +----c\r\na\r\n +---c\r\na\r\n +---b\r\n +----d"; String[] paths = input.split("(?m)^(?=\\S)"); Hierarchy hierarchy = new Hierarchy(); for(String path : paths){ if(path.length() > 0) hierarchy.addNode(path.replaceAll("([^a-z\r\n]*)", "")); } hierarchy.print(); }}试试这个合你要求不,估计有bug,没怎么检查 http://bbs.csdn.net/topics/390313307跟这个类似,差不多的思路 static class Node { private String name; private TreeSet<Node> nodeList;}如果要使用集合装子节点的话,可以直接使用TreeSet在放入的时候自动排好序 用Map,Map<String,Map<String,Map<String>>> put前看看是否有值 一个奇怪的问题 英文文章如何提取Tag?可以大概讲一下算法思想吗? 怎样改变鼠标为自定义图片 显示图像文件问题 蔡鸟问题!如何将sql的timedate传入的java的String中? 怎样求String s="(1+1)*2"的数值 急 如何用给定的密码进行加密啊?请大虾们给点指导! 听说现在java程序员工资最高,是真的吗? 高分求救????急急急! JDK1.3 下命令行“java -cp ... ”超过256个字符怎么办? Java JFrame架构中RootPane--GlassPane的应用 在做磁盘文件搜索的时候出现了空指针!
import java.io.*;
public class ListFiles{
public static void main(String[] args){
File f = new File("a");
System.out.println(f.getName());
tree(f,0);
}
private static void tree(File f,int level){
String preStr = "";
for(int i=0;i<=level;i++){
preStr += " +---";
}
File[] childs = f.listFiles();
for(int i=0;i<childs.length;i++){
System.out.println(preStr + childs[i].getName());
if(childs[i].isDirectory()){
tree(childs[i],level + 1);
}
}
}
}
public class Hierarchy {
static class Node {
private String name;
private List<Node> nodeList; public Node(String name) {
this.name = name;
nodeList = new ArrayList<Node>();
} public Node getSubNode(String name) {
for (Node subNode : nodeList) {
if (subNode.getName().equals(name))
return subNode;
}
return null;
} public String getName() {
return name;
} public void addSubNode(Node subNode) {
nodeList.add(subNode);
}
public void print(){
printNode(this, "+");
}
private void printNode(Node node, String prefix){
System.out.println(prefix + node.getName());
prefix += "---";
for(Node subNode : node.nodeList){
printNode(subNode, prefix);
}
}
}
private Node root; public Hierarchy() {
} public void addNode(String path) {
String[] nodeNames = path.split("\r\n");
Node currentNode = null;
for(String nodeName : nodeNames){
currentNode = addNodeIfNotExist(nodeName, currentNode);
}
}
public void print(){
if(root != null)
root.print();
else
System.out.println("empty");
}
public Node addNodeIfNotExist(String name, Node parent){
if(parent == null){
if(root == null){
root = new Node(name);
return root;
}else if(root.getName().equals(name))
return root;
else
throw new IllegalArgumentException("输入了一个不同的根节点");
}else{
Node targetNode = parent.getSubNode(name);
if(targetNode == null){
targetNode = new Node(name);
parent.addSubNode(targetNode);
}
return targetNode;
}
} public static void main(String[] args) {
String input = "a\r\n +---b\r\n +----c\r\na\r\n +---c\r\na\r\n +---b\r\n +----d";
String[] paths = input.split("(?m)^(?=\\S)");
Hierarchy hierarchy = new Hierarchy();
for(String path : paths){
if(path.length() > 0)
hierarchy.addNode(path.replaceAll("([^a-z\r\n]*)", ""));
}
hierarchy.print();
}}
试试这个合你要求不,估计有bug,没怎么检查
跟这个类似,差不多的思路
private String name;
private TreeSet<Node> nodeList;
}如果要使用集合装子节点的话,可以直接使用TreeSet在放入的时候自动排好序