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

Source: Internet
Author: User
Implementation principle: Sort by a sort field. The actual length of this field is the reply depth (when one character represents a layer of depth ).
Restrictions: the reply depth is limited only by the definition length of the sorting string (a bit like space for depth), and the number of replies per post (including the root Post and sub-post) is about 30 (when the SQL
Server uses Dictionary order, case-insensitive sorting, that is, case-insensitive letters), if SQL server uses Binary orders
The limit is 127 (255 ?).
Improvement Method: If you think it is not enough, you can use multiple characters to correspond to a depth (this is a bit difficult to calculate), or use several digits to represent a depth, such as 3
Digit ?? A maximum of 999 sub-posts are allowed. However, the length of the sorting field should be "3 * Maximum depth ")
Advantage: This method is regular ?? Even.
Fields related to the tree structure: rootid and orderstr (varchar type, which depends on the required depth. If you want to use a maximum of 20 layers of reply depth, this parameter is set to varchar.
(20), and so on)
For example: (the following orders are sorted by order by rootid + (1-sign (rootid) * lybid desc, orderstr, id desc)
Id rootid orderstr
1 0 empty string
2 1 z ?? Reply to the root post and use string z to initialize the sort string
___________________
3 1 y ?? Return to 1, and subtract 1 from the ascii code of the last character z of the sort string after 1 to form a new sort string.
The sorting result is:
Id rootid orderstr
1 0 empty string
3 1 y
2 1 z
___________________
4 1 x ?? Reply to 1 post. The sorting string method is the same as above.
The result is as follows:
Id rootid orderstr
1 0 empty string
4 1 x
3 1 y
2 1 z
___________________
5 1 xz ?? Reply to the 4 post and check whether the 4 post has a reply. If no, initialize the second character of the sorting string (add z after 4)
The sorting result is:
Id rootid orderstr
1 0 empty string
4 1 x
5 1 xz
3 1 y
2 1 z
___________________
6 1 xy ?? Reply to the 4 post, and subtract 1 from the ascii code of the last character of the sort string of the first sub-post
The sorting result is:
Id rootid orderstr
1 0 empty string
4 1 x
6 1 xy
5 1 xz
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.