Java 如何提高递归函数的运行时间
在解决这个问题时:我写了下面的代码,但它似乎运行得太慢了。是否有一种更快的方法可以使用FOR循环检查所有子节点w/o?排队会有帮助吗Java 如何提高递归函数的运行时间,java,recursion,runtime,Java,Recursion,Runtime,在解决这个问题时:我写了下面的代码,但它似乎运行得太慢了。是否有一种更快的方法可以使用FOR循环检查所有子节点w/o?排队会有帮助吗 public class DrawTree { HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); ArrayList<String> drawing
public class DrawTree {
HashMap<String, ArrayList<String>> map =
new HashMap<String, ArrayList<String>>();
ArrayList<String> drawing = new ArrayList<String>();
String root;
public String[] draw(int[] parents, String[] names) {
for(int x=0; x<parents.length; x++)
{
int parentindex = parents[x];
//root name
if(parentindex==-1)
{
root=names[x];
if(!map.containsKey(names[x]))
{
map.put(names[x], new ArrayList<String>());
}
continue;
}
//add parent, child to map
if (!map.containsKey(names[parentindex]))
map.put(names[parentindex],
new ArrayList<String>());
map.get(names[parentindex]).add(names[x]);
}
sketch("",root,false);
return drawing.toArray(new String[drawing.size()]);
}
//***IMPROVE RUN TIME - different algorithm??***
//method takes root and prefix?
public void sketch(String parent, String child, boolean addPipe){
StringBuilder toAdd = new StringBuilder();
//don't need to add connector pipe
if(!addPipe)
{
//number of spaces to add to prefix
int spaces = parent.indexOf('-')+1;
//add spaces to prefix
while(spaces>0)
{
toAdd.append(" ");
spaces--;
}
toAdd.append("+-"+child);
}
//index of pipe in parent, -1 if parent doesn't have pipe
int parentPipe = parent.indexOf('|');
//need to add connector pipe & parent has pipe
// (is a child of a subtree)
if(parentPipe>0)
{
//number of spaces to add to prefix
int spaces = parent.indexOf('-')+1;
//add spaces to prefix
while(spaces>0)
{
if(spaces==parentPipe) toAdd.append('|');
else toAdd.append(" ");
spaces--;
}
toAdd.append("+-"+child);
}
//need to add pipe and parent doesn't have pipe
if(addPipe && parentPipe<0)
{
int spaces = parent.indexOf('-')+1;
while(spaces>0)
{
if(spaces==2) toAdd.append('|');
else toAdd.append(" ");
}
toAdd.append("+-"+child);
}
//add child to list of tree drawing
String node = toAdd.toString();
drawing.add(node);
//System.out.println(node);
//loop through list of children, passing each recursively
//...count level?
if(map.containsKey(child))
{
//System.out.println("map works");
for(int x = 0; x<map.get(child).size(); x++)
{
boolean pipe = false;
if(x<(map.get(child).size()-1)) pipe=true;
//System.out.println(map.get(child).get(x));
sketch(node, map.get(child).get(x), pipe);
}
}
}
公共类绘图树{
HashMap映射=
新的HashMap();
ArrayList绘图=新建ArrayList();
弦根;
公共字符串[]绘制(int[]父字符串,字符串[]名称){
对于(int x=0;x0)
{
//要添加到前缀的空格数
int spaces=parent.indexOf('-')+1;
//在前缀中添加空格
while(空格>0)
{
if(spaces==parentPipe)添加到append(“|”);
否则添加。附加(“”);
空间--;
}
toAdd.追加(“+-”+子项);
}
//需要添加管道,而父级没有管道
if(addPipe&&parentPipe0)
{
如果(空格==2)添加到追加(“|”);
否则添加。附加(“”);
}
toAdd.追加(“+-”+子项);
}
//将子对象添加到树图形列表中
字符串节点=toAdd.toString();
添加(节点);
//System.out.println(节点);
//循环遍历子项列表,递归地传递每个子项
//…计数级别?
if(地图容器(子))
{
//System.out.println(“地图作品”);
for(intx=0;x我认为队列在这种情况下不会有帮助
public class DrawTree {
public String[] lines;
public int lineNum;
public String[] draw(int[] parents, String[] names) {
lines = new String[parents.length];
lineNum = 0;
drawLeaf(parents, names, -1, 0);
return lines;
}
public void drawLeaf(int[] parents, String[] names, int root, int depth) {
for (int i = 0; i < parents.length; i++) {
if (parents[i] == root) {
lines[lineNum] = "";
for (int j = 0; j < depth; j++) {
lines[lineNum] += " ";
}
lines[lineNum] += "+-" + names[i];
int pipeNum = lineNum - 1;
while ((pipeNum >= 0)
&& (lines[pipeNum].charAt(depth * 2) == ' ')) {
String oldLeaf = lines[pipeNum];
lines[pipeNum] = "";
for (int j = 0; j < oldLeaf.length(); j++) {
if (j == depth * 2) {
lines[pipeNum] += '|';
} else {
lines[pipeNum] += oldLeaf.charAt(j);
}
}
pipeNum--;
}
lineNum++;
drawLeaf(parents, names, i, 1 + depth);
}
}
}
}
公共类绘图树{
公共字符串[]行;
公共int-lineNum;
公共字符串[]绘制(int[]父字符串,字符串[]名称){
lines=新字符串[parents.length];
lineNum=0;
drawLeaf(父项,名称,-1,0);
回流线;
}
public void drawLeaf(int[]父项、字符串[]名称、int根、int深度){
for(int i=0;i=0)
&&(线条[pipeNum]。字符(深度*2)=''){
字符串oldLeaf=行[pipeNum];
行[pipeNum]=“”;
对于(int j=0;j
这个问题更适合于。定义“慢”的基础是什么?你有什么特别的时间安排吗?一个改进是map.containsKey(key)。它的时间几乎与map.get(key)相同。调用containsKey并逐个get没有意义。最好使用List List=map.get(key);if(List==null){list=newarraylist();map.put(key,list);}list.add(something);