Use the "use median sorting basis" to implement a tree structure (1)

Source: Internet
Author: User
In the compilation of BBS, people often ask how to implement the tree structure? The irresponsible answer is that recursive algorithms are used. Of course, recursion is a feasible method (binary tree calendars only seem to use recursive algorithms), but for BBS, in this case, a large number of SQL queries are required. (although stored procedures can be used, a faster algorithm should be considered to accelerate the speed ).
The following is a feasible algorithm for implementing the tree structure of complete screen discard.
?? The following provides another tree structure using the "using the median sort basis:
I. Main idea: add an ordernum sorting base field and return the value of ordernum in the same root post to the value of ordernum.
?? For concise description, only fields related to the tree structure are discussed here.
Add three redundant fields to the table. The "rootid" field is used to record the root id, the "deep" field is used to record the depth of the reply (when it is 0, it indicates the root paste), and the "ordernum" column is used to record the base of the sort ).
Table forum and (only columns related to the tree structure ):
Id? Rootid? Deep ?? Ordernum
Among them, id, rootid, and deep are int type (deep can be tinyint type), and ordernum is float type.
Example: (for simplicity, a small starting base should be used. In practice, a large starting base should be used and an integer power of 2 should be used, for example, 65536 = 2 ^ 16, the sorting mentioned below refers to sorting by ordernum from small to large ).
Id? Rootid ?? Deep ?? Ordernum
1 ??? 0 ???? 0 ?????? 0
2 ??? 1 ???? 1 ????? 64
______________________________
3 ??? 1 ???? 1 ????? 32 ?? Reply to the 1st post, and take the value of the base number 1 and 2 (0 + 64)/2
The sorting result is as follows:
Id? Rootid ?? Deep ?? Ordernum
1 ??? 0 ???? 0 ?????? 0
3 ??? 1 ???? 1 ????? 32
2 ??? 1 ???? 1 ????? 64
______________________________
4 ??? 1 ???? 2 ????? 48? Reply to the 3rd post, and obtain the base value of 3 and 2 (32 + 64)/2
The sorting result is as follows:
Id? Rootid ?? Deep ?? Ordernum
1 ??? 0 ???? 0 ?????? 0
3 ??? 1 ???? 1 ????? 32
4 ??? 1 ???? 2 ????? 48
2 ??? 1 ???? 1 ????? 64
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.