4

I am trying to write a function to convert a flat array with a path information into a tree representation of that array.

The goal would be to turn an array like the following:

[
{ :name => "a", :path => [ 'a' ] },
{ :name => "b", :path => [ 'a', 'b' ] },
{ :name => "c", :path => [ 'a', 'b', 'c' ] },
{ :name => "d", :path => [ 'a', 'd' ] },
{ :name => "e", :path => [ 'e' ] }
]

into one like this:

[{:node=>{:name=>"a", :path=>["a"]},
  :children=>
   [{:node=>{:name=>"b", :path=>["a", "b"]},
     :children=>
      [{:node=>{:name=>"c", :path=>["a", "b", "c"]}, :children=>[]}]},
    {:node=>{:name=>"d", :path=>["a", "d"]}, :children=>[]}]},
 {:node=>{:name=>"e", :path=>["e"]}, :children=>[]}]

The closest result I got with was with the following code:

class Tree

  def initialize
    @root = { :node => nil, :children => [ ] } 
  end 

  def from_array( array )
    array.inject(self) { |tree, node| tree.add(node) }
    @root[:children]
  end 

  def add(node)
    recursive_add(@root, node[:path].dup, node)
    self
  end 

  private

  def recursive_add(parent, path, node)
    if(path.empty?)
      parent[:node] = node
      return
    end 
    current_path = path.shift
    children_nodes = parent[:children].find { |child| child[:node][:path].last == current_path } 
    unless children_nodes
      children_nodes = { :node => nil, :children => [ ] } 
      parent[:children].push children_nodes
    end 
    recursive_add(children_nodes, path, node)
  end 
end

flat = [ 
  { :name => "a", :path => [ 'a' ] },
  { :name => "b", :path => [ 'a', 'b' ] },
  { :name => "c", :path => [ 'a', 'b', 'c' ] },
  { :name => "d", :path => [ 'a', 'd' ] },
  { :name => "e", :path => [ 'e' ] } 
]

require 'pp'
pp Tree.new.from_array( flat )

But it is quite verbose and I have the feeling that it might not be very effective for very large sets.

What would be the cleanest and most effective way to achieve that in ruby?

8
  • 4
    Post what you have, even if you think it's bad. Commented Oct 29, 2012 at 3:43
  • Please do not use an unexplained method or variable in the question. What is path? Commented Oct 29, 2012 at 4:32
  • @sawa sorry, it was a typo. path is a symbol. Commented Oct 29, 2012 at 4:48
  • 2
    I think you should try simplifying your tree. For e.g. if the name is unique then maybe use the name as key so that you can search easily. Commented Oct 29, 2012 at 4:48
  • @AndrewMarshall Thanks for pointing, I added my current source. Commented Oct 29, 2012 at 4:48

1 Answer 1

2

This is my try.

array = [
{ :name => "a", :path => [ 'a' ] },
{ :name => "b", :path => [ 'a', 'b' ] },
{ :name => "c", :path => [ 'a', 'b', 'c' ] },
{ :name => "d", :path => [ 'a', 'd' ] },
{ :name => "e", :path => [ 'e' ] }
]

array
.sort_by{|h| -h[:path].length}
.map{|h| {node: h, children: []}}
.tap{|array| 
  while array.first[:node][:path].length > 1
    child = array.shift
    array
    .find{|h| h[:node][:name] == child[:node][:path][-2]}[:children]
    .push(child)
  end
}

# => [
  {:node=>{:name=>"e", :path=>["e"]}, :children=>[]},
  {:node=>{:name=>"a", :path=>["a"]}, :children=>[
    {:node=>{:name=>"d", :path=>["a", "d"]}, :children=>[]},
    {:node=>{:name=>"b", :path=>["a", "b"]}, :children=>[
      {:node=>{:name=>"c", :path=>["a", "b", "c"]}, :children=>[]}
    ]}
  ]}
]
Sign up to request clarification or add additional context in comments.

1 Comment

Ruby feels like magic sometimes. Clean and effective. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.