Method and system for storing N-way tree multi-channel label data under relational database

By building an N forktree data storage data table in a relational database and generating a unique tag sequence number for child nodes, the technical problem of storing N forktree multi-channel tag data is solved, efficient data storage and query are achieved, and system performance is improved.

CN119862179APending Publication Date: 2025-04-22SHANDONG LANGCHAO YUNTOU INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411870185.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-18
Publication Date
2025-04-22

AI Technical Summary

Technical Problem

In relational databases, how to efficiently store N forktree multi-tagged data to represent the hierarchy of the data set is a technical challenge.

Method used

By constructing an N forktree data storage data table, recording the parent node and child node data, and generating a unique tag sequence number for newly inserted child nodes, the recursive function is used to trace back to the root node of the tree to determine the insertion position and tag sequence number of the child node.

Benefits of technology

It realizes efficient storage of N forktree multi-channel tag data in relational databases, avoids data redundancy and inconsistency, and improves the performance and scalability of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119862179A_ABST
    Figure CN119862179A_ABST
Patent Text Reader

Abstract

The invention discloses a storage method and system for N-way tree multi-path label data under a relational database, belongs to the technical field of data storage, and aims to solve the technical problem of how to store the N-way tree multi-path label data to represent a hierarchical structure of a data set. Comprising the following steps that a data table is constructed, specifically, an N-way tree data storage data table is constructed, the data table is used for recording father node and child node data, and label serial numbers of nodes in all depth-first search results are consistent; storing a data node: for a newly inserted child node c, pre-informing a father node of the child node c as a node b, querying all father nodes and child nodes of the node b, determining an insertion position and a label serial number of the child node c according to a query result, defining two lists for storing node information of the father node and the child node corresponding to the node b, a recursive function is defined to be traced to a root node of the tree, and a unique label serial number is generated for a child node c;
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of data storage, and in particular to a method and system for storing N-ary tree multi-path label data in a relational database. Background Art

[0002] An N-ary tree is a tree data structure in which each node can have any number of child nodes. Unlike a binary tree, the nodes of an N-ary tree do not have fixed left and right child node restrictions, which makes it very suitable for representing data sets with complex hierarchical structures. A multi-label system allows each entity to have multiple labels, which can more comprehensively describe the entity.

[0003] How to store multi-way label data based on an N-ary tree to represent the hierarchical structure of a data set is a technical problem that needs to be solved. Summary of the invention

[0004] The technical task of the present invention is to address the above shortcomings and provide a storage method and system for N-ary tree multi-way label data in a relational database to solve the technical problem of how to store N-ary tree multi-way label data to represent the hierarchical structure of a data set.

[0005] In a first aspect, the present invention provides a method for storing multi-path label data in an N-ary tree in a relational database, characterized by comprising the following steps:

[0006] Build data table: Build an N-ary tree data storage data table, which is used to record parent node and child node data. The label sequence number of the node in each depth-first search result is consistent;

[0007] Storing data nodes: For the newly inserted child node c, inform in advance that its parent node is node b, query all parent nodes and child nodes of node b, determine the insertion position and label serial number of the child node c according to the query results, define two lists for storing the node information of the parent node and child node corresponding to the node b, define a recursive function to trace back to the root node of the tree, and generate a unique label serial number for the child node c.

[0008] Preferably, querying all parent nodes and child nodes of node b, and determining the insertion position and label sequence number of the child node c according to the query result, comprises the following steps:

[0009] For the newly inserted child node c, query all parent nodes and child nodes of its parent node b;

[0010] If node b has no parent node or child node, node c is directly inserted into the data table, and the label sequence numbers of node b and node c are consistent;

[0011] If node b has only one parent node, node c is directly inserted into the data table, and the label sequence numbers of nodes a, b, and c are consistent;

[0012] If node b has only child nodes, node c and node b's child nodes are at the same level, node b and node c are a route, node b and other child nodes have a route respectively, node b and node c are newly inserted, and the sequence numbers of node b and node c are different from the label sequence numbers of other route nodes;

[0013] Node b has a parent node and child nodes, node c and the child nodes of node b are at the same level, node b and node c are on the same route, node b and other child nodes each have a route, the parent node of node b and all higher-level nodes are queried out, the queried higher-level nodes and node b and node c are inserted, and the label serial numbers of the queried higher-level nodes and node b and node c are different from the label serial numbers of other route nodes.

[0014] Preferably, for the two lists defined, each list includes the following two attributes:

[0015] serialTag, indicating the tag serial number;

[0016] code, indicating the node code.

[0017] Preferably, a recursive function is defined to trace back to the root node of the tree, comprising the following steps:

[0018] Define an empty list parents to store all parent nodes;

[0019] Set the current node to the node to be queried;

[0020] When the current node is not empty, perform the following operations: add the parent node of the current node to the list parents list, and set the current node as its parent node;

[0021] The loop ends when the parent node of the current node is None or the root node is reached;

[0022] Returns the parents list, which is a list of all parent nodes.

[0023] Preferably, generating a unique label serial number for the child node c includes the following steps:

[0024] Define a variable serialTag;

[0025] Use an incremental counter or a database auto-increment field to ensure the uniqueness of the serialTag; each time a new node is inserted, the value of the serialTag increases to ensure that the serialTag of each node is unique.

[0026] In a second aspect, the present invention provides a storage system for N-ary tree multi-way label data in a relational database, which is used to implement multi-way label data storage by using a storage method for N-ary tree multi-way label data in a relational database as described in any one of the first aspects, and the system includes a data table construction module and a data node storage module;

[0027] The data table construction module is used to perform the following: construct an N-ary tree data storage data table, the data table is used to record parent node and child node data, and the label sequence number of the node in each depth-first search result is consistent;

[0028] The data node storage module is used to perform the following: for a newly inserted child node c, inform in advance that its parent node is node b, query all parent nodes and child nodes of node b, determine the insertion position and label serial number of the child node c based on the query result, define two lists for storing node information of the parent node and child node corresponding to the node b, define a recursive function to trace back to the root node of the tree, and generate a unique label serial number for the child node c.

[0029] Preferably, when querying all parent nodes and child nodes of node b and determining the insertion position and label sequence number of the child node c according to the query result, the data node storage module is used to perform the following:

[0030] For the newly inserted child node c, query all parent nodes and child nodes of its parent node b;

[0031] If node b has no parent node or child node, node c is directly inserted into the data table, and the label sequence numbers of node b and node c are consistent;

[0032] If node b has only one parent node, node c is directly inserted into the data table, and the label sequence numbers of nodes a, b, and c are consistent;

[0033] If node b has only child nodes, node c and node b's child nodes are at the same level, node b and node c are a route, node b and other child nodes have a route respectively, node b and node c are newly inserted, and the sequence numbers of node b and node c are different from the label sequence numbers of other route nodes;

[0034] Node b has a parent node and child nodes, node c and the child nodes of node b are at the same level, node b and node c are on the same route, node b and other child nodes each have a route, the parent node of node b and all higher-level nodes are queried out, the queried higher-level nodes and node b and node c are inserted, and the label serial numbers of the queried higher-level nodes and node b and node c are different from the label serial numbers of other route nodes.

[0035] Preferably, for the two lists defined, each list includes the following two attributes:

[0036] serialTag, indicating the tag serial number;

[0037] code, indicating the node code.

[0038] Preferably, the data node storage module is used to perform the following to define a recursive function tracing back to the root node of the tree:

[0039] Define an empty list parents to store all parent nodes;

[0040] Set the current node to the node to be queried;

[0041] When the current node is not empty, perform the following operations: add the parent node of the current node to the list parents list, and set the current node as its parent node;

[0042] The loop ends when the parent node of the current node is None or the root node is reached;

[0043] Returns the parents list, which is a list of all parent nodes.

[0044] Preferably, the data node storage module is used to perform the following steps to generate a unique label serial number for the child node c:

[0045] Define a variable serialTag;

[0046] Use an incremental counter or a database auto-increment field to ensure the uniqueness of the serialTag; each time a new node is inserted, the value of the serialTag increases to ensure that the serialTag of each node is unique.

[0047] The storage method and system of N-ary tree multi-path label data in a relational database of the present invention has the following advantages:

[0048] 1. Through reasonable table structure design, data redundancy can be avoided and data inconsistency can be reduced. For example, a many-to-many relationship table can be used to represent the relationship between entities and tags;

[0049] 2. Storing N-ary tree multi-way label data in a relational database can not only make full use of the powerful functions of the database, but also improve the performance and scalability of the system through reasonable data model design and index optimization. In addition, transaction processing, concurrency control, data security and backup functions also provide guarantees for the stable operation of the system. Choosing a suitable relational database and reasonably designing the data model can significantly improve the efficiency of data storage and query, thereby improving the performance of the entire system and user experience. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0051] The present invention is further described below in conjunction with the accompanying drawings.

[0052] Figure 1 This is a flowchart of a method for storing multi-path label data in an N-ary tree in a relational database according to Embodiment 1;

[0053] Figure 2 This is a flowchart of storing data nodes in a method for storing multi-way label data in an N-ary tree in a relational database according to Example 1. DETAILED DESCRIPTION

[0054] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments so that those skilled in the art can better understand the present invention and implement it. However, the embodiments are not intended to limit the present invention. In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments may be combined with each other.

[0055] The embodiment of the present invention provides a method and system for storing N-ary tree multi-way label data in a relational database, which is used to solve the technical problem of how to store N-ary tree multi-way label data to represent the hierarchical structure of a data set.

[0056] Embodiment 1:

[0057] The present invention discloses a method for storing N-ary tree multi-path label data in a relational database, comprising two steps of constructing a data table and storing data nodes.

[0058] Step S100 constructs a data table: constructs an N-ary tree data storage data table, the data table is used to record parent node and child node data, and the label sequence number of the node in each depth-first search result is consistent.

[0059] Step S100 stores data nodes: for the newly inserted child node c, inform in advance that its parent node is node b, query all parent nodes and child nodes of node b, determine the insertion position and label serial number of the child node c according to the query result, define two lists for storing the node information of the parent node and child node corresponding to the node b, define a recursive function to trace back to the root node of the tree, and generate a unique label serial number for the child node c.

[0060] As a specific implementation, querying all parent nodes and child nodes of node b, and determining the insertion position and label sequence number of the child node c according to the query result, includes the following steps:

[0061] (1) For the newly inserted child node c, query all parent nodes and child nodes of its parent node b;

[0062] (2) If node b has no parent node or child node, node c is directly inserted into the data table, and the label sequence numbers of node b and node c are consistent;

[0063] (3) If node b has only one parent node, node c is directly inserted into the data table, and the label sequence numbers of nodes a, b, and c are consistent;

[0064] (4) If node b has only one child node, node c and node b’s child nodes are at the same level, node b and node c are a route, node b and other child nodes each have a route, newly insert node b and node c, and the sequence numbers of node b and node c are different from the label sequence numbers of other route nodes;

[0065] (5) Node b has a parent node and a child node. Node c and the child node of node b are at the same level. Node b and node c are on the same route. Node b and other child nodes each have a route. The parent node of node b and all nodes at higher levels are queried out, and the queried higher-level nodes as well as node b and node c are inserted. The label serial numbers of the queried higher-level nodes as well as node b and node c are different from the label serial numbers of other route nodes.

[0066] As a specific implementation, for the two lists defined, each list includes the following two attributes, namely serialTag and code, serialTag represents the label serial number; code represents the node code.

[0067] As a specific implementation, a recursive function is defined to trace back to the root node of the tree, including the following steps:

[0068] (1) Define an empty list parents to store all parent nodes;

[0069] (2) Set the current node as the node to be queried;

[0070] (3) When the current node is not empty, perform the following operations: add the parent node of the current node to the parents list and set the current node as its parent node;

[0071] (4) When the parent node of the current node is None or the root node is reached, the loop ends;

[0072] (5) Returns the parents list, which is a list of all parent nodes.

[0073] As a specific implementation, generating a unique label serial number for child node c includes the following steps:

[0074] (1) Define a variable serialTag;

[0075] (2) Use an incremental counter or a database auto-increment field to ensure the uniqueness of the serialTag;

[0076] (3) Each time a new node is inserted, the value of serialTag increases, ensuring that the serialTag of each node is unique.

[0077] The method disclosed in this embodiment can represent any number of child nodes, thereby better adapting to various hierarchical relationships in the real world. The multi-label system allows the same data item to be classified into multiple different categories, enhancing the flexibility of data organization.

[0078] Embodiment 2:

[0079] The present invention discloses a storage system for N-ary tree multi-path label data in a relational database, comprising a data table construction module and a data node storage module.

[0080] The data table construction module is used to perform the following: construct an N-ary tree data storage data table, the data table is used to record parent node and child node data, and the label sequence number of the node in each depth-first search result is consistent.

[0081] The data node storage module is used to perform the following: for a newly inserted child node c, inform in advance that its parent node is node b, query all parent nodes and child nodes of node b, determine the insertion position and label serial number of the child node c based on the query results, define two lists for storing node information of the parent node and child node corresponding to the node b, define a recursive function to trace back to the root node of the tree, and generate a unique label serial number for the child node c.

[0082] As a specific implementation, when querying all parent nodes and child nodes of node b and determining the insertion position and label sequence number of the child node c according to the query result, the data node storage module is used to perform the following:

[0083] (1) For the newly inserted child node c, query all parent nodes and child nodes of its parent node b;

[0084] (2) If node b has no parent node or child node, node c is directly inserted into the data table, and the label sequence numbers of node b and node c are consistent;

[0085] (3) If node b has only one parent node, node c is directly inserted into the data table, and the label sequence numbers of nodes a, b, and c are consistent;

[0086] (4) If node b has only one child node, node c and node b’s child nodes are at the same level, node b and node c are a route, node b and other child nodes each have a route, newly insert node b and node c, and the sequence numbers of node b and node c are different from the label sequence numbers of other route nodes;

[0087] (5) Node b has a parent node and a child node. Node c and the child node of node b are at the same level. Node b and node c are on the same route. Node b and other child nodes each have a route. The parent node of node b and all nodes at higher levels are queried out, and the queried higher-level nodes as well as node b and node c are inserted. The label serial numbers of the queried higher-level nodes as well as node b and node c are different from the label serial numbers of other route nodes.

[0088] As a specific implementation, for the two lists defined, each list includes the following two attributes, namely serialTag and code, serialTag represents the label serial number, and code represents the node code.

[0089] As a specific implementation, the data node storage module is used to perform the following to define a recursive function tracing back to the root node of the tree:

[0090] (1) Define an empty list parents to store all parent nodes;

[0091] (2) Set the current node as the node to be queried;

[0092] (3) When the current node is not empty, perform the following operations: add the parent node of the current node to the parents list and set the current node as its parent node;

[0093] (4) When the parent node of the current node is None or the root node is reached, the loop ends;

[0094] (5) Returns the parents list, which is a list of all parent nodes.

[0095] As a specific implementation, the data node storage module is used to perform the following to generate a unique label serial number for the child node c:

[0096] (1) Define a variable serialTag;

[0097] (2) Use an incremental counter or a database auto-increment field to ensure the uniqueness of the serialTag;

[0098] (3) Each time a new node is inserted, the value of serialTag increases, ensuring that the serialTag of each node is unique.

[0099] The system of this embodiment can execute the method disclosed in Embodiment 1 to realize the storage of multi-channel label data.

[0100] The above is a detailed introduction to the storage method and system for N-ary tree multi-way label data under the relational database provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for general technical personnel in this field, according to the idea of ​​the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.

Claims

1. A method for storing N-ary tree multi-path label data in a relational database, characterized in that: The steps include: Build data table: Build an N-ary tree data storage data table, which is used to record parent node and child node data. The label sequence number of the node in each depth-first search result is consistent; Storing data nodes: For the newly inserted child node c, inform in advance that its parent node is node b, query all parent nodes and child nodes of node b, determine the insertion position and label serial number of the child node c according to the query results, define two lists for storing the node information of the parent node and child node corresponding to the node b, define a recursive function to trace back to the root node of the tree, and generate a unique label serial number for the child node c.

2. The method for storing N-ary tree multi-path label data in a relational database according to claim 1, characterized in that: Querying all parent nodes and child nodes of node b, and determining the insertion position and label sequence number of the child node c according to the query result, includes the following steps: For the newly inserted child node c, query all parent nodes and child nodes of its parent node b; If node b has no parent node or child node, node c is directly inserted into the data table, and the label sequence numbers of node b and node c are consistent; If node b has only one parent node, node c is directly inserted into the data table, and the label sequence numbers of nodes a, b, and c are consistent; If node b has only child nodes, node c and node b's child nodes are at the same level, node b and node c are a route, node b and other child nodes have a route respectively, node b and node c are newly inserted, and the sequence numbers of node b and node c are different from the label sequence numbers of other route nodes; Node b has a parent node and child nodes, node c and the child nodes of node b are at the same level, node b and node c are on the same route, node b and other child nodes each have a route, the parent node of node b and all higher-level nodes are queried out, the queried higher-level nodes and node b and node c are inserted, and the label serial numbers of the queried higher-level nodes and node b and node c are different from the label serial numbers of other route nodes.

3. The method for storing N-ary tree multi-path label data in a relational database according to claim 1, characterized in that: For the two lists defined, each list includes the following two attributes: serialTag, indicating the tag serial number; code, indicating the node code.

4. The method for storing N-ary tree multi-path label data in a relational database according to claim 1, characterized in that: A recursive function traces back to the root of the tree and consists of the following steps: Define an empty list parents to store all parent nodes; Set the current node to the node to be queried; When the current node is not empty, perform the following operations: add the parent node of the current node to the parents list and set the current node as its parent node; The loop ends when the parent node of the current node is None or the root node is reached; Returns the parents list, which is a list of all parent nodes.

5. The method for storing N-ary tree multi-path label data in a relational database according to claim 3, characterized in that: Generate a unique label serial number for child node c, including the following steps: Define a variable serialTag; Use an incremental counter or a database auto-increment field to ensure the uniqueness of the serialTag; Each time a new node is inserted, the value of serialTag increases, ensuring that the serialTag of each node is unique.

6. A storage system for N-ary tree multi-way label data in a relational database, characterized in that: Used to realize multi-way label data storage by using a method for storing multi-way label data in an N-ary tree under a relational database as described in any one of claims 1 to 5, the system comprising a data table construction module and a data node storage module; The data table construction module is used to perform the following: construct an N-ary tree data storage data table, the data table is used to record parent node and child node data, and the label sequence number of the node in each depth-first search result is consistent; The data node storage module is used to perform the following: for a newly inserted child node c, inform in advance that its parent node is node b, query all parent nodes and child nodes of node b, determine the insertion position and label serial number of the child node c based on the query result, define two lists for storing node information of the parent node and child node corresponding to the node b, define a recursive function to trace back to the root node of the tree, and generate a unique label serial number for the child node c.

7. The storage system of N-ary tree multi-way label data in a relational database according to claim 6, characterized in that: When querying all parent nodes and child nodes of node b and determining the insertion position and label sequence number of the child node c according to the query result, the data node storage module is used to perform the following: For the newly inserted child node c, query all parent nodes and child nodes of its parent node b; If node b has no parent node or child node, node c is directly inserted into the data table, and the label sequence numbers of node b and node c are consistent; If node b has only one parent node, node c is directly inserted into the data table, and the label sequence numbers of nodes a, b, and c are consistent; If node b has only child nodes, node c and node b's child nodes are at the same level, node b and node c are a route, node b and other child nodes have a route respectively, node b and node c are newly inserted, and the sequence numbers of node b and node c are different from the label sequence numbers of other route nodes; Node b has a parent node and child nodes, node c and the child nodes of node b are at the same level, node b and node c are on the same route, node b and other child nodes each have a route, the parent node of node b and all higher-level nodes are queried out, the queried higher-level nodes and node b and node c are inserted, and the label serial numbers of the queried higher-level nodes and node b and node c are different from the label serial numbers of other route nodes.

8. The storage system of N-ary tree multi-way label data in a relational database according to claim 6, characterized in that: For the two lists defined, each list includes the following two attributes: serialTag, indicating the tag serial number; code, indicating the node code.

9. The storage system of N-ary tree multi-way label data in a relational database according to claim 6, characterized in that: The data node storage module is used to perform the following to define a recursive function tracing back to the root node of the tree: Define an empty list parents to store all parent nodes; Set the current node to the node to be queried; When the current node is not empty, perform the following operations: add the parent node of the current node to the parents list and set the current node as its parent node; The loop ends when the parent node of the current node is None or the root node is reached; Returns the parents list, which is a list of all parent nodes.

10. The storage system of N-ary tree multi-way label data in a relational database according to claim 6, characterized in that: The data node storage module is used to perform the following steps to generate a unique label serial number for the child node c: Define a variable serialTag; Use an incremental counter or a database auto-increment field to ensure the uniqueness of the serialTag; Each time a new node is inserted, the value of serialTag increases, ensuring that the serialTag of each node is unique.