Traversal of a tree + block code

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? :slight_smile:

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! :slight_smile:

聽聽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

路路路

--
Posted via http://www.ruby-forum.com/.

Andrew Gibson wrote:

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? :slight_smile:

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! :slight_smile:

  def self.depthFirstTraversal(parent , &block)
    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)

路路路

    end
  end

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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? :slight_smile:

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! :slight_smile:

  def self.depthFirstTraversal(parent , &block)
    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. :wink:

James Edward Gray II

路路路

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

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?

路路路

--
Posted via http://www.ruby-forum.com/.

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

路路路

On 5/1/06, Andrew Gibson <gibson_andrew@yahoo.com> wrote:

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?

--
Posted via http://www.ruby-forum.com/\.

--
Chiaroscuro
---
Liquid Development: http://liquiddevelopment.blogspot.com/