New research on unlimited directory tree optimal algorithm

zhaozj2021-02-16  51

(First, statement: Because many people see the enthusiasm of this article, the author is slightly modified and supplemented in this article) This article describes: 1. Data structure 2 of the directory tree. Explore the query complexity of the database in the specific application.

Table Structure: ID (Number) PID (Parent Node) NodePath (Path) NodeType (Status) Example Data: 1 0> F 2 0> D 3 2 0> 2> D 4 3 0> 2> 3> F Test Project: 1. Get the parent node of the current node: complexity: o (n) 2. Get the subordinate child node of the current node: complexity: o (n) 3. Get all the children and grandchildren nodes in the current node: complexity: o (n) 4. Get all straight ancestors nodes in the current node: complexity: o (n) 5. Delete, it is easier to implement when new nodes, mainly to achieve a node transfer is a bit trouble: set by transfer

The node is s, the transferred destination node is T, then s.NodePath = T.NodePath T.ID '>';

Check if there is a brother node, the NodeType of the parent node of s is changed to f; check the Nodetype of t, such as F

It is changed to D; modify the children's node nodepath in the table. Generally, this situation has a low chance. Explanation: I original intention is to apply this structure in the directory tree of the class resource manager, according to

Nodetype, you don't have to search the database when you draw a tree to determine if the node has a child node. As for use

In BBS post tree, there is a post in the post tree, you must take the whole post to the top of this.

Situation, there is a practical solution: add a field rootid (the root posts of the post): SELECT DISTANCT

Rootid from table order by postdate. This data structure supports unlimited graded trees, with regard to the depth of the tree, can determine the length of NodePath according to the actual situation

degree. Finally, why use "optimal", mainly organized the meaning, throwing brick, jade, just hope to cause

The attention of the majority of masters can make personal unique suggestions and discuss in communication. Author I also

No night slang.

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

New Post(0)