General operating logic for infinite categorical left and right value algorithms

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.