Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to improve runtime of a recursive function
    primarykey
    data
    text
    <p>In solving this problem: <a href="http://www.cs.duke.edu/csed/newapt/drawtree.html" rel="nofollow">http://www.cs.duke.edu/csed/newapt/drawtree.html</a> I wrote the code below but it seems to run too slow. Is there a faster way of checking all of the child nodes w/o using a FOR loop? Would a queue be helpful? </p> <pre><code>public class DrawTree { HashMap&lt;String, ArrayList&lt;String&gt;&gt; map = new HashMap&lt;String, ArrayList&lt;String&gt;&gt;(); ArrayList&lt;String&gt; drawing = new ArrayList&lt;String&gt;(); String root; public String[] draw(int[] parents, String[] names) { for(int x=0; x&lt;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&lt;String&gt;()); } continue; } //add parent, child to map if (!map.containsKey(names[parentindex])) map.put(names[parentindex], new ArrayList&lt;String&gt;()); 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&gt;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 &amp; parent has pipe // (is a child of a subtree) if(parentPipe&gt;0) { //number of spaces to add to prefix int spaces = parent.indexOf('-')+1; //add spaces to prefix while(spaces&gt;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 &amp;&amp; parentPipe&lt;0) { int spaces = parent.indexOf('-')+1; while(spaces&gt;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&lt;map.get(child).size(); x++) { boolean pipe = false; if(x&lt;(map.get(child).size()-1)) pipe=true; //System.out.println(map.get(child).get(x)); sketch(node, map.get(child).get(x), pipe); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload