Data structure of n-level classification

Source: Internet
Author: User
1. Data Structure of N-level classification

2. Data Structure and page display of my tree

3. Permission applications

The Chinese New Year is coming soon. I would like to give you a new year and wish you a wealth of money in the New Year. Now, we need to advance everything we do, and we need to advance our New Year's greetings.

The structure of this tree was also sent once in csdn a few years ago. Now, we can see that the main structure has not changed much. After such a long time, we still feel very useful. On this basis, other functions are also linked, such as "General Permissions" and configuration information. Yes, the permission has a lot to do with this. However, this relationship is not a "tightly coupled" concept. What is the specific relationship? I will talk about it later, O (partition _ partition) o ....
Because I like to use databases, we will mainly describe databases here.

1. Basic N-level classification structure.

Tree itself is an N-level classification, so let's start with this. N-level classification, generally think of such a table structure

[Test_tree]

It would have been enough to have three fields (ID, title, parentid). However, if only these three fields are available, it may be a bit difficult to use, therefore, we will add some auxiliary fields on this basis. Strictly speaking, most of the added fields are "redundant" fields, but sometimes "redundant" is also necessary.

2. Add a "Number"

For example, you can add a "Number". For example, the "tree structure demonstration" is called "01 ", his first subnode "Basic N-level classification" is called "0101", and the second subnode "adds a 'number'" is called "0102 ". For example.

Advantages: The added "Number" can implement many functions,

A. [number length/2] indicates the "depth" of the node (that is, the level of the node ),

B. Remove the last two digits from the [number], indicating the parent node ID of the node. You can also create a loop to locate the root node.

C. Order by number. You can sort the entire tree, and the order and number structure is consistent.

[Effect of order by Code]

The combination of these can achieve an effect, that is, you can use an SQL statement to obtain a record set with indentation effect, you can use this record set to directly bind the dropdownlist control. This avoids recursion.

To facilitate calling, we can create a view, such as v_test_tree2

Select Top 100 percent noteid, code,
Replace (space (LEN (CODE)/2-1 ),'','')
+ Notetitle as notetitle
From DBO. test_tree
Order by code

 

Ideas:

Use the space function to obtain the corresponding space based on the code length. Because the space is halfwidth, The halfwidth space in the item in the drop-down list box cannot be displayed, therefore, you need to use the replace function to replace the half-width control with space to achieve the effect of the position.

The binding effect is as follows.

Tree Structure demonstrationBasic N-level classificationAdd a "Number"My treeFunction demonstrationNews managementEmployee management

[Demo of the drop-down list box]

 

Disadvantages:
A. There is a capacity limit. In the above example, each level can have only 99 nodes. If more nodes are used, it cannot be displayed. Although 99 is enough, what if there is an exception? You can increase the number of digits to three. But it is not easy to modify.

B. You cannot use the "Number" as the primary or foreign key for multi-Table Association. Because once the function structure (or organizational structure) changes, this "Number" also changes. If the primary key is used for association, that is a terrible thing.

 

We have made some improvements to these shortcomings. The improvement result is the structure of my tree.

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.