开发者

Count number of nodes in a 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 how many nodes are in each level? Do I have to crawl the entire tree looking for sibling to do this开发者_运维知识库?

Thanks!


In ActiveRecord there is an attribute called counter_cache, it's done specifically for such cases. Check Counter Cache Column screencast by Ryan Bates.


I actually figured out a "quick" way to do this. Using the segment from "Depth of a Sub-Tree" on this page (link text) I am polling for depth using the given SQL with find_by_sql. I then iterate over the returned model results and count how many times each depth value occurs. Works great! Thanks all for looking and for your help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜