将扁平树解析为非扁平树的算法
我有以下扁平树:
id name parent_id is_directory
===========================================================
50 app 0 1
31 controllers 50 1
11 application_controller.rb 31 0
46 models 50 1
12 test_controller.rb 31 0
31 test.rb 46 0
我正在尝试找出一种算法,将其放入以下树结构中:
[{
id: 50,
name: app,
is_directory: true
children: [{
id: 31,
name: controllers,
is_directory: true,
children: [{
id: 11,
name: application_controller.rb
is_directory: false
},{
id: 12,
name: test_controller.rb,
is_directory: false
}],
},{
id: 46,
name: models,
is_directory: true,
children: [{
id: 31,
name: test.rb,
is_directory: false
}]
}]
}]
有人能给我指出正确的方向吗?我正在寻找步骤(例如,构建关联数组;循环遍历数组查找 x;等等)。
我使用的是 Ruby,因此我可以使用面向对象的语言功能。
I have the following flat tree:
id name parent_id is_directory
===========================================================
50 app 0 1
31 controllers 50 1
11 application_controller.rb 31 0
46 models 50 1
12 test_controller.rb 31 0
31 test.rb 46 0
and I am trying to figure out an algorithm for getting this into the following tree structuree:
[{
id: 50,
name: app,
is_directory: true
children: [{
id: 31,
name: controllers,
is_directory: true,
children: [{
id: 11,
name: application_controller.rb
is_directory: false
},{
id: 12,
name: test_controller.rb,
is_directory: false
}],
},{
id: 46,
name: models,
is_directory: true,
children: [{
id: 31,
name: test.rb,
is_directory: false
}]
}]
}]
Can someone point me in the right direction? I'm looking for steps (eg. build an associative array; loop through the array looking for x; etc.).
I'm using Ruby, so I have object-oriented language features at my disposal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 ruby 中,您应该能够使用哈希在线性时间 O(n) 内轻松完成此操作。
In ruby, you should be able to easily do it in linear time O(n) with a Hash.
我已经用递归和非递归研究了这个问题。我在这里放置了 2 个变体:
"parend_id" = "head_id" # 对于这些示例
递归:
优点:
缺点!:
非递归,有循环:
优点:
缺点:
两种方式的结果都是:
Resume
对于生产来说,最好使用第二种方式,可能有更优化的方式来实现它。
希望写的会有用
I had investigate the issue, with recursive and non recursive. I put here 2 variants:
"parend_id" = "head_id" # for those examples
Recursivly:
Pros:
Cons!:
Non recursevly, with cycle:
Pros:
Cons:
The result of the both ways is:
Resume
For production it is better to use second way, probably there's a more optimal way to realize it.
Hope written will be useful
要将元素添加到树中(步骤 3),您需要首先找到它们的父元素。树数据结构应该允许您很快地做到这一点,或者您可以使用包含按 id 索引的树节点的字典。
如果您提到您正在使用哪种语言,则可以建议更具体的解决方案。
To add element to the tree (step 3), you 'd need to find their parent first. A tree data structure should allow you to do that pretty quickly, or you can use a dictionary that contains tree nodes indexed by id.
If you mention which language you 're using a more specific solution could be suggested.
以下是我必须对 @daniel-beardsley 的回复进行的一些更改,以使其对我有用。
1)由于我是从 activeRecord 关系开始的,所以我首先执行“as_json”来转换为哈希值。请注意,所有键都是字符串,而不是符号。
2) 在我的例子中,没有父项的项的父值为 nil 而不是 0。
3) 我在“继续”表达式上遇到编译错误,所以我将其更改为“下一个”(有人可以向我解释一下吗< /strong> - 也许这是 @daniel-beardsley 在转换为 ruby 时出现的拼写错误?)
4)我在删除父项的项目时遇到了一些崩溃。我添加了代码来忽略这些 - 如果您愿意,您也可以将其放在根目录中
Here are a few changes I had to make to @daniel-beardsley's response to make it work for me.
1) Since I was starting with an activeRecord relation, I started by doing a "as_json"to convert to a hash. Note that all the keys were therefore strings, not symbols.
2) in my case items without parents had a parent value of nil not 0.
3) I got a compile error on the "continue" expression, so I changed that to "next" (can someone explain this to me -- maybe it was a typo by @daniel-beardsley when converting to ruby?)
4) I was getting some crashes for items with deleted parents. I added code to ignore these -- you could also put at the root if you prefer