Data processing method and device, equipment and storage medium
By using hash operation in the balanced tree data structure to generate the target node and record the identification of the predecessor and successor nodes in the linked list, the problem of low performance of query predecessor and successor nodes in the balanced tree is solved, and the query efficiency improvement of O(1) complexity is achieved.
Patent Information
- Application Number
- CN202510578793.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-06
- Publication Date
- 2025-08-19
AI Technical Summary
The performance of querying predecessor nodes and successor nodes in the balanced tree data structure is low, especially in the TreeMap structure, the query complexity increases logarithmicly with the increase in data scale.
The target node is generated by hashing the data to be inserted, and the node is inserted in the hash table. At the same time, the identification of the predecessor and successor nodes is recorded in the bidirectional linked list, forming an ordered linked list structure to ensure that the node has a direct pointing relationship in the linked list.
The complexity of data query is reduced, especially the query efficiency of the front-drive node and successor node is improved, and the query complexity is reduced from O(log n) to O(1).
Smart Images

Figure CN120508558A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a data processing method, apparatus, device, and storage medium. Background Art
[0002] Balanced tree data structures are widely used for storing and retrieving ordered data. For example, the TreeMap interface in the Java standard library implements an ordered mapping interface based on a balanced tree data structure. It maintains an ordered arrangement of data and supports operations such as range queries and key-value traversal.
[0003] However, due to the limitations of the balanced tree data structure, the time complexity of most operations in TreeMap grows logarithmically with the increase of data size, especially when querying data for successor nodes, there is a problem of low query performance. Summary of the Invention
[0004] The purpose of this application is to provide a data processing method, device, equipment and storage medium to address the deficiencies in the above-mentioned prior art, so as to solve the problem of low TreeMap query performance in the prior art.
[0005] To achieve the above objectives, the technical solutions adopted in this application are as follows:
[0006] In a first aspect, the present application provides a data processing method, the method comprising:
[0007] Perform a hash operation on the data to be inserted to obtain the key of the data to be inserted;
[0008] Generate a first target node corresponding to the data to be inserted according to the key of the data to be inserted and the data to be inserted, and insert the first target node into the hash table, where the first target node includes the key and the value;
[0009] Determine a predecessor node and a successor node of the first target node in the linked list according to the current first node and the last node of the linked list and the key of the first target node;
[0010] According to the predecessor node and the successor node of the first target node in the linked list, the identifier of the predecessor node and the identifier of the successor node are recorded in the first target node, and the first target node is inserted into the linked list, wherein the linked list is a bidirectional and ordered linked list, and each node in the linked list is sorted according to the order of the values in each node.
[0011] In a second aspect, the present application provides a data processing device, comprising:
[0012] A calculation module, used for performing a hash operation on the data to be inserted to obtain a key of the data to be inserted;
[0013] A generating module, configured to generate a first target node corresponding to the data to be inserted according to the key of the data to be inserted and the data to be inserted, and insert the first target node into a hash table, wherein the first target node includes a key and a value;
[0014] a determination module, configured to determine a predecessor node and a successor node of the first target node in the linked list based on the current first node and last node of the linked list and the key of the first target node;
[0015] A processing module is used to record the identifier of the predecessor node and the identifier of the successor node in the first target node according to the predecessor node and the successor node of the first target node in the linked list, and insert the first target node into the linked list, wherein the linked list is a bidirectional and ordered linked list, and each node in the linked list is sorted according to the order of the values in each node.
[0016] In a third aspect, an embodiment of the present application further provides an electronic device comprising: a processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor. When the electronic device is running, the processor communicates with the storage medium through the bus, and the processor executes the machine-readable instructions to perform the steps of a data processing method as described in any one of the first aspects.
[0017] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of a data processing method as described in any one of the first aspects are executed.
[0018] The beneficial effects of the present application are as follows: the key and value of the data to be inserted are combined into a node and inserted into a hash table, and the predecessor node and successor node in the linked list are determined according to the key of the node, and the identifiers of the predecessor and successor nodes are recorded in the node accordingly, and the node is inserted into the linked list. Through this insertion process, the target data can be quickly queried in the hash table or the data in the predecessor or successor node can be quickly queried when querying. Specifically, by recording the identifier of the predecessor node and the identifier of the successor node in the node, the problem of low query efficiency of the TreeMap structure in the prior art due to the non-adjacent positions of the predecessor node and the successor node is solved, the complexity of subsequent data queries is reduced, and especially the query efficiency of the predecessor node and the successor node is improved.
[0019] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can be obtained based on these drawings without creative work.
[0021] Figure 1 An example diagram of a TreeMap structure provided in an embodiment of the present application is shown;
[0022] Figure 2 A flow chart of a data processing method provided by an embodiment of the present application is shown;
[0023] Figure 3 A flowchart for determining a predecessor node and a successor node provided by an embodiment of the present application is shown;
[0024] Figure 4 Another flow chart for determining a predecessor node and a successor node provided by an embodiment of the present application is shown;
[0025] Figure 5 A schematic diagram of the structure of a hash table and a linked list provided in an embodiment of the present application is shown;
[0026] Figure 6 A flowchart of inserting a first target node provided by an embodiment of the present application is shown;
[0027] Figure 7 Another flowchart of inserting a first target node provided by an embodiment of the present application is shown;
[0028] Figure 8 A flowchart of querying node values provided by an embodiment of the present application is shown;
[0029] Figure 9 A flowchart of determining a third target node provided by an embodiment of the present application is shown;
[0030] Figure 10 A flowchart of querying the successor nodes of a specified node provided by an embodiment of the present application is shown;
[0031] Figure 11 A flowchart of querying a predecessor node of a specified node provided by an embodiment of the present application is shown;
[0032] Figure 12 A flowchart of updating node values provided by an embodiment of the present application is shown;
[0033] Figure 13A flowchart of deleting a node provided by an embodiment of the present application is shown;
[0034] Figure 14 A schematic structural diagram of a data processing device provided in an embodiment of the present application is shown;
[0035] Figure 15 A schematic structural diagram of an electronic device 150 provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0036] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. The components of the embodiments of the present application generally described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work are within the scope of protection of this application.
[0037] It should be noted that the term "comprising" will be used in the embodiments of the present application to indicate the existence of the features declared thereafter, but does not exclude the addition of other features.
[0038] Balanced tree structures are widely used in data storage and retrieval scenarios, and red-black trees are a common example. For example, TreeMap is an ordered mapping interface based on red-black trees, which can sort key-value pairs according to the natural order of the keys or a specified order.
[0039] However, the balance property of the red-black tree cannot guarantee the physical adjacency of consecutive key values. If you need to query the successor node or the predecessor node, you need to traverse up or down step by step from the current node. Under certain query modes, there may be problems with low query performance.
[0040] For example, Figure 1 This is a diagram showing an example of a TreeMap structure. Figure 1 Take the TreeMap structure in as an example. Assume that the current node t is 20. To find the successor node of the current node, if there is a right subtree for the current node in the TreeMap structure, you need to start traversing from the right subtree of the current node, find the leftmost node 22 in the right subtree, and use 22 as the successor node of the current node t.
[0041] Continue with Figure 1Take the TreeMap structure in as an example, assuming that the current node t is 25 and the current node t has no right subtree, then the successor node is in the parent node chain of the current node. When querying the successor node of the current node, it is necessary to traverse upwards until an ancestor node that is a child node of a left subtree is found. Then the parent node of the ancestor node can be used as the successor node, such as Figure 1 In the TreeMap structure, assuming that the current node t is 25, since t has no right subtree, it is necessary to traverse upward layer by layer and finally obtain the successor node 30.
[0042] The above example is only used to illustrate the query of the successor node. When querying the predecessor node, there is also the problem of traversing the balanced tree upward or downward. The specific examples are not described in detail in this application.
[0043] It can be seen that when determining predecessor nodes and successor nodes based on existing technologies, since continuous key values in a balanced tree structure cannot guarantee physical adjacency, some scenarios of querying predecessor nodes and successor nodes suffer from low query performance.
[0044] Based on this, the present application proposes a data processing method, which is aimed at the problem of low query performance in the scenario of querying predecessor nodes and successor nodes due to the balanced characteristics of the balanced tree structure. By sorting the nodes according to the natural order of the keys or the custom order, the identifiers of the predecessor nodes and successor nodes are recorded in the nodes, so that the nodes show a bidirectional linked list structure. Without changing the structure of the balanced tree itself, even if the predecessor nodes and successor nodes are not physically adjacent to the current node in the balanced tree, the data in the predecessor node or successor node of the query node can be quickly determined. This effectively reduces the complexity of data processing operations in the balanced tree structure and improves the efficiency of data processing.
[0045] Next, the data processing method of this application is combined with Figure 2 The execution subject of this method may be an electronic device, such as Figure 2 As shown, the method includes:
[0046] S201. Perform a hash operation on the data to be inserted to obtain a key of the data to be inserted.
[0047] The key can be obtained by performing a hash operation on the data. The data to be inserted can be of any data type and length. By performing a hash operation on the data to be inserted, the input data of any length can be converted into a fixed-length hash value, and the hash value is used as the key for the data to be inserted.
[0048] It should be understood that the key of the data to be inserted can also be obtained by other means, such as determining the key based on the natural attributes of the data to be inserted, or obtaining the key by performing operations on the data to be inserted based on custom rules. The specific method will not be described in detail in this application.
[0049] S202 : Generate a first target node corresponding to the data to be inserted according to the key of the data to be inserted and the data to be inserted, and insert the first target node into the hash table.
[0050] The first target node includes a key and a value. The key of the first target node can be the key of the data to be inserted, and the value of the first target node can be the data to be inserted. The representation of the first target node can be Entry<K,V> , where K represents the key of the data to be inserted and V represents the data to be inserted itself.
[0051] Optionally, the hash table includes multiple array elements, each array element is used to store a key-value pair, and each array index indicates an array element in the hash array. Inserting the first target node into the hash table may be determining an array element corresponding to the first target node in the hash table.
[0052] The key of the data to be inserted can indicate the position of the first target node in the hash table. A hash table is an array-based data structure that uses a hash function to calculate the array index corresponding to the key, enabling fast data access. In one possible implementation, the array index can be calculated based on the key of the data to be inserted, and the position indicated by the array index is used as the position of the first target node in the hash table.
[0053] It should be understood that the hash table in this application can be a hash table with an actual physical structure or a hash table without an actual physical structure, as long as the association between the position in the hash table and the first target node can be achieved. After calculating the array index based on the key of the data to be inserted, the array index can be recorded in the first target node, or the first target node can be inserted into the position corresponding to the array index in the hash table, thereby achieving the binding of the first target node to the stored position in the hash table.
[0054] Continuing with the above example, assume that the first target node is represented by Entry<K,V> , insert the first target node into the hash table, which can be the first target node as a value in the hash table, and the key of the first target node as the key of the hash table. At this time, the content stored in the hash table can be represented as HashMap <K,Entry<K,V> >.
[0055] S203 : Determine the predecessor node and successor node of the first target node in the linked list according to the current first node and last node of the linked list and the key of the first target node.
[0056] A hash table can be used in conjunction with a linked list to store the first target node. The linked list can be a doubly linked list, meaning that each node in the linked list stores the location information of its predecessor and successor nodes, and the data in the linked list is arranged in order. A linked list can contain multiple nodes, and the array elements in the hash table point to a node in the linked list.
[0057] It should be understood that if a hash conflict occurs in the array elements in the hash table, that is, when the array indexes calculated by the keys of multiple data to be inserted all point to the current array element, the new data can be added to the linked list corresponding to the array element.
[0058] If the array indexes calculated for multiple nodes all point to the same array element in the hash table, the nodes can be saved in the form of a linked list in the order in which the data is inserted, and the pointer of the array element points to the first node of the linked list.
[0059] The first node of the linked list may be the first node inserted into the hash table, and the last node of the linked list may be the last node inserted into the hash table.
[0060] In one possible implementation, a head node member variable head, a tail node member variable tail, and a hash table member variable treeMapLinkedHashMap can be added. The head node member variable head is used to record the head node in the linked list, the tail node member variable tail is used to record the tail node in the linked list, and the hash table member variable treeMapLinkedHashMap is used to record each node in the hash table.
[0061] As an example, after a set of data of the TreeMap structure is represented as a hash table and a linked list structure in this application, the predecessor node of the first target node in the linked list can be the predecessor node of the first target node in the TreeMap structure, and the successor node of the first target node in the linked list can be the successor node of the first target node in the TreeMap structure.
[0062] In an embodiment of the present application, the predecessor node and the successor node of a node have a direct and clear pointing relationship in the linked list, so that based on this pointing relationship, subsequent rapid query of the predecessor node and the successor node of the node is achieved.
[0063] Optionally, the nodes of the linked list store position information of the predecessor node and the successor node, and the linked list is an ordered linked list. Based on the first node and the last node of the linked list, all the nodes in the linked list can be obtained. By comparing the key of the first target node with the keys of the nodes in the linked list, the position of the first target node in the linked list can be determined, so that the node before the position is used as the predecessor node and the node after the position is used as the successor node.
[0064] S204 , recording the identifier of the predecessor node and the identifier of the successor node in the first target node according to the predecessor node and the successor node of the first target node in the linked list, and inserting the first target node into the linked list.
[0065] The linked list is a bidirectional, ordered list, where the nodes are sorted by the order of their values. The order of the node values can be a natural order, such as from largest to smallest or from smallest to largest, or a user-defined order.
[0066] Optionally, the first target node also includes an identifier of a predecessor node and an identifier of a successor node, wherein the identifier of the predecessor node may be a pointer to the predecessor node or a key of the predecessor node, and the identifier of the successor node may be a pointer to the successor node or a key of the successor node.
[0067] As a possible implementation, taking the implementation of the method of the present application in a TreeMap as an example, a member variable prev of the predecessor node and a member variable next of the successor node can be added to the key-value pair structure of each node in the TreeMap. Among them, the member variable prev of the predecessor node is used to record the identifier of the predecessor node, and the member variable next of the successor node is used to record the identifier of the successor node.
[0068] It is worth noting that when the identifier of the predecessor node and the identifier of the successor node are recorded in the first target node, this recorded information in the node will form a linked list, that is, by reading the identifier of the predecessor node in the first target node, the predecessor node of the first target node can be obtained, and by reading the identifier of the successor node in the first target node, the successor node of the first target node can be directly obtained.
[0069] In an embodiment of the present application, a node is inserted into a hash table by forming the key and value of the data to be inserted, and its predecessor node and successor node in the linked list are determined according to the key of this node, and the identifiers of the predecessor and successor nodes are recorded in the node accordingly, and the node is inserted into the linked list. Through this insertion process, it is possible to quickly query the target data in the hash table or quickly query the data in the predecessor or successor node when querying. Specifically, by recording the identifier of the predecessor node and the identifier of the successor node in the node, the problem of high query complexity and low efficiency caused by the fact that the positions of the predecessor node and the successor node in the TreeMap structure in the prior art are not necessarily adjacent is solved, the complexity of subsequent data queries is reduced, and especially the query efficiency of the predecessor node and the successor node is improved.
[0070] The following is a further explanation of the above-mentioned method of determining the predecessor node and successor node of the first target node in the linked list based on the current first node and tail node of the linked list and the key of the first target node, such as Figure 3 As shown, the above step S203 includes:
[0071] S301: Determine whether the linked list currently has a head node and a tail node.
[0072] S302: If yes, obtain the key in the tail node.
[0073] S303 : Determine the predecessor node and the successor node of the first target node in the linked list according to the key in the tail node and the key of the first target node.
[0074] Alternatively, you can check whether the first node and the last node in the linked list are empty. If the first node is not empty, it means that the first node exists. Similarly, if the last node is not empty, the last node exists. For example, if the content of the first node and the last node is "NULL", then the first node and the last node are considered to be empty.
[0075] If both the first and last nodes are not empty, the key of the last node is obtained. The key can indicate the storage position of the last node in the hash table. Based on the key of the last node and the key of the first target node, the insertion position of the first target node in the linked list can be determined. The node before the insertion position is used as the predecessor node, and the node after the insertion position is used as the successor node.
[0076] The following is a further explanation of the above-mentioned method of determining the predecessor node and the successor node of the first target node in the linked list based on the key in the tail node and the key of the first target node. Figure 4 As shown, the above step S303 includes:
[0077] S401: If the key of the first target node meets a preset condition compared with the key of the tail node, determine that the predecessor node of the first target node is the tail node and the successor node of the first target node is empty.
[0078] The preset condition is related to the order of the nodes in the linked list. As mentioned above, the order of the nodes in the linked list can be from large to small or from small to large, or can also be a user-defined order.
[0079] As a possible implementation method, if the arrangement order of the nodes in the linked list is from small to large, the preset condition can be that the key of the first target node is greater than the key of the tail node. At this time, the tail node can be used as the predecessor node of the first target node, and the successor node of the first target node can be set to empty. At this time, the first target node becomes the new tail node.
[0080] As another possible implementation, if the nodes in the linked list are arranged in descending order, the preset condition may be that the key of the first target node is smaller than the key of the tail node.
[0081] S402. Otherwise, starting from the tail node, search the linked list for the second target node that meets the preset conditions, and determine that the predecessor node of the first target node is the second target node, and the successor node of the first target node is the current successor node of the second target node.
[0082] If the key of the first target node and the key of the tail node do not meet the preset conditions, the nodes meeting the preset conditions can be searched in sequence in the linked list as the second target node.
[0083] Each node includes the identifier of the predecessor node and the identifier of the successor node. The search starts from the first node or the last node one by one until the key of the found node and the key of the first target node meet the preset conditions. The found node is then used as the second target node, and the first target node is inserted between the second target node and the successor node of the second target node. For example, the second target node can be used as the predecessor node of the first target node, and the successor node of the second target node can be used as the successor node of the first target node.
[0084] Exemplarily, you can start searching forward from the tail node one by one, and find the predecessor node of the tail node according to the identifier of the predecessor node of the tail node. If the key of the predecessor node of the tail node and the key of the first target node meet the preset conditions, the predecessor node of the tail node will be used as the second target node. If the preset conditions are not met, continue searching forward until the second target node that meets the preset conditions is found.
[0085] Figure 5 It is a structural diagram of a hash table and a linked list, where the red dotted line is used to point to the predecessor node of a node, the black dotted line is used to point to the successor node of a node, and the solid line is used to point to the node stored in the hash table, or to represent the connection relationship between linked lists. Figure 5 The last node in the linked list is the node where "30" is located, and the first node is the node where "10" is located. The linked list is arranged from smallest to largest. Suppose the first target node to be inserted is "32". The key of "32" is greater than the key of the last node "30". Therefore, the current last node "30" can be used as the predecessor node of "32" and the successor node of "32" is set to null. At this time, "32" is the current last node of the linked list.
[0086] In another example, if the first target node to be inserted is "27", since the key of the first target node "27" is smaller than the tail node "32", you can traverse forward from the tail node. Since the predecessor node "25" of the tail node is smaller than the first target node "27", you can use node "25" as the predecessor node of the first target node "27", and use the tail node "30" as the successor node of the first target node "27". At this time, the tail node of the linked list is still "30".
[0087] Next, the process of recording the identifier of the predecessor node and the identifier of the successor node in the first target node according to the predecessor node and the successor node of the first target node in the linked list and inserting the first target node into the linked list is described.
[0088] In the first implementation, the identifier of the predecessor node can be the position information of the predecessor node in the linked list, and the identifier of the successor node can be the position information of the successor node in the linked list. The position information includes the hash table position and the linked list position. For example, Figure 5 The location information of the middle node "20" can be expressed as "(0,2)", which means that the node "20" is the second node in the linked list of the 0th hash bucket of the hash table.
[0089] In the second implementation, the identifier of the predecessor node and the identifier of the successor node can also be pointers. Figure 6 This is the second implementation method to insert the first target node into the linked list. Figure 6 , the above-mentioned step S204 includes:
[0090] S601: Update the successor node of the predecessor node to the first target node.
[0091] Optionally, the identifier of the successor node of the predecessor node may be assigned as a pointer to the first target node, thereby establishing a connection between the first target node and the predecessor node.
[0092] S602: If the successor node of the first target node is empty, assign the identifier of the predecessor node in the first target node to a pointer to the predecessor node, and assign the identifier of the successor node in the first target node to empty.
[0093] If the first target node is inserted at the end of the linked list, and there is no successor node for the first target node as the tail node of the linked list, that is, the successor node of the first target node is empty, then the predecessor node of the first target node can be the tail node of the linked list before the first target node is inserted.
[0094] Continue to refer to Figure 5 , assuming the first target node is "32", insert the first target node into Figure 5After the tail node "30" of the linked list shown, node "30" is the predecessor node of node "32", and node "32" becomes the current tail node of the linked list. The identifier of the predecessor node of the first target node can be assigned to a pointer to node "30", and the identifier of the successor node of the first target node is assigned to empty.
[0095] S603. If the successor node of the first target node is not empty, the identifier of the predecessor node in the first target node is assigned to a pointer to the predecessor node, and the identifier of the successor node in the first target node is assigned to a pointer to the successor node, and the predecessor node of the successor node is updated to the first target node.
[0096] If the insertion position of the first target node is not the end of the linked list, for example, the first target node is inserted in the middle of the linked list, and the first target node has a predecessor node and a successor node. After determining the predecessor node and the successor node of the first target node based on the above step S402, the identifier of the successor node in the first target node can be assigned as a pointer to the position of the successor node in the linked list, and the identifier of the predecessor node in the first target node can be assigned as a pointer to the position of the predecessor node in the linked list.
[0097] It is worth noting that if the insertion position of the first target node is in the middle of the linked list, it is necessary not only to modify the identifier of the predecessor node and the identifier of the successor node stored in the first target node, but also to update the identifier of the successor node stored in the predecessor node of the first target node, and to update the identifier of the predecessor node stored in the successor node of the first target node.
[0098] For example, continue to refer to Figure 5 , assuming that the first target node is "23", based on the above S402 step, traversing from the tail node, it is determined that the predecessor node of the first target node is "22" and the successor node of the first target node is "25", then the identifier of the successor node in the first target node can be assigned to a pointer to the node "25", and the identifier of the predecessor node in the first target node can be assigned to a pointer to the node "22", and the identifier of the successor node of the node "22" can be updated to a pointer to the first target node, and the identifier of the predecessor node of the node "25" can be updated to a pointer to the first target node.
[0099] The following is a further explanation of the above-mentioned steps of generating the first target node corresponding to the data to be inserted according to the key of the data to be inserted and the data to be inserted, and inserting the first target node into the hash table. Figure 7 As shown, the above step S202 includes:
[0100] S701. Use the key of the data to be inserted as the key of the first target node and use the data to be inserted as the value of the first target node to generate the first target node.
[0101] The type of the data to be inserted can be a number, a string, or an object. As a possible implementation, the key of the data to be inserted can be set as the key of the first target data node, and the data to be inserted itself can be set as the value of the first target node. Based on this method, the first target node is generated, and the first target node includes the key of the data to be inserted and the data to be inserted itself.
[0102] Optionally, the key of the data to be inserted is used to uniquely identify the data to be inserted, and operations such as searching, inserting, and deleting the data to be inserted can be implemented through the key.
[0103] S702: Determine a first target position of the first target node in the hash table according to the key of the first target node.
[0104] Optionally, the key of the first target node may be mapped to a position in a hash table, and the first target position may be a position mapped to the hash table after the key of the first target node is calculated.
[0105] As a possible implementation manner, a hash function may be used to calculate the key of the first target node to obtain a hash value, and the hash value is used to indicate the first target position in the hash table.
[0106] For example, assuming that the first target node is (101, 25), after hashing the key "101" of the first target node, the resulting hash value is 1. Then 1 can be used as an index to search the hash table. The position in the hash table stores a pointer to a linked list, and the pointer points to a node or a group of nodes in the linked list.
[0107] S703: Insert the first target node into the first target position.
[0108] Alternatively, if there is no other node at the first target position, the first target node can be directly inserted into the first target position in the hash table. If other nodes are already stored in the first target position, the first target node needs to be inserted before or after the stored node, thereby inserting the first target node into the first target position.
[0109] After the data to be inserted is inserted into the hash table and the linked list, the data can be added, deleted, modified, and checked on the node according to the information in the hash table and the hash linked list.
[0110] The method comprises inserting a node into a hash table by combining the key and value of the data to be inserted, determining the predecessor node and successor node of the node in the linked list based on the key of the node, recording the identifiers of the predecessor and successor nodes in the node accordingly, and inserting the node into the linked list. This insertion process allows the target data to be quickly found in the hash table during a query, or the predecessor node or successor node of the node can be found based on the predecessor node or successor node, especially the query for the predecessor node and the successor node. The method of the present application can greatly reduce the complexity of the query, which is further explained below in conjunction with the embodiments.
[0111] Figure 8 It is a flowchart for data query, such as Figure 8 As shown, the steps for data query include:
[0112] S801: Obtain a query key and a query instruction.
[0113] Optionally, the query key may be a basis for locating the node where the data to be queried is located in the hash table and the linked list, and the query key may be an identification ID of the data.
[0114] Optionally, the query instruction may specify the query operation to be performed, including query conditions, query range, etc. Exemplarily, the query instruction may indicate the node corresponding to the query key, or multiple query nodes that meet specific query conditions.
[0115] S802: According to the query key and the query instruction, query in the hash table or linked list to obtain at least one third target node.
[0116] Optionally, the query key can be used as the input of the hash function, and the hash value can be calculated by the hash function. The hash value is used as the index to obtain the position of the query starting point in the hash table. Starting from the query starting point, the key of the node stored in the hash table is matched with the query key. If there is a match, the matched node can be used as the third target node.
[0117] For example, refer to Figure 5 , assuming that the hash value of the query key is 3, the node stored in the position with index value 3 is "22". If the key of node "22" matches the query key, node "22" can be used as the third target node.
[0118] If the query starting point points to multiple nodes in the linked list, you can start from the starting node of the linked list and query backward one by one. If the key of the node queried in the linked list matches the query key, the queried node will be used as the third target node.
[0119] For example, continue to refer to Figure 5, assuming that the hash value of the query key is 6, the nodes stored in the position with index value 6 include "25" and "30". Starting the query from the starting node, the key of node "30" matches the query key, then node "30" can be used as the third target node.
[0120] S803: Use the value of each third target node as the query result, and return the query result.
[0121] Optionally, if there are multiple query keys and the query instruction is an instruction to query multiple data, the third target node corresponding to each query key can be determined in sequence, and the value of each third target node can be used as the query result.
[0122] The following is a further explanation of the above-mentioned query key and query instruction, which is used to query the hash table or linked list to obtain at least one third target node. Figure 9 As shown, the above step S802 includes:
[0123] S901: If the query instruction instructs to query the value of the node to which the query key belongs, determine the second target position of the query key in the hash table.
[0124] S902: Use the node stored in the second target position as the third target node.
[0125] If the query instruction is an instruction to query data corresponding to the key, the second target position of the query key in the hash table can be determined.
[0126] Optionally, the second target position can be the position of the hash bucket storing the node in the hash table. If multiple nodes are stored in the hash bucket, that is, the hash values of multiple nodes all point to the hash bucket, then each node is stored in the form of a linked list, and the hash bucket points to the first node in the linked list. The second target position can be the starting node in the linked list stored in the hash bucket.
[0127] As another possible implementation, if a plurality of nodes are stored in the hash bucket, the second target position may further indicate the position of the node indicated by the query key in the linked list formed by these nodes.
[0128] Among them, when determining the second target position of the query key, the query key can be hashed to obtain the hash value of the query key, the hash value is used as the index to search the hash table to obtain the position corresponding to the index, and the position corresponding to the index is used as the second target position.
[0129] When querying nodes based on the query key, the node corresponding to the query key can be directly determined from the hash table and linked list based on the query key. The time complexity is O(1), which improves the efficiency of data query.
[0130] Next, the steps of querying the predecessor node and the successor node of a node are described with reference to the embodiments.
[0131] Figure 10 It is a flowchart for finding successor nodes. Figure 10 The process of searching for at least one third target node in a hash table or a linked list according to the query key and the query instruction includes:
[0132] S1001. If the query instruction indicates to query the value of the successor node of the node to which the query key belongs, determine the second target position of the query key in the hash table.
[0133] S1002. Read the node to which the query key belongs from the second target position.
[0134] The second target position is the position of the node indicated by the query key in the hash table. If the query instruction indicates that the node to be queried is a successor node of the node to which the query key belongs, the position of the node to which the query key belongs can be determined in the hash table first.
[0135] Optionally, the method for determining the second target position of the node indicated by the query key may be the same as the above-mentioned step S901, which is not described in detail in this application.
[0136] S1003: Read the identifier of the successor node recorded in the node to which the query key belongs.
[0137] S1004: Use the node pointed to by the identifier of the successor node in the linked list as the third target node.
[0138] Optionally, the node to which the query key belongs records an identifier of the successor node, and the position of the successor node can be determined based on the identifier of the successor node, and the third target node can be obtained by reading from the position.
[0139] For example, refer to Figure 5 , assuming that the node to which the query key belongs is "22", and the identifier of the recorded successor node points to node "25", then node "25" can be used as the third target node.
[0140] Figure 11 This is a flowchart for finding the predecessor node. Figure 11 The process of searching for at least one third target node in a hash table or a linked list according to the query key and the query instruction includes:
[0141] S1101. If the query instruction indicates to query the value of the predecessor node of the node to which the query key belongs, determine the second target position of the query key in the hash table.
[0142] S1102: Read the node to which the query key belongs from the second target position.
[0143] The second target position is the position of the node indicated by the query key in the hash table. If the query instruction indicates that the node to be queried is a predecessor node of the node to which the query key belongs, the position of the node to which the query key belongs can be first determined in the hash table. The method for determining the second target position of the node indicated by the query key can be the same as step S901 above, and this application will not repeat it here.
[0144] S1103: Read the identifier of the predecessor node recorded in the node to which the query key belongs.
[0145] S1104: Use the node pointed to by the identifier of the predecessor node in the linked list as the third target node.
[0146] Optionally, the node to which the query key belongs records an identifier of the predecessor node, and the position of the predecessor node can be determined based on the identifier of the predecessor node, and the third target node can be obtained by reading from the position.
[0147] For example, refer to Figure 5 , assuming that the node to which the query key belongs is "22", and the identifier of the recorded predecessor node points to node "20", then node "20" can be used as the third target node.
[0148] In one possible implementation, if the query instruction is to query the first key-value pair or the last key-value pair, the value of the first node can be directly read from the hash table to obtain the first key-value pair, and the value of the last node can be read from the hash table to obtain the last key-value pair.
[0149] In another possible implementation, if the query instruction is to query key-value pairs that meet preset conditions within a given range, the nodes that meet the preset conditions within the given range can be determined from the hash table based on the query instruction, and the key-value pairs in the nodes can be used as the nodes to be returned.
[0150] For example, assuming that the values of each node are stored in ascending order, if you need to query the minimum key-value pair greater than a given key, you can determine the position of the given key from the hash table, and use the successor node of the given key as the node to be returned. If you need to query the maximum key-value pair less than a given key, you can determine the position of the given key from the hash table, and use the predecessor node of the given key as the node to be returned.
[0151] In the embodiment of the present application, by recording the identifiers of the predecessor node and the successor node in the node, when querying the predecessor node or the successor node of a node, it is not necessary to traverse each node. The value of the predecessor node or the successor node can be directly obtained based on the identifier of the predecessor node or the successor node recorded in the node, thereby reducing the complexity of querying the predecessor node and the successor node. Specifically, based on the existing technology, the complexity of querying the predecessor node and the successor node from the TreeMap structure is O(log n), while when querying based on the method of the present application, the complexity is reduced to O(1).
[0152] Based on the method of the present application, the complexity of updating or deleting nodes can be greatly reduced, which will be further explained in conjunction with the embodiments below.
[0153] Figure 12 This is a flowchart for updating nodes, refer to Figure 12 , the method of the present application further includes:
[0154] S1201. Obtain update key and update value.
[0155] S1202: Query the node to be updated in the hash table according to the update key.
[0156] S1203: Update the value of the node to be updated to the updated value.
[0157] Optionally, the update key may be a basis for locating the node where the data to be updated is located in the hash table and the linked list, the update key may be an identification ID of the data, and the update value may be the data to be updated.
[0158] Optionally, the position of the node to be updated in the hash table may be determined according to the update key, and the value of the node to be updated may be updated to the update value.
[0159] For example, assume that the update key indicates that an update is required. Figure 5 The value of the node "22" is "24". After searching the hash table for the node "22" to be updated, the value of the node "22" can be replaced with "24".
[0160] Figure 13 This is a flowchart for deleting nodes. Figure 13 , the method of the present application further includes:
[0161] S1301. Get the delete key.
[0162] Optionally, the delete key may be a basis for locating the node where the data to be deleted is located in the hash table and the linked list. Exemplarily, the delete key may be an identification ID of the data.
[0163] S1302: Query the node to be deleted in the hash table according to the delete key.
[0164] Optionally, the position of the node to be deleted in the hash table can be determined according to the delete key, and the node to be deleted can be deleted from the hash table and the linked list.
[0165] S1303 , according to the identifier of the predecessor node and the identifier of the successor node recorded in the node to be deleted, delete the node to be deleted from the linked list, and delete the node to be deleted from the hash table.
[0166] After deleting a node, the identifiers of the predecessor node and the successor node recorded in the predecessor node and successor node of the node to be deleted in the linked list also need to be updated. For example, the identifier of the successor node recorded in the predecessor node of the node to be deleted is updated to the successor node of the node to be deleted, and the identifier of the predecessor node recorded in the successor node of the node to be deleted is updated to the predecessor node of the node to be deleted.
[0167] It should be understood that by constructing a hash table and a linked list, the present application can convert the operations on key-value pairs in the existing technology in the TreeMap into operations on nodes in the hash table, that is, by performing add, delete, modify and query operations on the nodes in the hash table, the key-value pairs in the TreeMap structure can be added, deleted, modified and checked.
[0168] In one possible implementation, taking the TreeMap structure as an example, if the node to be deleted has two child nodes, the successor node of the node to be deleted can be determined in the hash table first, the value of the successor node can be copied to the node to be deleted, and the successor node can be deleted. If the node to be deleted has only one child node, the child node can be connected to the parent node of the node to be deleted, and the node to be deleted can be deleted directly. If the node to be deleted has no child nodes, the node to be deleted can be deleted directly.
[0169] Based on the same inventive concept, a data processing device corresponding to the data processing method is also provided in the embodiment of the present application. Since the principle of solving the problem by the device in the embodiment of the present application is similar to the above-mentioned data processing method in the embodiment of the present application, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0170] Figure 14 A module structure diagram of a data processing device provided in an embodiment of the present application, such as Figure 14 As shown, the device includes:
[0171] Operation module 1401, configured to perform a hash operation on the data to be inserted to obtain a key of the data to be inserted;
[0172] A generating module 1402 is configured to generate a first target node corresponding to the data to be inserted according to the key of the data to be inserted and the data to be inserted, and insert the first target node into the hash table, wherein the first target node includes the key and the value;
[0173] Determining module 1403, for determining the predecessor node and successor node of the first target node in the linked list according to the current first node and last node of the linked list and the key of the first target node;
[0174] Processing module 1404 is used to record the identifier of the predecessor node and the identifier of the successor node in the first target node according to the predecessor node and the successor node of the first target node in the linked list, and insert the first target node into the linked list, wherein the linked list is a bidirectional and ordered linked list, and each node in the linked list is sorted according to the order of the values in each node.
[0175] Optionally, the determining module 1403 is specifically configured to:
[0176] Determine whether the linked list currently has a first node and a tail node;
[0177] If so, get the key in the tail node;
[0178] According to the key in the tail node and the key of the first target node, the predecessor node and the successor node of the first target node in the linked list are determined.
[0179] Optionally, the determining module 1403 is specifically configured to:
[0180] If the key of the first target node meets the preset condition compared with the key of the tail node, the predecessor node of the first target node is determined to be the tail node, and the successor node of the first target node is empty;
[0181] Otherwise, starting from the tail node, search the linked list for the second target node that meets the preset conditions, and determine the predecessor node of the first target node as the second target node, and the successor node of the first target node as the current successor node of the second target node.
[0182] Optionally, the processing module 1404 is specifically configured to:
[0183] Update the successor node of the predecessor node to the first target node;
[0184] If the successor node of the first target node is empty, the identifier of the predecessor node in the first target node is assigned to a pointer to the predecessor node, and the identifier of the successor node in the first target node is assigned to empty;
[0185] If the successor node of the first target node is not empty, the identifier of the predecessor node in the first target node is assigned to a pointer to the predecessor node, and the identifier of the successor node in the first target node is assigned to a pointer to the successor node, and the predecessor node of the successor node is updated to the first target node.
[0186] Optionally, the generating module 1402 is specifically configured to:
[0187] Using the key of the data to be inserted as the key of the first target node and the data to be inserted as the value of the first target node to generate the first target node;
[0188] Determining a first target position of the first target node in the hash table according to the key of the first target node;
[0189] Insert the first target node at the first target position.
[0190] Optionally, the device further includes a query module, configured to:
[0191] Get the query key and query command;
[0192] According to the query key and the query instruction, query in the hash table or the linked list to obtain at least one third target node;
[0193] The value of each third target node is used as the query result, and the query result is returned.
[0194] Optionally, the query module is further configured to:
[0195] If the query instruction indicates to query the value of the node to which the query key belongs, determining the second target position of the query key in the hash table;
[0196] The node stored in the second target location is used as the third target node.
[0197] Optionally, the query module is further configured to:
[0198] If the query instruction indicates to query the value of the successor node of the node to which the query key belongs, determining the second target position of the query key in the hash table;
[0199] Read the node to which the query key belongs from the second target location;
[0200] Read the identifier of the successor node recorded in the node to which the query key belongs;
[0201] The node pointed to by the identifier of the successor node in the linked list is used as the third target node.
[0202] Optionally, the query module is further configured to:
[0203] If the query instruction indicates to query the value of the predecessor node of the node to which the query key belongs, determining the second target position of the query key in the hash table;
[0204] Read the node to which the query key belongs from the second target location;
[0205] Read the ID of the predecessor node recorded in the node to which the query key belongs;
[0206] The node pointed to by the identifier of the predecessor node in the linked list is used as the third target node.
[0207] Optionally, the device further includes an updating module, which is configured to:
[0208] Get the update key and update value;
[0209] According to the update key, query the node to be updated in the hash table;
[0210] Update the value of the node to be updated to the updated value.
[0211] Optionally, the device further includes a deletion module, which is configured to:
[0212] Get the delete key;
[0213] According to the deletion key, query the hash table for the node to be deleted;
[0214] According to the identifier of the predecessor node and the identifier of the successor node recorded in the node to be deleted, the node to be deleted is deleted from the linked list, and the node to be deleted is deleted from the hash table.
[0215] The present application inserts a node into a hash table by forming the key and value of the data to be inserted, and determines the predecessor node and successor node in the linked list based on the key of this node, and records the identifiers of the predecessor and successor nodes in the node accordingly, and inserts the node into the linked list. Through this insertion process, the target data can be quickly queried in the hash table or the data in the predecessor or successor node can be quickly queried when querying. Specifically, by recording the identifier of the predecessor node and the identifier of the successor node in the node of the TreeMap, the predecessor node and the successor node that may not be adjacent in the original TreeMap structure have a direct pointing relationship, thereby reducing the complexity of subsequent data queries, especially improving the query efficiency of the predecessor node and the successor node.
[0216] The present application embodiment also provides a computer device 150, such as Figure 15 FIG. 1 is a schematic diagram of the structure of a computer device 150 provided in an embodiment of the present application, including: a processor 1501, a memory 1502, and optionally, a bus 1503. The memory 1502 stores machine-readable instructions (e.g., Figure 14 When the computer device 150 is running, the processor 1501 communicates with the memory 1502 via the bus 1503. When the machine-readable instructions are executed by the processor 1501, the steps of the data processing method in the above method embodiment are performed.
[0217] An embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the data processing method in the above method embodiment are executed.
[0218] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the system and device described above can refer to the corresponding process in the method embodiment, and will not be repeated in this application. In the several embodiments provided in this application, it should be understood that the disclosed system, device and method can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation. For example, multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.
[0219] In addition, the functional units in the various embodiments of the present application can be integrated into a processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. If the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0220] The above is only a specific implementation method of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with this technical field can easily think of changes or replacements within the technical scope disclosed in this application, which should be covered by the protection scope of the present application.
Claims
1. A data processing method, characterized in that: include: Perform a hash operation on the data to be inserted to obtain the key of the data to be inserted; Generate a first target node corresponding to the data to be inserted according to the key of the data to be inserted and the data to be inserted, and insert the first target node into the hash table, where the first target node includes the key and the value; Determine a predecessor node and a successor node of the first target node in the linked list according to the current first node and the last node of the linked list and the key of the first target node; According to the predecessor node and the successor node of the first target node in the linked list, the identifier of the predecessor node and the identifier of the successor node are recorded in the first target node, and the first target node is inserted into the linked list, wherein the linked list is a bidirectional and ordered linked list, and each node in the linked list is sorted according to the order of the values in each node.
2. The method according to claim 1, characterized in that The determining, based on the current first node and last node of the linked list and the key of the first target node, a predecessor node and a successor node of the first target node in the linked list includes: Determine whether the linked list currently has a first node and a tail node; If so, obtain the key in the tail node; A predecessor node and a successor node of the first target node in the linked list are determined according to the key in the tail node and the key of the first target node.
3. The method according to claim 2, characterized in that The determining, according to the key in the tail node and the key of the first target node, a predecessor node and a successor node of the first target node in the linked list includes: If the key of the first target node meets a preset condition compared to the key of the tail node, then the predecessor node of the first target node is determined to be the tail node, and the successor node of the first target node is empty; Otherwise, starting with the tail node, search the linked list for the second target node that meets the preset conditions in sequence, and determine that the predecessor node of the first target node is the second target node, and the successor node of the first target node is the current successor node of the second target node.
4. The method according to claim 1, wherein The step of recording an identifier of the predecessor node and an identifier of the successor node in the first target node according to the predecessor node and the successor node of the first target node in the linked list, and inserting the first target node into the linked list includes: Updating the successor node of the predecessor node to the first target node; If the successor node of the first target node is empty, assigning the identifier of the predecessor node in the first target node to a pointer to the predecessor node, and assigning the identifier of the successor node in the first target node to empty; If the successor node of the first target node is not empty, the identifier of the predecessor node in the first target node is assigned to a pointer to the predecessor node, and the identifier of the successor node in the first target node is assigned to a pointer to the successor node, and the predecessor node of the successor node is updated to the first target node.
5. The method according to claim 1, wherein Generating a first target node corresponding to the data to be inserted according to the key of the data to be inserted and the data to be inserted, and inserting the first target node into the hash table, includes: Using the key of the data to be inserted as the key of the first target node and using the data to be inserted as the value of the first target node to generate the first target node; Determining a first target position of the first target node in the hash table according to the key of the first target node; Insert the first target node into the first target position.
6. The method according to any one of claims 1 to 5, characterized in that The method further comprises: Get the query key and query command; According to the query key and the query instruction, query the hash table or the linked list to obtain at least one third target node; The value of each third target node is used as a query result, and the query result is returned.
7. The method according to claim 6, characterized in that The step of querying the hash table or the linked list to obtain at least one third target node according to the query key and the query instruction includes: If the query instruction indicates to query the value of the node to which the query key belongs, determining a second target position of the query key in the hash table; The node stored in the second target location is used as the third target node.
8. The method according to claim 6, characterized in that The step of querying the hash table or the linked list to obtain at least one third target node according to the query key and the query instruction includes: If the query instruction indicates to query the value of the successor node of the node to which the query key belongs, determining the second target position of the query key in the hash table; Read the node to which the query key belongs from the second target location; Reading the identifier of the successor node recorded in the node to which the query key belongs; The node pointed to by the identifier of the successor node in the linked list is used as the third target node.
9. The method according to claim 6, characterized in that The step of querying the hash table or the linked list to obtain at least one third target node according to the query key and the query instruction includes: If the query instruction indicates to query the value of the predecessor node of the node to which the query key belongs, determining the second target position of the query key in the hash table; Read the node to which the query key belongs from the second target location; Read the identifier of the predecessor node recorded in the node to which the query key belongs; The node pointed to by the identifier of the predecessor node in the linked list is used as the third target node.
10. The method according to any one of claims 1 to 5, characterized in that The method further comprises: Get the update key and update value; According to the update key, query the hash table for the node to be updated; Update the value of the node to be updated to the updated value.
11. The method according to any one of claims 1 to 5, characterized in that The method further comprises: Get the delete key; According to the deletion key, searching the hash table for a node to be deleted; According to the identifier of the predecessor node and the identifier of the successor node recorded in the node to be deleted, the node to be deleted is deleted from the linked list, and the node to be deleted is deleted from the hash table.
12. A data processing device, characterized in that: include: A calculation module, used for performing a hash operation on the data to be inserted to obtain a key of the data to be inserted; A generating module, configured to generate a first target node corresponding to the data to be inserted according to the key of the data to be inserted and the data to be inserted, and insert the first target node into a hash table, wherein the first target node includes a key and a value; a determination module, configured to determine a predecessor node and a successor node of the first target node in the linked list based on the current first node and last node of the linked list and the key of the first target node; A processing module is used to record the identifier of the predecessor node and the identifier of the successor node in the first target node according to the predecessor node and the successor node of the first target node in the linked list, and insert the first target node into the linked list, wherein the linked list is a bidirectional and ordered linked list, and each node in the linked list is sorted according to the order of the values in each node.
13. An electronic device, characterized in that: include: A processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the storage medium communicate via the bus, and the processor executes the machine-readable instructions to perform the steps of a data processing method as described in any one of claims 1 to 11.
14. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the data processing method according to any one of claims 1 to 11 are executed.
Citation Information
Patent Citations
A method and apparatus for storing data
CN109101438A
Data storage management method and device, equipment, medium and product
CN114896177A
Message pushing method and device, computer equipment and storage medium
CN117176796A
Dynamic lock-free hash tables
US20120323970A1