[LRUG] Pruning a tree

Andrew Stewart boss at airbladesoftware.com
Tue Jun 28 01:48:49 PDT 2011


On 27 Jun 2011, at 19:41, Viktor Tron wrote:
> def prune_and_sum(node,ids_to_preserve,result,counts)
>  found = ids_to_preserve.delete(node.id)
>  subtree_results = []
>  subtree_pruned = node.children.all? do |child|
>    prune_and_sum(child,ids_to_preserve,subtree_results,counts)
>  end
>  counts << node.count if found
>  results << subtree_results.empty? node : { node => subtree_results }
>  subtree_pruned && !found && node.destroy
> end
> 
> # ids_to_preserve is a hash of id => true
> counts = []
> results = []
> result prune_and_sum(root,ids_to_preserve,results,counts)
> sum = counts.inject(&:+)

Thanks for this.  I'll work through it now.  In the meantime...

> just quietly: are you sure your return value structure makes sense?

Not especially.  I simply want a tree...what would you suggest?

Yours,
Andy Stewart

-------
http://airbladesoftware.com



More information about the Chat mailing list