A tree encoding method and system

By using an alphabetical and numerical encoding structure and reflection-based function calls, the high complexity problem in tree structures is solved, achieving high efficiency in child node queries and node operations with O(n) time complexity, reducing algorithm complexity and improving database persistence efficiency.

CN115994141BActive Publication Date: 2026-04-14浪潮智慧城市科技有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
浪潮智慧城市科技有限公司
Filing Date
2022-12-06
Publication Date
2026-04-14

AI Technical Summary

Technical Problem

Existing methods for connecting nodes in tree structures have high time complexity and involve a lot of redundant computation when querying child nodes using pointer structures. Furthermore, when it is not necessary to obtain the hierarchical relationship of child nodes, a recursive method is required, which leads to excessively high algorithm complexity.

Method used

It adopts an alphanumeric encoding structure, inherits the parent node's encoding as the prefix of the child node's encoding, and generates encoding relationships by calling the encoding function through reflection, thereby reducing the complexity of tree structure queries. It uses generic classes for calling to achieve a query with O(n) time complexity.

Benefits of technology

This algorithm queries all child nodes in a tree structure in O(n) time complexity, reducing the impact of node deletion and addition on other nodes, lowering algorithm complexity, and improving database persistence efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115994141B_ABST
    Figure CN115994141B_ABST
Patent Text Reader

Abstract

This invention discloses a tree-based encoding method and system, belonging to the field of data structure representation technology. The method defines the encoding structure using letters and numbers; it binds encoding relationships by inheriting the parent node's encoding as a prefix for the child node's encoding, and concatenates this prefix with the child node's unique node to form the complete child node encoding; when generating encodings for added nodes, according to the encoding generation logic, it first determines the parent node and then the sibling nodes, and generates encodings in the order of the largest sibling node by sorting the sibling nodes; based on reflection, it calls the encoding function, establishes an Entity base class with necessary attributes and a rule class with generics, and implements the Class class through reflection. <iservice>The initialization of the tree structure. The application reduces the algorithm complexity of the tree structure inquiring all child nodes, and does not need to modify other nodes when adding and deleting, and guarantees the persistence of the database.< / iservice>
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data structure representation technology, specifically a tree-based encoding method and system. Background Technology

[0002] Existing methods for connecting nodes in tree structures use pointers, such as parent representation, child representation, and child-sibling representation. Parent representation adds parent node information to each node, suitable for finding the parent node of the current node; child representation adds pointers to each node, pointing sequentially to all child nodes, suitable for finding the child nodes of the current node; child-sibling representation adds pointers to each node, pointing to the first sibling node on the right, suitable for finding either child or sibling nodes.

[0003] Existing technologies use pointers as a data structure to query information about lower-level child nodes. However, to find all child nodes, a recursive method is required, which has high time complexity and involves excessive redundant calculations when the hierarchical relationship of child nodes is not needed. Summary of the Invention

[0004] The technical objective of this invention is to address the above-mentioned shortcomings by providing a tree-based encoding method and system that can reduce the algorithmic complexity of querying all child nodes in a tree structure, eliminate the need for recursion, and ensure database persistence by eliminating the need to modify other nodes during addition and deletion operations.

[0005] The technical solution adopted by this invention to solve its technical problem is:

[0006] A tree-based encoding method is proposed, in which the encoding structure is defined using a combination of letters and numbers; the encoding relationship is bound by using the inheritance of the parent node's encoding as a prefix for the child node's encoding, and this prefix is ​​concatenated with the child node's exclusive node to form the complete encoding of the child node;

[0007] When generating codes for added nodes, the encoding logic is followed by first determining the parent node and then the sibling nodes. The sibling nodes are sorted and the encoding order is generated in the order of the largest sibling node.

[0008] Based on reflection-based invocation of encoding functions, an Entity base class with necessary attributes and a CodeRule class with generics are established, and the Class is implemented through reflection. <iservice>For initialization, any class that needs to generate an encoding only needs to inherit the rule class, pass in the generic real class, and directly call the get encoding method to obtain the encoding.

[0009] This method defines an encoding structure of letters and numbers; it defines the generation logic of how to generate child nodes from parent nodes, thereby realizing a tree structure representation and reducing the query complexity of tree structure data; in terms of specific call implementation, it defines a call implementation method that can be used by multiple types of the same scenario.

[0010] This encoding method defines the relationships between nodes in a tree structure. The root node's encoding is defined sequentially, and child nodes inherit the parent node's encoding as the starting point. These encodings are then concatenated. This allows finding all child nodes of a given node in a tree structure in O(n) time complexity. Furthermore, by using a simple tree structure, it reduces pointer operations on the linked list during node deletion and addition, thus lowering the algorithm's time and space complexity. The use of generics for the calling mechanism class allows for the use of similar entity types, providing excellent reusability. Additionally, the use of class reflection for invocation enhances its scalability.

[0011] Preferably, in the encoding structure, the encoding of each level node is represented using a combination of alphanumeric serial numbers:

[0012] If the sequence number length is set to 2, the root node can be represented as A01, A02...Z99. If a node has child nodes, the child node generates its own unique code based on its parent node and according to the same rules. The child node is then concatenated with the code of its parent node to generate the node code. That is, the child nodes of the root node A01 are represented as A01A01, A01A02...A01Z99.

[0013] In this way, hierarchical levels can be distinguished by letters, and all subordinate and sub-sub-sub-nodes of a given node can be found at once using SQL fuzzy queries...

[0014] Furthermore, when generating the code, the maximum number of nodes at each level is determined first, and the length of the sequence number is set based on this.

[0015] Furthermore, the encoding generation logic determines whether the added element is a root node.

[0016] If it is a root node, query all root nodes, sort them according to the encoding order, select the node with the largest sequence number ROOTNODEmax, and add 1 to the sequence number of ROOTNODEmax to generate the encoding for adding the node;

[0017] If it is not the root node, query all child nodes of the parent node of the node to be added, and sort these child nodes according to their codes. Similarly, select the node with the largest sequence number, NODEmax. If the exclusive code sequence number of NODEmax has not reached the maximum value (99, 999...), add 1 to it to generate the code for the added node. If the sequence number has reached the maximum value, get the smallest sequence number (01, 001...) and get the next letter of the exclusive code letter of that node. Concatenate the parent node code, the generated letter, and the smallest sequence number to generate the code for the added node.

[0018] Furthermore, the method of invoking the encoding function based on reflection firstly involves setting up a base class CodeBase in the Entity layer according to the development rules of MVC. This base class is required to contain at least two member variables: one is the encoding, and the other is the parent class ID. The database operation interface of this parent class is then established and proxied by the database operation component.

[0019] Next, create a class `CodeRule`, which retrieves the code using the `execute` method. The `execute` method must pass the parent node ID. The `CodeRule` class has two generic types: one is `R extends CodeBase`, and the other is `Mimplements IService`. <r>IService is the service layer interface provided by the database operation component;

[0020] Finally, classes that require code generation only need to inherit from CodeRule and clearly specify the generic concrete class.

[0021] Preferably, the database operation component is Mybatis Plus.

[0022] Furthermore, the specific implementation method of the reflection-based invocation of the encoding function is as follows:

[0023] First, set the member variable Class in CodeRule. <IService <r>>serviceClass uses reflection to getGenericSuperClass() to get the generic parent class, casts the generic parent class to ParameterizedType, and then uses getActualTypeArguments to get all the generic real classes. The second real class is then used to initialize serviceClass.

[0024] Then, using the obtained serviceClass, IService is created using reflection's newInstance() method. <r>object;

[0025] Finally, the `execute` method is executed to obtain the generated encoding. `execute` first checks if it is a root node; if so, it uses IService... <r>The object performs a database query and sorts the data using encoding, then generates an encoding according to the rules of the encoding generation logic; otherwise, it uses IService. <r>The object queries the child nodes whose parent node ID is the passed ID, sorts them according to the encoding, and then generates the encoding according to the rules of the encoding generation logic.

[0026] This invention also claims a tree-based coding system, comprising a coding structure module, a coding generation module, and a coding function call module.

[0027] The encoding structure module is used to set the encoding structure;

[0028] The encoding generation module generates the encoding for adding nodes based on the encoding generation logic and the encoding structure set by the encoding structure module;

[0029] The encoding function call module implements reflection-based invocation of encoding functions;

[0030] The system generates the encoding for adding nodes based on the tree-based encoding method described above.

[0031] The present invention also claims a tree coding device, comprising: at least one memory and at least one processor;

[0032] The at least one memory is used to store a machine-readable program;

[0033] The at least one processor is configured to invoke the machine-readable program to execute the tree encoding method described above.

[0034] The present invention also claims protection for a computer-readable medium storing computer instructions that, when executed by a processor, cause the processor to perform the tree encoding method described above.

[0035] Compared with the prior art, the tree coding method and system of the present invention have the following advantages:

[0036] By using encoding to record node relationships, the space and time complexity of tree structure search algorithms can be reduced, and the impact of adding or deleting nodes on other nodes can be minimized.

[0037] All descendant node data can be retrieved without using recursive queries, reducing the time complexity of the child node query algorithm;

[0038] In the original simple tree structure, there is no connection between nodes of the same level, and the parent node does not know that there are no child node data records. If the relationship between them is recorded, a linked list needs to be added to point to the sibling node or to the child node. However, using this encoding structure, only one data record of the parent node is needed, and the related node information can be found through the encoding structure, reducing space complexity.

[0039] In traditional linked list tree structures, since sibling nodes and child nodes are linked sequentially, deleting or adding a node requires modifying the linked list, increasing the workload of the algorithm. However, using this encoding method, adding a new node only records the parent node's information, without modifying other node information. When deleting a node, due to the special encoding method, other sibling nodes and the parent node remain unaffected. Attached Figure Description

[0040] Figure 1 This is a diagram illustrating the tree coding structure in the tree coding method provided in this embodiment of the invention;

[0041] Figure 2 This is a tree-based encoding generation logic diagram provided in an embodiment of the present invention. Detailed Implementation

[0042] This invention provides a tree-based encoding method. The encoding structure is defined using a combination of letters and numbers. The encoding relationship is bound by using the parent node's encoding as a prefix for the child node's encoding, and this prefix is ​​concatenated with the child node's unique node to form the complete encoding of the child node.

[0043] When generating codes for added nodes, the encoding logic is followed by first determining the parent node and then the sibling nodes. The sibling nodes are sorted and the encoding order is generated in the order of the largest sibling node.

[0044] Based on reflection-based invocation of encoding functions, an Entity base class with necessary attributes and a CodeRule class with generics are established, and the Class is implemented through reflection. <iservice>For initialization, any class that needs to generate an encoding only needs to inherit the rule class, pass in the generic real class, and directly call the get encoding method to obtain the encoding.

[0045] This method defines an encoding structure of letters and numbers; it defines the generation logic of how to generate child nodes from parent nodes, thereby realizing a tree structure representation and reducing the query complexity of tree structure data; in terms of specific call implementation, it defines a call implementation method that can be used by multiple types of the same scenario.

[0046] This method uses encoding to connect the relationships between nodes in a tree structure, reducing the algorithmic complexity of querying all child nodes. When a node is generated, a unique code is added to it. Using this code, all descendant nodes can be queried directly without recursion, further reducing algorithmic complexity. Furthermore, adding or deleting nodes does not require modification of other nodes, ensuring database persistence.

[0047] The implementation of this method consists of three parts: the encoding structure, the encoding generation logic, and the reflection-based invocation of the encoding function.

[0048] 1. Encoding structure:

[0049] Each level of node is encoded using a combination of alphanumeric serial numbers:

[0050] When the sequence number length is set to 2, the root node can be represented as A01, A02, ..., Z99. If any of these nodes has child nodes, the child node generates its own unique code based on its parent node, following the same rules. This child node is then concatenated with its parent node's code to generate the node code, i.e., the child node of the root node A01, represented as A01A01, A01A02, ..., A01Z99. Figure 1 As shown.

[0051] In this way, hierarchical levels can be distinguished by letters, and all subordinate and sub-sub-sub-nodes of a given node can be found at once using SQL fuzzy queries...

[0052] 2. Encoding generation logic:

[0053] When generating the code, first determine the maximum number of nodes at each level, and then set the length of the sequence number based on this.

[0054] Then, determine whether the added node is the root node.

[0055] If it is a root node, query all root nodes, sort them according to the encoding order, select the node with the largest sequence number ROOTNODEmax, and add 1 to the sequence number of ROOTNODEmax to generate the encoding for adding the node;

[0056] If it's not the root node, query all child nodes of the parent node to be added, and sort these child nodes according to their codes. Again, select the node with the largest sequence number, NODEmax. If the sequence number of NODEmax is not yet at its maximum value (99, 999...), add 1 to it to generate the code for the added node. If the sequence number has already reached its maximum, obtain the smallest sequence number (01, 001...), and then obtain the letter following the unique code letter of that node. Concatenate the parent node code, the generated letter, and the smallest sequence number to generate the code for the added node. The process is as follows... Figure 2 As shown.

[0057] Thus, the addition and deletion of sibling nodes have almost zero impact on other nodes, reducing the complexity of the algorithm.

[0058] 3. Using reflection to call the encoding function:

[0059] To facilitate the use of the code generation method by all Java classes with the same requirements, two classes need to be set up in advance.

[0060] First, according to the MVC development rules, a base class CodeBase is set in the Entity layer. This base class is required to contain at least two member variables: one is the code, and the other is the parent class ID. Currently, the mainstream component for database operations in China is Mybatis Plus. Therefore, a Mapper interface for this parent class needs to be created and proxied by the Mybatis Plus component.

[0061] Next, create a class `CodeRule`, which retrieves the code using the `execute` method. The `execute` method must pass the parent node ID. The `CodeRule` class has two generic types: one is `R extends CodeBase`, and the other is `Mimplements IService`. <r>IService is the service layer interface provided by Mybatis Plus;

[0062] Finally, classes that require code generation only need to inherit from CodeRule and clearly specify the generic concrete class.

[0063] Specifically:

[0064] First, set the member variable Class in CodeRule. <IService <r>>serviceClass uses reflection to getGenericSuperClass() to get the generic parent class, casts the generic parent class to ParameterizedType, and then uses getActualTypeArguments to get all the generic real classes. The second real class is then used to initialize serviceClass.

[0065] Then, using the obtained serviceClass, IService is created using reflection's newInstance() method. <r>object;

[0066] Finally, the `execute` method is executed to obtain the generated encoding. `execute` first checks if it is a root node; if so, it uses IService... <r>The object performs a database query and sorts the data using encoding, then generates an encoding according to the rules of the encoding generation logic; otherwise, it uses IService. <r>The object queries the child nodes whose parent node ID is the passed ID, sorts them according to the encoding, and then generates the encoding according to the rules of the encoding generation logic.

[0067] This encoding method defines the relationships between nodes in a tree structure. The root node's encoding is defined sequentially, and child nodes inherit the parent node's encoding as the starting point. These encodings are then concatenated. This allows finding all child nodes of a given node in a tree structure in O(n) time complexity. Furthermore, by using a simple tree structure, it reduces pointer operations on the linked list during node deletion and addition, thus lowering the algorithm's time and space complexity. The use of generics for the calling mechanism class allows for the use of similar entity types, providing excellent reusability. Additionally, the use of class reflection for invocation enhances its scalability.

[0068] This invention also provides a tree-based coding system, including a coding structure module, a coding generation module, and a coding function call module.

[0069] The encoding structure module is used to set the encoding structure; it defines the encoding using a combination of letters and numbers; it binds the encoding relationship by using the parent node's encoding as a prefix for the child node's encoding, and then concatenates this prefix with the child node's exclusive node to form the complete encoding of the child node.

[0070] The encoding generation module generates the encoding for the added node based on the encoding generation logic and the encoding structure set by the encoding structure module. When generating the encoding for the added node, the parent node is determined first and then the sibling node is determined according to the encoding generation logic. The sibling nodes are sorted and the encoding order is generated following the largest sibling node.

[0071] Determine if the added node is the root node.

[0072] If it is a root node, query all root nodes, sort them according to the encoding order, select the node with the largest sequence number ROOTNODEmax, and add 1 to the sequence number of ROOTNODEmax to generate the encoding for adding the node;

[0073] If it is not the root node, query all child nodes of the parent node of the node to be added, and sort these child nodes according to their codes. Similarly, select the node with the largest sequence number, NODEmax. If the exclusive code sequence number of NODEmax has not reached the maximum value (99, 999...), add 1 to it to generate the code for the added node. If the sequence number has reached the maximum value, get the smallest sequence number (01, 001...) and get the next letter of the exclusive code letter of that node. Concatenate the parent node code, the generated letter, and the smallest sequence number to generate the code for the added node.

[0074] The encoding function call module implements reflection-based invocation of encoding functions; it establishes an Entity base class with necessary attributes and a generic rule (CodeRule) class, and implements the Class through reflection. <iservice>For initialization, any class that needs to generate an encoding only needs to inherit the rule class, pass in the generic real class, and directly call the get encoding method to obtain the encoding.

[0075] The system generates the code for adding nodes based on the tree coding method described in the above embodiments.

[0076] This invention also provides a tree coding apparatus, comprising: at least one memory and at least one processor;

[0077] The at least one memory is used to store a machine-readable program;

[0078] The at least one processor is configured to invoke the machine-readable program to execute the tree encoding method described above.

[0079] This invention also provides a computer-readable medium storing computer instructions, which, when executed by a processor, cause the processor to perform the tree encoding method described in the above embodiments. Specifically, a system or apparatus equipped with a storage medium storing software program code that implements the functions of any of the above embodiments can be provided, and the computer (or CPU or MPU) of the system or apparatus can read and execute the program code stored in the storage medium.

[0080] In this case, the program code read from the storage medium can itself implement the function of any of the above embodiments, and therefore the program code and the storage medium storing the program code constitute part of the present invention.

[0081] Examples of storage media used to provide program code include floppy disks, hard disks, magneto-optical disks, optical disks (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RAM, DVD-RW, DVD+RW), magnetic tapes, non-volatile memory cards, and ROMs. Alternatively, program code can be downloaded from a server computer via a communication network.

[0082] Furthermore, it should be clear that not only can the program code read by the computer be executed, but also the operating system or other components operating on the computer can be instructed based on the program code to perform some or all of the actual operations, thereby realizing the function of any of the embodiments described above.

[0083] Furthermore, it is understood that the program code read from the storage medium is written to the memory set in the expansion board inserted into the computer or to the memory set in the expansion unit connected to the computer. Then, based on the instructions of the program code, the CPU or other components installed on the expansion board or expansion unit execute some and all of the actual operations, thereby realizing the function of any of the embodiments described above.

[0084] The present invention has been shown and described in detail above with reference to the accompanying drawings and preferred embodiments. However, the present invention is not limited to these disclosed embodiments. Based on the above embodiments, those skilled in the art will know that more embodiments of the present invention can be obtained by combining the code review methods in the different embodiments. These embodiments are also within the protection scope of the present invention.< / iservice> < / r> < / r> < / r> < / r> < / r> < / iservice> < / r> < / r> < / r> < / r> < / r> < / iservice>

Claims

1. A tree-based coding method, characterized in that, The encoding structure uses a combination of letters and numbers for encoding definition; the encoding relationship is bound by using the parent node's encoding as a prefix for the child node's encoding, and this prefix is ​​concatenated with the child node's exclusive node to form the complete encoding of the child node. When generating codes for added nodes, the encoding logic is followed by first determining the parent node and then the sibling nodes. The sibling nodes are sorted and the encoding order is generated in the order of the largest sibling node. Based on reflection-based function invocation, an Entity base class with necessary attributes and a rule class with generics are established, and the Class is implemented through reflection. <iservice> For initialization, any class that needs to generate an encoding only needs to inherit the rule class and pass in the generic real class, and directly call the get encoding method to get the encoding;< / iservice> The encoding generation logic determines whether the added node is the root node. If it is a root node, query all root nodes, sort them according to the encoding order, select the node with the largest sequence number ROOTNODEmax, and add 1 to the sequence number of ROOTNODEmax to generate the encoding for adding the node; If it is not the root node, query all child nodes of the parent node of the node to be added, and sort these child nodes according to their codes. Similarly, select the node with the largest sequence number, NODEmax. If the unique code sequence number of NODEmax has not reached the maximum value, add 1 to it to generate the code for the added node; if the sequence number has reached the maximum value, get the minimum sequence number, and get the next letter of the unique code letter of that node. Concatenate the parent node code, the generated letter, and the minimum sequence number to generate the code for the added node. The method of invoking the encoding function based on reflection firstly involves setting a base class CodeBase in the Entity layer according to the MVC development rules. This base class is required to contain at least two member variables: one for encoding and one for the parent class ID. Then, a database operation interface for this parent class is established and proxied by the database operation component. Next, create a class `CodeRule`, which retrieves the code using the `execute` method. The `execute` method must pass the parent node ID. The `CodeRule` class has two generic types: one is `R extends CodeBase`, and the other is `Mimplements IService`. <r> IService is the service layer interface provided by the database operation component;< / r> Finally, classes that require code generation only need to inherit from CodeRule and clearly specify the generic concrete class.

2. The tree coding method according to claim 1, characterized in that, In the aforementioned encoding structure, the encoding of each level of node is represented using a combination of alphanumeric serial numbers: If a node has child nodes, the child node generates its own unique code based on its parent node, following the same rules, and then concatenates the child node's code with its parent node's code to generate the node code.

3. A tree-based coding method according to claim 1 or 2, characterized in that, When generating the code, first determine the maximum number of nodes at each level, and then set the length of the sequence number based on this.

4. The tree coding method according to claim 1, characterized in that, The database operation component is Mybatis Plus.

5. A tree-based coding method according to claim 1 or 4, characterized in that, The specific implementation method of invoking the encoding function based on reflection is as follows: First, set the member variable Class in CodeRule. <IService <r> > serviceClass obtains the generic parent class through reflection mechanism getGenericSuperClass(), casts the generic parent class to type ParameterizedType, then obtains all generic real classes through getActualTypeArguments, and takes the second real class to initialize serviceClass;< / r> Then, using the obtained serviceClass, IService is created using reflection's newInstance() method. <r> object;< / r> Finally, the `execute` method is executed to obtain the generated encoding. `execute` first checks if it is a root node; if so, it uses IService... <r>The object performs a database query and sorts the data using encoding, then generates an encoding according to the rules of the encoding generation logic; otherwise, it uses IService. <r>The object queries the child nodes whose parent node ID is the passed ID, sorts them according to the encoding, and then generates the encoding according to the rules of the encoding generation logic.< / r> < / r> 6. A tree-based coding system, characterized in that, It includes an encoding structure module, an encoding generation module, and an encoding function call module. The encoding structure module is used to set the encoding structure; The encoding generation module generates the encoding for adding nodes based on the encoding generation logic and the encoding structure set by the encoding structure module; The encoding function call module implements reflection-based invocation of encoding functions; The system implements the encoding generation of added nodes using the tree encoding method according to any one of claims 1 to 5.

7. A tree-based coding device, characterized in that... include: At least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is configured to invoke the machine-readable program to execute the method according to any one of claims 1 to 5.

8. A computer-readable medium, characterized in that, The computer-readable medium stores computer instructions that, when executed by a processor, cause the processor to perform the method described in any one of claims 1 to 5.

Citation Information

Patent Citations

  • Node encoding method and device and method for determining node affiliation

    CN111353277A

  • Entity object coding method and device, electronic equipment and storage medium

    CN113609233A