/**
* 遍历获取链路路径,使用深度优先
*/
public class IteratorPathUtil {
Map<String, List> pathMap=new HashMap();//记录全部从根节点到叶子结点的路径
public Map<String, List> getPathMap()//返回路径map
{
return pathMap;
}
public void iteratorNode(PathVo n, Stack<PathVo> pathstack)
{
pathstack.push(n);//入栈
List<PathVo> childlist=n.getPathVoList();
if(childlist==null)//没有孩子 说明是叶子结点
{
List lst=new ArrayList();
Iterator<PathVo> stackIt=pathstack.iterator();
while(stackIt.hasNext())
{
lst.add(stackIt.next().getZDeviceId());
}
pathMap.put(n.getZDeviceId(), lst);//保存路径信息
}
else
{
Iterator it=childlist.iterator();
while(it.hasNext())
{
PathVo child=(PathVo)it.next();
iteratorNode(child,pathstack);//深度优先 进入递归
pathstack.pop();//回溯时候出栈
}
}
}
}
网友评论