Number of nodes per level in a nested set

I am using Ruby (Ruby on Rails) and have a nested set of about 2000 nodes. Each node can only have two children.

What is the best way to determine the number of nodes at each level? Do I need to scan an entire tree looking for a sibling to do this?

Thank!

+2


source to share


2 answers


I actually figured out a "quick" way to do this. Using a segment from "Subtree Depth" on this page ( link text ) I am querying for depth using the given SQL using find_by_sql. Then I loop through the returned model results and count how many times each depth value occurs. Works great! Thanks everyone for watching and for your help!



0


source


ActiveRecord has a counter_cache attribute that is made specifically for such cases. Check out Cache Counter Column , screencast by Ryan Bates.



+1


source







All Articles