So how do we add, update, and delete a node for such a structure? There are generally two ways to add a node:
Retains the original name and parent structure, adds data to the data in the old way, and then uses the Rebuild_tree function to renumber the entire structure once each additional piece of data is added.
A more efficient approach is to change all the values that are located to the right of the new node. For example: We want to add a new fruit "strawberry" (strawberry) it will become the last child node of the "Red" node. First we need to make room for it. The right value of "Red" should be changed from 6 to 8, and the value of "yellow 7-10" should be changed to 9-12. By analogy, we can see that if we want to make room for the new value, we need to add 2 to all nodes with a value greater than 5 (5 is the right value of the last child node of "Red"). So we do database operations like this:
UPDATE Tree SET rgt=rgt+2 WHERE rgt>5;
UPDATE Tree SET lft=lft+2 WHERE lft>5;
This frees up space for the newly inserted value, and now you can create a new data node in the vacated space, with a value of 6 and 7, respectively.
INSERT into the tree SET lft=6, rgt=7, name= ' strawberry ';
Check it again! What do you think? It's going to be quick.
OK, now you can design your multilevel database structure in two different ways, depending on your personal judgment, but I prefer the second method for a large number of hierarchies. The first method is easier if you have smaller queries but need to add and update data frequently.
In addition, if the database is supported, you can also write Rebuild_tree () and space-making operations into the database-side trigger functions, which are automatically executed at the time of insertion and update, so that you can get better operational efficiencies, and your SQL statements for adding new nodes will become simpler.
Recursive method of class recursion
Posted by visitor on, May 31-9:18am.
I wrote a procedure with the recursive method, which is not exactly the same as the recursion in the article.
Preparing to migrate to Xoops:
Http://dev.xoops.org/modules/xfmod/project/?ulink
Memory overflow has occurred
But I'm going to continue to use recursion, but we need to continue to improve
I'd like to have a chance to talk to you about CMS
»reply to this comment
Or the comparison of two methods
Posted by visitor on, March 17-8:30pm.
After a careful study of this article, feel the benefit is not shallow, but then think, think there is a problem (for good memory, adjacent directory mode I called recursive method, the pre-sorted traversal tree algorithm I called the method of the pre-ordered tree):
1, the difference between the two methods is that recursion is the time to use the stack in the query recursion, the pre-sorted tree is to update the node to half (refers to the second half of the inserted node) node update. Although you also said that if there are more nodes, update frequently, the efficiency of the pre-sorting tree will be reduced, the use of recursion will be better, and if the node hierarchy is more, first recursion will lead to stack overflow, and furthermore, the efficiency of recursion itself is not high, plus each level of recursion to operate the database, the overall effect will not be ideal. My current practice is to get all the data out at once, and then recursively manipulate the array, will be better; if you make further improvements, you can add a root root node for each row of records (currently recording only adjacent parent nodes), so it is more efficient to find the branch tree, and it is also very convenient to update the tree. It should be a better way.
2, improve the recursive way, in the article in the calculation of the pre-sorted tree node in the left and right time in fact also used a way of traversal, by replacing the stack with an array, the stack is manually implemented and ejected; This method can also improve the efficiency of recursion if it is referred to the recursive algorithm and an array is substituted for the stack when recursion is made.
3, concurrency, if the concurrency is taken into account, especially when the tree is updated, the method of large Area update node information in the pre-sorted tree needs additional attention to the use of lock and transaction mechanism to ensure data consistency.
4, multiple root nodes or multiple parent nodes, in this case, it is obviously not a standard two-fork tree or multiple fork tree, the pre-ordered tree algorithm needs to be improved to adapt to, and the recursive method is easy to apply, so in this case, the adaptability of the recursion is strong. This is of course, because the recursive method is a form of the list, trees, graphs can be expressed by the linked list, of course, adaptable.
5, intuitive, if not program operation, directly observe the data stored in the database, it is obvious that the recursive way of storing data is more intuitive, and the pre-sorted tree data is difficult to read directly (for hierarchical relations), this in the data exchange will have an impact?
Overall, I personally prefer to use the recursive method, but always worried about the effect of recursion on efficiency, fortunately has not touched the larger classification level, recursive array substitution stack is a better way to improve. The pre-sorted tree is an efficient way to solve simple trees, and it should be very good with custom, especially it is convenient to reverse lookup from leaf node to root node.
Fwolf
Www.fwolf.com
»reply to this comment
I'm very glad to see your reply.
Posted by Shuke on, March 18-5:47am.
I'm very glad that you have finished reading this article so seriously. This article is actually originally published in the sitepoint.com, I translated it, I hope to beginners to introduce some of the introduction of methods, a primer. Your method is also very good, have a chance I will try. (If you are interested, why not put your method and the Code of implementation into a tutorial on the above example, so that you can use a more practical example to imitate it. If you are interested in studying how to save a multilevel structure in a database, there are two connections that are good for reference:
The common 4 medium method is introduced.
One query, array sort scripts I think your script is definitely better than this.
In addition, I see you also use Drupal, it also has an advanced function called Distributed user authentication system, as long as any Drupal site registration can be logged in to visit other Drupal site. It's very interesting.
Good luck!
»reply to this comment
The cycle of achievement has been achieved.
Posted by visitor on, March 25-10:10pm.
I have already read the information you provided last time, but honestly, there aren't too many new things in the first article, maybe I didn't see it too well, the second one is actually written by PHP3, the program structure is not looked at, use too many functions to cross.
I'm in a system where the user role needs to be graded, according to the idea of the array of traversal written down, there is no time to organize, first put here you see it, the database is ADODB, the program is directly from the system, want to be able to describe clearly, mainly using the PHP powerful array operation, Use loops for recursion. The annotation is a similar method, but the timing of the processing of the results is different.