Computer interlocking code bit search method based on ordered dictionary tree
By using a search method based on a sorted trie and utilizing a two-dimensional array to determine the starting search node and preorder traversal, the problem of low efficiency in code position search in existing technologies is solved, and efficient and flexible code position search is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CASCO SIGNAL LTD
- Filing Date
- 2023-12-25
- Publication Date
- 2026-07-03
AI Technical Summary
Existing code position search methods in computer interlocking systems have high time complexity in large-scale or long string scenarios, resulting in low search efficiency and failing to meet real-time response requirements.
A search method based on a sorted trie is adopted. By initializing the sorted trie data structure, inserting code point objects and reconstructing the tree structure, a two-dimensional array is used to determine the starting search node, and the search result set is determined by combining preorder traversal matching, thereby reducing the repeated search process.
It improves the efficiency and success rate of code position search, reduces node address space consumption, realizes efficient code position search function, and supports searching while inserting, thus enhancing the flexibility of the method.
Smart Images

Figure CN117725147B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer interlocking systems, and specifically relates to a computer interlocking code position search method based on a sorted trie. Background Technology
[0002] Currently, domestic computer interlocking systems have numerous internal interfaces, such as the operator display unit-interlocking machine and the interlocking machine-trackside equipment, as well as many external interfaces. These external interfaces communicate with external systems such as TCC (Train Control Center), RBC (Radio Block Center), neighboring station CBI (Computer Interlocking System), and fully electronic systems. The internal and external interfaces negotiate the code position table to achieve the transmission and reception of specific status information. Therefore, in the daily maintenance or simulation of the computer interlocking system, it is often necessary to perform operations such as matching and searching the code position table based on user-input keywords to track the status information of specific code position objects.
[0003] The current method for matching and searching the code position table based on keywords mainly involves traversing the code position table data structure item by item. In each loop cycle, the name string of the current code position object is compared with the keyword. If the keyword is a substring of the code position name string, the match is successful, the code position object is added to the search results set, and the next loop iteration begins. This method is essentially a "brute force" search with three loops: (1) The first loop traverses the code position table data structure item by item, and then enters the second loop; (2) The second loop traverses the name string of the current code position object byte by byte until the first byte of the keyword is found, and then enters the third loop; (3) The third loop traverses the name string and subsequent bytes of the keyword byte by byte. In the worst case, this method has a time complexity of O(m*n*k), where m is the size of the code position table, n is the average length of the code position object name, and k is the keyword length. When the number of code positions in the interlocking stations increases, or the average length of the code position object name increases, or the length of the keyword entered by the user increases, the time consumed by a single search will increase significantly. Therefore, the search method in the existing technology cannot meet the user's demand for high efficiency in code position search. In particular, in the real-time response search module that provides the search results set while inputting, unbearable delays and stuttering will occur. Summary of the Invention
[0004] The purpose of this invention is to provide a computer interlocking code point search method based on a sorted trie to meet the requirement of high efficiency in code point search.
[0005] To achieve the above objectives, this invention provides a computer interlocking code point search method based on a sorted trie, comprising:
[0006] Step S1: Initialize the sorted trie data structure, which includes a sorted trie containing a root node and several tree nodes, a sorted one-dimensional array sortedBitArr storing pointers to all code point objects, and a two-dimensional array val2NodesArr used to determine the starting search node of the trie.
[0007] Step S2: Insert all code point objects contained in the code point table in sequence. Each time an insertion is performed, reconstruct the sorted dictionary tree data structure according to the code point object name string. Insert the pointer of each code point object into the sorted one-dimensional array sortedBitArr in order, and refill the two-dimensional array val2NodesArr used to determine the starting search dictionary tree node according to the newly added tree node.
[0008] Step S3: Based on the keyword currently entered by the user, obtain all the starting search trie nodes based on the two-dimensional array val2NodesArr. Traverse the sorted trie in preorder from each starting search trie node. If the keyword is successfully matched, determine all matching code point objects by combining the sorted one-dimensional array sortedBitArr and put them into the search results set.
[0009] Preferably, the root node and each of the tree nodes include: a node representing the character value val, a pointer to the first child node firstChild, a pointer to the next right sibling node at the same level right, and an index startIdx where the node first appears in sortedBitArr.
[0010] Preferably, the initialization of the sorted trie data structure in step S1 includes step S11: initializing the trie root node; step S11 specifically includes the following steps:
[0011] Step S111: Initialize the node representation character value val of the root node of the trie to 0, indicating that root only represents the root node of the trie and does not represent any code point object associated node;
[0012] Step S112: Initialize the firstChild and rightNode of the root node of the trie to nullptr, indicating that no code point object has been inserted at present;
[0013] Step S113: Initialize the startIdx of the root node of the trie to 0, which means that the smallest index of all code point objects obtained by traversing down from the root node of the trie in sortedBitArr is 0.
[0014] Preferably, the initialization of the sorted trie data structure in step S1 includes step S12: initializing a sorted one-dimensional array sortedBitArr that stores pointers to all code point objects, wherein the sorted one-dimensional array sortedBitArr is sorted in ascending order according to the code point object name string.
[0015] Preferably, the initialization of the sorted trie data structure in step S1 includes step S13: initializing a two-dimensional array val2NodesArr used to determine the starting search node of the trie, wherein the first dimension of val2NodesArr represents the index of the character value mapping, and the second dimension represents an array of pointers to all trie nodes corresponding to the character value.
[0016] Preferably, step S2 specifically includes the following steps:
[0017] Step S21: Generate a code point object based on the code point table, wherein the code point object includes a code point name string;
[0018] Step S22: Initialize the current tree node curNode to the firstChild of the dictionary root node root, set the array of all successfully matched tree nodes to the traversal array parentNodes, and insert the dictionary root node root into parentNodes to indicate a successful match of the dictionary root node root.
[0019] Step S23: Extract the character char from the code position object name string in sequence, and reconstruct the sorted trie data structure according to the state of the current tree node curNode.
[0020] Here, the currently traversed tree node `curNode` represents the pointer `firstChild` to the first child node of the previous successfully matched tree node.
[0021] Preferably, if the current tree node curNode is nullptr, it means that there is no tree node corresponding to the current character char, the match fails, and a new tree node is created. The specific steps include the following:
[0022] Step S231: Initialize the firstChild and rightNode of the first new tree node to nullptr, and initialize the val of the first new tree node to the current character char;
[0023] Step S232: Initialize the startIdx of the first new tree node to the startIdx of the corresponding parent node + 1, and recursively perform a preorder traversal of all tree nodes in the parentNodes array, so that the startIdx of all tree nodes to the right of the first new tree node is incremented by 1; wherein, the all tree nodes to the right in parentNodes include the firstChild node, rightNode node and the derived nodes of the firstChild node and rightNode of all successfully matched nodes;
[0024] Step S233: Insert the code point object pointer into sortedBitArr, with the insertion position being the startIdx of the first new tree node;
[0025] Step S234: Based on the val value of the first new tree node, insert the first new tree node into the structure val2NodesArr.
[0026] Preferably, if there are still remaining characters in the current code position object name string that have not been processed, then a second new tree node for the remaining characters is created sequentially, which includes the following steps:
[0027] Step S235: Initialize the node representation character value val of the second new tree node to the current character char;
[0028] Step S236: Each time a second new tree node is created, the firstChild of the first new tree node is used. Since they are the name strings of the same code point object, the startIdx of the second new tree node is set to the startIdx of the first new tree node.
[0029] Step S237: Based on the val value of the second new tree node, insert each second new tree node into the structure of val2NodesArr to complete the insertion process of the code point object.
[0030] Preferably, if the current tree node curNode is not nullptr, then traverse rightNode sequentially to the right to match char. There are three cases: Case 1: If the val of a tree node matches char successfully, it means that there are tree nodes with the same prefix; Case 2: If the val of a tree node is greater than char, it means that the match failed and a third new tree node needs to be created; Case 3: If after traversing all the right tree nodes, no tree node with val equal to char is found, it means that the match failed and a fourth new tree node needs to be created.
[0031] Preferably, in case 1, if the name string of the current code point object has been processed, it means that the name string of the current code point object is a prefix of the name string of other code point objects. A recursive preorder traversal is performed on the startIdx of all child nodes derived from the current tree node curNode and all sibling nodes and their derived nodes at the same level on the right, incrementing their respective startIdx by 1, thus completing the insertion process of the code point object. Simultaneously, a recursive preorder traversal is performed on all tree nodes in the traversal array parentNodes, incrementing the startIdx of all tree nodes to the right of the current tree node curNode by 1. Here, all tree nodes to the right in parentNodes include the firstChild node, rightNode node, and derived nodes of all successfully matched nodes.
[0032] Preferably, in case 2, the creation of the third new tree node includes:
[0033] Step S241: Initialize the firstChild of the third new tree node to nullptr, the rightNode to curNode, and initialize val to the current character char;
[0034] Step S242: Set the startIdx of the third new tree node to equal the startIdx of curNode. Perform a recursive preorder traversal on the derived nodes generated downward from curNode and all sibling nodes and their derived nodes on the right side of the same level, so that the startIdx of each tree node is incremented by 1. At the same time, perform a recursive preorder traversal on all tree nodes in the traversal array parentNodes, so that the startIdx of all tree nodes located to the right of the third new tree node is incremented by 1. Wherein, all tree nodes on the right side in parentNodes include the firstChild node, rightNode node and derived nodes of all successfully matched nodes.
[0035] Step S243: Insert the code point object pointer into sortedBitArr, with the insertion position being the startIdx of the third new tree node;
[0036] Step S244: Based on the val value of the third new tree node, insert the third new tree node into the structure val2NodesArr.
[0037] Preferably, in case 3, the creation of the fourth new tree node includes:
[0038] Step S251: Initialize the firstChild and rightNode of the fourth new tree node to nullptr, and initialize val to the current character char;
[0039] Step S252: Set the startIdx of the fourth new tree node to the startIdx of the last traversed tree node + 1, and at the same time perform a recursive preorder traversal of all tree nodes in the traversal array parentNodes, so that the startIdx of all tree nodes to the right of the fourth new tree node is incremented by 1; wherein, the all tree nodes to the right in parentNodes include the firstChild node, rightNode node and the derived nodes of the firstChild node and rightNode of all successfully matched nodes.
[0040] Step S253: Insert the code point object pointer into sortedBitArr, with the insertion position being the startIdx of the fourth new tree node;
[0041] Step S254: Based on the val value of the fourth new tree node, insert the fourth new tree node into the structure val2NodesArr.
[0042] Preferably, if there are still characters in the current code position object name string that have not been processed, then repeat steps S235 to S237 to create tree nodes for the remaining characters in sequence to complete the insertion process of the code position object.
[0043] Preferably, after generating a code point object each time based on the code point table, the system checks whether the code point object name is duplicated to determine if the code point object is duplicated. If the code point object name is duplicated, the code point object is discarded and not inserted into the sorted trie structure.
[0044] Preferably, step S3 specifically includes the following steps:
[0045] Step S31: Based on the first character of the keyword currently entered by the user, determine all starting search trie nodes in combination with val2NodesArr;
[0046] Step S32: Iterate through each starting search trie node in turn, and set the current node curNode to the firstChild of the current starting search trie node.
[0047] Step S33: Iterate through the first character and subsequent characters of the input keyword char in sequence, and process them according to the state of curNode in each iteration;
[0048] Step S34: If the search is successful based on the state of curNode in this traversal, retrieve the startIdx of the last traversed tree node, determine the starting search index of the code point object that meets the search conditions in the sortedBitArr structure, retrieve the code point object and put it into the search result set, and obtain the search result set for this search.
[0049] Preferably, if curNode is nullptr, it means that the search failed and the process returns to step S32 to proceed to the next search; if curNode is not nullptr, then the process traverses rightNode sequentially to the right to match char.
[0050] Preferably, if, when curNode is not nullptr, the val of a certain tree node is found to be greater than char, or if the val of a certain tree node is not found to be equal to char after traversing all the right-hand tree nodes, it indicates that the search has failed and the process returns to step S32 to proceed to the next search.
[0051] Preferably, if the val of a certain tree node matches the char successfully and there are no unprocessed characters in the input keyword string, it means that the search is successful. The startIdx of the tree node is retrieved and the process continues in step S34. If there are still unprocessed characters in the input keyword string, the curNode is set as the firstChild of the curNode and the process returns to step S33 to proceed to the next traversal.
[0052] Preferably, after determining the starting search index that satisfies the search conditions, the sortedBitArr structure is traversed sequentially, code point objects are extracted and put into the search results set, until code point objects with different prefixes appear, and the current index is recorded as endIdx.
[0053] Preferably, if the startIdx of the subsequent starting search trie node is less than the endIdx, it means that the starting search trie node is the downstream subtree of the last traversed tree node of the previous successful search. If the search is successful, the search result set of this search is a subset of the currently known search result set, and this search needs to be skipped.
[0054] If the startIdx of the subsequent starting search trie node is greater than or equal to endIdx, it means that the starting search trie node is the right-hand node of the last traversed tree node of the previous successful search. If the search is successful, the search result set of this search does not intersect with the currently known search result set, so the search result sets are merged and endIdx is recorded again.
[0055] In summary, compared with the prior art, the computer interlocking code point search method based on a sorted trie provided by the present invention has the following beneficial effects:
[0056] (1) This invention determines the starting search index of the search results set in the sorted BitArr array by determining the starting search tree node and combining the preorder traversal matching, thereby reducing a large number of repetitive and unnecessary search processes and overcoming the high time complexity of the existing "brute force" traversal search method.
[0057] (2) By setting a two-dimensional array val2NodesArr to determine the starting search node of the trie, this invention overcomes the defect that the classic trie can only be traversed and searched from the root node, and overcomes the defect that the trie search method can only perform prefix matching, thus greatly improving the search efficiency, success rate and availability of the method.
[0058] (3) The present invention also sets member variables such as a pointer firstChild pointing to the first child node and a pointer rightNode pointing to the next right sibling node at the same level, which greatly reduces the address space consumption of each node compared with the classic multi-way tree.
[0059] (4) The present invention ensures the orderliness of the dictionary tree and sortedBitArr structure during the insertion of code bit objects, that is, it realizes the function of insertion and searching at the same time. For example, before insertion, it determines whether the code bit object is repeated based on the binary search method combined with sortedBitArr. Before the sorted dictionary tree structure is completed, the search operation can be performed, which greatly improves the flexibility of the search method. Attached Figure Description
[0060] Figure 1 This is a schematic diagram of a sorted trie structure according to an embodiment of the present invention;
[0061] Figure 2 This is a schematic diagram illustrating the determination of the initial search trie node set in one embodiment of the present invention;
[0062] Figure 3 This is a flowchart illustrating the search process for a specific starting search term node according to an embodiment of the present invention. Detailed Implementation
[0063] The following will be combined with the appendix in the embodiments of the present invention. Figure 1 ~Attached Figure 3 The technical solutions, structural features, objectives and effects achieved in the embodiments of the present invention will be described in detail.
[0064] It should be noted that the accompanying drawings are in a very simplified form and use non-precise proportions. They are only used to facilitate and clarify the purpose of illustrating the embodiments of the present invention, and are not intended to limit the implementation conditions of the present invention. Therefore, they have no substantial technical significance. Any modifications to the structure, changes in the proportional relationship, or adjustments to the size should still fall within the scope of the technical content disclosed in the present invention, provided that they do not affect the effects and objectives that the present invention can produce.
[0065] It should be noted that, in this invention, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only the expressly listed elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus.
[0066] This invention provides a computer interlocking code point search method based on a sorted trie, such as... Figures 1-3 As shown, the computer interlocking code point search method includes:
[0067] Step S1: Initialize the sorted trie data structure, which includes a sorted trie containing a root node and several tree nodes, a sorted one-dimensional array sortedBitArr storing pointers to all code point objects, and a two-dimensional array val2NodesArr used to determine the starting search node of the trie.
[0068] Step S2: Insert all code point objects contained in the code point table in sequence. Each time an insertion is performed, reconstruct the sorted dictionary tree data structure according to the code point object name string. Insert the pointer of each code point object into the sorted one-dimensional array sortedBitArr in order, and refill the two-dimensional array val2NodesArr used to determine the starting search dictionary tree node according to the newly added tree node.
[0069] Step S3: Based on the keyword currently entered by the user, obtain all the starting search trie nodes based on the two-dimensional array val2NodesArr. Traverse the sorted trie in preorder from each starting search trie node. If the keyword is successfully matched, determine all matching code point objects by combining the sorted one-dimensional array sortedBitArr and put them into the search results set.
[0070] The root node and each tree node include: a node representation character value (val), a pointer to the first child node (firstChild), a pointer to the next right sibling node at the same level (rightNode), and the index (startIdx) of the first occurrence of the node in sortedBitArr. Furthermore, val occupies 1 byte, firstChild occupies 4 bytes in a 32-bit program, rightNode occupies 4 bytes in a 32-bit program, and startIdx occupies 4 bytes. Therefore, without considering byte alignment, a node in a sorted trie (including the root node and tree nodes) will occupy 13 bytes.
[0071] Furthermore, the initialization of the sorted trie data structure described in step S1 specifically includes the following steps:
[0072] Step S11: Initialize the root node of the trie, which includes the following steps: Step S111: Initialize the node representation character value val of the root node of the trie to 0, indicating that root only represents the root node of the trie and does not represent any code point object associated node; Step S112: Initialize firstChild and rightNode of the root node of the trie to nullptr, indicating that no code point object has been inserted at present; Step S113: Initialize startIdx of the root node of the trie to 0, indicating that the smallest index of all code point objects obtained by traversing down from the root node of the trie in sortedBitArr is 0.
[0073] Step S12: Initialize the sorted one-dimensional array sortedBitArr, which stores pointers to all code point objects. The sorted one-dimensional array sortedBitArr is sorted in ascending order according to the code point object name string. Since there are no code point objects at the time of initialization, only a certain amount of space needs to be reserved for code point object pointers.
[0074] Step S13: Initialize the two-dimensional array val2NodesArr used to determine the starting search node of the trie, where the first dimension of val2NodesArr represents the index of the character value mapping and the second dimension represents the array of pointers to all trie nodes corresponding to the character value.
[0075] It should be noted that since the code point object name string only includes 37 characters: '-', '0-9', and 'A-Z', according to the ASCII sorting rules, the subscript corresponding to '-' is 0, the subscript corresponding to '0-9' is 1-10, and the subscript corresponding to 'A-Z' is 11-36. Since there are no other tree nodes of the trie except for the root node when initializing the sorted trie data structure, it is sufficient to initialize the first dimension of val2NodesArr to 37.
[0076] Furthermore, step S2 specifically includes the following steps:
[0077] Step S21: Generate a code point object based on the code point table, wherein the code point object includes a code point name string;
[0078] Step S22: Initialize the current tree node curNode to the firstChild of the dictionary root node root, set the array of all successfully matched tree nodes to the traversal array parentNodes, and insert the dictionary root node root into parentNodes to indicate a successful match of the dictionary root node root.
[0079] Step S23: Extract the characters char from the code point object name string in sequence, and reconstruct the sorted trie data structure according to the state of the currently traversed tree node curNode. Here, the currently traversed tree node curNode is the pointer firstChild to the first child node of the previous successfully matched tree node (i.e., the parent node of curNode).
[0080] Since the meaning of the current curNode is the pointer firstChild of the first child node of the previous parent node being traversed, in one embodiment, if the current tree node curNode is nullptr, it means that there is no tree node corresponding to the current character char, the match fails, and a new tree node is created, specifically including the following steps:
[0081] Step S231: Initialize the firstChild and rightNode of the first new tree node to nullptr, and initialize the val of the first new tree node to the current character char;
[0082] Step S232: Initialize the startIdx of the first new tree node to the startIdx of its corresponding parent node + 1. This is because the sorting order of the string corresponding to the newly created first tree node is always "slightly larger" than the sorting order of the string corresponding to its parent node, so the startIdx of the first new tree node is initialized to the startIdx of its corresponding parent node + 1. Furthermore, because a new child node has been inserted into the sorted trie, a recursive preorder traversal of all tree nodes in the parentNodes array is performed, incrementing the startIdx of all tree nodes to the right of the first new tree node by 1. These right-side tree nodes include the firstChild node, rightNode node, and derived nodes of all successfully matched nodes. Step S232 ensures that the entire sorted trie remains ordered after the insertion of the first new tree node.
[0083] Step S233: Insert the code point object pointer into sortedBitArr at the startIdx of the first new tree node, which also ensures the order of sortedBitArr.
[0084] Step S234: Based on the val value of the first new tree node, insert the first new tree node into the structure val2NodesArr.
[0085] Furthermore, if there are still remaining characters in the current code position object name string that have not been processed, then a second new tree node for the remaining characters is created sequentially, which includes the following steps:
[0086] Step S235: Initialize the node representation character value val of the second new tree node to the current character char;
[0087] Step S236: Each time a second new tree node is created, the firstChild of the first new tree node is used. Since they are the name strings of the same code point object, the startIdx of the second new tree node is set to the startIdx of the first new tree node.
[0088] Step S237: Based on the val value of the second new tree node, insert each second new tree node into the structure of val2NodesArr to complete the insertion process of the code point object.
[0089] In another embodiment, if the currently traversed tree node curNode is not nullptr, then the rightNode is traversed sequentially to the right to match char. There are three cases: Case 1: If the val of a tree node matches char successfully, it means that there are tree nodes with the same prefix; Case 2: If the val of a tree node is greater than char, it means that the match has failed and a third new tree node needs to be created; Case 3: If all the right tree nodes have been traversed and no tree node with val equal to char is found, it means that the match has failed and a fourth new tree node needs to be created.
[0090] In case 1, if the val of a tree node matches char successfully, it indicates that there are tree nodes with the same prefix. Furthermore, if all characters in the current code point object name string have been processed (i.e., there are no unprocessed characters in the current code point object name string), it means that the current code point object name string is a prefix of the name string of other code point objects. This implies that the string order of the code point object is "slightly smaller" than the order of the string corresponding to its firstChild child node. Therefore, a recursive preorder traversal is performed on the startIdx of all child nodes derived from the current tree node curNode and all sibling nodes and their derived nodes at the same level on the right, incrementing their startIdx by 1 to complete the insertion process of the code point object. At the same time, a recursive preorder traversal is performed on all tree nodes in the traversal array parentNodes, incrementing the startIdx of all tree nodes to the right of the first new tree node by 1. The right-side tree nodes in parentNodes include the firstChild node, rightNode node, and derived nodes of all successfully matched nodes, completing the insertion process of the code point object. Conversely, if the val of a tree node fails to match char, then the curNode is inserted into parentNodes, and the curNode is set as the firstChild of that tree node. Then, the process returns to step S23 to proceed to the next traversal.
[0091] In case 2, if the val of a certain tree node is greater than char, it indicates a matching failure and a third new tree node needs to be created; further, creating the third new tree node includes:
[0092] Step S241: Initialize the firstChild of the third new tree node to nullptr, the rightNode to curNode, and initialize val to the current character char;
[0093] Step S242: Set the startIdx of the third new tree node to equal the startIdx of curNode. Perform a recursive preorder traversal on the derived nodes generated downwards from curNode and all sibling nodes and their derived nodes on the right side of the same level, incrementing the startIdx of each tree node by 1. At the same time, perform a recursive preorder traversal on all tree nodes in the traversal array parentNodes, incrementing the startIdx of all tree nodes to the right of the third new tree node by 1. Here, all tree nodes on the right side in parentNodes include the firstChild node, rightNode node, and derived nodes of the firstChild node and rightNode of all successfully matched nodes. Step S242 ensures that the entire sorted trie remains ordered after the insertion of the third new tree node.
[0094] Step S243: Insert the code point object pointer into sortedBitArr, with the insertion position being the startIdx of the third new tree node;
[0095] Step S244: Based on the val value of the third new tree node, insert the third new tree node into the structure val2NodesArr. Further, if there are still characters left unprocessed in the current code position object name string, repeat steps S235 to S237 to create tree nodes for the remaining characters sequentially, ultimately completing the insertion process for the code position object.
[0096] In case 3, if after traversing all right-hand tree nodes, no tree node is found with a val equal to char, it indicates a matching failure and a fourth new tree node needs to be created; further, creating the fourth new tree node includes:
[0097] Step S251: Initialize the firstChild and rightNode of the fourth new tree node to nullptr, and initialize val to the current character char;
[0098] Step S252: Set the startIdx of the fourth new tree node to the startIdx of the last traversed tree node + 1. At the same time, perform a recursive preorder traversal of all tree nodes in the traversal array parentNodes, so that the startIdx of all tree nodes to the right of the fourth new tree node is incremented by 1. The right-hand tree nodes in parentNodes include the firstChild node, rightNode node, and derived nodes of the firstChild node and rightNode of all successfully matched nodes. Step S252 ensures that the entire sorted trie remains ordered after the fourth new tree node is inserted.
[0099] Step S253: Insert the code point object pointer into sortedBitArr, with the insertion position being the startIdx of the fourth new tree node;
[0100] Step S254: Based on the val value of the fourth new tree node, insert the fourth new tree node into the structure val2NodesArr. Similarly, if there are still characters left unprocessed in the current code position object name string, repeat steps S235 to S237 to create tree nodes for the remaining characters in sequence, and finally complete the insertion process of the code position object.
[0101] It should be further noted that in step S21, after each code point object is generated based on the code point table, it is necessary to check whether the code point object name is duplicated to determine if the code point object is duplicated. Specifically, if the code point object name is duplicated, the code point object is discarded, that is, the code point object is not inserted into the sorted trie structure. As an optional embodiment, the sorting rule based on the sortedBitArr structure is sorted according to the size of the code point object name string, so a binary search can be used to check whether a duplicate code point object already exists, with a time complexity of O(log₂N), where N is the length of the current sortedBitArr structure. After all code point objects are inserted, the final sorted trie structure is as follows. Figure 1 As shown.
[0102] Based on the sorted trie structure generated in steps S1 and S2, according to the keyword currently entered by the user, all starting search trie nodes are obtained from the two-dimensional array val2NodesArr. The sorted trie is then traversed in preorder from each starting search trie node. If the keyword matches successfully, the code point object is determined by combining it with the sorted one-dimensional array sortedBitArr and added to the search results set. Specifically, the following steps are included:
[0103] Step S31: Based on the first character of the keyword currently entered by the user, determine all starting search trie nodes in combination with val2NodesArr;
[0104] Step S32: Iterate through each starting search trie node in turn, and set the current node curNode to the firstChild of the current starting search trie node.
[0105] Step S33: Iterate through the first character and subsequent characters of the input keyword char in sequence, and process them according to the state of curNode in each iteration;
[0106] S34. If the search is successful based on the state of curNode in this traversal, retrieve the startIdx of the last traversed tree node, determine the starting search index of the code point object that meets the search conditions in the sortedBitArr structure, retrieve the code point object and put it into the search result set, and obtain the search result set for this search.
[0107] The traversal process described in step S33 based on the state of curNode includes the following states: (1) If curNode is nullptr, it means that the search failed and the process returns to step S32 to proceed to the next search; (2) If curNode is not nullptr, the process traverses rightNode sequentially to the right to match char.
[0108] State (2) includes two cases: First case: If the val of a certain tree node is greater than char; or if all the right-hand tree nodes have been traversed but no tree node has a val equal to char, the search fails and returns to step S32 to proceed to the next search. Second case: If the val of a certain tree node matches char successfully, and there are no unprocessed characters in the input keyword string (i.e., all the input keyword strings have been processed), the search succeeds. The startIdx of the tree node is retrieved, and the process continues in step S34. If there are still unprocessed characters in the input keyword string, the curNode is set as the firstChild of the curNode, and the process returns to step S33 to proceed to the next traversal.
[0109] In step S34, after determining the starting search index that satisfies the search conditions, due to the top-down traversal of the tree structure, all code point object name strings that satisfy the search conditions must have the same prefix (the prefix end character is the character corresponding to the tree node). Therefore, the sortedBitArr structure is traversed sequentially, code point objects are taken out and put into the search results set until code point objects with different prefixes appear, and the current index is recorded as endIdx.
[0110] The necessity of using the endIdx of each successful search record to filter subsequent searches is as follows:
[0111] (1) If the startIdx of the subsequent starting search trie node is less than the endIdx, it means that the subsequent starting search trie node is the downstream subtree of the last traversed tree node in this search. Even if the search is successful, the search result set of this search is a subset of the currently known search result set, so this search needs to be skipped.
[0112] (2) If the startIdx of the subsequent starting search trie node is greater than or equal to endIdx, it means that the starting search trie node is the right-hand node of the last traversed tree node of the previous successful search. If the search is successful, the search result set of this search does not intersect with the current known search result set. Then the search result sets are merged and endIdx is recorded again.
[0113] The following specific embodiments illustrate the computer interlocking code point search method based on a sorted trie provided by the present invention. First, refer to... Figure 1 Those skilled in the art can easily understand the structure of a sorted trie and its insertion process. Based on this, refer to Figure 2-3 This section describes the process by which this search method searches for all code points that meet the conditions based on user-input keywords.
[0114] First refer to Figure 1 , Figure 1 Since the initialization process has already been completed in the existing sorted trie structure, the initialization process described in step S1 will not be described again. Furthermore, taking the insertion of a code point object named "1DG-DGJ" as an example, the code point object insertion process of this method is introduced to assist in understanding the sorted trie structure, including the following steps:
[0115] Initialize the current tree node curNode to the firstChild of the root node root of the dictionary tree, set char to the first character '1' of the code point object name, and set the array of all successfully matched tree nodes to parentNodes, which at this time includes root; since the current tree node curNode is not nullptr, traverse rightNode in turn to try to match char.
[0116] Specifically, starting from the current tree node `curNode`, traverse to the nodes to the right in sequence (`curNode = curNode->rightNode`), checking if there exists a tree node whose `val == char` (i.e., the node's character value is the same as the current `char`). If not, create and insert a new tree node. When traversing to the first node, a match (`curNode->val == char`) is found, and the `curNode` is inserted into the `parentNodes` array, recorded as `curNode = curNode->firstChild`, and `char = 'D'`.
[0117] Repeat the above process, and check the 'D', 'G', '-', 'D', and 'G' characters following the code point object name string in turn. It is found that the corresponding tree nodes already exist, so there is no need to create new tree nodes.
[0118] For the last character 'J', starting from curNode, traverse the nodes to the right in sequence. A match is found at the first node, so no new tree node needs to be created. Since the code point object was checked for duplicates before insertion, the current sorted trie must contain a code point object with the prefix "1DG-DGJ". Therefore, after inserting the new code point object (i.e., "1DG-DGJ"), the startIdx of the tree node (represented as a subtree of curNode) corresponding to subsequent characters needs to be incremented by 1, indicating that the position of the code point object with the first occurrence of this prefix in sortedBitArr should be shifted one position to the right. Similarly, the startIdx of all right sibling nodes and derived nodes of the tree node curNode, and all right sibling nodes and derived nodes of each element in the parentNodes array, are also incremented by 1.
[0119] After determining that the startIdx of the tree node curNode is 1, that is, determining the insertion position of the code point object in sortedBitArr, the final result is as follows: Figure 1 As shown, the code point objects in the sorted one-dimensional array `sortedBitArr` are, in order, {(1DG-D), (1DG-DGJ), (1DG-DGJ-DI), (1DG-G), (1DG-R), (1DG-W), (2-DBJ)...}. Since no new tree nodes are created during this code point object insertion process, no operation is needed on `val2NodesArr`. The code point object insertion process is now complete, and the trie structure remains ordered after insertion.
[0120] Furthermore, refer to Figures 2-3 ,exist Figure 1 Based on the existing sorted trie structure, this paper introduces the code point object search process of this method, taking the search for a set of code point objects with the keyword "DG" as an example, to help understand the sorted trie structure. It includes the following steps:
[0121] Step S31: Based on the first character 'D' of the current keyword, determine all starting search trie nodes using val2NodesArr, denoted as starting tree node 1 (node1), starting tree node 2 (node2), ...;
[0122] Step S32: Take out node1 and set the current traversal node curNode to the firstChild of the current starting search trie node, that is, curNode = node1->firstChild;
[0123] Step S33: Set char = the first character of the keyword followed by the character 'G'; Since curNode is not nullptr, start from the current node curNode and traverse to the right node in turn (curNode = curNode->rightNode) to match char, that is, determine whether there is a tree node whose val == char. If it does not exist, the search for the current starting dictionary node fails.
[0124] When traversing to the first node, a match (curNode->val == char) is successful. Since char is the last character of the keyword, the startIdx of curNode is retrieved, and the starting search index of the code point object that satisfies the search condition in the sortedBitArr structure is determined to be 0. Starting from 0, code point objects are retrieved sequentially and added to the search results set until a code point object with a different prefix, "2-DBJ", is encountered, and the current index is recorded as endIdx as 5.
[0125] Similarly, repeat step S32: specifically, retrieve node2, and set the current traversal node curNode to the firstChild of the current starting search trie node, i.e., curNode = node2->firstChild. Since the startIdx(0) of curNode is less than the endIdx(5) of the previous successful search record, it indicates that the current curNode is the downstream child node of the previous successful search trie node. Therefore, even if the search with node2 as the starting search node is successful, the resulting search result set is a subset of the currently known search result set. Therefore, skip node2 and do not proceed with the subsequent search process.
[0126] Step S34: After traversing all the initial search dictionary nodes (including node1 and node2), the code point search process ends, and the final search result set is {(1DG-D), (1DG-DGJ-DI), (1DG-G), (1DG-R), (1DG-W), ...}. Comparing with the keyword "DG", the search result set meets the expected result.
[0127] In summary, compared with the prior art, the computer interlocking code point search method based on sorted trie provided by the present invention determines the starting search tree node and combines preorder traversal matching to determine the starting search index of the search result set in the sorted code point object array sortedBitArr, thereby reducing a large number of repetitive and unnecessary search processes and overcoming the high time complexity of the existing "brute force" traversal search method.
[0128] Furthermore, by setting a two-dimensional array val2NodesArr to determine the starting search node of the trie, this invention overcomes the limitation that the classic trie can only be traversed and searched starting from the root node, and overcomes the limitation that the trie search method can only perform prefix matching, thus greatly improving search efficiency, success rate and usability.
[0129] Meanwhile, this invention also sets member variables such as a pointer firstChild pointing to the first child node and a pointer rightNode pointing to the next right sibling node at the same level, which greatly reduces the address space consumption of each node compared to the classic multi-way tree.
[0130] Furthermore, this invention ensures the orderliness of the trie and sortedBitArr structure during the insertion of code point objects, thus realizing the function of insertion and searching simultaneously. For example, before insertion, the binary search method combined with sortedBitArr is used to determine whether the code point object is duplicated. Search operations can be performed before the sorted trie structure is completed, which greatly improves the flexibility of the method.
[0131] Although the present invention has been described in detail through the preferred embodiments above, it should be understood that the above description should not be considered as a limitation of the present invention. Various modifications and substitutions to the present invention will be apparent to those skilled in the art after reading the above description. Therefore, the scope of protection of the present invention should be defined by the appended claims.
Claims
1. A method for searching a computer interlocking code bit based on a ranked trie, characterized by, include: Step S1: Initialize the sorted trie data structure, which includes a sorted trie containing a root node and several tree nodes, a sorted one-dimensional array sortedBitArr storing pointers to all code point objects, and a two-dimensional array val2NodesArr used to determine the starting search node of the trie. Step S2: Insert all code point objects contained in the code point table in sequence. Each time an insertion is performed, reconstruct the sorted dictionary tree data structure according to the code point object name string. Insert the pointer of each code point object into the sorted one-dimensional array sortedBitArr in order, and refill the two-dimensional array val2NodesArr used to determine the starting search dictionary tree node according to the newly added tree node. Step S3: Based on the keyword currently entered by the user, obtain all the starting search trie nodes based on the two-dimensional array val2NodesArr. Traverse the sorted trie in preorder from each starting search trie node. If the keyword is successfully matched, determine all matching code point objects by combining the sorted one-dimensional array sortedBitArr and put them into the search results set.
2. The ranking trie-based computer interlocking bit search method according to claim 1, wherein, The root node and each of the tree nodes include: a node representing the character value val, a pointer to the first child node firstChild, a pointer to the next right sibling node at the same level right, and an index startIdx where the node first appears in sortedBitArr.
3. The ranking trie-based computer interlocking bit search method according to claim 2, wherein, Step S1, which involves initializing the sorted trie data structure, includes step S11: initializing the trie root node; step S11 specifically includes the following steps: Step S111: Initialize the node representation character value val of the root node of the trie to 0, indicating that root only represents the root node of the trie and does not represent any code point object associated node; Step S112: Initialize the firstChild and rightNode of the root node of the trie to nullptr, indicating that no code point object has been inserted at present; Step S113: Initialize the startIdx of the root node of the trie to 0, which means that the smallest index of all code point objects obtained by traversing down from the root node of the trie in sortedBitArr is 0.
4. The ranking trie-based computer interlocking bit search method according to claim 2, wherein, The initialization of the sorted trie data structure in step S1 includes step S12: initializing a sorted one-dimensional array sortedBitArr that stores pointers to all code point objects, wherein the sorted one-dimensional array sortedBitArr is sorted in ascending order according to the code point object name string.
5. The ranking trie-based computer interlocking bit search method according to claim 2, wherein, The initialization of the sorted trie data structure in step S1 includes step S13: initializing a two-dimensional array val2NodesArr used to determine the starting search node of the trie, wherein the first dimension of val2NodesArr represents the index of the character value mapping, and the second dimension represents an array of pointers to all trie nodes corresponding to the character value.
6. The computer interlocking code point search method based on a sorted trie as described in claim 2, characterized in that, Step S2 specifically includes the following steps: Step S21: Generate a code point object based on the code point table, wherein the code point object includes a code point name string; Step S22: Initialize the current tree node curNode to the firstChild of the dictionary root node root, set the array of all successfully matched tree nodes to the traversal array parentNodes, and insert the dictionary root node root into parentNodes to indicate a successful match of the dictionary root node root. Step S23: Extract the character char from the code position object name string in sequence, and reconstruct the sorted trie data structure according to the state of the current tree node curNode. Here, the current tree node curNode represents the pointer firstChild, which is the first child node of the previous tree node that was successfully matched.
7. The ranking trie-based computer interlocking bit search method according to claim 6, wherein, If the current tree node curNode is nullptr, it means that there is no tree node corresponding to the current character char, the match fails, and a new tree node is created. The specific steps include the following: Step S231: Initialize the firstChild and rightNode of the first new tree node to nullptr, and initialize the val of the first new tree node to the current character char; Step S232: Initialize the startIdx of the first new tree node to the startIdx of the corresponding parent node + 1, and recursively perform a preorder traversal of all tree nodes in the parentNodes array, so that the startIdx of all tree nodes to the right of the first new tree node is incremented by 1; wherein, the all tree nodes to the right in parentNodes include the firstChild node, rightNode node and the derived nodes of the firstChild node and rightNode of all successfully matched nodes; Step S233: Insert the code point object pointer into sortedBitArr, with the insertion position being the startIdx of the first new tree node; Step S234: Based on the val value of the first new tree node, insert the first new tree node into the structure val2NodesArr.
8. The ranking trie-based computer interlocking bit search method according to claim 7, wherein, If there are still remaining characters in the current code position object name string that have not been processed, then create a second new tree node for the remaining characters in sequence, which includes the following steps: Step S235: Initialize the node representation character value val of the second new tree node to the current character char; Step S236: Each time a second new tree node is created, the firstChild of the first new tree node is used. Since they are the name strings of the same code point object, the startIdx of the second new tree node is set to the startIdx of the first new tree node. Step S237: Based on the val value of the second new tree node, insert each second new tree node into the structure of val2NodesArr to complete the insertion process of the code point object.
9. The ranking trie-based computer interlocking bit search method according to claim 8, wherein, If the current traversal node curNode is not nullptr, then traverse rightNode sequentially to the right, matching char, which includes three cases: Case 1: If the val of a certain tree node matches the char successfully, it means that there are tree nodes with the same prefix. Case 2: If the val of a certain tree node is greater than char, it means that the match failed and a third new tree node needs to be created; Case 3: If after traversing all the right-hand tree nodes, no tree node is found whose val is equal to char, it means that the match has failed and a fourth new tree node needs to be created.
10. The ranking trie-based computer interlocking bit search method according to claim 9, wherein, In Case 1, if the name string of the current code point object has been processed, it means that the name string of the current code point object is a prefix of the name string of other code point objects. A recursive preorder traversal is performed on the startIdx of all child nodes derived from the current tree node curNode and all sibling nodes and their derived nodes at the same level on the right, incrementing their startIdx by 1, thus completing the insertion process of the code point object. Simultaneously, a recursive preorder traversal is performed on all tree nodes in the traversal array parentNodes, incrementing the startIdx of all tree nodes to the right of the current tree node curNode by 1. The right-hand tree nodes in parentNodes include the firstChild node, rightNode node, and derived nodes of all successfully matched nodes.
11. The ranking trie-based computer interlocking bit search method according to claim 9, wherein, In case 2, creating the third new tree node includes: Step S241: Initialize the firstChild of the third new tree node to nullptr, the rightNode to curNode, and initialize val to the current character char; Step S242: Set the startIdx of the third new tree node to equal the startIdx of curNode. Perform a recursive preorder traversal on the derived nodes generated downward from curNode and all sibling nodes and their derived nodes on the right side of the same level, so that the startIdx of each tree node is incremented by 1. At the same time, perform a recursive preorder traversal on all tree nodes in the traversal array parentNodes, so that the startIdx of all tree nodes located to the right of the third new tree node is incremented by 1. Wherein, all tree nodes on the right side in parentNodes include the firstChild node, rightNode node and derived nodes of all successfully matched nodes. Step S243: Insert the code point object pointer into sortedBitArr, with the insertion position being the startIdx of the third new tree node; Step S244: Based on the val value of the third new tree node, insert the third new tree node into the structure val2NodesArr.
12. The ranking trie-based computer interlocking bit search method according to claim 9, wherein, In case 3, creating the fourth new tree node includes: Step S251: Initialize the firstChild and rightNode of the fourth new tree node to nullptr, and initialize val to the current character char; Step S252: Set the startIdx of the fourth new tree node to the startIdx of the last traversed tree node + 1, and at the same time perform a recursive preorder traversal of all tree nodes in the traversal array parentNodes, so that the startIdx of all tree nodes to the right of the fourth new tree node is incremented by 1; wherein, the all tree nodes to the right in parentNodes include the firstChild node, rightNode node and the derived nodes of the firstChild node and rightNode of all successfully matched nodes. Step S253: Insert the code point object pointer into sortedBitArr, with the insertion position being the startIdx of the fourth new tree node; Step S254: Based on the val value of the fourth new tree node, insert the fourth new tree node into the structure val2NodesArr.
13. The ranking dictionary tree based computer interlocking code bit search method according to claim 11 or 12, characterized in that, If there are still characters left to be processed in the current code position object name string, repeat steps S235 to S237 to create tree nodes for the remaining characters in sequence, and complete the insertion process of the code position object.
14. The ranking trie-based computer interlocking bit search method according to claim 6, wherein, Each time a code point object is generated based on the code point table, the code point object name is checked to determine if the code point object is duplicated. If the code point object name is duplicated, the code point object is discarded and not inserted into the sorted trie data structure.
15. The ranking trie-based computer interlock code bit search method according to claim 2, wherein, Step S3 specifically includes the following steps: Step S31: Based on the first character of the keyword currently entered by the user, determine all starting search trie nodes in combination with val2NodesArr; Step S32: Iterate through each starting search trie node in turn, and set the current node curNode to the firstChild of the current starting search trie node. Step S33: Iterate through the first character and subsequent characters of the input keyword char in sequence, and process them according to the state of curNode in each iteration; Step S34: If the search of the curNode state is successful, retrieve the startIdx of the last traversed tree node, determine the starting search index of the code point object that meets the search conditions in the sortedBitArr structure, retrieve the code point object and put it into the search result set, and obtain the search result set of this search.
16. The ranking trie-based computer interlock code bit search method of claim 15, wherein, If curNode is nullptr, it means that the search failed and return to step S32 to proceed to the next search; if curNode is not nullptr, then traverse rightNode to the right in sequence to match char.
17. The ranking trie-based computer interlock code bit search method of claim 16, wherein, If, when curNode is not nullptr, a tree node with a val greater than char is found during traversal, or if a tree node with a val equal to char is not found after traversing all right-hand tree nodes, the search fails and returns to step S32 to proceed to the next search.
18. The ranking trie-based computer interlock code bit search method of claim 16, wherein, If the val of a tree node matches the char successfully and there are no unprocessed characters in the input keyword string, the search is successful. The startIdx of the tree node is retrieved and the process continues in step S34. If there are still unprocessed characters in the input keyword string, the curNode is set as the firstChild of the curNode and the process returns to step S33 to proceed to the next traversal.
19. The ranking trie-based computer interlock code bit search method of claim 15, wherein, After determining the starting search index that satisfies the search conditions, the sortedBitArr structure is traversed sequentially, code point objects are extracted and added to the search results set, until code point objects with different prefixes are found, and the current index is recorded as endIdx.
20. The computer interlocking code point search method based on a sorted trie as described in claim 19, characterized in that, If the startIdx of the subsequent starting search trie node is less than the endIdx, it means that the starting search trie node is the downstream subtree of the last traversed tree node of the previous successful search. If the search is successful, the search result set of this search is a subset of the currently known search result set, and this search needs to be skipped. If the startIdx of the subsequent starting search trie node is greater than or equal to endIdx, it means that the starting search trie node is the right-hand node of the last traversed tree node of the previous successful search. If the search is successful, the search result set of this search does not intersect with the currently known search result set, so the search result sets are merged and endIdx is recorded again.