CN110704420A - Method and device for realizing tree structure - Google Patents

Method and device for realizing tree structure Download PDF

Info

Publication number
CN110704420A
CN110704420A CN201810648859.1A CN201810648859A CN110704420A CN 110704420 A CN110704420 A CN 110704420A CN 201810648859 A CN201810648859 A CN 201810648859A CN 110704420 A CN110704420 A CN 110704420A
Authority
CN
China
Prior art keywords
node
path value
nodes
path
identification code
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201810648859.1A
Other languages
Chinese (zh)
Inventor
牛冬
孙桂勇
姚浩
方刚
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Century TAL Education Technology Co Ltd
Original Assignee
Beijing Century TAL Education Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Century TAL Education Technology Co Ltd filed Critical Beijing Century TAL Education Technology Co Ltd
Priority to CN201810648859.1A priority Critical patent/CN110704420A/en
Publication of CN110704420A publication Critical patent/CN110704420A/en
Pending legal-status Critical Current

Links

Images

Abstract

A method and a device for realizing a tree structure comprise the following steps: establishing a database table with a tree structure, wherein the database table comprises an identification code field and a path value field; setting an identification code in an identification code field of each node; setting a path value in a path value field of each node, wherein the path value of the root node is the identification code of the root node, and the path values of other nodes are the product of the identification codes of all path nodes from the current node to the root node; wherein the identification code is a prime number that is not repeatedly assigned. By the method and the device, the complexity of database operation can be reduced, and the operation time can be saved.

Description

Method and device for realizing tree structure
Technical Field
The invention relates to the field of computer application, in particular to a method and a system for processing data in a tree structure.
Background
In various application system development based on the relational database, the data of a tree structure is required to be stored. For example, on the pad mall platform of a home learning advisor, there are web disk related functions. The core of the network disk function is a tree structure formed by folders and files and related operations of adding, deleting, modifying and searching.
Currently, typical 4 methods for storing the tree structure in the database are: adjacency list representation (AdjacencyList), materialized path representation (pathentries), closure representation (ClosureTable), and interval nested representation (nestsets).
The adjacency list representation method is to add a field for recording the ID of the father node of the current node on the basis of the original data information. The method is characterized in that according to inheritance relationships among nodes, a father node describing a certain node is displayed, and therefore a two-bit relationship table is established. However, the process of searching and deleting the sub-tree and the parent path of a node is complicated, and the process can be completed only by recursive operation. With each recursive operation, the database IO has a time overhead.
The path representation method needs to record all path nodes from the current node to the root node on a data entity, and each table of the database needs to open up a field quantity equal to the tree depth N for recording the path nodes of all the nodes.
The closure table representation method maintains two tables, one table is original information, and the other table stores child node information of all nodes; the method solves the problem of data integrity, partially solves the problem of difficulty in increasing and deleting and the problem of easy error, but the method realizes a relatively better scheme in a mode of increasing a table and wasting nearly one time of data rows, and needs a large amount of complex calculation when a sub-tree is searched, deleted and moved.
The interval nesting representation method adds two fields to each node, wherein a Left Index number (Left _ Index) and a Right Index number (Right _ Index) are formed in each node, intervals [ Left _ Index and Right _ Index ] formed by the Index numbers represent the range of the start and the end of each node, and the difference value of the two numbers represents the number of all child nodes of the current node. The data nodes are operated by using the information provided by the index, so that the storage space is saved, and the query efficiency is improved. The interval nesting representation method can quickly query the path information and the child node information of the positions of the nodes, but the left and right index values of all father nodes need to be modified when the nodes are added and deleted, so that a large amount of time of a CPU is needed.
Based on the problems, a novel tree structure implementation mode is provided, a database table structure is constructed by introducing prime number characteristics in the structural design of the database table, and the tree structure can be operated efficiently and simply by only one database table.
Disclosure of Invention
The invention aims to provide a method and a system for processing data in a tree structure, aiming at the defects of the prior art, and the method and the system can efficiently and conveniently perform operations such as addition, deletion, modification, query and the like on nonlinear data in a database.
In order to solve the technical problems, the invention provides the following technical scheme:
a method for realizing a tree structure comprises the following steps:
establishing a database table with a tree structure, wherein the database table comprises an identification code field and a path value field;
setting an identification code in an identification code field of each node;
setting a path value in a path value field of each node, wherein the path value of the root node is the identification code of the root node, and the path values of other nodes are the product of the identification codes of all path nodes from the node to the root node;
the identification code is a prime number which is not repeatedly allocated.
The path value of each node can be decomposed into a group of unique prime numbers, the group of prime numbers are respectively the identification codes of all path nodes from the node to a root node, and the number of the group of prime numbers is the level of the node.
When a node is newly added, the path value of the father node of the node to be added is searched, the identification code is distributed to the node to be added, and the path value of the node to be added is calculated based on the path value of the father node of the node to be added and the identification code distributed to the node to be added.
When deleting a node, deleting the node and the node with the node level larger than the node according to the path value of the node, wherein the path value of the node can divide the node with the node path value.
When searching the subtree node, according to the path value of the node, the level of the searched node is larger than the node, and the path value of the node can divide the node of the node path value.
Wherein the change node comprises a change node having,
step 1, searching a set of nodes to be changed and subtree nodes thereof;
step 2, searching a father node of the node to be changed;
step 3, stripping father nodes of the set of the nodes to be changed and subtree nodes thereof;
step 4, searching the changed nodes;
and 5, attaching the set of the node to be changed and the subtree node thereof to the changed node.
Wherein, step 3, the father node stripping is carried out on the collection of the nodes to be changed and the subtree nodes thereof, comprising,
dividing the path value of each node in the set of the node to be changed and the subtree node thereof by the path value of the father node of the node to be changed, and respectively storing the result in the path value field of each node.
Wherein step 5, the set of nodes to be changed and their subtree nodes are appended to the changed node, including,
and respectively multiplying the path value of each node in the set of the node to be changed and the subtree node thereof by the path value of the changed node, and respectively storing the result in the path value field of each node.
The embodiment of the present invention further provides an implementation apparatus for a tree structure, including:
the table building module is used for building a database table with a tree structure, and the database table comprises an identification code field and a path value field;
the identification code module is used for setting an identification code in the identification code field of each node;
a path value module, configured to set a path value in a path value field of each node, where the path value of the root node is an identifier of the root node, and the path values of other nodes are a product of identifiers of all path nodes from the node to the root node;
wherein, the identification code is a prime number which is not repeatedly allocated.
The embodiment of the present invention further provides an implementation apparatus for a tree structure, including: a memory and a processor; wherein the memory stores instructions; the processor is configured to perform the method of embodiment one according to instructions stored in the memory.
The embodiment of the invention provides a novel tree structure implementation mode, a data table structure is constructed by introducing prime number characteristics in the structural design of a database table, the characteristic that a composite number obtained by multiplying prime numbers can be decomposed into a group of unique prime numbers is utilized, the tree structure can be directly operated by only using the database table constructed by introducing the prime number characteristics, and the time consumption of data operation can be reduced.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings needed to be used in the description of the embodiments are briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without creative efforts.
FIG. 1 is a flow chart of a tree structure implementation method of the present invention
FIG. 2 is a tree structure with each node assigned a prime number and a prime number product according to the present invention
FIG. 3 is a diagram of a tree structure implementation apparatus according to the present invention
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
In order to solve the problems of complex computation and more time consumption of the tree structure of the existing database, the embodiment of the invention provides a method and a device for realizing the tree structure. The tree structure according to the embodiment of the present invention is a nested structure of a hierarchy. The outer layer and the inner layer of a tree structure have similar structures, so the structure can be represented recursively. The various dendrograms in the classic data structure are a typical tree structure: a tree can be simply represented as root, left sub-tree, right sub-tree. The left and right subtrees have their own subtrees. Alternatively, other tree-like structure forms may be also referred to, and are not specifically limited herein.
Example one
First, a method for implementing a tree structure according to a first embodiment of the present invention is described below.
It should be noted that the implementation method of a tree structure provided in the first embodiment of the present invention is executed by a database, where the database may be independent database software in the related art, or may be a functional plug-in the database software; in addition, the database can be applied to electronic equipment, and the electronic equipment is terminal equipment and/or a server.
As shown in fig. 1, a method for implementing a tree structure according to an embodiment of the present invention includes the following steps:
s10, establishing a tree-structured database table, wherein the database table comprises an identification code field and a path value field;
s20, setting an identification code in the identification code field of each node;
s30, setting a path value in the path value field of each node, wherein the path value of the root node is the identification code thereof, and the path values of other nodes are the product of the identification codes of all path nodes from the node to the root node;
wherein the identification code is a prime number that is not repeatedly assigned.
The embodiment of the invention provides a novel tree structure implementation mode, a data table structure is constructed by introducing prime number characteristics in the structural design of a database table, the characteristic that the resultant obtained by multiplying prime numbers can be decomposed into a group of unique prime factors is utilized, the tree structure can be directly operated by only using the database table constructed by introducing the prime number characteristics, and the time consumption of data operation can be reduced.
The implementation method of the tree structure is described in detail step by step below.
For S10, a tree structured database table is established, the database table including an identifier field and a path value field.
The core fields of a database table are as shown in the following table:
numbering Name of field Type of field Description of the invention
1 index int Node number (self-increment)
2 name varchar Node name
3 nodeType tinyint The node type is as follows: intermediate node, leaf node
4 areaCode int Region coding
5 fileUrl varchar File storage path represented by leaf node
6 nodeLevel int Hierarchy of nodes with root node as first level
7 nodePrimeNum int Identification code field: prime numbers assigned to nodes
8 nodePrimeProduct bigint Path value field: prime number product from root node to current node
Table 1: database table core field
As can be seen from the table, each field in the database table describes various information of the database table, such as node number, node name, node type, region code, file storage path of leaf node, node hierarchy, identification code field, and path value field. The node numbers are self-increasing numbers automatically distributed by the database for each node, and the numbers of each node in the data structure are not repeated; the node name represents the name of the current node, and a user can modify the node name as required; the node type indicates whether the node is an intermediate node or a leaf node; since a typical embodiment of the present invention is a network disk, which can be bound to the region, the region coding parameters are set, for example, the beijing region is set to 010, and the tianjin region is set to 022, which will be described in detail below; the file storage path of the leaf node is a file accessible address; the hierarchy of the node represents that the node is at the fourth level in the whole tree structure, wherein the root node is set as the first level, and the hierarchy of the subsequent nodes is increased progressively; the identifier field and the path value field are the core of the present invention, and the following description will be focused on.
Referring to fig. 1, the present invention assigns an identification code field and a path value field to each node. The identification code field is filled with an identification code, and the identification code is a prime number which is not repeatedly distributed in the invention, namely 1 is filled in the root node, and 3, 5, 7 and 11 … … are filled in other nodes. Filling a path value into the path value field, wherein the path value is the product of the identification codes of all path nodes from the current node to the root node; for the root node, the path value is 1.
The path value of each node is decomposed into a group of unique prime numbers, the group of prime numbers represents the complete path from the current node to the root node, and the number of the group of prime factors represents the hierarchy of the current node. For example, 190 × 19 × 5 × 2 × 1, that is, for the current node with a path value of 190, the identifiers of the path nodes from the current node to the root node are 19, 5, 2, and 1, respectively, and the hierarchy of the current node is 4. Therefore, each node is allocated with a nonrepetitive and unique prime number as the identification code, the path value of the current node is calculated by the identification code of each path node from the current node to the root node, and the path and the hierarchy of the current node can be uniquely obtained by the identification code and the path value, so that the operations of adding, deleting, searching, changing and the like of the node can be simply and quickly carried out, the calculation amount is reduced, and the processing time is saved.
For S20, setting a unique identification code in the identification code field of each node; wherein, the identification code is a prime number which is not repeatedly allocated.
This step is a process of setting a non-repeating prime number for the identification code field of each node.
As shown in fig. 2, which shows a tree structure with each node assigned a prime number and a prime number product according to the method of the present invention. The storage data of each node in the tree structure in the database table is as follows:
Figure BDA0001704182090000051
Figure BDA0001704182090000061
table 2: data storage table of each node in tree structure in database table
In fig. 2, identification codes assigned to nodes having node names a to K are prime numbers 1, 3, 5, 7, 11, 13, 17, 19, 23, and 29, respectively. Wherein, the node A is a root node, and the product of the prime number and the prime number is 1. The path value set in the path value field of each node, that is, the prime number product, is the product of the identification codes from the root node to the current node. Taking node L as an example, its assigned prime number is 23, its computed prime number product is 759, its node level is level 4, and its path to root node a is L-F-C-a. According to the prime number product 759-23 × 11 × 3 × 1, a unique set of prime numbers 23, 11, 3, 1 corresponding to the prime number product of the node L can be obtained, the number of the set of prime numbers is 4, that is, according to the level of the current node corresponding to the set of prime numbers is 4, and the path to the root node a is also L-F-C-a.
In the node types in table 2, "1" represents an intermediate node and "0" represents a leaf node.
The invention can uniquely determine the node path and the level of the current node according to the non-repeated identification code (prime number) distributed for each node and the calculated path value (prime number product), thereby being capable of conveniently searching the father node of a certain node, and simultaneously, the path value of the node is obtained by multiplying the prime number of each path node from the node to the root node, thereby searching the subtree node by calculating whether the prime number product of the high-level node can divide the prime number product of the current node completely. These operational rights are the complexity of O (1). Therefore, operations such as adding, deleting, changing and searching of nodes can be simply carried out, the complexity of node operation is reduced, and complex calculation is reduced.
When the method is adopted to generate the tree structure, a batch of prime numbers are required to be generated, and when the batch of prime numbers is about to be used, another batch of prime numbers are generated. As shown in table 3, the number of corresponding prime numbers in a certain range of values:
range of prime number generation <=10000 <=100000 <=1000000 <=10000000 <=100000000 <=500000000
Number of prime numbers 1229 9592 78498 664579 5761455 26355867
Table 3: prime number in a range of values
In the cloud disk or the network disk of a small and medium-sized enterprise, which is a typical application of the present invention, 500000000 is far less than the int maximum, 26355867 folders or files are already sufficiently used, and bigint is used herein, so the total number of primes is more and is completely available for distribution.
Meanwhile, the calculation of decomposing the composite number into a group of unique prime numbers is very efficient, for example, only 1ms is needed for calculating 223092870 corresponding prime number group.
Therefore, according to the mode of the embodiment of the invention, the time consumed by the node operation can be greatly reduced.
Then in a highly concurrent scenario, how to implement non-duplicate allocation of prime numbers is the key to the implementation of the present invention. In order to realize non-repetitive assignment of prime numbers, the present invention exemplifies the following manner:
step 1: the prime number list PList is stored offline into Redis.
Setting the initial length of the prime number list PList to be PListSize 10000, and setting a timing task in the background to scan the consumption situation of the prime number list, namely searching the value of the Index of the current region in the Redis through get command, and if the Index is PList 0.8, expanding the capacity of the PList by one time. This ensures that the prime values in the prime list PList are used sufficiently.
Step 2: the value of the prime number list index is obtained.
In the present invention, the network disk and the region binding are taken as an example, and therefore, the initial index corresponding to each region stored in the Redis is 0.
For example, for Beijing area, the Value of Key/Value in Redis to the initial Index is 010-Prime-Index: 0;
for Tianjin, the Value of Key/Value vs. initial Index in Redis is 022-Prime-Index: 0.
after the Index value of the response region is obtained in the above manner, a self-increment atomic operation API of Redis, that is, an INCR command, is adopted, after the INCR command is executed, the corresponding Index value becomes the original Index +1, and then the latest Index value is returned. Because the operation is the self-increment atomic operation, different indexes acquired by different threads can be ensured under the high concurrency condition, and thus, the data cannot be repeatedly acquired when the prime number in the PList is acquired based on the indexes.
And step 3: and acquiring a corresponding prime number from PList according to the Index value.
And S30, setting a path value in the path value field of each node, wherein the path value of the root node is the identification code thereof, and the path values of other nodes are the product of the identification codes of all path nodes from the node to the root node.
This step is for setting the path value of its path value field for each node. The path value is set to the product of the identification codes of all path nodes from the current node to the root node, and of course, for the root node, the path value is set to be the same as its identification code.
As shown in fig. 2, which shows a tree structure with each node assigned a prime number and a prime number product according to the method of the present invention. The calculation logic of the prime number product is as follows: the product of the prime numbers of all path nodes from the root node to the current node. Taking the G node as an example:
the father node is A, B, D in turn, and the prime numbers allocated to A (root node) are: 1,
b is assigned a prime number of 2, the prime number product of B is 2 × 1 ═ 2;
d is assigned a prime number of 5, the prime number product of B is 5 × 2 × 1 ═ 10;
g is assigned a prime number of 13, the prime number product of B is 13 × 5 × 2 × 1 ═ 130.
Therefore, by assigning the prime number to the current node, the prime number product, i.e., the path value, of the current node can be directly calculated and stored into the path value field of the current node in the database table for various operations on the node.
The operations on the database table structure mainly involve operations such as addition, deletion, modification, and lookup of nodes, which are described in detail below.
1. Adding nodes
When nodes are added, whether a tree structure exists at present or not, namely whether a root node exists or not, needs to be judged. And it can be directly determined whether there is a root node by determining whether there is a parent node for the current node.
Step 1: judging whether father node information exists or not;
if the current node has a parent node, go to step 2 for further processing, for example, in fig. 2, when node D is to be added, go to step 2 because it has a parent node B.
If the current node does not have a parent node, go to step 3 for further processing, for example, in fig. 2, when node C is to be added, go to step 3 because it does not have a parent node.
Step 2: and searching the path value of the father node and distributing the prime number of the identification code for the new node. And calculating the path value (prime number product) of the new node based on the path value of the father node and the identification code (prime number) distributed for the new node, and storing in a warehouse, and ending.
And step 3: and adding root node information. (without the need for other steps)
The default assigned identifier of the root node is 1, the path value is also 1, and the level is 1. And (4) storing the information of the following nodes in a warehouse, and then performing the step 4.
And 4, step 4: and adding new node information. The new node is assigned an identification code (prime number) when the path value (prime number product) of the new node is the same as the assigned identification code (prime number) (because the root node assigned data is 1).
2. Deleting nodes
The deleting nodes are divided into a deleting single node and a deleting subtree node.
A single node as referred to herein is a leaf node, i.e. the node located at the extreme end of the tree structure, below which no subtree exists. Therefore, when a single node is deleted, the node can be directly deleted according to parameters such as the serial number, the name, the identification code and the like of the node.
When deleting the subtree node, searching the database for nodes with a node level larger than the current node according to the path value of the current node, and deleting the nodes and the current node by dividing all the nodes of the path value of the current node by the path value of the node.
As shown in fig. 2, if the node B is to be deleted, the nodes found in the hierarchy condition that are larger than the B hierarchy are:
D、E、F、G、H、I、L、K.
the node D, G, H, I may be found in a way that satisfies "the path value of the node may divide the current node path value entirely".
Path values through node D, G, H, I to find and delete these nodes.
Therefore, whether a single node is deleted or a subtree node is deleted, the method can be implemented in O (1).
3. Searching node
The lookup node may be a lookup single node, a parent path of the lookup node, a subtree node of the lookup node, and a nearest parent node of the lookup node.
If a single node needs to be searched, the corresponding data can be directly searched through parameters such as the name, the number, the assigned identification code, the path value and the like of the node, and a specific searching method is known in the art and is not limited in any way.
If a parent path of a node needs to be searched, a path value of a current node is decomposed into a unique group of prime numbers according to the characteristic that a composite number obtained by multiplying prime numbers can be decomposed into a unique group of prime factors, so that a unique group of prime numbers can be obtained according to the path value of the current node, the prime numbers are identification codes of all path nodes from the current node to a root node, and the path nodes are ordered according to a hierarchy to obtain the parent path of the current node. For example, taking node L in fig. 2 as an example, the path value (prime product) is 759, and based on the prime product 759 ═ 23 × 11 × 3 × 1, a unique set of prime numbers 23, 11, 3, and 1 corresponding to the prime product of node L can be obtained, and the nodes corresponding to the set of prime factors are L, F, C, A, respectively, and the parent path of current node L is L-F-C-a by hierarchical sorting.
If a subtree node of a node needs to be searched, searching a database for data of which the node level is greater than the current node and the node path value can be divided into the data of the current node path value according to the characteristic that the composite number obtained by multiplying prime numbers can be decomposed into a unique group of prime factors. For example, in fig. 2, when a subtree node of node B needs to be searched, the nodes of the B hierarchy level where the large nodes are found in the hierarchy condition are: D. e, F, G, H, I, L, K are provided. Meanwhile, the node D, G, H, I can be found by satisfying the condition that the path value of the node can be divided by the data of the current node path value. Thus, the sub-tree of the current node B may be obtained as node D, G, H, I.
If the nearest father node of the node needs to be searched, the prime number product of the current node is decomposed into a unique group of prime numbers according to the characteristic that the composite number obtained by multiplying the prime numbers can be decomposed into a unique group of prime factors,
therefore, a unique set of prime numbers can be obtained according to the path value of the current node, wherein the prime numbers are identification codes of all path nodes from the current node to the root node, the hierarchy of only one node in the path nodes is 1 less than that of the current node, and the node is the nearest parent node. For example, taking node L in fig. 2 as an example, its prime number product is 759, and based on its prime number product 759 ═ 23 × 11 × 3 × 1, it can be found that the unique set of prime numbers corresponding to the path values of node L is 23, 11, 3, 1, and the nodes corresponding to this set of prime numbers are L, F, C, A, respectively, and only node F among these four parent nodes has a level 1 less than that of the current node L, so node F is the parent node of node L.
The complexity of this operation is O (1).
4. Change node
The change node may be a name of the change node, and a subtree node of the change node.
If the name of a node needs to be changed, corresponding data can be directly searched through parameters such as the name, the number, the identification code, the path value and the like of the node, and the name of the node can be directly updated after the corresponding data is searched.
If the subtree node of the node needs to be changed, the operation is represented as the migration of the subtree node, and the following description takes the example that the node D and the subtree node thereof are migrated to the node C in FIG. 2 as an example.
Step 1, searching a set of nodes to be changed and subtree nodes thereof;
with the method described above in the sub-tree node part of the search node, the node D to be changed and its set CSet of sub-tree nodes { D, G, H, I } can be obtained with O (1) complexity.
Step 2, searching a father node of the node to be changed;
the method described above for finding the nearest parent node can be used to obtain the nearest parent node B of the node D to be modified.
Step 3, stripping father nodes of the node to be changed and the set of subtree nodes of the node to be changed;
in order to migrate the nodes and the subtree nodes thereof, the paths of the nodes need to be changed, and the first step of changing the paths of the nodes needs to strip the nodes to be changed and the subtree nodes thereof from the father nodes of the current nodes.
In the present invention, the path value of each node in Cset is divided by the path value of the current parent node, and the results are stored in the path value fields of the respective nodes, respectively. For example, the path values of the nodes D, G, H, and I in CSet are changed from 10, 130, 170, and 190 to 5, 65, 85, and 95, respectively.
Step 4, searching the changed nodes;
and the modified node C can be directly inquired according to parameters such as node name, serial number, identification code, path value and the like.
And 5, attaching the set of the node to be changed and the subtree node thereof to the changed node.
In order to migrate a node and a subtree node thereof, a set Cset stripped of an original parent node needs to be attached to a new parent node, so that the purpose of migrating to the position of the new node is achieved.
In the invention, the path value of each node in the set Cset is multiplied by the path value of the migrated node, so that the path value of each node after the node to be changed and the subtree thereof are migrated can be obtained. For example, after the node D is migrated to the node C, the path values of the nodes D, G, H, and I in the set CSet are changed to 15, 195, 255, and 285, respectively.
The complexity of this operation is O (1).
Example two
Corresponding to the method embodiment, the embodiment of the invention also provides a device for realizing the tree structure.
As shown in fig. 3, the block diagram of an apparatus for implementing a tree structure according to an embodiment of the present invention includes a table creating module 110, an identification code module 120, and a path value module 130.
And the table building module 110 is used for building a tree-structured database table, wherein the database table comprises an identification code field and a path value field.
And an identification code module 120, configured to set an identification code in the identification code field of each node, where the identification code is a prime number that is not repeatedly allocated.
A path value module 130, configured to set a path value in the path value field of each node, where the path value of the root node is the identifier of the root node, and the path values of other nodes are the product of the identifiers of all path nodes from the current node to the root node.
EXAMPLE III
In an embodiment of the present invention, an apparatus for implementing a tree structure is further provided, which includes: a memory and a processor; wherein the memory stores instructions; the processor is configured to perform the method as described above according to instructions stored in the memory.
It can be clearly understood by those skilled in the art that, for convenience and brevity of description, the specific working processes of the modules and the instructions described above may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the several embodiments provided in the present application, it should be understood that the disclosed apparatus and method may be implemented in other ways. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the modules is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: various media capable of storing program codes, such as a usb disk, a removable hard disk, a Read-only memory (ROM), a Random Access Memory (RAM), a magnetic disk, or an optical disk.
All the embodiments in the present specification are described in a related manner, and the same and similar parts among the embodiments may be referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, as for the apparatus embodiment, since it is substantially similar to the method embodiment, the description is relatively simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.

Claims (10)

1. A method for realizing a tree structure comprises the following steps:
establishing a database table with a tree structure, wherein the database table comprises an identification code field and a path value field;
setting an identification code in an identification code field of each node;
setting a path value in a path value field of each node, wherein the path value of the root node is the identification code of the root node, and the path values of other nodes are the product of the identification codes of all path nodes from the node to the root node;
wherein the identification code is a prime number that is not repeatedly assigned.
2. The method of claim 1, wherein the path value of each node can be decomposed into a set of unique prime numbers, the set of prime numbers are respectively the identification codes of the nodes of the paths from the node to the root node, and the number of the set of prime numbers is the hierarchy of the node.
3. The method according to claim 1 or 2, wherein, when a node is newly added, the path value of the parent node of the node to be added is searched for, and the identification code is assigned to the node to be added, and the path value of the node to be added is calculated based on the path value of the parent node of the node to be added and the identification code assigned to the node to be added.
4. The method according to any of claims 1-3, wherein when a node is deleted, the node and nodes having a node hierarchy larger than the node are deleted according to the node's path value, and the node's path value is divisible by the node's path value.
5. The method according to any of claims 1-4, wherein when searching for a subtree node, a node whose level is larger than the node is searched for according to the node's path value, and the node's path value is divisible by the node's path value.
6. The method of any of claims 1-5, wherein altering a node comprises,
wherein the change node comprises a change node having,
step 1, searching a set of nodes to be changed and subtree nodes thereof;
step 2, searching a father node of the node to be changed;
step 3, stripping father nodes of the set of the nodes to be changed and subtree nodes thereof;
step 4, searching the changed nodes;
and 5, attaching the set of the node to be changed and the subtree node thereof to the changed node.
7. The method of claim 6, wherein step 3, parent node stripping of the set of nodes to be changed and their subtree nodes, comprises,
dividing the path value of each node in the set of the node to be changed and the subtree node thereof by the path value of the father node of the node to be changed, and respectively storing the result in the path value field of each node.
8. The method of claim 6, wherein step 5, appending the set of nodes to be changed and their subtree nodes to the changed node, comprises,
and respectively multiplying the path value of each node in the set of the node to be changed and the subtree node thereof by the path value of the changed node, and respectively storing the result in the path value field of each node.
9. An implementation apparatus of a tree structure, comprising:
the table building module is used for building a database table with a tree structure, and the database table comprises an identification code field and a path value field;
the identification code module is used for setting an identification code in the identification code field of each node;
a path value module, configured to set a path value in a path value field of each node, where the path value of the root node is an identifier of the root node, and the path values of other nodes are a product of identifiers of all path nodes from the node to the root node;
wherein, the identification code is a prime number which is not repeatedly allocated.
10. An apparatus for implementing a tree structure, comprising: a memory and a processor; wherein the memory stores instructions; the processor is configured to perform the method of any of claims 1-8 according to instructions stored in the memory.
CN201810648859.1A 2018-06-22 2018-06-22 Method and device for realizing tree structure Pending CN110704420A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201810648859.1A CN110704420A (en) 2018-06-22 2018-06-22 Method and device for realizing tree structure

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201810648859.1A CN110704420A (en) 2018-06-22 2018-06-22 Method and device for realizing tree structure

Publications (1)

Publication Number Publication Date
CN110704420A true CN110704420A (en) 2020-01-17

Family

ID=69192281

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201810648859.1A Pending CN110704420A (en) 2018-06-22 2018-06-22 Method and device for realizing tree structure

Country Status (1)

Country Link
CN (1) CN110704420A (en)

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090138429A1 (en) * 2007-11-26 2009-05-28 International Business Machines Corporation Sructure Based Storage, Query, Update and Transfer of Tree-Based Documents
US7827219B2 (en) * 2008-02-29 2010-11-02 International Business Machines Corporation Method for encoding, traversing, manipulating and querying a tree
CN103200108A (en) * 2013-04-19 2013-07-10 电子科技大学 Prime number route network generation method
CN104199824A (en) * 2014-07-21 2014-12-10 武汉传神信息技术有限公司 Method for judging node relation on tree-shaped data
CN105095237A (en) * 2014-04-30 2015-11-25 国际商业机器公司 Method and device used for generating schema of not-only-structured-query-language database
CN105979023A (en) * 2016-05-10 2016-09-28 中山大学 WSN (Wireless Sensor Networks) address distribution method based on prime number method and on-demand address lending

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090138429A1 (en) * 2007-11-26 2009-05-28 International Business Machines Corporation Sructure Based Storage, Query, Update and Transfer of Tree-Based Documents
US7827219B2 (en) * 2008-02-29 2010-11-02 International Business Machines Corporation Method for encoding, traversing, manipulating and querying a tree
CN103200108A (en) * 2013-04-19 2013-07-10 电子科技大学 Prime number route network generation method
CN105095237A (en) * 2014-04-30 2015-11-25 国际商业机器公司 Method and device used for generating schema of not-only-structured-query-language database
CN104199824A (en) * 2014-07-21 2014-12-10 武汉传神信息技术有限公司 Method for judging node relation on tree-shaped data
CN105979023A (en) * 2016-05-10 2016-09-28 中山大学 WSN (Wireless Sensor Networks) address distribution method based on prime number method and on-demand address lending

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
吴艳等: "基于DOM的XML文档素数编码算法", 《信息系统工程》 *

Similar Documents

Publication Publication Date Title
US7698257B2 (en) Apparatus and method for recursively rationalizing data source queries
CN106649464A (en) Method of building Chinese address tree and device
DE112012005533T5 (en) Supportive query and a query
CN111324577B (en) Yml file reading and writing method and device
CN107067200B (en) Operation method and device for bill of material data
CN108460041A (en) The treating method and apparatus of data
CN103678550A (en) Mass data real-time query method based on dynamic index structure
CN106557307B (en) Service data processing method and system
CN110727687A (en) Material list conversion method and system
CN110928939A (en) Method for converting Sql result set into Json data based on tree structure
CN112148680A (en) File system metadata management method based on distributed graph database
CN111475511A (en) Data storage method, data access method, data storage device, data access device and data access equipment based on tree structure
CN109254962B (en) Index optimization method and device based on T-tree and storage medium
CN116662019B (en) Request distribution method and device, storage medium and electronic device
CN105025013A (en) A dynamic IP coupling model based on a priority Trie tree
CN110597805B (en) Memory index structure processing method
CN107239568A (en) Distributed index implementation method and device
CN115329504B (en) BOM construction method based on complex product structure
CN110704420A (en) Method and device for realizing tree structure
CN106980673A (en) Main memory database table index updating method and system
CN116010664A (en) Data processing method and system based on MPTT and parent searching
CN115982177A (en) Data collection method, device, equipment and medium based on tree dimensionality
CN108959584A (en) A kind of method and device of the processing diagram data based on community structure
CN106569986A (en) Character string replacement method and device
CN112488642B (en) Cloud file management method based on structured labels and taking object as core

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
RJ01 Rejection of invention patent application after publication
RJ01 Rejection of invention patent application after publication

Application publication date: 20200117