原理:
我们先把树按照水平方式摆开。从根节点开始(“Food”),然后他的左边写上1。然后按照树的顺序(从上到下)给“Fruit”的左边写上2。这样,你沿着树的边界走啊走(这就是“遍历”),然后同时在每个节点的左边和右边写上数字。最后,我们回到了根节点“Food”在右边写上18。下面是标上了数字的树,同时把遍历的顺序用箭头标出来了。
我们称这些数字为左值和右值(如,“Food”的左值是1,右值是18)。正如你所见,这些数字按时了每个节点之间的关系。因为“Red”有3和6两个值,所以,它是有拥有1-18值的“Food”节点的后续。同样的,我们可以推断所有左值大于2并且右值小于11的节点,都是有2-11的“Fruit” 节点的后续。这样,树的结构就通过左值和右值储存下来了。这种数遍整棵树算节点的方法叫做“改进前序遍历树”算法。
表结构设计:
常用的操作:
下面列出一些常用操作的SQL语句
返回完整的树(Retrieving a Full Tree)
SELECT node.name
FROM nested_category node, nested_category parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND parent.name = 'electronics'
ORDER BY node.lft
返回某结点的子树(Find the Immediate Subordinates of a Node)
SELECT V.*
FROM (SELECT node.name,
(COUNT(parent.name) - (AVG(sub_tree.depth) + 1)) depth
FROM nested_category node,
nested_category parent,
nested_category sub_parent,
(SELECT V.*
FROM (SELECT node.name, (COUNT(parent.name) - 1) depth
FROM nested_category node, nested_category parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'portable electronics'
GROUP BY node.name) V,
nested_category T
WHERE V.name = T.name
ORDER BY T.lft) sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.name = sub_tree.name
GROUP BY node.name) V,
nested_category T
WHERE V.name = T.name
and V.depth <= 1
and V.depth > 0
ORDER BY T.Lft
返回某结点的祖谱路径(Retrieving a Single Path)
SELECT parent.name
FROM nested_category node, nested_category parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'flash'
ORDER BY node.lft
返回所有节点的深度(Finding the Depth of the Nodes)
SELECT V.*
FROM (SELECT node.name, (COUNT(parent.name) - 1) depth
FROM nested_category node, nested_category parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name) V,
nested_category T
WHERE V.name = T.name
ORDER BY T.Lft
返回子树的深度(Depth of a Sub-Tree)
SELECT V.*
FROM (SELECT node.name,
(COUNT(parent.name) - (AVG(sub_tree.depth) + 1)) depth
FROM nested_category node,
nested_category parent,
nested_category sub_parent,
(SELECT V.*
FROM (SELECT node.name, (COUNT(parent.name) - 1) depth
FROM nested_category node, nested_category parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'portable electronics'
GROUP BY node.name) V,
nested_category T
WHERE V.name = T.name
ORDER BY T.lft) sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.name = sub_tree.name
GROUP BY node.name) V,
nested_category T
WHERE V.name = T.name
ORDER BY T.Lft
返回所有的叶子节点(Finding all the Leaf Nodes)
SELECT name FROM nested_category WHERE rgt = lft + 1
插入节点(Adding New Nodes)
LOCK TABLE nested_category WRITE;
SELECT @myRight := rgt FROM nested_category WHERE name = 'TELEVISIONS';
UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myRight;
UPDATE nested_category SET lft = lft + 2 WHERE lft > @myRight;
INSERT INTO nested_category
(name, lft, rgt)
VALUES
('GAME CONSOLES', @myRight + 1, @myRight + 2);
UNLOCK TABLES;
删除节点(Deleting Nodes)
LOCK TABLE nested_category WRITE;
SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1
FROM nested_category
WHERE name = 'GAME CONSOLES';
DELETE FROM nested_category WHERE lft BETWEEN @myLeft AND @myRight;
UPDATE nested_category SET rgt = rgt - @myWidth WHERE rgt > @myRight;
UPDATE nested_category SET lft = lft - @myWidth WHERE lft > @myRight;
UNLOCK TABLES;
分享到:
相关推荐
安装 go get github.com/griffinqiu/go-nested-set用法定义模型您必须使用nestedset Stuct标记来定义Gorm模型,如下所示: 支持结构标签: id -int64-节点的主键parent_id -sql.NullInt64-ParentID列,null为根lft...
Nested Set Model Adjacency Model HOWTO Importing and Exporting Methods HOWTO User Modules Show/Hide Columns Table to jqGrid Post Data module HOWTO jQuery UI Integrations Introduction Solutions
Nested Set Model Adjacency Model HOWTO Importing and Exporting Methods HOWTO User Modules Show/Hide Columns Table to jqGrid Post Data module HOWTO jQuery UI Integrations Introduction Solutions
嵌套集PHP 类嵌套集请求使用嵌套集树
Consistency and isolation are the most important in describing SQL Server’s locking model. It is up to the application to define what consistency means, and isolation in some form is needed to ...
嵌套集模型 Yii 框架嵌套集模型是一种用于在关系数据库中表示嵌套集(也称为树或...2.为这个类创建对象 $model = new MenuNested ();3. 调用类方法 $mode - > insertMenu ( $parentId , $insertedArra ) ; 以及更多方法
##Deep 模型设置和触发 model.on("change")## 开箱即用,Backbone 没有让我们能够正确监控模型属性超出model.attributes对象的状态变化。 如果你想使用model.on("change:someObject.property", function(evt) { } )...
- C++ 2.1 support, including the new nested class specifications, and support of C++ 3.0 templates. - Support for pre-compiled headers for substantial time savings during subsequent recompiles. ...
- C++ 2.1 support, including the new nested class specifications, and support of C++ 3.0 templates. - Support for pre-compiled headers for substantial time savings during subsequent recompiles. ...
* The TClientDataSet.ApplyUpdates method fails if the dataset contains only changes to fields that have the ProviderFlags.pfnUpdate property set to False (Quality Central 2338). * TClientDataSet does...
骨干嵌套模型 骨干嵌套模式,就是要在嵌套机型轻巧,简单的解决方案。 它没有任何花哨的内容,只是简单的关系(最小2.1 KB)。... set ( { "author" : { "name" : "Heber J. Grant" , "title" : "President" ,
12.9 The Object Model of JavaScript 524 12.10 Implementation of Object-Oriented Constructs 527 Summary • Review Questions • Problem Set •Programming Exercises 530 Chapter 13 Concurrency ...
Micro Series 8051 C-Compiler V4.10A/DOS ... Usage: icc8051 {<options>} <sourcefile> {<options>} Sourcefile: 'C' source file with default extension: .c Environment: QCC8051 ...-C Enable nested comments
jQuery doesn’t have a concept of an underlying data model, so to get the number of items you have to infer it from the number of TRs in a table or the number of DIVs with a certain CSS class....
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...PEP 343: The ‘with...
The Internet Explorer Event Model Section 19.4. The Netscape 4 Event Model Chapter 20. Compatibility Techniques Section 20.1. Platform and Browser Compatibility Section 20.2. Language ...
Opening a New GUI in the Layout Editor . . . . . . . . . . . . . . 2-11 Setting the GUI Figure Size . . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Adding the Components . . . . . . . . . . . ....
Chapter 7 Database Design and the E-R Model 7.1 Overview of the Design Process 259 7.2 The Entity-Relationship Model 262 7.3 Constraints 269 7.4 Removing Redundant Attributes in Entity Sets 272 7.5 ...
4.16 Stack operations for nested subroutines 4.17 Block copy with LDM and STM 4.18 Memory accesses 4.19 The Read-Modify-Write operation 4.20 Optional hash with immediate constants 4.21 Use of macros ...
nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [org.jbpm.pvm.internal.model.ExecutionImpl#50001] 解决: hibernate.dialect=org.hibernate.dialect.MySQL5...