New research on optimal algorithm of infinite-level directory tree

Source: Internet
Author: User
(First of all: because I see a lot of people interested in this article, the author slightly modified and supplemented this article)
This article specifically describes: 1. Data structure of a directory tree
2. The query complexity of database in specific application is discussed.
Table structure: ID (number) PID (parent node) nodepath (path) NodeType (state)

Sample data: 1 0 0> F
2 0 0> D
3 2 0>2> D
4 3 0>2>3> F
Test items: 1. Gets the parent node of the current node: complexity: O (N)
2. To get the subordinate child nodes of the current node: complexity: O (N)
3. Gets all descendants nodes of the current node: complexity: O (N)
4. Gets all immediate ancestor nodes of the current node: complexity: O (N)
5. Delete, the new node is relatively easy to implement, mainly to achieve node transfer a little trouble: set to be transferred
The node is S, and the destination node is T, then s.nodepath=t.nodepath+t.id+ ' > ';
Check if S has sibling node, the NodeType of the parent node of no s is changed to F; check nodetype of T, such as F
Change D; Modify the descendants node Nodepath of s in the table. In general, the chances of this happening are low.
Description: My original intention is that the structure is mainly used in the class Resource Manager directory tree, according to the
NodeType, when drawing a tree, you don't have to search the database to determine if the node still has child nodes. As for
In the BBS post tree, like a post tree has a post the latest, it must be the whole post tree top
Situation, a practical solution: Add a field Rootid (Post's root post): Select Distanct
Rootid from Table order by postdate.
This data structure supports infinite-level trees, and the depth of the tree can be determined according to the actual situation Nodepath long
Degree. Finally, why I use the "best", mainly have the meaning of Jijiang, just hope to cause
The attention of the vast majority of experts, can put forward individual unique suggestions, in exchange for extensive discussion. The author himself must also
There is no portentous meaning.

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.