Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It can be done: you just have to write some code to actually iterate through the children in the way you want (as opposed to relying on <code>for</code>).</p> <p>More explicitly, you'll have to write code to iterate through a list of children and check if the "depth" crossed your threshold. Here's some <code>Haskell</code> code (I'm really sorry, I'm not fluent in Scala, but this can probably be easily transliterated):</p> <p><a href="http://ideone.com/O5gvhM" rel="noreferrer">http://ideone.com/O5gvhM</a></p> <p>In this code, I've basically replaced the <code>for</code> loop for an explicit recursive version. This allows me to stop the recursion if the number of visited nodes is already too deep (i.e., <code>limit</code> is not positive). When I recurse to examine the next child, I subtract the number of nodes the <code>dfs</code> of the previous child visited and set this as the limit for the next child.</p> <p>Functional languages are fun, but they're a huge leap from imperative programming. It really makes you pay attention to the concept of <em>state</em>, because all of it is excruciatingly explicit in the arguments when you go functional.</p> <p><strong>EDIT:</strong> Explaining this a bit more.</p> <p>I ended up converting from "print just the leaf nodes" (which was the original algorithm from the OP) to "print all nodes". This enabled me to have access to the number of nodes the subcall visited through the length of the resulting list. If you want to stick to the leaf nodes, you'll have to carry around how many nodes you have already visited:</p> <p><a href="http://ideone.com/cIQrna" rel="noreferrer">http://ideone.com/cIQrna</a></p> <p><strong>EDIT again</strong> To clear up this answer, I'm putting all the Haskell code on ideone, and I've transliterated my Haskell code to Scala, so this can stay here as the definite answer to the question:</p> <pre class="lang-scala prettyprint-override"><code>case class Node[T](label:T, children:Seq[Node[T]]) case class TraversalResult[T](num_visited:Int, labels:Seq[T]) def dfs[T](node:Node[T], limit:Int):TraversalResult[T] = limit match { case 0 =&gt; TraversalResult(0, Nil) case limit =&gt; node.children match { case Nil =&gt; TraversalResult(1, List(node.label)) case children =&gt; { val result = traverse(node.children, limit - 1) TraversalResult(result.num_visited + 1, result.labels) } } } def traverse[T](children:Seq[Node[T]], limit:Int):TraversalResult[T] = limit match { case 0 =&gt; TraversalResult(0, Nil) case limit =&gt; children match { case Nil =&gt; TraversalResult(0, Nil) case first :: rest =&gt; { val trav_first = dfs(first, limit) val trav_rest = traverse(rest, limit - trav_first.num_visited) TraversalResult( trav_first.num_visited + trav_rest.num_visited, trav_first.labels ++ trav_rest.labels ) } } } val n = Node(0, List( Node(1, List(Node(2, Nil), Node(3, Nil))), Node(4, List(Node(5, List(Node(6, Nil))))), Node(7, Nil) )) for (i &lt;- 1 to 8) println(dfs(n, i)) </code></pre> <p>Output:</p> <pre><code>TraversalResult(1,List()) TraversalResult(2,List()) TraversalResult(3,List(2)) TraversalResult(4,List(2, 3)) TraversalResult(5,List(2, 3)) TraversalResult(6,List(2, 3)) TraversalResult(7,List(2, 3, 6)) TraversalResult(8,List(2, 3, 6, 7)) </code></pre> <p>P.S. this is my first attempt at Scala, so the above probably contains some horrid non-idiomatic code. I'm sorry.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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