从物化路径构建树

发布于 2024-12-10 05:43:36 字数 858 浏览 0 评论 0原文

我在使用 ruby​​ 从物化路径构建树结构时遇到问题。

假设我有一个排序结果集(来自 couchdb):

[
  { :key => [], :value => "Home" },
  { :key => ["about"], :value => "About" },
  { :key => ["services"], :value => "Services" },
  { :key => ["services", "plans"], :value => "Plans" },
  { :key => ["services", "training"], :value => "Training" },
  { :key => ["services", "training", "python"], :value => "Python" },
  { :key => ["services", "training", "ruby"], :value => "Ruby" }
]

我只需要它作为 ruby​​ 中的树,以下哈希就足够了:

{ :title => "Home", :path => [], :children => [
  { :title => "About", :path => ["about"] }, 
  { :title => "Services", :path => ["services"], :children => [
    { :title => "Plans", :path => ["services", "plans"] }
  ]}
]}

有人可以帮助我吗?

I have a trouble building a tree structure from materialized path using ruby.

Assuming I have a sorted result set (from couchdb):

[
  { :key => [], :value => "Home" },
  { :key => ["about"], :value => "About" },
  { :key => ["services"], :value => "Services" },
  { :key => ["services", "plans"], :value => "Plans" },
  { :key => ["services", "training"], :value => "Training" },
  { :key => ["services", "training", "python"], :value => "Python" },
  { :key => ["services", "training", "ruby"], :value => "Ruby" }
]

I just need this as a tree in ruby,the following hash is good enough:

{ :title => "Home", :path => [], :children => [
  { :title => "About", :path => ["about"] }, 
  { :title => "Services", :path => ["services"], :children => [
    { :title => "Plans", :path => ["services", "plans"] }
  ]}
]}

Could anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-12-17 05:43:36

您只需要一个简单的帮助器类和一点递归即可:

class Tree
  attr_reader :root

  def initialize
    @root = { :title => 'Home', :path => [ ], :children => [ ] }
  end

  def add(p)
    r_add(@root, p[:key].dup, p[:value])
    self
  end

private

  def r_add(h, path, value)
    if(path.empty?)
      h[:title] = value 
      return
    end

    p = path.shift
    c = h[:children].find { |c| c[:path].last == p } 
    if(!c)
      c = { :title => nil, :path => h[:path].dup.push(p), :children => [ ] }
      h[:children].push(c)
    end
    r_add(c, path, value)
  end

end

然后:

t = a.inject(Tree.new) { |t, h| t.add(h) }
h = t.root

将在 h 中给出:

{:title =>"Home", :path=>[], :children=>[
  {:title=>"About", :path=>["about"], :children=>[]},
  {:title=>"Services", :path=>["services"], :children=>[
    {:title=>"Plans", :path=>["services", "plans"], :children=>[]},
    {:title=>"Training", :path=>["services", "training"], :children=>[
      {:title=>"Python", :path=>["services", "training", "python"], :children=>[]}, 
      {:title=>"Ruby", :path=>["services", "training", "ruby"], :children=>[]}
    ]}
  ]}
]}

如果重要的话,您可以整理出空的 :children

A simple helper class and a bit of recursion is all you need:

class Tree
  attr_reader :root

  def initialize
    @root = { :title => 'Home', :path => [ ], :children => [ ] }
  end

  def add(p)
    r_add(@root, p[:key].dup, p[:value])
    self
  end

private

  def r_add(h, path, value)
    if(path.empty?)
      h[:title] = value 
      return
    end

    p = path.shift
    c = h[:children].find { |c| c[:path].last == p } 
    if(!c)
      c = { :title => nil, :path => h[:path].dup.push(p), :children => [ ] }
      h[:children].push(c)
    end
    r_add(c, path, value)
  end

end

And then:

t = a.inject(Tree.new) { |t, h| t.add(h) }
h = t.root

would give this in h:

{:title =>"Home", :path=>[], :children=>[
  {:title=>"About", :path=>["about"], :children=>[]},
  {:title=>"Services", :path=>["services"], :children=>[
    {:title=>"Plans", :path=>["services", "plans"], :children=>[]},
    {:title=>"Training", :path=>["services", "training"], :children=>[
      {:title=>"Python", :path=>["services", "training", "python"], :children=>[]}, 
      {:title=>"Ruby", :path=>["services", "training", "ruby"], :children=>[]}
    ]}
  ]}
]}

You can sort out the empty :children if they matter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文