A tree-like structure using median ordering cardinal Method--let recursion roll aside

Source: Internet
Author: User
recursive | Sorting using median ordering cardinal method to achieve tree-like structure

In the compilation of BBS, people often ask how to implement tree structure. A more irresponsible answer is to use a recursive algorithm. Of course, recursion is a viable option (a binary tree can be used as a recursive algorithm only), but for BBS, this is bound to do a lot of SQL queries (although you can use stored procedures to do, but to fundamentally speed up, you should consider the faster algorithm).
The following is a feasible algorithm for the implementation of a tree-like structure with complete screen-discard.

Here's another way to implement a tree structure using the median sort cardinality method:
First, the main idea: Add a sort base digital segment ordernum, reply to the same root post inserted posts, sorted cardinal Ordernum take both of the median value.
For the simplicity of the narrative, only the fields related to the tree structure are discussed here.

Add three redundant fields to the table, rootid--is used to record the depth that the root id,deep--is used to record the reply (the root sticker when 0), and the ordernum--sort cardinality (the key).

Table Forum with (only columns related to tree structure):
ID Rootid Deep Ordernum
Where the ID, Rootid, deep are int (deep can be tinyint type), ordernum for float type.

For example: (in order to be simple, using a small starting sort cardinality, in practical applications, should use a large starting base, and should take 2 of the integer power, such as 65536=2^16, the following said sort refers to ordernum from small to large sort).
ID Rootid Deep Ordernum
1 0 0 0
2 1 1 64
______________________________
3 1 1 32 reply 1th, take 1, 2 cardinal value i.e. (0+64)/2

After sorting, the result is:
ID Rootid Deep Ordernum
1 0 0 0
3 1 1 32
2 1 1 64
______________________________
4 1 2 48 reply 3rd post, take 3, 2 cardinal value that is (32+64)/2

After sorting, the result is:
ID Rootid Deep Ordernum
1 0 0 0
3 1 1 32
4 1 2 48
2 1 1 64
______________________________
5 1 3 56 reply 4th post, take 4, 2 cardinal value that is (48+64)/2

The result of sorting is:
ID Rootid Deep Ordernum
1 0 0 0
3 1 1 32
4 1 2 48
5 1 3 56
2 1 1 64
______________________________
6 1 2 40 reply 3rd post, take 3, 4 cardinal value that is (32+48)/2

The result of sorting is:
ID Rootid Deep Ordernum
1 0 0 0
3 1 1 32
6&nb



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.