First, calculate the number of child nodes of a node. $num = ($AR-$AL-1)/2;
Second, find all the child nodes of the a node.
SELECT * from the tree where L > $AL and R < $AR ORDER BY L ASC;
Third, find all the parent nodes of the a node.
SELECT * from the tree where L < $AL and R > $AR order by L Desc;
Iv. adding nodes. You need to make room for the left and right values for the nodes you want to increase. Then insert the new node into the database. Where is the increase? This requires a reference, in the following four cases.
1. Increase the child node b,b as the first child node under the a node.
Update tree Set L = L + 2 where L > $AL; Update Tree Set r = R + 2 where r > $AL; Insert into tree (name, L, R) VALUES (' B ', $AL +1, $AL +2);
2. Under the A node, add the child node B,b as the last child node.
Update tree Set L = L + 2 where L >= $AR; Update Tree Set r = R + 2 where r >= $AR; Insert into tree (name, L, R) VALUES (' B ', $AR-1, $AR);
3. Add Node B after the A node, B as the sibling node of a.
Update tree Set L = L + 2 where L > $AR; Update Tree Set r = R + 2 where r > $AR; Insert into tree (name, L, R) VALUES (' B ', $AR +1, $AR +2);
4. In front of the a node, add Node B, b as the sibling node of a.
Update tree Set L = L + 2 where L >= $AL; Update Tree Set r = R + 2 where r >= $AL; Insert into tree (name, L, R) VALUES (' B ', $AL, $AR);
V. Delete the A node. First of all, the node and all of its child nodes accounted for the left and right space, delete the nodes, and then update the left and right values of the other nodes.
$num = $AR-$AL + 1; Delete from tree where L >= $AL and R <= $AR; Update tree Set R = $num where r > $AR; Update tree Set L = L $num where l > $AR;
Six, mobile node. Mobile nodes simply use left and right values to solve too complex, it is recommended to use the parent node in the table structure of the field, when moving, change the value of the parent node, and then reconstruct the left and right values of the entire tree.
Reprinted from http://www.cnblogs.com/x00479/
General operating logic for infinite categorical left and right value algorithms