CN114186102A - Tree structure data construction method and device and computer equipment - Google Patents

Tree structure data construction method and device and computer equipment Download PDF

Info

Publication number
CN114186102A
CN114186102A CN202111536633.0A CN202111536633A CN114186102A CN 114186102 A CN114186102 A CN 114186102A CN 202111536633 A CN202111536633 A CN 202111536633A CN 114186102 A CN114186102 A CN 114186102A
Authority
CN
China
Prior art keywords
data
node
branch
node data
child
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
CN202111536633.0A
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.)
Shenzhen Waterward Information Co Ltd
Original Assignee
Shenzhen Waterward Information 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 Shenzhen Waterward Information Co Ltd filed Critical Shenzhen Waterward Information Co Ltd
Priority to CN202111536633.0A priority Critical patent/CN114186102A/en
Publication of CN114186102A publication Critical patent/CN114186102A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/90Details of database functions independent of the retrieved data types
    • G06F16/901Indexing; Data structures therefor; Storage structures
    • G06F16/9027Trees

Abstract

The application provides a method and a device for constructing tree-structured data and computer equipment. And then traversing all node data, classifying all node data with the same node father id into the same set, and obtaining a plurality of child node data sets. And finally, traversing all the node data again by the construction system, taking each node data as a root node, screening elements of a sub-node data set corresponding to each node data respectively according to the node id of each node data to be used as sub-nodes, and constructing to obtain tree structure data. The method and the device avoid the system stack required by the tree structure data constructed in a recursion mode, and avoid the defect that stack memory overflow is easy to occur when the hierarchy of the tree structure data is too large.

Description

Tree structure data construction method and device and computer equipment
Technical Field
The present application relates to the field of data construction technologies, and in particular, to a method and an apparatus for constructing tree-structured data, and a computer device.
Background
In the prior art, the tree structure data is generally constructed in a recursive manner, but the recursive manner needs system stacking, so that the space consumption is relatively large. If the hierarchy of the fruit tree is too large, stack memory overflow is easy to occur in the process of constructing the data structure.
Disclosure of Invention
The application mainly aims to provide a method and a device for constructing tree structure data and computer equipment, and aims to overcome the defect that stack memory overflow is easy to occur when the hierarchy of the existing tree structure data is too large.
In order to achieve the above object, the present application provides a method for constructing tree structure data, including:
acquiring a plurality of node data, wherein each node data is marked with a node id and a node father id, the node id is the id of the node data, and the node father id is the id of father node data corresponding to the node data;
traversing all node data, and classifying all the node data with the same node father id into the same set to obtain a plurality of child node data sets;
traversing all the node data again, taking each node data as a root node, screening elements of the child node data set corresponding to each node data respectively according to the node id of each node data to be used as child nodes, and constructing to obtain tree structure data.
The present application further provides a device for constructing tree structure data, including:
the first acquisition module is used for acquiring a plurality of node data, each node data is marked with a node id and a node father id, the node id is the id of the node data, and the node father id is the id of father node data corresponding to the node data;
the classification module is used for traversing all node data, classifying the node data with the same node father id into the same set, and obtaining a plurality of child node data sets;
and the construction module is used for traversing all the node data again, taking each node data as a root node, screening elements of the child node data set corresponding to each node data respectively according to the node id of each node data to be used as child nodes, and constructing to obtain tree structure data.
The present application further provides a computer device comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of any one of the above methods when executing the computer program.
The present application also provides a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, carries out the steps of the method of any of the above.
According to the method, the device and the computer equipment for constructing the tree-shaped structure data, a construction system firstly obtains a plurality of node data, each node data is marked with a node id and a node father id, wherein the node id is the id of the node data, and the node father id is the id of father node data corresponding to the node data. And then traversing all node data, classifying all node data with the same node father id into the same set, and obtaining a plurality of child node data sets. And finally, traversing all the node data again by the construction system, taking each node data as a root node, screening elements of a sub-node data set corresponding to each node data respectively according to the node id of each node data to be used as sub-nodes, and constructing to obtain tree structure data. Compared with the prior art, the method and the device avoid the system stack required by constructing the tree structure data in a recursion mode, avoid the defect that stack memory overflow is easy to occur when the hierarchy of the tree structure data is too large, have a simple implementation mode, and can quickly construct an infinite hierarchy tree structure.
Drawings
FIG. 1 is a schematic diagram illustrating steps of a method for constructing tree structure data according to an embodiment of the present application;
fig. 2 is a block diagram of an overall structure of a method and an apparatus for constructing tree structure data in an embodiment of the present application;
fig. 3 is a block diagram schematically illustrating a structure of a computer device according to an embodiment of the present application.
The implementation, functional features and advantages of the objectives of the present application will be further explained with reference to the accompanying drawings.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the present application is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the present application and are not intended to limit the present application.
Referring to fig. 1, an embodiment of the present application provides a method for constructing tree structure data, including:
s1, acquiring a plurality of node data, wherein each node data is marked with a node id and a node father id, the node id is the id of the node data, and the node father id is the id of father node data corresponding to the node data;
s2, traversing all node data, classifying the node data with the same node father id into the same set, and obtaining a plurality of child node data sets;
and S3, traversing all the node data again, taking each node data as a root node, screening the elements of the child node data set corresponding to each node data respectively according to the node id of each node data to be used as child nodes, and constructing to obtain tree structure data.
In this embodiment, the construction system obtains a plurality of node data which are input in advance or input in real time, where each node data is labeled with a node id and a node parent id, the node id represents an id of the node data, and the node parent id represents an id of parent node data corresponding to the node data. The construction system traverses all node data, classifies all node data with the same node father id into the same set, and accordingly obtains a plurality of child node data sets, and elements contained in a single child node data set (the elements in the set are the node data) all belong to child node data of the same father node data. The construction system traverses all the node data again, takes the node id of each node data as a root node, screens a child node data set corresponding to each node data respectively (namely the node parent id of the element contained in the child node data set is the same as the node id of the node data serving as the root node), and takes all the elements in the corresponding child node data set as child nodes of the node data. Meanwhile, the construction system performs data merging and construction according to the corresponding relation between the root node and the child node among the node data in the traversal process, so as to obtain complete tree structure data, wherein the tree structure data comprises all the node data, and each node data in the tree structure data exists in the form of the root node or the child node.
Preferably, root parent nodes of different tree structure data (i.e. initial root nodes of the tree structure data) have node parent ids of different forms, so that when a plurality of different types of data tables are entered into a construction system and respective corresponding tree structure data needs to be formed according to the respective data tables, node data contained in the respective data tables can be distinguished from one another, and confusion is not generated. In practice, if there are several numbers of root parent nodes, there will be several different trees.
When the tree structure data is constructed, the system stack required by constructing the tree structure data in a recursion mode is avoided, so that the defect that stack memory overflow is easy to occur when the hierarchy of the tree structure data is too large is effectively avoided, the implementation mode is simple, and an infinite hierarchy tree structure can be quickly constructed.
Further, the step of traversing all the node data again, taking each node data as a root node, screening, according to the node id of each node data, elements of the child node data set corresponding to each node data respectively as child nodes, and constructing tree structure data includes:
s301, with the first node data in sequence as a root node, screening elements of a corresponding first sub-node data set according to the node id of the first node data in sequence as sub-nodes, and constructing to obtain first branch data;
s302, sequentially taking the second node data as a root node, screening corresponding elements of a second node data set according to the node id of the second node data as child nodes, and judging whether the root node data of the first branch data is the elements of the second node data set or whether the second node data is the child node data of the first branch data;
s303, if the first node data in the sequence is an element of the second node data set, forming second branch data according to the second node data in the sequence and the element of the second node data set, and merging the first branch data to the second branch data by taking root node data of the first branch data as child node data of the second branch data to form third branch data;
s304, if the second node data is the child node data of the first branch data, forming second branch data according to the second node data and the elements of the second node data set, and merging the second branch data to the first branch data by taking the root node data of the second branch data as the child node data of the first branch data to form fourth branch data;
s305, if the root node data of the first branch data is not an element of the second node data set and the second ordered node data is not a child node data of the first branch data, forming second branch data according to the second ordered node data and the element of the second node data set, and keeping the first branch data and the second branch data independent;
and S306, traversing all the node data according to the rule, gradually forming new branch data, and combining the new branch data on the basis of the existing branch data to obtain the tree structure data.
In this embodiment, in the process of traversing all the node data, the construction system first uses the first-ranked node data as a root node, and screens the corresponding first node data set according to the node id of the first-ranked node data, and uses elements included in the first node data set as the first-ranked node data adjacent nodes to form first branch data. Then, the construction system takes the second node data as a root node, screens a corresponding second node data set according to the node id of the second node data, takes the element of the second node data set as a child node of the second node data, and judges whether the root node data of the first branch data is the element of the second node data set or whether the second node data is the child node data of the first branch data. And if the first node data in the sequence belongs to elements in the second node data set, constructing second branch data by taking the second node data in the sequence as a root node and taking the elements contained in the second node data set as child nodes. And simultaneously taking the root node data of the first branch data as child node data of the second branch data to merge the first branch data into the second branch data, and forming a new third branch data by the first branch data and the second branch data.
And if the second node data in the sequence is child node data of the first branch data, the construction system takes the second node data in the sequence as a root node and elements of the second node data set as child nodes to construct and obtain second branch data. The root node data of the second branch data is used as child node data of the first branch data, so that the second branch data is merged with the first branch data, and the second branch data and the first branch data form new fourth branch data.
If the root node of the first branch data is not an element of the second node data set and the ordered second node data is not child node data of the first branch data, the construction system constructs the second branch data by taking the ordered second node data as the root node and the element of the second node data set as the child node. And because the corresponding relation between the root node and the child node does not exist before the first branch data and the second branch data, the first branch data and the second branch data are kept independent from each other, and when new branch data with the corresponding relation between the root node and the child node appears in the subsequent traversal process, the first branch data and the second branch data are merged. And traversing all the node data by the construction system according to the rule, gradually forming new branch data, combining the new branch data on the basis of the existing branch data according to the corresponding relation between the root node and the child node, and finally obtaining complete tree structure data containing all the node data.
In the scheme, each branch data is constructed by traversing parent node id of a child node data set by using the node id of the current node data (namely the current root node) on the basis of the current node data, searching for target data and forming a connected data branch, and in addition, the current node data is compared with root node data of other formed branch data (at the moment, if no other formed branch data exists, the comparison with the root node data of the branch data is not needed, and the comparison content comprises the node id and the parent node id so as to connect the current node data with the formed branch data), and the connected data branch is formed after the target data is found. In this embodiment, each branch data is constructed by connecting multiple branches (including the basic child node data set and other branch data already formed) associated with one node data as completely as possible. When the last node data is used as the current root node data, traversing and connecting the parent node id of the basic byte point data set and the root node data of other formed branch data to form the branch data of the last node data, and actually forming the last tree structure data at the same time.
Further, the step of traversing all the node data again, taking each node data as a root node, screening, according to the node id of each node data, elements of the child node data set corresponding to each node data respectively as child nodes, and constructing tree structure data includes:
s307, screening elements of the sub-node data set corresponding to each node data respectively according to the node id of each node data by taking each node data as a root node, and respectively constructing branch data corresponding to each node data;
and S308, traversing all the branch data, taking the node father id of the root node data of each branch data as a merging reference, merging the root node data of each branch data as a child node onto the corresponding branch data, and forming complete tree structure data.
In the scheme, the method is mainly divided into 2 steps, wherein the first step is that on the basis of current node data (namely current root node), parent node id of a child node data set is traversed by using node id of the current node data to form corresponding branch data, and after all the node data are traversed, a plurality of branch data are formed; and then, entering a second step, and performing data arrangement and branch connection on all the branch data to form final tree structure data.
Preferably, in the step of using each node data as a root node, screening, according to a node id of each node data, elements of the child node data set corresponding to each node data respectively as child nodes, and constructing respective branch data corresponding to each node data respectively, the merging step of the single branch data is:
s3081, acquiring a first node father id of the root node data of the first branch data;
s3082, obtaining second branch data where the root node data is located according to the first node father id matching;
s3083, merging the first branch data to the second branch data, wherein the root node data of the first branch data is used as the child node of the second branch data during merging.
In another embodiment, the construction system firstly takes each node data as a root node, screens a corresponding child node data set according to the node id of each node data, and then respectively constructs branch data corresponding to each node data by taking the node data as the root node and taking the element of the node data set as a child node. After branch data corresponding to each node data is formed, the construction system traverses all the branch data, takes the node father id of the root node data of each branch data as a merging reference, merges the root node data of each branch data as a child node to the corresponding branch data, and accordingly forms complete tree structure data containing all the branch data (namely containing all the node data).
Specifically, in the process of traversing all the branch data by the construction system, the merging logic of the single branch data is as follows: the construction system obtains a first node parent id of root node data of first branch data (the first branch data is any branch data, the root node data is node data serving as a root node in the branch data, and the first node parent id is a node parent id of the node data), and then matches second branch data where the root node data is located according to the first node parent id (namely, in the current step, the root node data is regarded as a child node, so that corresponding parent node data (namely, previous layer node data relative to the current node data) can be found according to the first node parent id of the root node data, and the node id of the parent node data is the same as the first node parent id). The construction system merges the first branch data into the second branch data, the merged root node data of the first branch data is used as a child node of the second branch data (namely, in the original second branch data, the root node data of the first branch data exists as a child node, and the step merges the first branch data into the corresponding second score by tracing the source); and according to the merging logic, merging the branch data in sequence, and merging all the branch data to form complete tree structure data.
Examples are as follows: the data of each node are respectively as follows: id: 100000, name: major china, pid: 0; id: 102000, name: guangdong province, pid: 100000; id: 101000, name: jiangsu province, pid: 100000; id: 102010, name: shenzhen city, pid: 102000, respectively; id: 102020, name: guangzhou city, pid: 102000, respectively; id: 101010, name: nanjing, pid: 102000, respectively; id: 102021, name: baoan area, pid: 102010, respectively; id: 102022, name: southern mountain area, pid: 102010(id is node id, pid is node parent id). In the first traversal process, the node data with the same node parent id are classified into the same set, namely a child node data set is formed: { Dazhonghua }, { Guangdong province, Jiangsu province }, { Shenzhen city, Guangzhou city }, { Nanjing city }, { Baoan district, Nanshan district }. During the second pass, all the node data are taken as root nodes, corresponding child node data sets are screened according to node ids of all the node data to be taken as child nodes, and the constructed branch data comprise: branch data a: the root node is: major china, child nodes are: guangdong province, Jiangsu province; branch data B: the root node is: guangdong province, child nodes are: shenzhen city, Guangzhou city; branch data C: the root node is: in Jiangsu province, the child nodes are: nanjing City; branch data D: the root node is: shenzhen city, child nodes are: baoan area, southern mountain area; branch data E: the root node is: guangzhou city, child nodes are: empty; branch data F: the root node is: nanjing City, child nodes are: empty; branch data G: the root node is: the child nodes in the Baoan area are as follows: empty; branch data H: the root node is: in the southern mountain area, the child nodes are: and (4) is empty. When traversing all branches for the third time, taking the node father id of the root node data of each branch data as a merging reference, merging the root node data of each branch data as a child node onto the corresponding branch data, namely: the node father id of the root node of the branch data A has no corresponding node data, and the branch data A is kept unchanged; the root node data of the branch data B is child nodes of the branch data a (for example, the root node of the branch data B is the root node of the branch data a, the parent id of the node of the branch data B is 100000, and the node id of the grand Chinese in the branch data a is 100000, that is, the parent id of the node of the former and the node id of the latter form a match here), so the root node data of the branch data B is merged as the child nodes of the branch data a, the subsequent other branch data are merged according to the above rule, and after all the branch data are merged, the complete tree structure data is obtained.
Further, after the step of traversing all node data, classifying the node data having the same node parent id into the same set, and obtaining a plurality of child node data sets, the method includes:
s4 uses the node parent id corresponding to each child node data set as tag information, and associates each tag information with each child node data set.
In this embodiment, after the construction system obtains a plurality of child node data sets, all elements (elements, that is, node data) in the same child node data set have the same node parent id; therefore, the construction system takes the node parent id corresponding to each child node data set as the mark information, and associates the mark information with each child node data set correspondingly, such as: pid: 100000{ Guangdong province, Jiangsu province }, so that when branch data are constructed subsequently, corresponding node father id can be obtained through direct matching, and child node data sets corresponding to the node data can be rapidly screened.
Further, in the step of traversing all the node data again, taking the node id of each node data as a root node, screening elements of the child node data set corresponding to each node data as child nodes, and constructing to obtain a plurality of branch data, the construction step of a single branch data is as follows:
s3071, acquiring a first node id of the first node data;
s3072, screening a first child node data set corresponding to the second node father id which is the same as the first node id from each child node data set;
s3073, taking all second node data in the first child node data set as child nodes of the first node data, and constructing to obtain the branch data.
In this embodiment, in the process of constructing multiple pieces of branch data by the construction system according to each piece of node data in sequence, the construction logics of the single piece of branch data are the same, and the following description is given to the construction steps of the single piece of branch data: the construction system obtains a first node id of the first node data, and then screens a first child node data set corresponding to a second parent node id which is the same as the first node id from each child node data set. All elements in the first child node data set correspond to the second parent node id, and the second parent node id is the same as the first node id, that is, all elements in the first node data set belong to child nodes of the first node data. Therefore, the construction system takes the first node data as a root node, and all the second node data in the first node data set as child nodes of the first node data, so as to construct a branch data.
Further, before the step of acquiring the data of the plurality of nodes, the method includes:
s5, acquiring original data, and acquiring a node id and a node father id of the original data;
and S6, marking the node id at the front end of the original data, and marking the father id of the node at the back end of the original data to form the node data.
In this embodiment, the construction system obtains original data (for example, data of china, guangdong province, and the like) input by a user, and obtains a node id and a node parent id of the original data labeled by the user. Then, the construction system labels the node id at the front end of the original data and labels the node parent id at the back end of the original data, so as to form node data (such as id: 102000, name: Guangdong province, pid: 100000), which is convenient for the construction of the subsequent tree structure data.
Further, after the step of traversing all the node data again, taking each of the node data as a root node, and screening, according to the node id of each of the node data, elements of the child node data set corresponding to each of the node data as child nodes, and constructing tree structure data, the method includes:
s7, monitoring whether a third node data is entered, wherein the third node data is new node data;
s8, if the fact that third node data are recorded is monitored, calling a third node father id of the third node data, and matching according to the third node father id to obtain a corresponding first root node on the tree structure data;
and S9, taking the third node data as a child node of the first root node, and updating the tree structure data.
In this embodiment, after the tree structure data is obtained by construction, the construction system monitors in real time whether the user enters new node data, that is, third node data (one or more third node data may be used, and the number is not specifically limited here). And if the third node data is monitored to be input, the system is constructed to call a third node father id of the third node data, and a corresponding first root node on the tree structure data is obtained according to matching of the third node father id. And then, taking the third node data as a child node of the first root node, merging the third node data to the first root node, and updating the merged third node data to obtain new tree structure data, wherein the new tree structure data comprises the third node data.
Referring to fig. 2, an embodiment of the present application further provides a device for constructing tree structure data, including:
the first obtaining module 1 is configured to obtain a plurality of node data, where each node data is labeled with a node id and a node parent id, where the node id is an id of the node data, and the node parent id is an id of parent node data corresponding to the node data;
the classification module 2 is used for traversing all node data, classifying the node data with the same node father id into the same set, and obtaining a plurality of child node data sets;
and the building module 3 is configured to traverse all the node data again, take each node data as a root node, screen, according to the node id of each node data, an element of the child node data set corresponding to each node data, and build to obtain tree structure data.
Further, the building module 3 includes:
the first construction unit is used for taking the first ordered node data as a root node, screening elements of a corresponding first child node data set according to the node id of the first ordered node data to be used as child nodes, and constructing to obtain first branch data;
a judging unit, configured to sequentially take second node data in a sequence as a root node, screen, according to a node id of the second node data in the sequence, a corresponding element of a second node data set as a child node, and judge whether root node data of the first branch data is an element of the second node data set, or judge whether the second node data in the sequence is child node data of the first branch data;
a first merging unit, configured to form second branch data according to the second node data and the element of the second node data set if the first node data is the element of the second node data set, and merge the first branch data with the second branch data by using a root node data of the first branch data as a child node data of the second branch data to form a third branch data;
a second merging unit, configured to form second branch data according to the second node data and elements of the second node data set if the second node data is child node data of the first branch data, and merge the second branch data with the first branch data by using root node data of the second branch data as child node data of the first branch data to form fourth branch data;
a second constructing unit, configured to form second branch data according to the second sorted node data and the elements of the second node data set if the root node data of the first branch data is not an element of the second node data set and the second sorted node data is not child node data of the first branch data, and keep the first branch data and the second branch data independent from each other;
and the traversal unit is used for traversing all the node data according to the rule, gradually forming new branch data, and combining the new branch data on the basis of the existing branch data to obtain the tree structure data.
Further, the building module 3 further includes:
a third construction unit, configured to use each node data as a root node, filter, according to a node id of each node data, an element of the child node data set corresponding to each node data, as a child node, and respectively construct branch data corresponding to each node data;
and the third merging unit is used for traversing all the branch data, taking the node father id of the root node data of each branch data as a merging reference, merging the root node data of each branch data as a child node onto the corresponding branch data, and forming complete tree structure data.
Further, the third merging unit includes:
a first obtaining subunit, configured to obtain a first node parent id of root node data of the first branch data;
the matching child unit is used for obtaining second branch data where the root node data is located according to the first node father id matching;
and a merging subunit, configured to merge the first branch data with the second branch data, where a root node data of the first branch data is used as a child node of the second branch data during merging.
Further, the building device further includes:
and the association module 4 is configured to use a node parent id corresponding to each child node data set as tag information, and perform corresponding association between each tag information and each child node data set respectively.
Further, the third building unit includes:
the second acquiring subunit is used for acquiring a first node id of the first node data;
a screening subunit, configured to screen, from each of the child node data sets, a first child node data set corresponding to the second node parent id that is the same as the first node id;
and the constructing subunit is configured to use all the second node data in the first child node data set as child nodes of the first node data, and construct the branch data.
Further, the building device further includes:
the second obtaining module 5 is configured to obtain original data, and obtain a node id and a node parent id of the original data;
and the marking module 6 is used for marking the node id at the front end of the original data and marking the node father id at the rear end of the original data to form the node data.
Further, the building device further includes:
the monitoring module 7 is used for monitoring whether third node data is entered, wherein the third node data is new node data;
the calling module 8 is configured to, if it is monitored that third node data is entered, call a third node parent id of the third node data, and obtain a corresponding first root node on the tree structure data according to matching of the third node parent id;
and the updating module 9 is configured to update the tree structure data by using the third node data as a child node of the first root node.
In this embodiment, each module and unit in the building apparatus are used to correspondingly execute each step in the building method of the tree structure data, and a specific implementation process thereof is not described in detail herein.
In the apparatus for tree-structured data provided in this embodiment, a building system first obtains a plurality of node data, where each node data is labeled with a node id and a node parent id, where the node id is an id of the node data, and the node parent id is an id of parent node data corresponding to the node data. And then traversing all node data, classifying all node data with the same node father id into the same set, and obtaining a plurality of child node data sets. And finally, traversing all the node data again by the construction system, taking each node data as a root node, screening elements of a sub-node data set corresponding to each node data respectively according to the node id of each node data to be used as sub-nodes, and constructing to obtain tree structure data. Compared with the prior art, the method and the device avoid the system stack required by constructing the tree structure data in a recursion mode, avoid the defect that stack memory overflow is easy to occur when the hierarchy of the tree structure data is too large, have a simple implementation mode, and can quickly construct an infinite hierarchy tree structure.
Referring to fig. 3, a computer device, which may be a server and whose internal structure may be as shown in fig. 3, is also provided in the embodiment of the present application. The computer device includes a processor, a memory, a network interface, and a database connected by a system bus. Wherein the computer designed processor is used to provide computational and control capabilities. The memory of the computer device comprises a nonvolatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program, and a database. The internal memory provides an environment for the operation of an operating system and computer programs in the non-volatile storage medium. The database of the computer device is used for storing data such as node data. The network interface of the computer device is used for communicating with an external terminal through a network connection. The computer program is executed by a processor to implement a method of building tree structured data.
The processor executes the steps of the method for constructing the tree structure data:
s1, acquiring a plurality of node data, wherein each node data is marked with a node id and a node father id, the node id is the id of the node data, and the node father id is the id of father node data corresponding to the node data;
s2, traversing all node data, classifying the node data with the same node father id into the same set, and obtaining a plurality of child node data sets;
and S3, traversing all the node data again, taking each node data as a root node, screening the elements of the child node data set corresponding to each node data respectively according to the node id of each node data to be used as child nodes, and constructing to obtain tree structure data.
Further, the step of traversing all the node data again, taking each node data as a root node, screening, according to the node id of each node data, elements of the child node data set corresponding to each node data respectively as child nodes, and constructing tree structure data includes:
s301, with the first node data in sequence as a root node, screening elements of a corresponding first sub-node data set according to the node id of the first node data in sequence as sub-nodes, and constructing to obtain first branch data;
s302, sequentially taking the second node data as a root node, screening corresponding elements of a second node data set according to the node id of the second node data as child nodes, and judging whether the root node data of the first branch data is the elements of the second node data set or whether the second node data is the child node data of the first branch data;
s303, if the first node data in the sequence is an element of the second node data set, forming second branch data according to the second node data in the sequence and the element of the second node data set, and merging the first branch data to the second branch data by taking root node data of the first branch data as child node data of the second branch data to form third branch data;
s304, if the second node data is the child node data of the first branch data, forming second branch data according to the second node data and the elements of the second node data set, and merging the second branch data to the first branch data by taking the root node data of the second branch data as the child node data of the first branch data to form fourth branch data;
s305, if the root node data of the first branch data is not an element of the second node data set and the second ordered node data is not a child node data of the first branch data, forming second branch data according to the second ordered node data and the element of the second node data set, and keeping the first branch data and the second branch data independent;
and S306, traversing all the node data according to the rule, gradually forming new branch data, and combining the new branch data on the basis of the existing branch data to obtain the tree structure data.
Further, the step of traversing all the node data again, taking each node data as a root node, screening, according to the node id of each node data, elements of the child node data set corresponding to each node data respectively as child nodes, and constructing tree structure data includes:
s307, screening elements of the sub-node data set corresponding to each node data respectively according to the node id of each node data by taking each node data as a root node, and respectively constructing branch data corresponding to each node data;
and S308, traversing all the branch data, taking the node father id of the root node data of each branch data as a merging reference, merging the root node data of each branch data as a child node onto the corresponding branch data, and forming complete tree structure data.
Preferably, in the step of using each node data as a root node, screening, according to a node id of each node data, elements of the child node data set corresponding to each node data respectively as child nodes, and constructing respective branch data corresponding to each node data respectively, the merging step of the single branch data is:
s3081, acquiring a first node father id of the root node data of the first branch data;
s3082, obtaining second branch data where the root node data is located according to the first node father id matching;
s3083, merging the first branch data to the second branch data, wherein the root node data of the first branch data is used as the child node of the second branch data during merging.
Further, after the step of traversing all node data, classifying the node data having the same node parent id into the same set, and obtaining a plurality of child node data sets, the method includes:
s4 uses the node parent id corresponding to each child node data set as tag information, and associates each tag information with each child node data set.
Further, in the step of traversing all the node data again, taking the node id of each node data as a root node, screening elements of the child node data set corresponding to each node data as child nodes, and constructing to obtain a plurality of branch data, the construction step of a single branch data is as follows:
s3071, acquiring a first node id of the first node data;
s3072, screening a first child node data set corresponding to the second node father id which is the same as the first node id from each child node data set;
s3073, taking all second node data in the first child node data set as child nodes of the first node data, and constructing to obtain the branch data.
Further, before the step of acquiring the data of the plurality of nodes, the method includes:
s5, acquiring original data, and acquiring a node id and a node father id of the original data;
and S6, marking the node id at the front end of the original data, and marking the father id of the node at the back end of the original data to form the node data.
Further, after the step of traversing all the node data again, taking each of the node data as a root node, and screening, according to the node id of each of the node data, elements of the child node data set corresponding to each of the node data as child nodes, and constructing tree structure data, the method includes:
s7, monitoring whether a third node data is entered, wherein the third node data is new node data;
s8, if the fact that third node data are recorded is monitored, calling a third node father id of the third node data, and matching according to the third node father id to obtain a corresponding first root node on the tree structure data;
and S9, taking the third node data as a child node of the first root node, and updating the tree structure data.
An embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored, where the computer program, when executed by a processor, implements a method for constructing tree-structured data, where the method for constructing tree-structured data specifically includes:
s1, acquiring a plurality of node data, wherein each node data is marked with a node id and a node father id, the node id is the id of the node data, and the node father id is the id of father node data corresponding to the node data;
s2, traversing all node data, classifying the node data with the same node father id into the same set, and obtaining a plurality of child node data sets;
and S3, traversing all the node data again, taking each node data as a root node, screening the elements of the child node data set corresponding to each node data respectively according to the node id of each node data to be used as child nodes, and constructing to obtain tree structure data.
Further, the step of traversing all the node data again, taking each node data as a root node, screening, according to the node id of each node data, elements of the child node data set corresponding to each node data respectively as child nodes, and constructing tree structure data includes:
s301, with the first node data in sequence as a root node, screening elements of a corresponding first sub-node data set according to the node id of the first node data in sequence as sub-nodes, and constructing to obtain first branch data;
s302, sequentially taking the second node data as a root node, screening corresponding elements of a second node data set according to the node id of the second node data as child nodes, and judging whether the root node data of the first branch data is the elements of the second node data set or whether the second node data is the child node data of the first branch data;
s303, if the first node data in the sequence is an element of the second node data set, forming second branch data according to the second node data in the sequence and the element of the second node data set, and merging the first branch data to the second branch data by taking root node data of the first branch data as child node data of the second branch data to form third branch data;
s304, if the second node data is the child node data of the first branch data, forming second branch data according to the second node data and the elements of the second node data set, and merging the second branch data to the first branch data by taking the root node data of the second branch data as the child node data of the first branch data to form fourth branch data;
s305, if the root node data of the first branch data is not an element of the second node data set and the second ordered node data is not a child node data of the first branch data, forming second branch data according to the second ordered node data and the element of the second node data set, and keeping the first branch data and the second branch data independent;
and S306, traversing all the node data according to the rule, gradually forming new branch data, and combining the new branch data on the basis of the existing branch data to obtain the tree structure data.
Further, the step of traversing all the node data again, taking each node data as a root node, screening, according to the node id of each node data, elements of the child node data set corresponding to each node data respectively as child nodes, and constructing tree structure data includes:
s307, screening elements of the sub-node data set corresponding to each node data respectively according to the node id of each node data by taking each node data as a root node, and respectively constructing branch data corresponding to each node data;
and S308, traversing all the branch data, taking the node father id of the root node data of each branch data as a merging reference, merging the root node data of each branch data as a child node onto the corresponding branch data, and forming complete tree structure data.
Preferably, in the step of using each node data as a root node, screening, according to a node id of each node data, elements of the child node data set corresponding to each node data respectively as child nodes, and constructing respective branch data corresponding to each node data respectively, the merging step of the single branch data is:
s3081, acquiring a first node father id of the root node data of the first branch data;
s3082, obtaining second branch data where the root node data is located according to the first node father id matching;
s3083, merging the first branch data to the second branch data, wherein the root node data of the first branch data is used as the child node of the second branch data during merging.
Further, after the step of traversing all node data, classifying the node data having the same node parent id into the same set, and obtaining a plurality of child node data sets, the method includes:
s4 uses the node parent id corresponding to each child node data set as tag information, and associates each tag information with each child node data set.
Further, in the step of traversing all the node data again, taking the node id of each node data as a root node, screening elements of the child node data set corresponding to each node data as child nodes, and constructing to obtain a plurality of branch data, the construction step of a single branch data is as follows:
s3071, acquiring a first node id of the first node data;
s3072, screening a first child node data set corresponding to the second node father id which is the same as the first node id from each child node data set;
s3073, taking all second node data in the first child node data set as child nodes of the first node data, and constructing to obtain the branch data.
Further, before the step of acquiring the data of the plurality of nodes, the method includes:
s5, acquiring original data, and acquiring a node id and a node father id of the original data;
and S6, marking the node id at the front end of the original data, and marking the father id of the node at the back end of the original data to form the node data.
Further, after the step of traversing all the node data again, taking each of the node data as a root node, and screening, according to the node id of each of the node data, elements of the child node data set corresponding to each of the node data as child nodes, and constructing tree structure data, the method includes:
s7, monitoring whether a third node data is entered, wherein the third node data is new node data;
s8, if the fact that third node data are recorded is monitored, calling a third node father id of the third node data, and matching according to the third node father id to obtain a corresponding first root node on the tree structure data;
and S9, taking the third node data as a child node of the first root node, and updating the tree structure data.
It will be understood by those skilled in the art that all or part of the processes of the methods of the above embodiments may be implemented by hardware associated with instructions of a computer program, which may be stored on a non-volatile computer-readable storage medium, and when executed, may include processes of the above embodiments of the methods. Any reference to memory, storage, database, or other medium provided herein and used in the examples may include non-volatile and/or volatile memory. Non-volatile memory can include read-only memory (ROM), Programmable ROM (PROM), Electrically Programmable ROM (EPROM), Electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms such as Static RAM (SRAM), Dynamic RAM (DRAM), Synchronous DRAM (SDRAM), double-rate SDRAM (SSRSDRAM), Enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), Rambus Direct RAM (RDRAM), direct bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, apparatus, first object, or method that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, apparatus, first object, or method. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of another identical element in a process, apparatus, first object or method that comprises the element.
The above description is only for the preferred embodiment of the present application and not intended to limit the scope of the present application, and all modifications of equivalent structures and equivalent processes, which are made by the contents of the specification and the drawings of the present application, or which are directly or indirectly applied to other related technical fields, are intended to be included within the scope of the present application.

Claims (10)

1. A method for constructing tree-structured data, comprising:
acquiring a plurality of node data, wherein each node data is marked with a node id and a node father id, the node id is the id of the node data, and the node father id is the id of father node data corresponding to the node data;
traversing all node data, and classifying all the node data with the same node father id into the same set to obtain a plurality of child node data sets;
traversing all the node data again, taking each node data as a root node, screening elements of the child node data set corresponding to each node data respectively according to the node id of each node data to be used as child nodes, and constructing to obtain tree structure data.
2. The method according to claim 1, wherein the step of traversing all the node data again, using each of the node data as a root node, and screening, according to a node id of each of the node data, elements of the child node data sets corresponding to each of the node data as child nodes to construct tree structure data includes:
taking the first node data as a root node, screening elements of a corresponding first child node data set according to the node id of the first node data as child nodes, and constructing to obtain first branch data;
sequentially taking the second node data as a root node, screening corresponding elements of a second node data set according to the node id of the second node data as child nodes, and judging whether the root node data of the first branch data is the elements of the second node data set or whether the second node data is the child node data of the first branch data;
if the node data of the first order is an element of the second node data set, forming second branch data according to the node data of the second order and the element of the second node data set, and merging the first branch data to the second branch data by taking root node data of the first branch data as child node data of the second branch data to form third branch data;
if the second node data is the child node data of the first branch data, forming second branch data according to the second node data and the elements of the second node data set, and merging the second branch data to the first branch data by taking the root node data of the second branch data as the child node data of the first branch data to form fourth branch data;
if the root node data of the first branch data is not an element of the second node data set and the second-ordered node data is not child node data of the first branch data, forming second branch data according to the second-ordered node data and the element of the second node data set, and keeping the first branch data and the second branch data independent from each other;
and traversing all the node data according to the rule, gradually forming new branch data, and combining the new branch data on the basis of the existing branch data to obtain the tree structure data.
3. The method according to claim 1, wherein the step of traversing all the node data again, using each of the node data as a root node, and screening, according to a node id of each of the node data, elements of the child node data sets corresponding to each of the node data as child nodes to construct tree structure data includes:
screening elements of the child node data set corresponding to each node data respectively according to the node id of each node data to serve as child nodes by taking each node data as a root node, and respectively constructing branch data corresponding to each node data;
traversing all the branch data, taking the node father id of the root node data of each branch data as a merging reference, merging the root node data of each branch data as a child node onto the corresponding branch data, and forming complete tree structure data.
4. The method for constructing tree structure data according to claim 3, wherein in the step of traversing all branch data, taking a node parent id of a root node data of each branch data as a merging reference, and merging the root node data of each branch data as a child node onto the corresponding branch data to form complete tree structure data, the merging step of a single branch data is:
acquiring a first node father id of root node data of first branch data;
obtaining second branch data where the root node data is located according to the first node father id matching;
and merging the first branch data to the second branch data, wherein the root node data of the first branch data is used as a child node of the second branch data during merging.
5. The method according to claim 3, wherein in the step of constructing branch data corresponding to each node data by using each node data as a root node, and by filtering elements of the child node data sets corresponding to each node data according to a node id of each node data as child nodes, the step of constructing a single branch data is:
acquiring a first node id of first node data;
screening a first child node data set corresponding to the second node parent id which is the same as the first node id from each child node data set;
and taking all second node data in the first child node data set as child nodes of the first node data, and constructing to obtain the branch data.
6. The method for constructing tree structure data according to claim 1, wherein the step of obtaining the plurality of node data is preceded by:
acquiring original data, and acquiring a node id and a node father id of the original data;
and marking the node id at the front end of the original data, and marking the node father id at the back end of the original data to form the node data.
7. The method for constructing tree structure data according to claim 1, wherein the step of traversing all node data again, using each node data as a root node, and screening, according to a node id of each node data, elements of the child node data sets corresponding to each node data as child nodes, and constructing to obtain tree structure data includes:
monitoring whether a third node data is input, wherein the third node data is new node data;
if the fact that third node data are input is monitored, a third node father id of the third node data is called, and a corresponding first root node on the tree structure data is obtained according to the matching of the third node father id;
and updating the tree structure data by taking the third node data as a child node of the first root node.
8. An apparatus for constructing tree-structured data, comprising:
the first acquisition module is used for acquiring a plurality of node data, each node data is marked with a node id and a node father id, the node id is the id of the node data, and the node father id is the id of father node data corresponding to the node data;
the classification module is used for traversing all node data, classifying the node data with the same node father id into the same set, and obtaining a plurality of child node data sets;
and the construction module is used for traversing all the node data again, taking each node data as a root node, screening elements of the child node data set corresponding to each node data respectively according to the node id of each node data to be used as child nodes, and constructing to obtain tree structure data.
9. A computer device comprising a memory and a processor, the memory having stored therein a computer program, characterized in that the processor, when executing the computer program, implements the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, carries out the steps of the method of any one of claims 1 to 7.
CN202111536633.0A 2021-12-15 2021-12-15 Tree structure data construction method and device and computer equipment Pending CN114186102A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202111536633.0A CN114186102A (en) 2021-12-15 2021-12-15 Tree structure data construction method and device and computer equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111536633.0A CN114186102A (en) 2021-12-15 2021-12-15 Tree structure data construction method and device and computer equipment

Publications (1)

Publication Number Publication Date
CN114186102A true CN114186102A (en) 2022-03-15

Family

ID=80605191

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111536633.0A Pending CN114186102A (en) 2021-12-15 2021-12-15 Tree structure data construction method and device and computer equipment

Country Status (1)

Country Link
CN (1) CN114186102A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115934829A (en) * 2022-11-30 2023-04-07 湖北清江水电开发有限责任公司 Data display method for drainage basin cascade power station monitoring system database

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115934829A (en) * 2022-11-30 2023-04-07 湖北清江水电开发有限责任公司 Data display method for drainage basin cascade power station monitoring system database
CN115934829B (en) * 2022-11-30 2024-04-19 湖北清江水电开发有限责任公司 Data display method of basin step power station monitoring system database

Similar Documents

Publication Publication Date Title
CN109614309B (en) Method, device, computer equipment and storage medium for comparing test results
CN110442603B (en) Address matching method, device, computer equipment and storage medium
CN110515973B (en) Data query optimization method, device, equipment and storage medium
US10261967B2 (en) Data extraction
US20160252890A1 (en) Method for searching cross-regional power supply area based on cim model and system thereof
CN111324577B (en) Yml file reading and writing method and device
CN109344258A (en) A kind of intelligent self-adaptive sensitive data identifying system and method
CN112612908A (en) Natural resource knowledge graph construction method and device, server and readable memory
CN109308258A (en) Building method, device, computer equipment and the storage medium of test data
CN109285024B (en) Online feature determination method and device, electronic equipment and storage medium
CN114186102A (en) Tree structure data construction method and device and computer equipment
CN114153980A (en) Knowledge graph construction method and device, inspection method and storage medium
CN113157904B (en) Sensitive word filtering method and system based on DFA algorithm
CN111506573A (en) Database table partitioning method and device, computer equipment and storage medium
CN113806647A (en) Method for identifying development framework and related equipment
CN109389972B (en) Quality testing method and device for semantic cloud function, storage medium and equipment
CN105893445A (en) Data processing method, server and terminal device
CN114791985A (en) Domain name matching method and device and prefix tree updating method and device
WO2017043664A1 (en) Information management device, and file management method
CN110472125B (en) Multistage page cascading crawling method and equipment based on web crawler
CN112286916A (en) Data processing method, device, equipment and storage medium
CN113031835A (en) Menu data processing method and device
CN105930453A (en) Repeatability analyzing method and device
TWI292106B (en) Method for constructing traceability ontology map and recoding medium storing traceability ontology map
CN111125449B (en) Object information storage method, device and storage medium

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