在嵌套集树中移动节点
我正在使用 mySQL 处理邻接列表,但无法(至少我自己)进行足够合理的查询所需的思考,以便能够移动一组节点(以及最终的子节点)。
该表包含以下列:
id name left right
非常感谢!
I am working on an adjacency list with mySQL and can not (atleast by myself) do the thinking needed to make a decent enough query to be able to move a set of nodes (together with eventual children nodes) around.
The table has following columns:
id name left right
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个解决方案,让您只需使用一个输入参数即可将节点移动到树中的任何位置 - 节点的新左侧位置 (newpos)。
基本上分为三组:
在 psuedo-sql 中,它看起来像这样:
:distance 变量是新旧位置之间的距离,:width 是子树的大小,:tmppos 用于跟踪更新期间移动的子树。这些变量定义为:
有关完整的代码示例,请参阅我的博客
https://rogerkeays.com/how-to-move-a-node-in-nested-sets-with-sql
如果您喜欢这个解决方案,请投票。
Here is a solution that lets you move a node to any position in the tree with just a single input parameter - the new left position (newpos) of the node.
Fundamentally there are three sets:
In psuedo-sql, it looks like this:
The :distance variable is the distance between the new and old positions, the :width is the size of the subtree, and :tmppos is used to keep track of the subtree being moved during the updates. These variables are defined as:
For a complete code example, see my blog at
https://rogerkeays.com/how-to-move-a-node-in-nested-sets-with-sql
If you like this solution, please up-vote.
我很确定该表使用嵌套集设计,而不是邻接列表。如果使用邻接列表,它将具有类似
parent_id
的列,而不是left
和right
。移动节点是嵌套集中的皇家 PITA。您必须为移动的每个节点重新编号所有
left
和right
值。如果移动子树,最简单的方法是一次删除一个节点,在每个节点删除后对
left
和right
字段重新编号。然后,一旦您删除了整个子树(并以某种方式在应用程序中保留了子树的结构),请将子树重新插入到树中的目标位置,再次重新编号left
和 <每次插入的 code>right 字段。更新:我最近写了一篇关于如何在不同的分层数据设计中移动子树的博客,我比嵌套集更喜欢它。我将这种设计称为闭合表。
请参阅http://www.mysqlperformanceblog.com/ 2011/02/14/移动闭包表中的子树/
I'm pretty sure that table is using the Nested Sets design, not Adjacency List. If it were using Adjacency List, it would have a column like
parent_id
instead ofleft
andright
.Moving nodes is royal PITA in Nested Sets. You have to renumber all the
left
andright
values for each node you move.If you move a subtree, the easiest way to do this is to remove the nodes one at a time, renumbering the
left
andright
fields after each node removal. Then once you have removed the whole subtree (and preserved the structure of the subtree in your application somehow), re-insert the subtree at its destination location in the tree, again re-numbering theleft
andright
fields per insert.update: I recently wrote a blog about how to move subtrees in a different hierarchical data design which I like better than nested sets. I call this design Closure Table.
See http://www.mysqlperformanceblog.com/2011/02/14/moving-subtrees-in-closure-table/
使用嵌套集模型(具有左列和右列)在类别树中移动子树的步骤是:
1. 将 lft 和 rgt 列转换为您想要移动的类别及其子类别的负对应项(这将暂时从树中“删除”子树)
2.如果要将子树向上移动(或在嵌套集合表示中“向左”移动),请将子树的新父级与其旧的左(或右,在第二种情况下)限制之间的所有类别向右移动,否则(向下移动子树时)向右。这涉及将这些类别的左列和右列设置为其值加上(或减去,在第二种情况下)子树(或要移动的类别)左列和右列之间的距离
3. 之后,您所要做的就是将左列和右列恢复为正值,同时减去(或在第二种情况下添加)其左极限与新父左列之间的差值(或者在第二种情况下在父级左极限和右极限之间)
这一切看起来都很复杂,用一种情况表达,所以我将其分解为两种情况:
The steps to move a subtree around in a category tree using the nested set model (with left and right columns) are:
1. transform the lft and rgt columns in their negative counterparts for the category you wish to move and its subcategories (this will "remove" the subtree from the tree, for the time being)
2. if you are moving the subtree up (or "left" in the nested set representation), move all the categories between the new parent of the subtree and its old left(or right, in the second case) limit to the right, otherwise (when moving the subtree down) to the right. This involves setting the left and right columns of these categories to their values plus (or minus, in the second case) the distance between the left and right column of the subtree (or the category to be moved)
3. after this, all you have to do is to revert the left and right columns to the positive values and in the same time substract (or add, in the second case) the difference between its left limit and the new parent left column (or between the parent left and the right limit in the second case)
This all seems very complicated, expressed in one case, so I broke it down to the two cases:
我有一个更简单、更容易阅读的sql,它非常适合我。它形成了一个典型的嵌套集合结构,包含 id、rgt、lft、level(也可以在没有 level 的情况下工作):
Ive got a simpler and easier to read sql, it works perfectly for me. It made for a typical nested set structure with id, rgt, lft, level (works without level too):
当您从数学角度思考时,您可以使用单个 UPDATE 语句来完成这一切。对于任何移动操作,当您知道节点的左侧和右侧以及希望其最终到达的插入位置时,您可以一次性重新计算树中的所有左侧和右侧。您只需要确保新位置不在移动节点本身内,这可以通过简单的 WHERE 子句来完成:
请参阅 https://sedna-soft.de/articles/nested-sets-move-subtree/
When you think about it mathematically, you could do it all with a single UPDATE statement. For any move operation, when you know the LEFT and RIGHT of your node and the insertion position you want it to end up at, you can recompute all of the LEFTs and RIGHTs in your tree in one go. You only need to make sure, the new position is not within the moving node itself, which can be done with a simple WHERE clause:
See https://sedna-soft.de/articles/nested-sets-move-subtree/