[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

looping through a tree from trunk to leaves

Nanyang Zhan

4/20/2007 6:45:00 PM

I've just built a acts_as_tree model,
now I need to render it out as a tree structure in view.
and I also need a action method to loop through every row of the
database following the tree structure ( from root to it's branch, then
to the ends of the branch, then go back for other branches...). I think
both the view helper and action method need to use a similar loop style,
and this code may be popular, at lease useful. Is there any article
about this subject?

I've get a view snip, btw:
http://snippets.dzone.com/tag/ac...

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

7 Answers

Phillip Gawlowski

4/20/2007 6:53:00 PM

0

Nanyang Zhan wrote:
> I've just built a acts_as_tree model,
> now I need to render it out as a tree structure in view.
> and I also need a action method to loop through every row of the
> database following the tree structure ( from root to it's branch, then
> to the ends of the branch, then go back for other branches...). I think
> both the view helper and action method need to use a similar loop style,
> and this code may be popular, at lease useful. Is there any article
> about this subject?
>
> I've get a view snip, btw:
> http://snippets.dzone.com/tag/ac...
>

You might want to tell the Rails folk, too.
http://www.rubyonrails.com...

--
Phillip "CynicalRyan" Gawlowski
http://cynicalryan....
http://clothred.rub...

Rule of Open-Source Programming #8:

Open-Source is not a panacea.

Rich Morin

4/20/2007 7:57:00 PM

0

At 3:45 AM +0900 4/21/07, Nanyang Zhan wrote:
> I've just built a acts_as_tree model,
> now I need to render it out as a tree structure in view.
> and I also need a action method to loop through every row ...

The word "loop" may be leading you in the wrong direction. If
you hear the words "tree" or "hierarchy", you should be looking
at recursive solutions. So, in pseudo-ruby:


# walk - level is either an item or a tree of items
#
def walk(level)
if level.is_leaf?
# handle leaf action
else
# handle branch pre-action
level.each { |item| walk(item) }
# handle branch post-action
end
end

In my (limited) experience, recursive code tends to be very
small and simple, once written. Writing it, however, can be
challenging, in that it requires a different way of thinking
than iterative code does.


There are also tree-walk routines that will simply allow you
to iterate through the nodes of a tree. Whether there is a
routine such as this for your particular tree, I don't know.

-r
--
http://www.cf... Rich Morin
http://www.cf.../resume rdm@cfcl.com
http://www.cf.../weblog +1 650-873-7841

Technical editing and writing, programming, and web development

Suraj Kurapati

4/20/2007 8:57:00 PM

0

Nanyang Zhan wrote:
> from root to it's branch,
> then to the ends of the branch,
> then go back for other branches...

This is a Depth First Search (DFS) tree traversal:

http://en.wikipedia.org/wiki/Depth-fi...

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

Gene Tani

4/20/2007 10:06:00 PM

0

On Apr 20, 11:45 am, Nanyang Zhan <s...@hotmail.com> wrote:
> I've just built a acts_as_tree model,
> now I need to render it out as a tree structure in view.
> and I also need a action method to loop through every row of the
> database following the tree structure ( from root to it's branch, then
> to the ends of the branch, then go back for other branches...). I think
> both the view helper and action method need to use a similar loop style,
> and this code may be popular, at lease useful. Is there any article
> about this subject?
>
> I've get a view snip, btw:http://snippets.dzone.com/tag/ac...
>
> --
> Posted viahttp://www.ruby-....

google thru *rails-talk* for ajax tree control, widgets, etc, you'll
see:

http://ajaxonrails.wordpress.com/2006/11/26/ajaxonrailsdra...
http://www.epiphyte.ca/code/live...

and lots more

Roseanne Zhang

4/20/2007 10:20:00 PM

0

Suraj Kurapati wrote:
> Nanyang Zhan wrote:
> This is a Depth First Search (DFS) tree traversal:
>
> http://en.wikipedia.org/wiki/Depth-fi...

Way to go!!!!


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

Nanyang Zhan

4/20/2007 10:29:00 PM

0

Rich Morin wrote:
> The word "loop" may be leading you in the wrong direction. If
> you hear the words "tree" or "hierarchy", you should be looking
> at recursive solutions.

Now it seems obviously "loop" IS a wrong word. Both "walk" and "travel"
are far more fit the idea.

I'll start form a very simple tree, no cross. and your code certainly
give my a idea to start. thanks.



Suraj Kurapati wrote:

> This is a Depth First Search (DFS) tree traversal:
>
> http://en.wikipedia.org/wiki/Depth-fi...

And thank you, Suraj Kurapati. The link and the terms no only help solve
my problem, but also open a world of search relate algorithm to me.


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

julian.kamil@gmail.com

4/21/2007 1:48:00 AM

0

This will do for 'MyModel':

def view(args)
args[:children].each do |child|
printf("%03d %s%s\n", child.id, args[:level], child.class.name)
view(:children => child.children, :level =>
"#{args[:level].gsub(/./, ' ')} ...")
end
end

view(:children => MyModel.find(:all, :conditions => [ "parent_id IS
NULL" ]), :level => '')

Julian I. Kamil <julian.kamil@gmail.com>

On Apr 20, 2:45 pm, Nanyang Zhan <s...@hotmail.com> wrote:
> I've just built a acts_as_tree model,
> now I need to render it out as a tree structure in view.
> and I also need a action method to loop through every row of the
> database following the tree structure ( from root to it's branch, then
> to the ends of the branch, then go back for other branches...). I think
> both the view helper and action method need to use a similar loop style,
> and this code may be popular, at lease useful. Is there any article
> about this subject?
>
> I've get a view snip, btw:http://snippets.dzone.com/tag/ac...
>
> --
> Posted viahttp://www.ruby-....