我自己的解决方案:
public class MyTree
{
static Set<String> fileDataList = new HashSet<String>();
static
{
fileDataList.add("A,B");
fileDataList.add("A,B");
fileDataList.add("A,H");
fileDataList.add("B,C");
fileDataList.add("B,D");
fileDataList.add("D,E");
fileDataList.add("E,F");
fileDataList.add("F,G");
fileDataList.add("G,C");
fileDataList.add("H,I");
fileDataList.add("H,J");
fileDataList.add("J,K");
fileDataList.add("K,L");
fileDataList.add("L,M");
fileDataList.add("M,K");
fileDataList.add("L,N");
fileDataList.add("N,O");
fileDataList.add("N,Q");
fileDataList.add("O,P");
fileDataList.add("P,A");
}
static Map<String, Set<String>> dataMap =new HashMap<String, Set<String>>();
static Map<String, Set<Node>> nodeMap =new HashMap<String, Set<Node>>();
public static void main(String args[]) throws IOException
{
//String fileName= "data.csv";
//fileDataList = JSSTest.getFileData(fileName);
//String lineageFor="100";
String lineageFor="A";
//Build map
for(String kv : fileDataList)
{
String arr[] = kv.split(",");
String p = arr[0];
String c = arr[1];
if(dataMap.containsKey(p))
{
dataMap.get(p).add(c);
}
else
{
Set<String> list = new HashSet<String>();
list.add(c);
dataMap.put(p, list);
}
}
//Get the lineage for Application
Node lineage = getLineage(lineageFor);
print(lineage, "");
}
private static void print(Node node, String space)
{
System.out.println(space + node.getName());
if(node.getInNode() != null && node.getInNode().size() > 0)
{
for(Node child:node.getInNode())
{
if(child.getRecursive() == null)
{
print(child, space+".");
}
else
{
System.out.println(space + child.getName());
System.out.println(space + "."+ child.getRecursive().getName());
}
}
}
}
/**
* Get Lineage
* @param appName
* @return
*/
private static Node getLineage(String appName)
{
Node node = new Node(appName);
Set<String> allInNodes = new HashSet<String>();
setInnerNodes(node, dataMap.get(appName), allInNodes);
return node;
}
private static void setInnerNodes(Node node, Set<String> childrenList, Set<String> allInNodes)
{
if(childrenList == null) return;
for(String child:childrenList)
{
Node containedNode = nodeContains(node, child);
if(containedNode != null)
{
node.setRecursive(new Node(child));
continue;
}
Node childNode = new Node(child);
node.addNode(childNode);
if(dataMap.containsKey(child))
{
setInnerNodes(childNode, dataMap.get(child), allInNodes);
}
}
}
static int nodeCounter=1;
private static Node nodeContains(Node node, String child)
{
if(node.getName().equals(child)) return node;
if(node.getParent() != null)
{
if(node.getParent().getName().equals(child)) return node.getParent();
if(node.getParent().getParent() != null && nodeContains(node.getParent().getParent(), child) != null)
{
return node.getParent();
}
}
return null;
}
}
class Node
{
private String name;
private Node parent;
private Node recursive;
public Node getParent()
{
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
private Set<Node> inNode = new LinkedHashSet<Node>();
public Node(String name)
{
this.name=name;
}
public void addNode(Node child)
{
child.parent=this;
this.inNode.add(child);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Node> getInNode() {
return inNode;
}
public void setInNode(Set<Node> inNode) {
this.inNode = inNode;
}
public Node getRecursive() {
return recursive;
}
public void setRecursive(Node recursive) {
this.recursive = recursive;
}
}