Note that there are some explanatory texts on larger screens.

plurals
  1. POSlow building list of paths
    text
    copied!<p>I'm building a list of hashes that represent root to node paths in a tree. My functions work but they are incredibly slow over large tree structures - is there a better way? I've tried building the list in one function but I get unique hashes where I don't want them.</p> <pre><code>public ArrayList&lt;Integer&gt; makePathList(AbstractTree&lt;String&gt; tree){ StringBuilder buffer = new StringBuilder(); ArrayList&lt;Integer&gt; pl = new ArrayList&lt;Integer&gt;(); ArrayList&lt;StringBuilder&gt; paths = getPaths(tree, buffer); for(StringBuilder sb : paths){ pl.add(sb.toString().hashCode()); } return pl; } public ArrayList&lt;StringBuilder&gt; getPaths(AbstractTree&lt;String&gt; tree, StringBuilder parent){ ArrayList&lt;StringBuilder&gt; list = new ArrayList&lt;StringBuilder&gt;(); parent.append("/"); parent.append(tree.getNodeName()); list.add(new StringBuilder(parent)); if (!tree.isLeaf()){ int i = 0; Iterator&lt;AbstractTree&lt;String&gt;&gt; child = tree.getChildren().iterator(); while (i &lt; tree.getChildren().size()){ list.addAll(getPaths(child.next(), new StringBuilder(parent))); i++; } } return list; } </code></pre> <p>UPDATE:</p> <p>Marcin's suggestion to make the hash during tree traversal gives the wrong answer, but perhaps that is the way I have done it?</p> <pre><code>public ArrayList&lt;Integer&gt; getPaths(AbstractTree&lt;String&gt; tree, StringBuilder parent){ ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(); parent.append("/"); parent.append(tree.getNodeName()); list.add(new StringBuilder(parent).toString().hashCode()); if (!tree.isLeaf()){ int i = 0; Iterator&lt;AbstractTree&lt;String&gt;&gt; child = tree.getChildren().iterator(); while (i &lt; tree.getChildren().size()){ list.addAll(getPaths(child.next(), new StringBuilder(parent))); i++; } } return list; } </code></pre>
 

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