Data processing method and device, equipment and storage medium
By using hash operations and doubly linked lists to record the identifiers of predecessor and successor nodes in a balanced tree data structure, the problem of low query performance in TreeMap is solved, and fast query with O(1) complexity is achieved.
Patent Information
- Application Number
- CN202510578793.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-06
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2045-05-06
AI Technical Summary
In balanced tree data structures, querying predecessor and successor nodes has low performance, especially in TreeMap, where query complexity increases logarithmically with the increase of data size.
The target node is generated by performing a hash operation on the data to be inserted, and then inserted into the hash table. At the same time, the identifiers of the predecessor and successor nodes are recorded in the doubly linked list to form an ordered linked list structure, ensuring that the node can be quickly located in the hash table and the linked list.
The query complexity has been reduced, especially the query efficiency of predecessor and successor nodes has been improved, with the query complexity reduced from O(log n) to O(1).
Smart Images

Figure CN120508558B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to a data processing method, apparatus, device, and storage medium. Background Technology
[0002] Balanced tree data structures are widely used for storing and retrieving ordered data. For example, the TreeMap in the Java standard library is an ordered mapping interface implemented based on a balanced tree data structure, where data is arranged in an ordered manner 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 increases logarithmically with the increase of data size, especially when querying data for successor nodes, resulting in low query performance. Summary of the Invention
[0004] The purpose of this application is to address the shortcomings of the prior art by providing a data processing method, apparatus, device, and storage medium to solve the problem of low query performance of TreeMap in the prior art.
[0005] To achieve the above objectives, the technical solution adopted in this application is as follows:
[0006] In a first aspect, this 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] Based on the key of the data to be inserted and the data to be inserted, a first target node corresponding to the data to be inserted is generated, and the first target node is inserted into the hash table. The first target node includes a key and a value.
[0009] Based on the current first and last nodes of the linked list, and the key of the first target node, determine the predecessor and successor nodes of the first target node in the linked list.
[0010] Based on the predecessor and successor nodes of the first target node in the linked list, the identifiers of the predecessor and successor nodes are recorded in the first target node, and the first target node is inserted into the linked list. The linked list is a doubly ordered linked list, and the nodes in the linked list are sorted according to the order of the values in each node.
[0011] Secondly, this application provides a data processing apparatus, the apparatus comprising:
[0012] The calculation module is used to perform hash operations on the data to be inserted to obtain the key of the data to be inserted;
[0013] The generation module is used to generate a first target node corresponding to the data to be inserted based on the key of the data to be inserted and the data to be inserted, and to insert the first target node into a hash table, wherein the first target node includes a key and a value;
[0014] The determination module is used to determine the predecessor and successor nodes of the first target node in the linked list based on the current first and tail nodes of the linked list and the key of the first target node.
[0015] The processing module is configured 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 in the linked list, and insert the first target node into the linked list, wherein the linked list is a doubly ordered linked list, and the nodes in the linked list are sorted according to the order of the values in each node.
[0016] Thirdly, embodiments of this application also provide an electronic device, including: 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 communicates with the storage medium 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 the first aspects.
[0017] Fourthly, embodiments of this application also provide a computer-readable storage medium storing a computer program, which, when executed by a processor, performs the steps of a data processing method as described in any one of the first aspects.
[0018] The beneficial effects of this application are as follows: The key and value of the data to be inserted are combined to form a node and inserted into the hash table. Based on the node's key, its predecessor and successor nodes in the linked list are determined, and the identifiers of the predecessor and successor nodes are recorded in the node accordingly. This insertion process allows for rapid retrieval of target data from the hash table or data from the predecessor or successor node during queries. Specifically, by recording the identifiers of the predecessor and successor nodes in the node, the low query efficiency caused by the non-adjacent positions of predecessor and successor nodes in existing TreeMap structures is solved, reducing the complexity of subsequent data queries, and especially improving the query efficiency of predecessor and successor nodes.
[0019] To make the above-mentioned objectives, features and advantages of this application more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description
[0020] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0021] Figure 1 An example diagram of a TreeMap structure provided in an embodiment of this application is shown;
[0022] Figure 2 A flowchart of a data processing method provided in an embodiment of this application is shown;
[0023] Figure 3 This document illustrates a flowchart of a method for determining predecessor and successor nodes according to an embodiment of this application.
[0024] Figure 4 This document illustrates another flowchart for determining predecessor and successor nodes provided in an embodiment of this application.
[0025] Figure 5 This illustration shows a structural diagram of a hash table and a linked list provided in an embodiment of this application;
[0026] Figure 6 This document illustrates a flowchart of an embodiment of inserting a first target node.
[0027] Figure 7 This document illustrates another flowchart of inserting a first target node according to an embodiment of this application;
[0028] Figure 8 This document illustrates a flowchart of a query node value provided in an embodiment of this application.
[0029] Figure 9 This application provides a flowchart for determining a third target node according to an embodiment of the present application.
[0030] Figure 10 This document illustrates a flowchart of a method for querying the successor node of a specified node, as provided in an embodiment of this application.
[0031] Figure 11 This document illustrates a flowchart of a method for querying the predecessor node of a specified node, as provided in an embodiment of this application.
[0032] Figure 12 This document illustrates a flowchart of an embodiment of updating node values provided in this application.
[0033] Figure 13This document illustrates a flowchart of a node deletion method provided in an embodiment of this application.
[0034] Figure 14 This paper shows a schematic diagram of the structure of a data processing apparatus provided in an embodiment of the present application;
[0035] Figure 15 A schematic diagram of the structure of an electronic device 150 provided in an embodiment of this application is shown. Detailed Implementation
[0036] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. The components of the embodiments of this application described and shown in the accompanying drawings can generally be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of this application provided in the accompanying drawings is not intended to limit the scope of the claimed application, but merely represents selected embodiments of this application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without inventive effort 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 this application to indicate the presence 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 type of balanced tree structure. Taking TreeMap as an example, TreeMap is an ordered mapping interface based on a red-black tree, which can sort key-value pairs according to the natural order of the keys or a specified order.
[0039] However, the balance property of red-black trees 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 upwards or downwards from the current node step by step. Under certain query modes, there may be problems with low query performance.
[0040] For example, Figure 1 This is a sample diagram of the TreeMap structure. Figure 1 Taking the TreeMap structure in the example, assuming the current node t is 20, to find the successor node of the current node, if the current node has a right subtree in the TreeMap structure, we need to start traversing from the right subtree of the current node, find the leftmost node 22 in the right subtree, and take 22 as the successor node of the current node t.
[0041] Continue with Figure 1Taking the TreeMap structure as an example, assuming the current node t is 25 and has no right subtree, its successor node is in the parent node chain of the current node. To find the successor node, we need to traverse upwards until we find an ancestor node that is a child of a left subtree. Then, we can use the parent node of that ancestor node as the successor node. Figure 1 In the TreeMap structure, assuming the current node t is 25, since t has no right subtree, it needs to be traversed upwards level by level to 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 needing to traverse the balanced tree upwards or downwards. Specific examples will not be elaborated here.
[0043] It can be seen that when determining predecessor and successor nodes based on existing technology, the existence of physical adjacency is not guaranteed for consecutive key values in a balanced tree structure. Therefore, some scenarios involving querying predecessor and successor nodes suffer from low query performance.
[0044] Based on this, this application proposes a data processing method to address the low query performance issue in scenarios where querying predecessor and successor nodes is difficult due to the balancing characteristics of balanced tree structures. By sorting nodes according to their natural or custom key order and recording the identifiers of predecessor and successor nodes within each node, the nodes exhibit a doubly linked list structure. Without altering the structure of the balanced tree itself, even if the predecessor and successor nodes are not physically adjacent to the current node in the balanced tree, the data in the predecessor or successor node of the query node can be quickly determined. This effectively reduces the complexity of data processing operations in balanced tree structures and improves data processing efficiency.
[0045] The following section discusses the data processing methods used in this application. Figure 2 This will be explained. The subject executing this method can 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 the key of the data to be inserted.
[0047] The key can be obtained by hashing the data. The data to be inserted can be of any data type and any length. By hashing the data to be inserted, input data of any length can be converted into a hash value of fixed length, and the hash value is used as the key of the data to be inserted.
[0048] It should be understood that the key of the data to be inserted can also be obtained in other ways, such as determining the key based on the natural attributes of the data to be inserted, or obtaining the key by performing calculations on the data to be inserted based on custom rules. The specific methods will not be elaborated here.
[0049] S202. Based on the key of the data to be inserted and the data to be inserted, generate the first target node corresponding to 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 an 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 used to store key-value pairs, and each array index indicates an array element in the hash array. Inserting the first target node into the hash table can be done by determining the array element in the hash table corresponding to the first target node.
[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 can be 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 it can achieve the association between the position in the hash table and the first target node. 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 between the first target node and the stored position in the hash table.
[0054] Continuing with the example above, suppose the first target node is represented as Entry.<K,V> Inserting the first target node into the hash table can be done by using the first target node as a value in the hash table and using the key of the first target node as the key of the hash table. In this case, the content stored in the hash table can be represented as a HashMap. <K,Entry<K,V> >
[0055] S203. Based on the current first and last nodes of the linked list and the key of the first target node, determine the predecessor and successor nodes of the first target node in the linked list.
[0056] Hash tables can be combined with linked lists 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 position information of its predecessor and successor nodes, and the data in the linked list is arranged in an ordered manner. A linked list can include 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 collision occurs in the array elements of the hash table, that is, when the array indices calculated from 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 indices obtained from calculating multiple nodes all point to the same array element in the hash table, then the nodes can be stored in a linked list in the order of data insertion, and the pointers of the array elements can be set to the first node of the linked list.
[0059] The first node of the linked list can be the first node inserted into the hash table, and the last node of the linked list can be the last node inserted into the hash table.
[0060] In one possible implementation, a new member variable `head` can be added for the first node, a member variable `tail` for the last node, and a member variable `treeMapLinkedHashMap` for the hash table. The member variable `head` is used to record the first node in the linked list, the member variable `tail` is used to record the last node in the linked list, and the member variable `treeMapLinkedHashMap` is used to record each node in the hash table.
[0061] As an example, after representing a set of data in the TreeMap structure as a hash table and 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 this embodiment of the application, the predecessor node and the successor node of a node have a direct and clear pointing relationship in the linked list, thereby enabling a fast query of the predecessor node and the successor node of the node based on this pointing relationship.
[0063] Optionally, the nodes of the linked list store the position information of the predecessor and successor nodes, and the linked list is an ordered linked list. Based on the first and last nodes of the linked list, all 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 insertion position of the first target node in the linked list can be determined, and the node before that position is taken as the predecessor node, and the node after that position is taken as the successor node.
[0064] S204. Based on the predecessor and successor nodes of the first target node in the linked list, record the identifier of the predecessor node and the identifier of the successor node in the first target node, and insert the first target node into the linked list.
[0065] The linked list is a doubly linked list that is ordered, with each node sorted according to the order of its values. The order of the node values can be a natural order, such as descending or ascending, or a user-defined order.
[0066] Optionally, the first target node also includes the identifier of the predecessor node and the identifier of the successor node. The identifier of the predecessor node can be a pointer to the predecessor node or the key of the predecessor node. The identifier of the successor node can be a pointer to the successor node or the key of the successor node.
[0067] As one possible implementation, taking the method of this application implemented in a TreeMap as an example, a member variable `prev` for the predecessor node and a member variable `next` for the successor node can be added to the key-value pair structure of each node in the TreeMap. The `prev` member variable of the predecessor node is used to record the identifier of the predecessor node, and the `next` member variable of the successor node is used to record the identifier of the successor node.
[0068] It is worth noting that when the identifiers of the predecessor and successor nodes 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 this embodiment, the key and value of the data to be inserted are combined to form a node and inserted into a hash table. The predecessor and successor nodes in the linked list are determined based on the node's key, and the identifiers of the predecessor and successor nodes are recorded in the node accordingly. This insertion process allows for quick retrieval of target data from the hash table or data from the predecessor or successor node during queries. Specifically, by recording the identifiers of the predecessor and successor nodes in the node, the high query complexity and low efficiency caused by the fact that the predecessor and successor nodes are not necessarily adjacent in the TreeMap structure of the prior art are solved. This reduces the complexity of subsequent data queries, especially improving the query efficiency of predecessor and successor nodes.
[0070] The following is a further explanation of how to determine the predecessor and successor nodes of the first target node in the linked list based on the current first and tail nodes and the key of the first target node. Figure 3 As shown, step S203 above includes:
[0071] S301. Determine whether the linked list currently has a head node and a tail node.
[0072] S302. If yes, then retrieve the key from the tail node.
[0073] S303. Based on the key in the tail node and the key of the first target node, determine the predecessor and successor nodes of the first target node in the linked list.
[0074] Optionally, you can check if the first and last nodes of 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, it means that the last node exists. For example, if the content of the first and last nodes is "NULL", then the first and last nodes are considered to be empty.
[0075] If neither the first node nor the tail node is empty, the key in the tail node is retrieved. This key indicates the storage location of the tail node in the hash table. Based on the key in the tail 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 taken as the predecessor node, and the node after the insertion position is taken as the successor node.
[0076] The following is a further explanation of how to determine the predecessor and successor nodes 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 satisfies the 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.
[0078] Among them, the preset conditions are related to the arrangement order of the nodes in the linked list. As mentioned above, the order of the nodes in the linked list can be from largest to smallest or from smallest to largest, or it can be a user-defined order.
[0079] As one possible implementation, if the nodes in the linked list are arranged in ascending order, the preset condition can be that the key of the first target node is greater than the key of the tail node. In this case, 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 null. 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 can be that the key of the first target node is less 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 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.
[0082] If the key of the first target node and the key of the tail node do not meet the preset conditions, then the node that meets the preset conditions can be searched in the linked list in turn as the second target node.
[0083] Each node includes the identifier of the predecessor node and the identifier of the successor node. Starting from the first node or the last node, the search proceeds one by one until the key of the found node meets the preset condition with the key of the first target node. Then, the found node is 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] For example, you can start from the tail node and search backwards one by one. Find the predecessor node of the tail node based on 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, then the predecessor node of the tail node is taken as the second target node. If the preset conditions are not met, continue to search backwards until the second target node that meets the preset conditions is found.
[0085] Figure 5 This is a schematic diagram of the structure of a hash table and a linked list. Red dashed lines point to the predecessor node of a node, black dashed lines point to the successor node, and solid lines point to nodes stored in the hash table or represent the connections between linked lists. (See reference...) Figure 5 The tail node of the linked list is the node containing "30", and the first node is the node containing "10". The linked list is arranged in ascending order. Assuming the first target node to be inserted is "32", and the key of "32" is greater than the key of the tail node "30", we can make the current tail node "30" the predecessor node of "32" and set the successor node of "32" to null. At this time, "32" is the current tail 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 less than that of the tail node "32", we can start traversing backward from the tail node. Since the predecessor node of the tail node "25" is less than that of the first target node "27", we can use node "25" as the predecessor node of the first target node "27" and 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] The following section explains the process of recording the identifiers of the predecessor and successor nodes of the first target node in the linked list based on the predecessor and successor nodes of the first target node, and inserting the first target node into the linked list.
[0088] In the first implementation, the identifier of the predecessor node can be its position information in the linked list, and the identifier of the successor node can be its position information in the linked list. The position information includes the hash table position and the linked list position. For example, Figure 5 The position information of node "20" can be represented as "(0,2)", which means that 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 identifiers of the predecessor and successor nodes can also be pointers. Figure 6 This is a description of the process for inserting the first target node into the linked list in the second implementation method. (Refer to...) Figure 6 The above S204 step 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 can be assigned to 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, then the identifier of the predecessor node in the first target node is assigned a pointer to the predecessor node, and the identifier of the successor node in the first target node is assigned an empty value.
[0093] If the first target node is inserted at the end of the linked list, then the first target node, as the tail node of the linked list, has no successor node; that is, the successor node of the first target node is empty. Therefore, the predecessor node of the first target node can be the tail node of the linked list before the insertion of the first target node.
[0094] Continue to refer to Figure 5 Assuming the first target node is "32", insert the first target node. Figure 5After the tail node "30" of the linked list shown, node "30" is now the predecessor node of node "32". Node "32" becomes the current tail node of the linked list. Then, the identifier of the predecessor node of the first target node can be assigned a pointer to node "30", and the identifier of the successor node of the first target node can be assigned an empty value.
[0095] S603. If the successor node of the first target node is not empty, then the identifier of the predecessor node in the first target node is assigned to a pointer to the predecessor node, 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 at the end of the linked list, for example, if the first target node is inserted in the middle of the linked list, then the first target node has a predecessor node and a successor node. After determining the predecessor node and successor node of the first target node based on the above S402 step, the identifier of the successor node in the first target node can be assigned 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 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 identifiers of the predecessor node and the successor node stored in the first target node, but also to update the identifiers of the successor node stored in the predecessor node of the first target node, and update the identifiers of the predecessor node stored in the successor node of the first target node.
[0098] For example, continue to refer to Figure 5 Assuming the first target node is "23", based on the above S402 step, starting from the tail node, we determine that the predecessor node of the first target node is "22" and the successor node of the first target node is "25". Then, we can assign the identifier of the successor node in the first target node to a pointer to node "25", assign the identifier of the predecessor node in the first target node to a pointer to node "22", update the identifier of the successor node of node "22" to a pointer to the first target node, and update the identifier of the predecessor node of node "25" to a pointer to the first target node.
[0099] The following is a further explanation of how to generate the first target node corresponding to the data to be inserted based on the key and the data to be inserted, and how to insert 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 data to be inserted can be of type number, string, or object, etc. One possible implementation is to set the key of the data to be inserted as the key of the first target data node, and set the data to be inserted itself as the value of the first target node. The first target node is generated in this way, and it includes both the key of the data to be inserted and the data 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 performed through the key.
[0103] S702. Determine the first target position of the first target node in the hash table based on the key of the first target node.
[0104] Optionally, the key of the first target node can be mapped to a position in the hash table, and the first target position can be the position in the hash table after calculating the key of the first target node.
[0105] As one possible implementation, a hash function can be used to calculate the key of the first target node to obtain a hash value, which is used to indicate the first target position in the hash table.
[0106] For example, assuming the first target node is (101, 25), after hashing the key "101" of the first target node, the hash value obtained is 1. Then, 1 can be used as an index to look up 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] Optionally, if there are no other nodes 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 at the first target position, the first target node needs to be inserted before or after the stored nodes to insert the first target node into the first target position.
[0109] After inserting the data to be inserted into the hash table and the linked list, you can perform data addition, deletion, modification and query operations on the nodes based on the information in the hash table and the hash linked list.
[0110] By combining the key and value of the data to be inserted into a hash table to form a node, and determining its predecessor and successor nodes in the linked list based on the node's key, the identifiers of the predecessor and successor nodes are recorded in the node accordingly, and the node is then inserted into the linked list. This insertion process allows for quick retrieval of target data from the hash table or based on the predecessor or successor nodes of a node. Especially for queries involving predecessor and successor nodes, the method of this application can significantly reduce query complexity. Further explanation is provided below with reference to embodiments.
[0111] Figure 8 This is a flowchart illustrating the process of data querying, such as... Figure 8 As shown, the steps for performing a data query include:
[0112] S801, Get the query key and query command.
[0113] Optionally, the query key can be the basis for locating the node containing the data to be queried in the hash table and linked list, and the query key can be the data's identifier ID.
[0114] Optionally, a query instruction can specify the query operation to be performed, including query conditions, query range, etc. For example, a query instruction can specify the node corresponding to the query key, or multiple query nodes that meet specific query conditions.
[0115] S802. Based on the query key and query instruction, at least one third target node is obtained by querying the hash table or linked list.
[0116] Optionally, the query key can be used as input to a hash function to calculate a hash value. The hash value can then be used as an index to obtain the starting position of the query in the hash table. Starting from the query starting point, the keys of the nodes stored in the hash table are matched with the query key. If a match is found, the matched node can be used as the third target node.
[0117] For example, refer to Figure 5 Assuming the hash value of the query key is 3, the node stored at index 3 is "22". If the key of node "22" matches the query key, then node "22" can be used as the third target node.
[0118] If the starting point of the query points to multiple nodes in the linked list, the query can proceed sequentially from the starting node. If the key of the node found in the linked list matches the query key, the found node will be used as the third target node.
[0119] For example, continue to refer to Figure 5Assuming the hash value of the query key is 6, the nodes stored at index 6 include "25" and "30". Starting from the starting node, if the key of node "30" matches the query key, then node "30" can be used as the third target node.
[0120] S803. Take the values of each third target node as the query results and return the query results.
[0121] Optionally, if there are multiple query keys and the query instruction is an instruction to query multiple data, then the third target node corresponding to each query key can be determined sequentially, and the value of each third target node can be used as the query result.
[0122] The following is a further explanation of how to retrieve at least one third target node from a hash table or linked list based on the query key and query command, such as... Figure 9 As shown, the above S802 step includes:
[0123] S901. If the query instruction indicates that the value of the node to which the query key belongs is to be queried, then the second target position of the query key in the hash table is determined.
[0124] S902, Use the node stored in the second target location as the third target node.
[0125] If the query instruction is the instruction to query the data corresponding to the query key, then the second target position of the query key in the hash table can be determined.
[0126] Optionally, the second target location can be the location of the hash bucket in the hash table where the nodes are stored. If the hash bucket stores multiple nodes, 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 of the linked list. The second target location can be the starting node in the linked list stored in the hash bucket.
[0127] As another possible implementation, if the hash bucket stores multiple nodes, the second target location can further indicate the position of the node indicated by the query key in the linked list formed by these nodes.
[0128] 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 then used as an index to look up the position corresponding to the index in the hash table, and the position corresponding to the index is used as the second target position.
[0129] When querying a node based on a query key, the node corresponding to the query key can be directly determined from the hash table and the linked list based on the query key, with a time complexity of O(1), which improves the efficiency of data query.
[0130] The steps for querying the predecessor and successor nodes of a node will be explained below with reference to an example.
[0131] Figure 10 This is a flowchart illustrating the process of finding a successor node. (Refer to...) Figure 10 The process of retrieving at least one third target node from a hash table or linked list based on the query key and query command includes:
[0132] S1001. If the query instruction indicates that the value of the successor node of the node to which the query key belongs is to be queried, then the second target position of the query key in the hash table is determined.
[0133] S1002, Read the node to which the query key belongs from the second target location.
[0134] The second target location is the position of the node indicated by the query key in the hash table. If the query instruction indicates that the node being queried is the 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 first in the hash table.
[0135] Optionally, the method for determining the second target location of the node indicated by the query key can be the same as the above-described step S901, and will not be repeated here.
[0136] S1003. Read the identifier of the successor node recorded in the node to which the query key belongs.
[0137] S1004. Take 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 the identifier of the successor node. Based on the identifier of the successor node, the location of the successor node can be determined, and the third target node can be read from that location.
[0139] For example, refer to Figure 5 If the node to which the query key belongs is "22", and the identifier of the successor node of the record points to node "25", then node "25" can be used as the third target node.
[0140] Figure 11 This is a flowchart illustrating the process of finding the predecessor node. (Refer to...) Figure 11 The process of retrieving at least one third target node from a hash table or linked list based on the query key and query command includes:
[0141] S1101. If the query instruction indicates that the value of the predecessor node of the node to which the query key belongs is to be queried, then the second target position of the query key in the hash table is determined.
[0142] S1102, Read the node to which the query key belongs from the second target location.
[0143] The second target location is the position of the node indicated by the query key in the hash table. If the query instruction indicates that the node being queried is the predecessor 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. The method for determining the second target location of the node indicated by the query key can be the same as the above S901 step, and will not be repeated here.
[0144] S1103. Read the identifier of the predecessor node recorded in the node to which the query key belongs.
[0145] S1104. Take 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 the identifier of the predecessor node. Based on the identifier of the predecessor node, the location of the predecessor node can be determined, and the third target node can be read from that location.
[0147] For example, refer to Figure 5 If the node to which the query key belongs is “22”, and the identifier of the predecessor node of the record 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 read directly 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, then 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 the values of each node are stored in ascending order, if you need to find the smallest 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 find the largest 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 this embodiment, by recording the identifiers of predecessor and successor nodes in the nodes, it is not necessary to traverse every node when querying the predecessor or successor node of a node. The value of the predecessor or successor node can be directly obtained based on the identifiers recorded in the node, thus reducing the complexity of querying predecessor and successor nodes. Specifically, the complexity of querying predecessor and successor nodes from a TreeMap structure using existing technology is O(log n), while the complexity of querying using the method of this application is reduced to O(1).
[0152] Based on the method of this application, the complexity of updating or deleting nodes can be greatly reduced. The following will further explain this with reference to the embodiments.
[0153] Figure 12 This is a flowchart illustrating the process of updating nodes, see reference. Figure 12 The method of this application also includes:
[0154] S1201, Get the update key and update value.
[0155] S1202. Based on the update key, query the hash table for the node to be updated.
[0156] S1203. Update the value of the node to be updated to the updated value.
[0157] Optionally, the update key can be the basis for locating the node containing the data to be updated in the hash table and linked list; the update key can be the data's identifier ID. The update value can be the data that needs to be updated.
[0158] Optionally, the location of the node to be updated in the hash table can be determined based on the update key, and the value of the node to be updated can be updated to the update value.
[0159] For example, suppose the update key indicates that an update is needed. Figure 5 The value of node "22" is "24". After querying the hash table for the node "22" to be updated, the value of node "22" can be replaced with "24".
[0160] Figure 13 This is a flowchart illustrating the process of deleting a node. (Refer to...) Figure 13 The method of this application also includes:
[0161] S1301, Get the delete key.
[0162] Optionally, the deletion key can be the basis for locating the node containing the data to be deleted in the hash table and linked list. For example, the deletion key can be the data's identifier ID.
[0163] S1302. Based on the deletion key, query the hash table for the node to be deleted.
[0164] Optionally, the location of the node to be deleted in the hash table can be determined based on the deletion key, and the node to be deleted can be removed from the hash table and the linked list.
[0165] S1303. Based on the identifiers of the predecessor and successor nodes 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 and successor nodes 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, this application can transform the operations on key-value pairs in a TreeMap in the prior art into operations on nodes in the hash table. That is, by performing CRUD operations on nodes in the hash table, the CRUD operations on key-value pairs in the TreeMap structure can be realized.
[0168] In one possible implementation, using a TreeMap structure as an example, if the node to be deleted has two child nodes, the successor node of the node to be deleted in the hash table can be determined first, the value of the successor node can be copied to the node to be deleted, and then 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 then the node to be deleted can be deleted. If the node to be deleted has no child nodes, it can be deleted directly.
[0169] Based on the same inventive concept, this application also provides a data processing device corresponding to the data processing method. Since the principle of the device in this application to solve the problem is similar to the data processing method described above in this application, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be described again.
[0170] Figure 14 A module structure diagram of a data processing device provided in an embodiment of this application is shown below. Figure 14 As shown, the device includes:
[0171] The operation module 1401 is used to perform hash operations on the data to be inserted to obtain the key of the data to be inserted;
[0172] The generation module 1402 is used to generate a first target node corresponding to the data to be inserted based on the key of the data to be inserted and the data to be inserted, and to insert the first target node into the hash table. The first target node includes a key and a value.
[0173] The determination module 1403 is used to determine the predecessor and successor nodes of the first target node in the linked list based on the current first and last nodes of the linked list and the key of the first target node.
[0174] The processing module 1404 is used to record the identifiers of the predecessor node and the successor node in the first target node according to the predecessor node and the successor node in the linked list, and insert the first target node into the linked list. The linked list is a doubly ordered linked list, and the nodes in the linked list are sorted according to the order of the values in each node.
[0175] Optionally, module 1403 is specifically used for:
[0176] Determine if the linked list currently contains a head node and a tail node;
[0177] If so, retrieve the key from the tail node;
[0178] Based on the key in the tail node and the key of the first target node, determine the predecessor and successor nodes of the first target node in the linked list.
[0179] Optionally, module 1403 is specifically used for:
[0180] If the key of the first target node satisfies 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.
[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 used for:
[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, then the identifier of the predecessor node in the first target node is assigned a pointer to the predecessor node, and the identifier of the successor node in the first target node is assigned an empty value.
[0185] If the successor node of the first target node is not empty, then the identifier of the predecessor node in the first target node is assigned to a pointer to the predecessor node, 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 generation module 1402 is specifically used for:
[0187] 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;
[0188] Based on the key of the first target node, determine the first target position of the first target node in the hash table;
[0189] Insert the first target node into the first target position.
[0190] Optionally, the device further includes a query module for:
[0191] Get the query key and query command;
[0192] Based on the query key and query instruction, at least one third target node can be obtained by querying the hash table or linked list.
[0193] The values of each third target node are used as query results, and the query results are returned.
[0194] Optionally, the query module is also used for:
[0195] If the query instruction specifies that the value of the node to which the query key belongs is to be retrieved, then the second target position of the query key in the hash table is determined;
[0196] The node stored in the second target location is used as the third target node.
[0197] Optionally, the query module is also used for:
[0198] If the query instruction indicates that the value of the successor node of the node to which the query key belongs is to be queried, then the second target position of the query key in the hash table is determined;
[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 taken as the third target node.
[0202] Optionally, the query module is also used for:
[0203] If the query instruction indicates that the value of the predecessor node of the node to which the query key belongs is to be retrieved, then the second target position of the query key in the hash table is determined;
[0204] Read the node to which the query key belongs from the second target location;
[0205] Read the identifier 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 taken as the third target node.
[0207] Optionally, the device further includes an update module, which is used for:
[0208] Get the update key and update value;
[0209] Based on the update key, look up 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 used for:
[0212] Get the delete key;
[0213] Based on the deletion key, look up the node to be deleted in the hash table;
[0214] Based on the identifiers of the predecessor and successor nodes recorded in the node to be deleted, delete the node to be deleted from the linked list and also delete the node to be deleted from the hash table.
[0215] This application inserts a node into a hash table by combining the key and value of the data to be inserted. The predecessor and successor nodes in the linked list are determined based on the node's key, and the identifiers of the predecessor and successor nodes are recorded in the node accordingly. This insertion process allows for quick retrieval of target data or data in the predecessor or successor nodes during queries. Specifically, by recording the identifiers of the predecessor and successor nodes in the TreeMap nodes, a direct pointing relationship is established between predecessor and successor nodes that might not be adjacent in the original TreeMap structure. This reduces the complexity of subsequent data queries, especially improving the query efficiency of predecessor and successor nodes.
[0216] This application also provides a computer device 150, such as... Figure 15 The diagram shown is a schematic representation of the structure of a computer device 150 provided in an embodiment of this application. It includes a processor 1501, a memory 1502, and optionally, a bus 1503. The memory 1502 stores machine-readable instructions executable by the processor 1501 (e.g., ...). Figure 14 (The execution instructions corresponding to each module in the device, etc.) When the computer device 150 is running, the processor 1501 and the memory 1502 communicate through 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 executed.
[0217] This application also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, performs the steps of the data processing method described in the above method embodiments.
[0218] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems and devices described above can be referred to the corresponding processes in the method embodiments, and will not be repeated here. In the several embodiments provided in this application, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. The device embodiments described above are merely illustrative. For example, the division of modules is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, 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 displayed or discussed mutual coupling or direct coupling or communication connection can be through some communication interfaces; the indirect coupling or communication connection of devices or modules can be electrical, mechanical, or other forms.
[0219] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. If the functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0220] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application.
Claims
1. A data processing method, characterized by, include: Perform a hash operation on the data to be inserted to obtain the key of the data to be inserted; Based on the key of the data to be inserted and the data to be inserted, a first target node corresponding to the data to be inserted is generated, and the first target node is inserted into the hash table. The first target node includes a key and a value. Based on the current first and last nodes of the linked list, and the key of the first target node, determine the predecessor and successor nodes of the first target node in the linked list. Based on the predecessor and successor nodes of the first target node in the linked list, the identifiers of the predecessor and successor nodes are recorded in the first target node, and the first target node is inserted into the linked list. The linked list is a doubly ordered linked list, and the nodes in the linked list are sorted according to the order of the values in each node. The method further includes: Get the query key and query command; Based on the query key and the query instruction, at least one third target node is obtained by querying the hash table or the linked list; The values of each of the third target nodes are used as query results, and the query results are returned. The step of retrieving at least one third target node from the hash table or the linked list based on the query key and the query instruction includes: If the query instruction indicates that the value of the successor node of the node to which the query key belongs is to be queried, then the second target position of the query key in the hash table is determined; Read the node to which the query key belongs from the second target location; Read 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 taken as the third target node.
2. The method of claim 1, wherein, The step of determining the predecessor and successor nodes of the first target node in the linked list based on the current first and tail nodes of the linked list and the key of the first target node includes: Determine whether the linked list currently has a head node and a tail node; If so, then retrieve the key from the tail node; Based on the key in the tail node and the key of the first target node, determine the predecessor and successor nodes of the first target node in the linked list.
3. The method of claim 2, wherein, The step of determining the predecessor and successor nodes 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 includes: If the key of the first target node satisfies 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 from the tail node, the second target node that meets the preset conditions is searched sequentially in the linked list, and the predecessor node of the first target node is determined as 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 of claim 1, wherein, The step of recording the identifiers of the predecessor and successor nodes in the linked list based on the predecessor and successor nodes of the first target node, and inserting the first target node into the linked list, includes: Update the successor node of the predecessor node to the first target node; If the successor node of the first target node is empty, then the identifier of the predecessor node in the first target node is assigned a pointer to the predecessor node, and the identifier of the successor node in the first target node is assigned an empty value. If the successor node of the first target node is not empty, then the identifier of the predecessor node in the first target node is assigned a pointer to the predecessor node, the identifier of the successor node in the first target node is assigned a pointer to the successor node, and the predecessor node of the successor node is updated to the first target node.
5. The method of claim 1, wherein, The step of generating a first target node corresponding to the data to be inserted based on 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: The key of the data to be inserted is used as the key of the first target node, and the data to be inserted is used as the value of the first target node to generate the first target node; Based on the key of the first target node, determine the first target position of the first target node in the hash table; Insert the first target node into the first target position.
6. The method according to claim 1, characterized in that, The step of retrieving at least one third target node from the hash table or the linked list based on the query key and the query instruction includes: If the query instruction indicates that the value of the node to which the query key belongs is to be queried, then the second target position of the query key in the hash table is determined; The node stored in the second target location is used as the third target node.
7. The method according to claim 1, characterized in that, The step of retrieving at least one third target node from the hash table or the linked list based on the query key and the query instruction includes: If the query instruction indicates that the value of the predecessor node of the node to which the query key belongs is to be queried, then the second target position of the query key in the hash table is determined; 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 taken as the third target node.
8. The method according to any one of claims 1-5, characterized in that, The method further includes: Get the update key and update value; Based on 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.
9. The method according to any one of claims 1-5, characterized in that, The method further includes: Get the delete key; Based on the deletion key, query the hash table for the node to be deleted; Based on the identifiers of the predecessor and successor nodes recorded in the node to be deleted, the node to be deleted is deleted from the linked list and from the hash table.
10. A data processing apparatus, characterized in that, include: The calculation module is used to perform hash operations on the data to be inserted to obtain the key of the data to be inserted; The generation module is used to generate a first target node corresponding to the data to be inserted based on the key of the data to be inserted and the data to be inserted, and to insert the first target node into a hash table, wherein the first target node includes a key and a value; The determination module is used to determine the predecessor and successor nodes of the first target node in the linked list based on the current first and tail nodes of the linked list and the key of the first target node. The processing module is configured 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 doubly ordered linked list, and the nodes in the linked list are sorted according to the order of the values in each node. The query module is used for: Obtain the query key and query instruction; based on the query key and query instruction, query the hash table or the linked list to obtain at least one third target node; use the value of each third target node as the query result and return the query result; The query module is specifically used for: If the query instruction indicates that the value of the successor node of the node to which the query key belongs is to be queried, then the second target position of the query key in the hash table is determined; Read the node to which the query key belongs from the second target location; Read 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 taken as the third target node.
11. An electronic device, characterized in that, include: The device includes 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 in operation, the processor communicates with the storage medium 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 9.
12. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, performs the steps of a data processing method as described in any one of claims 1 to 9.
Citation Information
Patent Citations
A method and apparatus for storing data
CN109101438A
Data storage management method and device, equipment, medium and product
CN114896177A
