Method for quickly and normatively generating tree structure

Through the Java reflection mechanism and custom annotations, the tree structure is automatically built, and the problems of large development workload and irregular structure in the existing technology are solved, and the rapid and standardized tree structure generation is achieved, which improves the development efficiency and code unity.

CN120257939APending Publication Date: 2025-07-04GUANGXI ZHUANG AUTONOMOUS REGION INFORMATION CENT (GUANGXI ZHUANG AUTONOMOUS REGION BIG DATA RES INST)
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510171291.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-17
Publication Date
2025-07-04

AI Technical Summary

Technical Problem

When building tree structure data, the development workload is large, the generation speed is slow, and the structure is not standardized. Especially when multiple tables need to build tree structures, the structure cannot be unified, resulting in low development efficiency and ineffective code.

Method used

Using the Java reflection mechanism and custom annotations, we create GenerateTree interface, BaseTree objects and TreeSelf custom annotations, spanning tree structure tools, defining root node and child node judgment methods, and automatically building a tree structure using generics and reflection mechanisms to unify the data format.

Benefits of technology

Reduced development workload, improved development efficiency, unified tree structure data format, improved code robustness and reusability, and avoided structural differences caused by different developers.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120257939A_ABST
    Figure CN120257939A_ABST
Patent Text Reader

Abstract

The invention discloses a method for quickly and normatively generating a tree structure, which belongs to the technical field of computers, and comprises the following steps: S1, creating a tree structure generation tool, and S2, creating a target object, and calling the tree structure generation tool to process an input array object so as to generate the tree structure. Wherein the step S1 comprises the steps of creating a Generate Tree interface object, creating a BaseTree object, creating a TreeSelf custom annotation and creating a tree generation tool object. A root node judgment method and a child node judgment method are defined in the Generate Tree interface object, and a method for judging whether an incoming object realizes the Generate Tree interface or not, a method for acquiring root node data and a method for acquiring child nodes according to father node data are defined in the tree generation tool object. The method can reduce the development workload, improve the development efficiency, improve the code robustness, and standardize the specific structure of the same system spanning tree structure data.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of computer technology, and particularly relates to a method for quickly and standardly generating a tree structure. Background Art

[0002] A tree structure is a hierarchical data structure, suitable for representing data sets with hierarchical relationships or requiring fast search operations. Most systems on the market use tree structure data, such as functions like file directories, organizational structures, and comments. Currently, there are mainly the following two methods for constructing tree structure data:

[0003] First, using the lazy loading method, first query the root node, and then query the child nodes through the root, repeating this process until the leaf nodes are queried. The advantage of this approach is that when the data volume is large, it can save the memory resources consumed by constructing the tree structure and quickly display the data to the user. The disadvantage is that when viewing the child nodes, it is necessary to query the database through the background, consuming database connections and network access resources, and it is suitable for situations with a large data volume.

[0004] Second, retrieve all the relevant data and then construct the tree structure data according to the rules, mainly including the following steps: creating a target object, obtaining all the data from the database, defining a tree structure object, and writing the logic for creating the tree. This method is suitable for use when the data volume is small.

[0005] Regarding the second method, when there are multiple tables in the system that need to construct tree structures, it is necessary to repeatedly write the code for constructing the tree, resulting in a large development workload, a slow speed for generating the tree structure, and at the same time, due to different table fields and developers, the finally constructed tree structures may vary and cannot be unified, that is, the tree structure cannot be generated standardly.

[0006] For example, if a system needs to construct multiple tree structure objects, such as Chinese province information, fruit variety information, musical instrument type information, etc. If different developers are involved in the development, the following problems may occur:

[0007] 1. The defined tree structure objects may be inconsistent. For example, when developer A who develops Chinese province information defines the tree structure object, the displayed field is set to cityName, while when developer B who develops fruit variety information defines the tree structure object, the displayed field is set to fruitName, then the finally generated tree structure objects will be inconsistent. The differences in the tree structure objects will lead to the following problems: First, front-end personnel need to rewrite the components adapted to the new data structure due to the different structures. Second, if the interface needs to be docked with other systems, the docking party needs to write the receiving logic according to different data structures, both of which will increase the development workload and result in low development efficiency.

[0008] 2. The logic for creating a tree structure cannot be reused and needs to be rewritten. For the same reason as above, since the target objects are different and the fields in the objects are different, although the logic for generating the tree is the same, the actual target objects called are different, resulting in the inability to reuse the method for creating the tree. For example, the method for obtaining the display value in one object is getCityName, and in another it is getFruitName. Since they are different, they cannot be reused. If they are defined as the same method, it will lead to problems with non-standard object fields. For instance, if the city name and fruit name are both uniformly named name, it needs to be rewritten, resulting in repetitive work.

[0009] To solve the above problems, the present invention proposes a method for quickly and standardly generating a tree structure. Summary of the Invention

[0010] The present invention provides a method for quickly and standardly generating a tree structure, which can reduce the development workload, improve the development efficiency, enhance the robustness of the code, and at the same time standardize the specific structure of the tree structure data generated in the same system, so as to solve the problems of large development workload and non-standard tree structure generation caused by retrieving all data and constructing tree structure data according to rules in the prior art.

[0011] To achieve the above object, the technical solution adopted by the present invention is:

[0012] A method for quickly and standardly generating a tree structure, comprising the following steps:

[0013] S1: Create a tree structure generation tool, which specifically includes the following steps:

[0014] S11: Create a GenerateTree interface object in the Java language environment. Method 1 and Method 2 are defined within the GenerateTree interface object. Method 1 is the root node judgment method, and Method 2 is the child node judgment method;

[0015] S12: Create a BaseTree object for storing tree data and defining the tree structure;

[0016] S13: Create a TreeSelf custom annotation. An enumeration object is defined within the TreeSelf custom annotation, and the

[0017] attributes of the enumeration object include key and text;

[0018] S14: Create a tree generation tool object. The tree generation tool object includes Method 3, Method 4, and Method 5. Method 3 is the method for judging whether the passed-in object implements the GenerateTree interface, Method 4 is the method for obtaining the root node data, and Method 5 is the method for obtaining the child nodes based on the parent node data.

[0019] S2: Create a target object as a data carrier and pass it into an array object.

[0020] S3: Call the tree structure generation tool created in step S1 to process the array object of the target object created in step S2, and then generate a tree structure, which specifically includes the following steps:

[0021] S31: Pass in the array object of the target object.

[0022] S32: Call the third method to determine whether the passed-in array object implements the GenerateTree interface object. If

[0023] yes, execute the next step; if not, then end the execution.

[0024] S33: Loop through the array object, call the first method to obtain the root node of the array object, and use the root node as the parent node.

[0025] S34: Call the fourth method to obtain the attributes of the specific target object according to the Java reflection mechanism, loop through the attributes of the object, and determine whether there is a TreeSelf custom annotation in the attributes. If so, determine whether the enumeration attribute in the TreeSelf custom annotation is key or text, and obtain the reader method of the key attribute and the reader method of the text attribute through reflection.

[0026] S35: Call the fifth method and pass in the parent node, the array object, the reader method of the key attribute, and the reader method of the text attribute.

[0027] S36: Loop through the array object, call the second method to obtain all the child nodes of the current parent node, and store them in the child node array.

[0028] S37: Determine whether there are child nodes. If so, execute the next step; if not, return the BaseTree object and

[0029] end the execution.

[0030] S38: Loop through the child node array, obtain the values of key and text through the reader method of the key attribute and the reader method of the text attribute, and assign them to the specific BaseTree object. Finally, generate an array of BaseTree objects and assign the array of BaseTree objects to the parent node. In the loop, recursively call the fifth method. After the recursion ends, the construction of the tree structure can be completed.

[0031] Furthermore, in step S11, using the properties of Java language generics, create the GenerateTree interface object, define the first method and the second method, and pass in generic types as parameters.

[0032] Further, in step S12, the tree structure defined by the BaseTree object can be modified according to different system requirements.

[0033] Further, in step S13, create a TreeSelf custom annotation to identify the property key and property text used by the target object.

[0034] Further, in step S32, the third method first determines whether the passed-in array object is empty, and then uses the Java keyword instanceof to determine whether the data object in the passed-in array object implements the GenerateTree interface object. If so, execute the fourth method; otherwise, return an empty object.

[0035] Further, in step S35, the passed-in attributes also include the full name of the parent node.

[0036] Further, in step S38, in the loop, use the current loop's child node as the parent node and recursively call the fifth method.

[0037] Due to the above technical solutions, the present invention has the following beneficial effects:

[0038] 1. The present invention generates tree structure data based on the Java reflection mechanism and custom annotations. Developers only need to consider writing methods for judging child nodes and root nodes, adding annotations to the required fields, and the steps of generating and constructing the tree are uniformly generated by the tool. This can reduce the development workload, improve development efficiency, and unify the data structure.

[0039] 2. The present invention uniformly defines the tree structure object, and the overall structure will not vary due to different developers. In addition, the logic for creating the tree can be reused. In the case of different fields in the target object, the same set of logic for creating the tree can be used. Developers only need to implement the defined interface, judge the root node and child nodes according to their own needs, and use custom annotations to mark the text and ID to be displayed, then they can create tree structure data through the method. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] Figure 1 is a flowchart of the tool for generating the tree structure in the present invention;

[0041] Figure 2 is a flowchart of generating the tree structure by the tree structure generation tool in the present invention; DETAILED DESCRIPTION OF THE EMBODIMENTS

[0042] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.

[0043] A method for quickly and regularly generating a tree structure includes steps S1 and S2.

[0044] As Figure 1 shown, step S1 is to create a tree structure generation tool, which specifically includes steps S11 - S14.

[0045] S11: Create a GenerateTree interface object in the java language environment. Method 1 and Method 2 are defined within the GenerateTree interface object. Method 1 is a root node judgment method, and Method 2 is a child node judgment method. Specifically, by using the properties of java language generics, the GenerateTree interface object is created, Method 1 and Method 2 are defined, and the input parameters are generics. These two methods are not specifically implemented and need to be implemented by developers according to their own needs. The purpose of doing this is to improve the flexibility of program development and not be restricted to a certain implementation method.

[0046] Examples of the specific implementation of the root node judgment method:

[0047] Example 1: When the user creates root node data, the root node ID (unique identifier) can be defined as a constant. For example, if the root node ID is set to 0000001, then to judge the root node, it only needs to judge whether the ID of the current data is 0000001;

[0048] Example 2: When the data uses the form of recording the parent ID to express the parent - child relationship, it can be judged whether the current data is the root node by judging whether the parent ID is empty. If the attribute value of the parent ID is empty, then the data is the root node.

[0049] Examples of the specific implementation of the child node judgment method:

[0050] Example 1: When the data uses the form of recording the parent ID to express the parent - child relationship, it can be judged whether the parent ID of the current object is equal to the ID of the input object. If they are equal, then the current object is the child node of the input object.

[0051] Example 2: When the data uses the forms of 0100, 0101, 0102 to represent the parent - child relationship, it can be judged whether the current object is the child node of the input object by intercepting the string.

[0052] S12: Create a BaseTree object to store tree data and define the tree structure, unifying the data format when generating the tree. The tree structure defined by the BaseTree object can be modified according to different system requirements. As shown in Table 1, it mainly defines some common attributes.

[0053]

[0054] Table 1

[0055] S13: Create a TreeSelf custom annotation. An enumeration object is defined within the TreeSelf custom annotation, and the attributes of the enumeration object include key and text. The TreeSelf custom annotation is created to identify the property key and property text used by the target object. Specifically, it is used to identify the key to be distinguished and the text to be displayed in the target object.

[0056] S14: Create a tree generation tool object. The tree generation tool object includes Method Three, Method Four, and Method Five. Method Three is a method for determining whether the passed-in object implements the GenerateTree interface. Method Four is a method for obtaining the root node data. Method Five is a method for obtaining child nodes based on the parent node data.

[0057] S2: Create a target object as a data carrier and pass in an array object.

[0058] As Figure 2 shown, step S3 is to call the tree structure generation tool created in step S1 to process the array object of the target object created in step S2, thereby generating a tree structure, specifically including steps S31 - S38.

[0059] S31: Pass in the array object of the target object.

[0060] S32: Call Method Three to determine whether the passed-in array object implements the GenerateTree interface object. If so, execute the next step; if not, end the execution. Specifically, Method Three first determines whether the passed-in array object is empty, and then uses the Java keyword instanceof to determine whether the data object in the passed-in array object implements the GenerateTree interface object. If so, execute Method Four; if not, return an empty object.

[0061] S33: Loop through the array object, call Method One to obtain the root node of the array object, and use the root node as the parent node.

[0062] S34: Invoke the fourth method, obtain the attributes of the specific target object according to the Java reflection mechanism, loop through the attributes of the object, and determine whether there is a TreeSelf custom annotation in the attributes. If so, determine whether the enumerated attribute in the TreeSelf custom annotation is key or text, and obtain the reader method for the key attribute and the reader method for the text attribute through reflection.

[0063] S35: Invoke the fifth method, and pass in the parent node, the array object, the reader method for the key attribute, the reader method for the text attribute, and the full name of the parent node.

[0064] S36: Loop through the array object, invoke the second method to obtain all the child nodes of the current parent node, and store them in the child node array.

[0065] S37: Determine whether there are child nodes. If so, execute the next step. If not, return the BaseTree object and end the execution.

[0066] S38: Loop through the child node array, obtain the values of key and text through the reader method for the key attribute and the reader method for the text attribute, and assign them to the specific BaseTree object. Finally, generate an array of BaseTree objects, assign the array of BaseTree objects to the parent node; in the loop, use the current looped child node as the parent node, recursively call method five, and after the recursion ends, the construction of the tree structure can be completed.

[0067] The present invention verifies the feasibility of the above method for quickly and standardly generating a tree structure through an embodiment. Among them, the specific invocations of the first method, the second method, the third method, the fourth method, and the fifth method are the key points of the present invention. The following discloses some software codes to better illustrate the technical solution of the present invention.

[0068] Part of the code implemented by the third method is as follows:

[0069]

[0070]

[0071] Part of the code implemented by the fourth method is as follows:

[0072]

[0073]

[0074]

[0075]

[0076] The partial code implemented by Method Five is as follows:

[0077]

[0078]

[0079]

[0080] The present invention generates tree structure data according to the Java reflection mechanism and custom annotations. Developers only need to consider writing methods for judging child nodes and root nodes, add annotations to the required fields, and the steps of generating and constructing the tree are uniformly generated by the tool. This can reduce the development workload, improve development efficiency, and unify the data structure.

[0081] The present invention defines the tree structure object uniformly, and the overall structure will not vary due to different developers. In addition, the logic for creating the tree can be reused. In the case where the fields of the target object are different, the same set of logic for creating the tree can be used. Developers only need to implement the defined interfaces, implement the judgment of the root node and child nodes according to their own needs, and use custom annotations to mark the text and ID to be displayed, then they can create tree structure data through the method. The reason why the same method for creating the tree structure can be used for different fields is that Java can obtain the characteristics of the runtime class, can obtain the fields and methods in the current object, obtain the custom annotations on the fields through the fields, and judge the fields of the key and text to be displayed.

[0082] The above description is a detailed description of the preferred feasible embodiments of the present invention, but the embodiments are not used to limit the scope of the patent application of the present invention. Any equivalent changes or modifications made under the technical spirit disclosed by the present invention shall fall within the scope of the patent covered by the present invention.

Claims

1. A method for quickly and standardly generating a tree structure, characterized in that, It includes the following steps: S1: Create a tree structure generation tool, which specifically includes the following steps: S11: Create a GenerateTree interface object in the Java language environment. Method 1 and Method 2 are defined within the GenerateTree interface object. Method 1 is the root node judgment method, and Method 2 is the child node judgment method; S12: Create a BaseTree object for storing tree data and defining the tree structure; S13: Create a TreeSelf custom annotation. An enumeration object is defined within the TreeSelf custom annotation, and the attributes of the enumeration object include key and text; S14: Create a tree generation tool object. The tree generation tool object includes Method 3, Method 4, and Method 5. Method 3 is the method for judging whether the passed-in object implements the GenerateTree interface. Method 4 is the method for obtaining the root node data. Method 5 is the method for obtaining child nodes based on the parent node data; S2: Create a target object as a data carrier and pass in an array object; S3: Call the tree structure generation tool created in step S1 to process the array object of the target object created in step S2, thereby generating a tree structure, which specifically includes the following steps: S31: Pass in the array object of the target object; S32: Call Method 3 to judge whether the passed-in array object implements the GenerateTree interface object. If so, execute the next step; if not, end the execution; S33: Loop through the array object, call Method 1 to obtain the root node of the array object, and use the root node as the parent node; S34: Call Method 4 to obtain the attributes of the specific target object according to the Java reflection mechanism, and loop through the attributes of the object to judge whether there is a TreeSelf custom annotation in the attributes. If so, judge whether the enumeration attribute in the TreeSelf custom annotation is key or text, and obtain the reader method for the key attribute and the reader method for the text attribute through reflection; S35: Call Method 5, passing in the parent node, the array object, the reader method for the key attribute, and the reader method for the text attribute; S36: Loop through the array object, call Method 2 to obtain all child nodes of the current parent node, and store them in the child node array; S37: Judge whether there are child nodes. If so, execute the next step; if not, return the BaseTree object and end the execution; S38: Loop through the child node array, obtain the values of key and text through the reader method for the key attribute and the reader method for the text attribute, and assign them to the specific BaseTree object. Finally, generate an array of BaseTree objects and assign the array of BaseTree objects to the parent node. During the loop, recursively call Method 5. After the recursion ends, the construction of the tree structure can be completed.

2. A method for quickly and standardly generating a tree structure according to claim 1, characterized in that: In step S11, use the properties of Java language generics to create the GenerateTree interface object, define Method 1 and Method 2, and the passed-in parameters are generics.

3. A method for quickly and standardly generating a tree structure according to claim 1, characterized in that: In step S12, the tree structure defined by the BaseTree object can be modified according to different system requirements.

4. A method for quickly and standardly generating a tree structure according to claim 1, characterized in that: In step S13, create a TreeSelf custom annotation to identify the property key and property text used by the target object.

5. A method for quickly and standardly generating a tree structure according to claim 1, characterized in that: In step S32, in Method 3, first judge whether the incoming array object is empty, and then use the Java keyword instanceof to judge whether the data object in the incoming array object implements the GenerateTree interface object. If so, execute Method 4; if not, return an empty object.

6. A method for quickly and standardly generating a tree structure according to claim 1, characterized in that: In step S35, the incoming properties also include the full name of the parent node.

7. A method for quickly and standardly generating a tree structure according to claim 1, characterized in that: In step S38, in the loop, use the current loop's child node as the parent node and recursively call Method 5.