Data structure conversion method and device
The data to be converted is added to the tree structure through object references, which solves the problem of memory usage and low efficiency caused by the recursive algorithm, and achieves efficient data structure conversion.
Patent Information
- Application Number
- CN202310369875.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-07
- Publication Date
- 2024-10-15
AI Technical Summary
Among the existing data structure conversion methods, the recursive algorithm takes up a lot of memory space and is inefficient, and the stack push and pop operations are frequent, resulting in low data structure conversion efficiency.
By obtaining the target identifier of the data to be converted, the data is added to the tree structure based on the object reference method, which reduces memory usage and push and pop operations, and improves data structure conversion efficiency.
The efficiency of data structure conversion is significantly improved, especially when processing large amounts of data. The conversion time is reduced from ten seconds to one second, ensuring the performance and speed of data structure conversion.
Smart Images

Figure CN118779367A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a data structure conversion method and device. Background Art
[0002] The data in a database table or file is usually stored one by one, which makes the levels of the data unclear. In order to more intuitively and clearly understand the level relationship between the data in the database table or file, it is necessary to convert the data structure of the database table or file to store the data in the database table or file in a tree structure, so as to display the level relationship between the data through the tree structure.
[0003] At present, data structure conversion is usually performed using a recursive algorithm, which can convert data in a database table or file into data stored in a data structure. However, in the process of data structure conversion, the recursive algorithm uses a lot of memory space, and the recursive process requires continuous stacking and popping, resulting in low efficiency of data structure conversion. Summary of the invention
[0004] In view of this, the present application proposes a data structure conversion method and device, the main purpose of which is to improve the efficiency of data structure conversion.
[0005] In order to achieve the above objectives, this application mainly provides the following technical solutions:
[0006] In a first aspect, the present application provides a data structure conversion method, the data structure conversion method comprising:
[0007] Acquire at least two data to be converted; wherein each data to be converted has a mapped target identifier; the target identifier is an identifier of the mapped data to be converted; the data to be converted includes a parent identifier, and the parent identifier is a target identifier mapped to the parent data of the data to be converted; the parent data is included in the at least two data to be converted;
[0008] Based on the parent identifier included in each of the data to be converted, determining the level corresponding to each of the data to be converted in the tree structure;
[0009] Based on the target identifier of each data to be converted mapping, each data to be converted reference is added as a node in a corresponding level in the tree structure.
[0010] In some embodiments of the present application, based on the parent identifier included in each of the data to be converted, the level corresponding to each of the data to be converted in the tree structure is determined, including: setting the structural conversion order of the at least two data to be converted based on the parent identifier, and traversing each of the data to be converted according to the structural conversion order; if the parent identifier included in the currently traversed data to be converted is a preset identifier, then the top level of the tree structure is determined as the level corresponding to the currently traversed data to be converted in the tree structure.
[0011] In some embodiments of the present application, based on the target identifier of each data mapping to be converted, each data reference to be converted is added as a node in the corresponding level in the tree structure, including: adding the currently traversed data reference to be converted as the top-level node of the tree structure through the target identifier of the currently traversed data mapping to be converted.
[0012] In some embodiments of the present application, the data structure conversion method also includes: if the parent identifier included in the currently traversed data to be converted is a non-preset identifier, then determining the target node in the tree structure, and determining the next level of the level where the target node is located as the level corresponding to the currently traversed data to be converted in the tree structure; wherein the target identifier mapped to the data to be converted corresponding to the target node is the same as the parent identifier included in the currently traversed data to be converted.
[0013] In some embodiments of the present application, based on the target identifier of each data mapping to be converted, each data reference to be converted is added as a node in the corresponding level in the tree structure, including: through the target identifier of the data mapping to be converted currently traversed, the data reference to be converted currently traversed is added as a child node of the target node in the corresponding level in the tree structure.
[0014] In some embodiments of the present application, a reference to the data to be converted that is currently traversed is added as a child node of the target node in the corresponding level in the tree structure, including: if the target node does not have a target field for representing a child node, a target field is added to the data to be converted corresponding to the target node; and the reference to the data to be converted that is currently traversed is added as the value of the target field.
[0015] In some embodiments of the present application, the currently traversed data reference to be converted is added as a child node of the target node in the corresponding level in the tree structure, including: if the target node has a user
[0016] If the target field represents a child node, the currently traversed data reference to be converted is added as the value of the target field, and a separator is added between the currently added value and the existing value of the target field.
[0017] In some embodiments of the present application, the data structure conversion method also includes: for the data to be converted that is not stored in a mapping structure: converting the data to be converted into a mapping structure for storage; wherein the mapping structure includes a key and a value that has a mapping relationship with the key; the key is the target identifier of the data to be converted; and the value is the data to be converted.
[0018] In some embodiments of the present application, the data structure conversion method further includes: if all the data to be converted are referenced and added as nodes in the corresponding level in the tree structure, then the tree structure is output as a data structure conversion result.
[0019] In a second aspect, the present application provides a data structure conversion device, the data structure conversion device comprising:
[0020] An acquisition module, used for acquiring at least two data to be converted; wherein each data to be converted has a mapped target identifier; the target identifier is an identifier of the mapped data to be converted itself; the data to be converted includes a parent identifier, and the parent identifier is a target identifier mapped to the parent data of the data to be converted; the parent data is included in the at least two data to be converted;
[0021] A determination module, configured to determine the level corresponding to each of the to-be-converted data in the tree structure based on the parent identifier included in each of the to-be-converted data;
[0022] The adding module is used to add each reference of the data to be converted as a node in a corresponding level in the tree structure based on the target identifier mapped to each data to be converted.
[0023] In a third aspect, the present application provides a computer-readable storage medium, wherein the storage medium includes a stored program, wherein when the program is running, the device where the storage medium is located is controlled to execute the data structure conversion method of the first aspect.
[0024] In a fourth aspect, the present application provides an electronic device, comprising: a memory for storing a program; and a processor, coupled to the memory, for running the program to execute the data structure conversion method of the first aspect.
[0025] The method and device for data structure conversion provided by the present application, when determining that data structure conversion is required, first obtain all the data to be converted that participate in the data structure conversion, each of which has a mapped target identifier, and the target identifier is the identifier of the mapped data to be converted itself. The data to be converted here includes a parent identifier, and the parent identifier is the target identifier mapped by the parent data of the data to be converted, and the parent data is included in the acquired data to be converted. Then, based on the parent identifier included in each data to be converted, determine the corresponding level of each data to be converted in the tree structure. Finally, based on the target identifier mapped by each data to be converted, add each data to be converted as a node in the corresponding level in the tree structure. It can be seen that in the scheme provided by the embodiment of the present application, since the acquired data to be converted all have a mapped target identifier, when performing data structure conversion, the data to be converted is obtained by object reference according to the target identifier, and the acquired data to be converted is added to the tree structure by reference addition. In this way, the occupation of memory space by the process of data structure conversion can be reduced, and the stack push and pop operations in the data conversion process can be reduced, thereby improving the efficiency of data structure conversion.
[0026] The above description is only an overview of the technical solution of the present application. In order to more clearly understand the technical means of the present application, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present application more obvious and easy to understand, the specific implementation methods of the present application are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0028] Figure 1 A flow chart of a data structure conversion method provided by an embodiment of the present application is shown;
[0029] Figure 2 A schematic diagram of the structure of a data structure conversion device provided by an embodiment of the present application is shown;
[0030] Figure 3 A structural schematic diagram of a data structure conversion device provided in another embodiment of the present application is shown. DETAILED DESCRIPTION
[0031] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.
[0032] At present, data structure conversion is usually performed using a recursive algorithm, which can convert data in a database table or file into data stored in a data structure. However, in the process of data structure conversion, the recursive algorithm uses a lot of memory space, and the recursive process requires continuous stacking and popping, resulting in low efficiency of data structure conversion.
[0033] The inventor has found through research that in order to improve the efficiency of data structure conversion, the data to be converted can be obtained by object reference, and the obtained data to be converted can be added to the tree structure by reference addition. In this way, the object reference method can reduce the memory space occupied by the data structure conversion process, and can reduce the stack push and pop operations in the data structure conversion process, so that the data structure conversion can be performed faster.
[0034] Based on the above considerations, the embodiment of the present application provides a technical solution for data structure conversion, specifically: obtain all data to be converted that participate in the data structure conversion, each of which has a mapped target identifier, and the target identifier is the identifier of the mapped data to be converted itself. The data to be converted here includes a parent identifier, and the parent identifier is the target identifier mapped by the parent data of the data to be converted, and the parent data is included in the acquired data to be converted. Then, based on the parent identifier included in each data to be converted, determine the corresponding level of each data to be converted in the tree structure. Finally, based on the target identifier mapped by each data to be converted, add each data to be converted as a node in the corresponding level in the tree structure. It can be seen that in the solution provided by the embodiment of the present application, since the acquired data to be converted all have a mapped target identifier, when performing data structure conversion, the data to be converted is obtained by object reference according to the target identifier, and the acquired data to be converted is added to the tree structure by reference addition. In this way, the occupation of memory space by the process of data structure conversion can be reduced, and the stack push and pop operations in the data conversion process can be reduced, thereby improving the efficiency of data structure conversion. Even if the amount of data to be converted is large, the performance and speed of data structure conversion can be guaranteed.
[0035] The inventor has found through testing that it takes ten seconds to perform data structure conversion on 50,000 data to be converted by using a recursive algorithm, while it takes one second to perform data structure conversion on these 50,000 data to be converted by using the technical solution for data structure conversion provided in the embodiment of the present application. It can be seen that the technical solution for data structure conversion provided in the embodiment of the present application can optimize the time for data structure conversion and improve the efficiency of data structure conversion. The inventor has also found through testing that the more data to be converted the technical solution for data structure conversion provided in the embodiment of the present application processes, the more obvious the advantage.
[0036] Based on the above technical solution of data structure conversion, the embodiment of the present application provides a data structure conversion method and device. The data structure conversion method and device provided by the embodiment of the present application are specifically described below.
[0037] like Figure 1 As shown, the embodiment of the present application provides a data structure conversion method, which mainly includes the following steps 101 to 103:
[0038] 101. Obtain at least two data to be converted; wherein each data to be converted has a mapped target identifier; the target identifier is an identifier of the mapped data to be converted itself; the data to be converted includes a parent identifier, and the parent identifier is a target identifier mapped to the parent data of the data to be converted; the parent data is included in the at least two data to be converted.
[0039] The data in a database table or file is stored one by one, which makes the levels between these data unclear. In order to understand the level relationship between the data in the database table or file more intuitively and clearly, the data structure of the data in the database table or file can be converted to display the level relationship between the data in a tree structure.
[0040] In order to improve the efficiency of data structure conversion, the embodiment of the present application needs to obtain the data to be converted by object reference, and add the obtained data to be converted to the tree structure by reference addition. Therefore, before obtaining the data to be converted, it is necessary to detect whether the data to be converted is stored in a mapping (Map) structure. The mapping structure storage here refers to the existence of a mapped target identifier for the data to be converted, and the target identifier is the identifier of the mapped data to be converted itself.
[0041] If it is detected that all the data to be converted are stored in a mapping structure, then these data to be converted are obtained for subsequent data structure conversion. If it is detected that there are data to be converted that are not stored in a mapping structure in the data to be converted, then for the data to be converted that are not stored in a mapping structure: convert the data to be converted into a mapping structure for storage. The mapping structure includes a key and a value that has a mapping relationship with the key. The key is the target identifier of the data to be converted, and the value is the data to be converted.
[0042] When the data to be converted are stored in a mapping structure, these data to be converted are obtained for data structure conversion. Each of the obtained data to be converted has a mapped target identifier, and the target identifier is the identifier of the mapped data to be converted itself. The data to be converted also includes a parent identifier, and the parent identifier is the target identifier mapped to the parent data of the data to be converted. The parent data of the data to be converted exists in the obtained data to be converted, so the target identifier and the parent identifier of the data to be converted can be used as the main basis for data structure conversion.
[0043] The specific content of the data to be converted in the embodiment of the present application is related to the application scenario of the data structure conversion, and the present embodiment does not limit the specific content of the data to be converted. In principle, data with a hierarchical relationship in any application scenario can be processed as data to be converted, and the data structure conversion is performed on these data to be converted.
[0044] For example, for a company with a large organizational structure, it will involve a large number of employees. There are many job levels between employees, and there are superior-subordinate relationships between the levels. In order to more clearly and intuitively understand the hierarchical relationship between employees in the company, the employee information of each employee can be used as data to be converted, and the data structure of these employee information is converted to store and display the hierarchical relationship between the employee information through a tree structure. For example, the acquired data to be converted includes data to be converted 1 and data to be converted 2. Data 1 to be converted includes the following content "id: 1, pid: 0, name: 'xsx', age: 28, and the target identifier "1" exists in the mapping relationship of data 1 to be converted. Data 2 to be converted includes the following content "id: 2, pid: 1, name: 'xxx', age: 29", and the target identifier "2" exists in the mapping relationship of data 2 to be converted. The corresponding value of id is the identifier of the data to be converted itself, that is, the target identifier. The corresponding value of pid is the target identifier mapped by the parent data of the data to be converted. The corresponding value of name is the employee name of the employee described by the data to be converted; the corresponding value of age is the age of the employee described by the data to be converted. Data 1 to be converted and data 2 to be converted and their respective mapped target identifiers can exist in the following mapping structure:
[0045] 1: {id: 1, pid: 0, name: 'xsx', age: 28}, 2: {id: 2, pid: 1, name: 'xxx', age: 29}
[0046] 102. Determine the level corresponding to each to-be-converted data in the tree structure based on the parent identifier included in each to-be-converted data.
[0047] For a piece of data to be converted, the parent identifier included in it can indicate whether there is parent data in the acquired data to be converted, and when there is parent data, which piece of data to be converted in the acquired data to be converted is the parent data. It can be seen that the parent identifier included in the data to be converted can reflect the hierarchical relationship between the acquired data to be converted. Therefore, the corresponding level of each piece of data to be converted in the tree structure can be determined based on the parent identifier included in each piece of data to be converted.
[0048] Based on the parent identifier included in each to-be-converted data, the specific process of determining the level corresponding to each to-be-converted data in the tree structure may include the following steps 102A to 102C:
[0049] 102A. Set a structural conversion order of the at least two data to be converted based on the parent identifier, and traverse each data to be converted according to the structural conversion order.
[0050] The parent identifier included in the data to be converted can reflect the hierarchical relationship between the acquired data to be converted, so the structural conversion order of each data to be converted can be set based on the parent identifier. When setting the structural conversion order, it is performed according to the hierarchy from the top level to the bottom level. In addition, in the structural conversion order, the data to be converted with the same parent identifier are sorted continuously.
[0051] Exemplarily, the acquired data to be converted include data 1 to 3 to be converted, and the data 1 to 3 to be converted and their respective mapped target identifiers exist in the following mapping structure:
[0052] 1: {id: 1, pid: 0, name: 'xsx', age: 28}, 2: {id: 2, pid: 1, name: 'xxx', age: 29}, 3: {id: 3, pid: 1, name: 'yst', age: 26}
[0053] It can be seen that the parent identifier of the data to be converted 1 is 0, which indicates that the data to be converted 1 has no parent data. The parent identifier of the data to be converted 2 is 1, which indicates that the parent data of the data to be converted 2 is the data to be converted 1. The parent identifier of the data to be converted 3 is 1, which indicates that the parent data of the data to be converted 2 is the data to be converted 1. The structural conversion order of the above data to be converted 1 to 3 set based on the parent identifier (from the top level to the bottom level) is: data to be converted 1, data to be converted 2, data to be converted 3. It can be seen from the structural conversion order that since the data to be converted 2 and the data to be converted 3 have the same parent identifier 1, the data to be converted 2 and the data to be converted 3 are arranged continuously in the structural conversion order.
[0054] After the structure conversion sequence is set, in order to ensure that all the data to be converted can undergo data structure conversion, each data to be converted is traversed according to the structure conversion sequence until all the acquired data to be converted are traversed.
[0055] 102B. If the parent identifier included in the currently traversed data to be converted is a preset identifier, the top level of the tree structure is determined as the level corresponding to the currently traversed data to be converted in the tree structure.
[0056] The preset identifier is a basis for determining whether the data to be converted has parent data, and can be flexibly set based on business needs. It should be noted that the preset identifier needs to be different from the target identifier of all the acquired data to be converted.
[0057] If the parent identifier of the currently traversed data to be converted is a preset identifier, it means that the currently traversed data to be converted does not have parent data in the acquired data to be converted, and it is data that can be used as a top-level node, so the top level of the tree structure is determined as the level corresponding to the currently traversed data to be converted in the tree structure. The tree structure here is the data storage structure that the acquired data to be converted needs to have after the data structure conversion.
[0058] Exemplarily, the acquired data to be converted includes the above-mentioned data to be converted 1 to 3, the data to be converted that is currently matched according to the structural conversion order is data to be converted 1, and the parent identifier pid included in data to be converted 1 is the preset identifier 0. The preset identifier 0 indicates that the data to be converted 1 does not have parent data in the acquired data to be converted, so the top level of the tree structure is determined to be the level corresponding to the currently traversed data to be converted 1 in the tree structure.
[0059] 102C. If the parent identifier included in the currently traversed data to be converted is a non-preset identifier, determine the target node in the tree structure, and determine the next level of the level where the target node is located as the level corresponding to the currently traversed data to be converted in the tree structure; wherein the target identifier mapped to the data to be converted corresponding to the target node is the same as the parent identifier included in the currently traversed data to be converted.
[0060] If the parent identifier included in the currently traversed data to be converted is a non-preset identifier, it means that the currently traversed data to be converted has parent data in the acquired data to be converted, which cannot be used as data for setting the top-level node, so it is necessary to determine the target node in the node of the current tree structure. The target identifier mapped to the data to be converted corresponding to the target node is the same as the parent identifier included in the currently traversed data to be converted, that is, the data to be converted corresponding to the target node is the parent data of the currently traversed data to be converted. Since the data to be converted corresponding to the target node is the parent data of the currently traversed data to be converted, the node corresponding to the currently traversed data to be converted will inevitably be set as the child node of the target node, so the next level of the level where the target node is located is determined as the level corresponding to the currently traversed data to be converted in the tree structure.
[0061] Exemplarily, the acquired data to be converted includes the above-mentioned data 1 to 3 to be converted, and the currently matched data to be converted is determined to be data 2 to be converted according to the structural conversion order. The data to be converted 2 and its mapped target identifier are represented as follows: 2: {id: 2, pid: 1, name: 'xxx', age: 29}. It can be seen that the parent identifier pid included in the data to be converted 2 is 1, and the parent identifier of the data to be converted 2 is not the preset identifier 0. Therefore, the existing nodes in the current tree structure are traversed, and after traversal, it is determined that the target identifier 1 mapped to the data to be converted 1 corresponding to the top-level node in the tree structure is the same as the parent identifier "1" included in the currently traversed data to be converted 2, so the top-level node in the tree structure is determined to be the target node. The next level of the top level where the target node is located is determined as the level corresponding to the currently traversed data to be converted 2 in the tree structure.
[0062] 103. Based on the target identifier of each data to be converted mapping, add each data to be converted reference as a node in a corresponding level in the tree structure.
[0063] In order to avoid confusion in data structure conversion, each time a data to be converted is traversed in the order of structure conversion, and after determining the corresponding level of the currently traversed data to be converted in the tree structure, the currently traversed data to be converted needs to be added to the tree structure. Since the currently traversed data to be converted corresponds to different levels in the tree structure, the specific process of adding each data to be converted reference as a node in the corresponding level in the tree structure based on the target identifier mapped to each data to be converted includes the following two situations:
[0064] First, when the top level of the tree structure is determined as the level corresponding to the currently traversed data to be converted in the tree structure, the currently traversed data to be converted reference is added as the top level node of the tree structure through the target identifier mapped by the currently traversed data to be converted.
[0065] In order to improve the efficiency of data structure conversion, the embodiment of the present application needs to obtain the data to be converted by object reference, and add the obtained data to be converted to the tree structure by reference addition. This can reduce the stack push and pop operations in the data conversion process, so that the data structure conversion can be performed faster.
[0066] Exemplarily, the data to be converted currently traversed is data to be converted 1, and the top level of the tree structure is determined as the level corresponding to the data to be converted 1 currently traversed in the tree structure. The data to be converted 1 and its mapped target identifier are represented as follows: 1: {id: 1, pid: 0, name: 'xsx', age: 28}, therefore, in accordance with the object reference method, the data to be converted 1 is referenced by the target identifier "1", and the reference to the data to be converted 1 is added as the top node of the tree structure.
[0067] The tree structure with the top-level nodes added is:
[0068] Const result = [{
[0069] name: 'xsx',
[0070] age:28,
[0071] id: 1,
[0072] pid:0,}]
[0073] The second method is to determine the next level of the level where the target node is located as the level corresponding to the currently traversed data to be converted in the tree structure, and add the currently traversed data to be converted reference as a child node of the target node in the corresponding level in the tree structure through the target identifier mapped by the currently traversed data to be converted.
[0074] Similarly, in order to improve the efficiency of data structure conversion, the embodiment of the present application needs to obtain the currently traversed data to be converted by object reference, and add the currently traversed data to be converted as a child node of the target node in the corresponding level in the tree structure by reference addition. This can reduce the stack push and pop operations in the data conversion process, so that the data structure conversion can be performed faster.
[0075] The specific process of adding the currently traversed data reference to be converted as a child node of the target node in the corresponding level in the tree structure includes the following two steps:
[0076] One is, if the target node does not have a target field for representing a child node, then a target field is added for the data to be converted corresponding to the target node; and a reference to the currently traversed data to be converted is added as a value of the target field.
[0077] If the target node does not have a target field, it means that the target node has not yet been added with a child node, so it is necessary to add a target field for the data to be converted corresponding to the target node. The target field here is used to indicate that the target node has a child node, and the child node is the value corresponding to the target field. The target field can be set based on business needs, and this embodiment does not make specific limitations. Exemplarily, the target field is children.
[0078] Exemplarily, the data to be converted currently traversed is data 2 to be converted, and the target identifier of data 2 to be converted and its mapping is represented as follows: 2: {id: 2, pid: 1, name: 'xxx', age: 29}. The top-level node in the tree structure is determined to be the target node, and the next level of the top level where the target node is located is determined as the level corresponding to the currently traversed data 2 to be converted in the tree structure. Since the top-level node in the current tree structure has not yet added child nodes, the top-level node does not have a target field for representing child nodes, so the target field children is added to the data to be converted corresponding to the target node, forming a tree structure as shown below:
[0079] Const result = [{
[0080] name: 'xsx',
[0081] age:28,
[0082] id: 1,
[0083] pid: 0,
[0084] children:[]
[0085] }]
[0086] Then, through the target identifier "2" of the currently traversed data mapping 2 to be converted, the reference of the currently traversed data 2 to be converted is added as the value of the target field children, forming a tree structure as shown below:
[0087] Const result = [{
[0088] name: 'xsx',
[0089] age:28,
[0090] id: 1,
[0091] pid: 0,
[0092] children:[{name:'xxx',age:29,id:2,pid:1}]
[0093] }]
[0094] Another is that, if the target node has a target field for representing a child node, the currently traversed data reference to be converted is added as the value of the target field, and a separator is added between the currently added value and the existing value of the target field.
[0095] If the target node has a target field, it means that the target node has been added with a child node, and the currently traversed data to be converted needs to be a new child node of the target node. Therefore, the currently traversed data to be converted is added as the value of the target field, so that the currently traversed data to be converted becomes a new child node of the target node.
[0096] After adding the currently traversed data reference to be converted as the value of the target field, in order to avoid confusion between the currently added value and the existing value of the target field, a separator is added between the currently added value and the existing value of the target field. The separator here can be determined based on business needs and is not specifically limited in this embodiment. Exemplarily, the separator is a comma.
[0097] Exemplarily, the data to be converted that is currently traversed is data 3 to be converted, and the target identifier of data 3 to be converted and its mapping is represented as follows: 3: {id: 3, pid: 1, name: 'yst', age: 26}. The top-level node in the tree structure is determined to be the target node, and the next level of the top level where the target node is located is determined as the level corresponding to the currently traversed data 3 to be converted in the tree structure. Since the top-level node in the current tree structure has added the child node corresponding to data 2 to be converted, the top-level node has a target field for representing the child node. At this time, the reference of the currently traversed data 3 to be converted is added as the value of the target field children, and a separator "," is added between the currently added value and the existing value of the target field, forming a tree structure as shown below:
[0098] Const result = [{
[0099] name: 'xsx',
[0100] age:28,
[0101] id: 1,
[0102] pid: 0,
[0103] children:[{name:'xxx',age:29,id:2,pid:1},{name:'yst',age:26,id:3,pid:1,}]
[0104] }]
[0105] In either of the above two situations, after adding the reference of the currently traversed data to be converted to the tree structure, the current tree structure needs to be used as the tree structure for determining the corresponding level of the next traversed data to be converted.
[0106] Furthermore, if all the data to be converted are added as nodes in the corresponding levels in the tree structure, the tree structure is output as the data structure conversion result.
[0107] If all the data to be converted are referenced and added as nodes in the corresponding levels of the tree structure, it means that the acquired data to be converted have completed the data structure conversion. Therefore, in order to enable users to use the converted tree structure, the tree structure is output as the data structure conversion result. The output method here can be determined based on business needs, and includes at least the following two methods: First, the tree structure is input into a specified file, so that it is convenient to retrieve the tree structure from the specified file for use later. Second, the tree structure is displayed in a specified interface for users to view.
[0108] The data structure conversion method provided by the embodiment of the present application, when determining that the data structure conversion needs to be performed, first obtains all the data to be converted that participate in the data structure conversion, and each of the data to be converted here has a mapped target identifier, and the target identifier is the identifier of the mapped data to be converted itself. The data to be converted here includes a parent identifier, and the parent identifier is the target identifier mapped by the parent data of the data to be converted, and the parent data is included in the acquired data to be converted. Then based on the parent identifier included in each data to be converted, determine the corresponding level of each data to be converted in the tree structure. Finally, based on the target identifier mapped by each data to be converted, add each data to be converted as a node in the corresponding level in the tree structure. It can be seen that in the scheme provided by the embodiment of the present application, since the acquired data to be converted all have a mapped target identifier, when performing data structure conversion, the data to be converted is obtained by object reference according to the target identifier, and the acquired data to be converted is added to the tree structure by reference addition. In this way, the occupation of memory space by the process of data structure conversion can be reduced, and the stack push and pop operations in the data conversion process can be reduced, thereby improving the efficiency of data structure conversion.
[0109] Furthermore, an embodiment of the present application also provides a data structure conversion device, such as Figure 2 As shown, the data structure conversion device includes:
[0110] The acquisition module 21 is used to acquire at least two data to be converted; each of the data to be converted has a mapped target identifier; the target identifier is an identifier of the mapped data to be converted; the data to be converted includes a parent identifier, and the parent identifier is a target identifier mapped to the parent data of the data to be converted; the parent data is included in the at least two data to be converted;
[0111] A determination module 22, configured to determine the level corresponding to each of the to-be-converted data in the tree structure based on the parent identifier included in each of the to-be-converted data;
[0112] The adding module 23 is used to add each of the references of the data to be converted as a node in a corresponding level in the tree structure based on the target identifier of each mapping of the data to be converted.
[0113] The data structure conversion device provided by the embodiment of the present application, when determining that data structure conversion is required, first obtains all the data to be converted that participate in the data structure conversion, wherein each data to be converted here has a mapped target identifier, and the target identifier is the identifier of the mapped data to be converted itself. The data to be converted here includes a parent identifier, and the parent identifier is the target identifier mapped by the parent data of the data to be converted, and the parent data is included in the acquired data to be converted. Then, based on the parent identifier included in each data to be converted, the corresponding level of each data to be converted in the tree structure is determined. Finally, based on the target identifier mapped by each data to be converted, each data to be converted is referenced and added as a node in the corresponding level in the tree structure. It can be seen that in the scheme provided by the embodiment of the present application, since the acquired data to be converted all have a mapped target identifier, when performing data structure conversion, the data to be converted is obtained by object reference according to the target identifier, and the acquired data to be converted is added to the tree structure by reference addition. In this way, the occupation of memory space by the process of data structure conversion can be reduced, and the stack push and pop operations in the data conversion process can be reduced, thereby improving the efficiency of data structure conversion.
[0114] In some embodiments of the present application, Figure 3 As shown, the determination module 22 includes:
[0115] A setting unit 221, configured to set a structure conversion order of the at least two data to be converted based on a parent identifier, and traverse each of the data to be converted according to the structure conversion order;
[0116] The first determining unit 222 is configured to determine the top level of the tree structure as the level corresponding to the currently traversed data to be converted in the tree structure if the parent level identifier included in the currently traversed data to be converted is a preset identifier.
[0117] In some embodiments of the present application, Figure 3 As shown, the adding module 23 includes:
[0118] The first adding unit 231 is used to add the reference of the currently traversed data to be converted as a top-level node of the tree structure through the target identifier of the currently traversed data to be converted mapping.
[0119] In some embodiments of the present application, Figure 3 As shown, the determination module 22 also includes:
[0120] The second determination unit 223 is used to determine the target node in the tree structure if the parent identifier included in the currently traversed data to be converted is a non-preset identifier, and determine the next level of the level where the target node is located as the level corresponding to the currently traversed data to be converted in the tree structure; wherein the target identifier mapped to the data to be converted corresponding to the target node is the same as the parent identifier included in the currently traversed data to be converted.
[0121] In some embodiments of the present application, Figure 3 As shown, the adding module 23 includes:
[0122] The second adding unit 232 is used to add the reference of the currently traversed data to be converted as a child node of the target node in the corresponding level in the tree structure through the target identifier of the currently traversed data to be converted mapping.
[0123] In some embodiments of the present application, Figure 3 As shown, the second adding unit 232 is specifically used to add a target field for the data to be converted corresponding to the target node if the target node does not have a target field for representing a child node; and add the reference of the currently traversed data to be converted as the value of the target field.
[0124] In some embodiments of the present application, Figure 3 As shown, the second adding unit 232 is specifically used to add the currently traversed data reference to be converted as the value of the target field if the target node has a target field for representing a child node, and add a separator between the currently added value and the existing value of the target field.
[0125] In some embodiments of the present application, Figure 3 As shown, the data structure conversion device also includes:
[0126] The conversion module 24 is used for: for the data to be converted that is not stored in a mapping structure: converting the data to be converted into a mapping structure for storage; wherein the mapping structure includes a key and a value that has a mapping relationship with the key; the key is the target identifier of the data to be converted; and the value is the data to be converted.
[0127] In some embodiments of the present application, Figure 3 As shown, the data structure conversion device also includes:
[0128] The output module 25 is used to output the tree structure as a data structure conversion result if all the data to be converted are referenced and added as nodes in the corresponding level in the tree structure.
[0129] In the data structure conversion device provided in the embodiment of the present application, the detailed description of the method adopted during the operation of each functional module can be found in the corresponding detailed description of the method in the above-mentioned data structure conversion method embodiment, which will not be repeated here.
[0130] Furthermore, an embodiment of the present application also provides a computer-readable storage medium, wherein the storage medium includes a stored program, wherein when the program is executed, the device where the storage medium is located is controlled to execute the above-mentioned data structure conversion method.
[0131] Furthermore, an embodiment of the present application also provides an electronic device, which includes: a memory for storing a program; and a processor, coupled to the memory, for running the program to execute the above-mentioned data structure conversion method.
[0132] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0133] It is understandable that the related features in the above methods and devices can be referenced to each other. In addition, the "first", "second" and the like in the above embodiments are used to distinguish the embodiments, but do not represent the advantages and disadvantages of the embodiments.
[0134] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0135] The algorithm and display provided herein are not inherently related to any particular computer, virtual system or other device. Various general purpose systems can also be used together with the teachings based on this. According to the above description, it is obvious to construct the structure required for this type of system. In addition, the application is not directed to any specific programming language either. It should be understood that various programming languages can be utilized to realize the content of the application described herein, and the description of the above specific language is to disclose the preferred embodiment of the application.
[0136] In addition, the memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.
[0137] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0138] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data cutover device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data cutover device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0139] These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data switching device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0140] These computer program instructions can also be loaded onto a computer or other programmable data transfer device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0141] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0142] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0143] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.
[0144] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0145] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
[0146] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.
Claims
1. A data structure conversion method, It is characterized in that The method comprises: Acquire at least two data to be converted; wherein each data to be converted has a mapped target identifier; the target identifier is an identifier of the mapped data to be converted; the data to be converted includes a parent identifier, and the parent identifier is a target identifier mapped to the parent data of the data to be converted; the parent data is included in the at least two data to be converted; Based on the parent identifier included in each of the data to be converted, determining the level corresponding to each of the data to be converted in the tree structure; Based on the target identifier of each data to be converted mapping, each data to be converted reference is added as a node in a corresponding level in the tree structure.
2. The method according to claim 1, It is characterized in that Determining the level corresponding to each of the to-be-converted data in the tree structure based on the parent identifier included in each of the to-be-converted data includes: Setting a structural conversion order of the at least two data to be converted based on the parent identifier, and traversing each of the data to be converted according to the structural conversion order; If the parent identifier included in the currently traversed data to be converted is a preset identifier, the top level of the tree structure is determined as the level corresponding to the currently traversed data to be converted in the tree structure.
3. The method according to claim 2, It is characterized in that Based on the target identifier of each data to be converted mapping, each data to be converted reference is added as a node in a corresponding level in the tree structure, including: By using the target identifier of the currently traversed data to be converted mapping, the currently traversed data to be converted reference is added as a top-level node of the tree structure.
4. The method according to claim 2, It is characterized in that The method further comprises: If the parent identifier included in the currently traversed data to be converted is a non-preset identifier, the target node in the tree structure is determined, and the next level of the level where the target node is located is determined as the level corresponding to the currently traversed data to be converted in the tree structure; wherein the target identifier mapped to the data to be converted corresponding to the target node is the same as the parent identifier included in the currently traversed data to be converted.
5. The method according to claim 4, It is characterized in that Based on the target identifier of each data to be converted mapping, each data to be converted reference is added as a node in a corresponding level in the tree structure, including: By using the target identifier of the currently traversed data to be converted mapping, the currently traversed data to be converted reference is added as a child node of the target node in the corresponding level in the tree structure.
6. The method according to claim 5, It is characterized in that Adding the currently traversed data reference to be converted as a child node of the target node in the corresponding level in the tree structure includes: If the target node does not have a target field for representing a child node, adding a target field to the data to be converted corresponding to the target node; Add the currently traversed data reference to be converted as the value of the target field; and / or, Add the currently traversed data reference to be converted as a child node of the target node in the corresponding level in the tree structure, including: If the target node has a target field for representing a child node, the currently traversed data reference to be converted is added as the value of the target field, and a separator is added between the currently added value and the existing value of the target field.
7. The method according to any one of claims 1 to 6, It is characterized in that The method further comprises: For the data to be converted that is not stored in a mapping structure: convert the data to be converted into a mapping structure for storage; wherein the mapping structure includes a key and a value that has a mapping relationship with the key; the key is the target identifier of the data to be converted; and the value is the data to be converted; and / or, The method further comprises: If all the data to be converted are added as nodes in the corresponding levels in the tree structure, the tree structure is output as a data structure conversion result.
8. A data structure conversion device, It is characterized in that The device comprises: An acquisition module, used for acquiring at least two data to be converted; wherein each data to be converted has a mapped target identifier; the target identifier is an identifier of the mapped data to be converted itself; the data to be converted includes a parent identifier, and the parent identifier is a target identifier mapped to the parent data of the data to be converted; the parent data is included in the at least two data to be converted; A determination module, configured to determine the level corresponding to each of the to-be-converted data in the tree structure based on the parent identifier included in each of the to-be-converted data; The adding module is used to add each reference of the data to be converted as a node in a corresponding level in the tree structure based on the target identifier mapped to each data to be converted.
9. A computer-readable storage medium, It is characterized in that The storage medium includes a stored program, wherein when the program is executed, the device where the storage medium is located is controlled to execute the data structure conversion method described in any one of claims 1 to 7.
10. An electronic device, It is characterized in that The electronic device comprises: Memory, used to store programs; A processor, coupled to the memory, is used to run the program to execute the data structure conversion method described in any one of claims 1 to 7.
Citation Information
Patent Citations
Storage method for document data in document warehouse system
CN101369268A
Data structure conversion method and apparatus
CN105279177A
A method of converting json data with parent-child relationships into tree-structured data
CN109460410A
Tree data synchronization method and synchronous circulation model thereof, electronic equipment and medium
CN115858686A
Cited By
Method and device for realizing conversion from xml tiled structure to nested structure by using prefix tree and medium
CN122174796A
