# Traversal of a tree + block code

**URL:** <https://www.ruby-forum.com/t/traversal-of-a-tree-block-code/59213>\
**Category:** Ruby\
**Created:** [May 1, 2006, 9:04pm UTC](https://www.ruby-forum.com/t/traversal-of-a-tree-block-code/59213 "2006-05-01T21:04:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![esquilax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@esquilax](https://www.ruby-forum.com/u/esquilax)\
**Post date:** [May 1, 2006, 9:04pm UTC](https://www.ruby-forum.com/t/traversal-of-a-tree-block-code/59213/1 "2006-05-01T21:04:49Z")

</div>

Hi:

I want to be able to traverse a tree and for each node perform some  
random code…sounds like an ideal situation for blocks, right? 🙂

My problem is that I’m not sure how to keep passing the code block in  
the recursive call. This is my best attempt so far… i’m not very  
strong in this area so any advice would be welcome! 🙂

def self.depthFirstTraversal(parent , &block)  
children = parent.children  
children.each do |child|  
block.call(child)  
self.depthFirstTraversal(child) do |node|  
block.call(node)  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![esquilax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@esquilax](https://www.ruby-forum.com/u/esquilax)\
**Post date:** [May 1, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/traversal-of-a-tree-block-code/59213/2 "2006-05-01T22:05:24Z")

</div>

Ahhh…found my solution:

def self.depthFirstTraversal(parent , &block)  
children = parent.children  
children.each do |child|  
block[child]  
self.depthFirstTraversal(child, &block)  
end  
end

I’m not familiar with the [] for passing the argument to the proc(?) …  
anyone have any comments?

---

<div class="post-metadata">

**Author:** ![esquilax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@esquilax](https://www.ruby-forum.com/u/esquilax)\
**Post date:** [May 3, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/traversal-of-a-tree-block-code/59213/3 "2006-05-03T18:58:16Z")

</div>

Andrew G. wrote:

> ```
> children = parent.children
> children.each do |child|
> block.call(child)
> 
> ```

Instead of…

> ```
> self.depthFirstTraversal(child) do |node|
> block.call(node)
> end
> 
> ```

try this…

```
    depthFirstTraversal(child, &block)
```

---

<div class="post-metadata">

**Author:** ![esquilax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@esquilax](https://www.ruby-forum.com/u/esquilax)\
**Post date:** [May 3, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/traversal-of-a-tree-block-code/59213/4 "2006-05-03T18:58:45Z")

</div>

that’s block.call(child), no?

---

<div class="post-metadata">

**Author:** ![esquilax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@esquilax](https://www.ruby-forum.com/u/esquilax)\
**Post date:** [May 3, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/traversal-of-a-tree-block-code/59213/5 "2006-05-03T18:58:17Z")

</div>

On May 1, 2006, at 2:04 PM, Andrew G. wrote:

> ```
> children = parent.children
> children.each do |child|
> block.call(child)
> self.depthFirstTraversal(child) do |node|
> 
> ```

depthFirstTraversal(child, &block)

> ```
> block.call(node)
> end
> 
> ```

Lose these two lines.

> ```
> end
> 
> ```
> 
> end

Just FYI, Ruby naming conventions are to use methods\_like\_this,  
notLikeThisJavaStyle. 😉

James Edward G. II
