Rapid algorithm of tree forum

zhaozj2021-02-08  292

The rapid algorithm of the tree forum left Light 2001.10.9

The implementation algorithm for the tree forum (ie the stepped forum) is a problem that has been discussed. Summarize, it is generally nothing more than two: the first is recursive. This way is the simplest, the idea is the clearer, but the efficiency is also low, especially when the page is positioned. Since each recursive call must be performed, a database query must be performed so that it is disastrous when the load is in a large number of concurrent requests. So this algorithm is generally not practical. The second is to increase a sort field, and the idea is to use a special design field, such as a sorting string or median sort base, to implement the insertion of the post, when displayed, only need to perform a query for each primary post, will All obtained records are displayed sequentially. This approach has greatly improved efficient, but still is not ideal, and the inserted code has increased unnecessary complexity, and often leads to a restriction in the support level. Is there a way to achieve a tree forum simple and efficient? I thought of an algorithm that should exceed any similar algorithms I have seen on the display speed, and it is not complicated. The idea is very simple: it is completely ignored that the tree structure itself is considered to be a simple sequence table. This is only a query that only one query is required regardless of any form of page. So how do you implement a tree structure? The method is to add two formatted fields, a sequence of record sequence tables, a hierarchy of a record tree, and format the original trendy tree forum. The specific implementation is as follows: 1. Database structure. Only the required fields are listed, all of the INT: ID: Post sequence ORDERNUM: Sort field, according to the order of display from large to small, the earliest post is 1LVELNUM: Tree level, 0 is the main post, with this 2, display. Using a statement: "Select * from article order by ordernum desc", get the required recordset. Traverse record set, check its LevelNum field, set the corresponding indentation. 0 is the primary post, does not indent, and indent the 1st layer, so that it is pushed, then display it. 3, insert. The key is how to set the ORDERNUM field. Divided into two cases: First, a new master post, which is equivalent to adding a record in the sequence table. This is the easiest, just by query, get max (ORDERNUM), and then set the ORDERNUM field to this value plus 1. The second is followed, which is equivalent to inserting a record in the middle of the sequence table. The method is to get the ID of the insertion point (ie, the parent ID ID), give the stickers to the new post, and then add all the recorded ORDERNUMs before the insertion point (including the insert point itself) 1, equal to Let them move one in the order in the order and take a position. For example, you want to follow the post after an ID 23, first select the ID23 ORDERNUM, assume that 18, give it a new post, then "Update Article Set ORdernum = OrderNum 1 Where Ordernum> = 18 ", all posts after the new post is moved. Efficiency analysis: The display speed should not be faster, just a simple Select, sort an int field, and support unlimited reply level.

转载请注明原文地址:https://www.9cbs.com/read-1036.html

New Post(0)