The algorithm has the following data structures: 1, LfT for left 2, RGT for right 3, LVL for the level below this figure is a typical structure:
Let's look at some of the ways to use
1, see the whole tree (a) How many nodes (including their own), directly look at the root node on the line (right-left+1)/2 = (20-1+1)/2 = 10
This number has 10 nodes.
2. View the path from Node A to E
1Select * fromTreewhereLfTbetween 1 and 6 andRGTbetween 7 and - Order byLfT
The result is a,b,d,e the data of these 4 nodes, and according to the Order of access paths
If the relationship between 2 nodes is not hierarchical, the query has no results
The reverse is the same, you can get the bottom of a node, to the path of the ancestor node
1Select * fromTreewhereLfTbetween 1 and 6 andRGTbetween 7 and - Order byLfTdesc
The only difference is that the sort is reversed on the line.
3, get all the nodes under a node, and follow the tree structure return we use B as an example
select &NBSP; * from &NBSP;TREE&NBSP; Span style= "color: #0000FF;" >where lft > 2 &NBSP; and &NBSP; RIGHT< 11 &NBSP; order &NBSP; by lft
The result is c,d,e,f, and the order is correct.
4, get all the next Level 2 sub-node We have a example, this time with the LVL parameter, because a level is 1, so we query level is not greater than 3.
1Select * fromTreewhereLfT>2 and right< One andlvl<=3 Order byLfT
Let's look at our new method for adding a node. Below the root node, we add an X node to the right of the G node
The job we're going to do is
1, the right parameter of the G node is 13
2, change all the affected nodes, to the new node free space, all the left node larger than the G-node, all increase 2
1 update tree set lft=lft+2 where lft >
All of the right nodes are larger than the G-node, increasing by 2.
1 update tree set rgt=rgt+2 where RGT >
3, the new node on the empty seat, lft=14,rgt=15, so that the completion of a new node to increase the operation.
Algorithm Description:
1. All categories left and right values > Insert node left node record right value of all + 2
2. Insert node Lvalue = insert position left node record right value + 1, right value = insert position left node record right value + 2
For other related instructions, please refer to:
http://be-evil.org/tb.php?sc=67fcc&id=168
Http://blog.csdn.net/wisewillpower/archive/2008/04/19/2306461.aspx
Pre-sorted traversal tree algorithm (modified preorder tree traversal Algorith