在嵌套集树中移动节点 [英] Move node in Nested Sets tree

查看:111
本文介绍了在嵌套集树中移动节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mySQL处理邻接表,但不能(至少自己做)做一个足够体面的查询以能够移动一组节点(连同最终的子节点)所需的思考.

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.

该表包含以下列:

 id     name     left     right

非常感谢!

推荐答案

我很确定该表使用的是嵌套集设计,而不是邻接表.如果使用的是邻接列表,则该列将具有parent_id而不是leftright的列.

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 of left and right.

移动节点是嵌套集中的皇家PITA.您必须为移动的每个节点重新编号所有leftright值.

Moving nodes is royal PITA in Nested Sets. You have to renumber all the left and right values for each node you move.

如果移动子树,最简单的方法是一次删除一个节点,并在每次删除节点后重新编号leftright字段.然后,一旦您删除了整个子树(并以某种方式保留了子树的结构),则将该子树重新插入其在树中的目标位置,并再次为每次插入重新编号leftright字段

If you move a subtree, the easiest way to do this is to remove the nodes one at a time, renumbering the left and right 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 the left and right fields per insert.

更新:我最近写了一个博客,介绍如何在不同的分层数据设计中移动子树,我更喜欢嵌套集.我将此设计称为关闭表.
参见 http://www.mysqlperformanceblog.com/2011/02/14/moving-subtrees-in-closure-table/

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/

这篇关于在嵌套集树中移动节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆