Method for Finding Data Path Nodes in a Tree Structure

By converting tree structure data into hierarchical lists and looping recursively, the problem of high computational volume of tree structure data path query is solved, and query efficiency is improved.

CN114168591BActive Publication Date: 2025-08-05四川启睿克科技有限公司
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202111331767.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-11-11
Publication Date
2025-08-05
Estimated Expiration
2041-11-11

AI Technical Summary

Technical Problem

In the prior art, when querying the tree structure data path, it is necessary to traverse the leaf nodes one by one from the root node, resulting in large amount of calculation and affecting the user experience.

Method used

Convert the tree structure data into a hierarchical list form, and recursively loop the leaf nodes and hierarchical list to find out all node ids on the root node to the leaf node path.

Benefits of technology

This greatly reduces the path search time from leaf node to root node and improves query efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114168591B_ABST
    Figure CN114168591B_ABST
Patent Text Reader

Abstract

The present invention discloses a method for finding path nodes of tree-structured data, including: after receiving a list of leaf node IDs to be saved, obtaining the tree-structured data; converting the tree-structured data into hierarchical structure data in the form of a hierarchical list; recursively looping through the tree-structured data, looping through the list of leaf node IDs and the hierarchical list, and finding all node IDs on the path from the root node to the leaf node; compared with directly obtaining all node IDs on the path from the root node to the leaf node according to the tree structure and then obtaining them after conversion, the present invention greatly reduces the amount of calculation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of tree - shaped data search, and particularly to a method for finding path nodes of tree - shaped structure data. Background Art

[0002] When saving user permissions, in order to facilitate converting the user's permissions into a permission tree, all node IDs of the path from the root node to each leaf node, that is, the permission node path, are saved in the database. However, the structure of the permission tree obtained by the front - end extends from the root node to each leaf node. If it is necessary to query all node IDs on the path from a certain leaf node to the root node, since the leaf node has no parent - node information, it can only traverse from the root node to all leaf nodes one by one until the specified leaf node is found, which is very complex to process and greatly affects the user experience. Summary of the Invention

[0003] To solve the problems existing in the prior art, the purpose of the present invention is to provide a method for finding path nodes of tree - shaped structure data. Compared with directly obtaining all node IDs on the path from the root node to the leaf node according to the tree - shaped structure and then obtaining them after conversion, the calculation amount of the present invention is greatly reduced.

[0004] To achieve the above purpose, the technical solution adopted by the present invention is: A method for finding path nodes of tree - shaped structure data, comprising the following steps:

[0005] Step ⅰ: After receiving the list of leaf - node IDs to be saved, obtain the tree - shaped structure data;

[0006] Step ⅱ: Convert the tree - shaped structure data into hierarchical structure data in the form of a hierarchical list;

[0007] Step ⅲ: Recursively loop through the tree - shaped structure data, loop through the list of leaf - node IDs and the hierarchical list, and find all node IDs on the path from the root node to the leaf node.

[0008] As a further improvement of the present invention, in step ⅰ, the tree - shaped structure data includes: the current permission ID and the parent - permission ID. The first layer is the root node, which has no upper - level permission and is defined as - 1. The sub - node information data format is a list. If the current node is a leaf node, the sub - node information is empty. Each piece of data in the sub - node information list also contains the current permission ID, the parent - permission ID, and the sub - node information.

[0009] As a further improvement of the present invention, in step ⅱ, each layer of the hierarchical structure data consists of several Map objects, and each Map object only contains the permission ID of the current layer and the corresponding parent - permission ID.

[0010] As a further improvement of the present invention, in step 3, recursively looping the tree - shaped structure data, the loop of the leaf node id list and the hierarchical list specifically includes:

[0011] For the first recursion, loop to fetch the unique root - node data, including the permission id and the parent - permission id. Since it is the first layer currently and the hierarchical result is empty, use the root - node id as the key and the - 1 defined by the parent node as the value to create key - value pair information as the first layer of the hierarchical result.

[0012] Then perform the second recursion. The passed - in tree - shaped structure is converted from the previous root node to the child - node information of the root node. Since it is not root - node information, there may be multiple passed - in tree - shaped menus. Loop through the passed - in tree - shaped menus, use the id of the passed - in tree - shaped menu as the key and the parent - node id as the value to create a Map object as the second layer of the hierarchical result, and keep recursing until the leaf nodes are recorded.

[0013] As a further improvement of the present invention, in step 3, finding all the node ids on the path from the root node to the leaf nodes specifically includes:

[0014] After obtaining the hierarchical result, loop through the list of leaf nodes passed in from the outer loop and loop through the previously obtained hierarchical result. First, record the leaf - node id. Starting from the last layer, that is, the leaf - node layer, loop to find the corresponding parent - node id of the passed - in leaf node, then record the parent - node id. Then loop through the second - last layer, and based on the parent - node id of the leaf node, find and record the parent - node id of the previous level until the root node. By looping like this, all the nodes on the paths from the root node to the passed - in leaf nodes are found.

[0015] The beneficial effects of the present invention are:

[0016] The present invention converts the tree - shaped structure data into a hierarchical structure according to the layer conversion list, places the root node in the first layer, the secondary nodes in the second layer, and the leaf nodes in the last layer. According to the leaf - node id, search for and record the parent - node id from the last layer outwards until the root node, greatly reducing the path - finding time from the leaf node to the root node. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] Figure 1 It is a flowchart of an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0018] The embodiments of the present invention will be described in detail below with reference to the drawings.

[0019] Embodiment 1

[0020] As Figure 1As shown, a method for finding data path nodes in a tree structure includes:

[0021] After receiving the list of leaf nodes to be saved, first, according to the general processing method, obtain the permission tree data of the tree structure, which includes the following data: the current permission ID, the parent ID. The first layer is the root node, which has no upper-level permission and is -1. The data format of the child node information is a list. If the current node is a leaf node, the child node information is empty. Each piece of data in the child node information list also includes the current permission ID, the parent ID, and the child node information.

[0022] Then, convert the tree structure. The converted data format is a multi-layer list, where each layer consists of several Map objects, and each Map only contains the permission ID of the current layer and the corresponding parent permission ID.

[0023] Recursively loop through the tree structure data. In the first recursion, loop through and take out the unique root node data, including the permission ID and the parent permission ID. Since it is the first layer and the layering result is empty, create a key-value pair with the root node ID as the key and the parent node -1 as the value, which serves as the first layer of the layering result. At this time, there is only one piece of data in the layering result, recording the root node and the corresponding parent node. Then, perform the second recursion. At this time, the input tree structure is converted from the previous root node to the child node information of the root node. The specific processing method is that since it is not root node information, there may be multiple input tree menus this time. Loop through these menus, create a Map object with the ID of these menus as the key and the parent node ID as the value, which serves as the second layer of the layering result, and continue recursively until the leaf nodes are recorded.

[0024] After obtaining the layering result, loop through the list of leaf nodes passed from the outer layer, and loop through the previously obtained layering result in the inner layer. First, record the leaf node ID. Starting from the last layer, that is, the leaf node layer, loop to find the parent node ID corresponding to the passed leaf node, then record the parent node ID, and then loop through the second-to-last layer. According to the parent node ID of the leaf node, find the parent node ID of the previous level and record it until the root node. By looping like this, all the nodes on the paths from the root node to each of the passed leaf nodes are found.

[0025] Embodiment 2

[0026] A certain project requires permission control. Since the permission control framework is not used, it is decided to complete the permission verification function by itself. As this embodiment only involves the saving of permissions and does not involve the processing of permissions, it is only related to permission saving here. Two related data tables are designed for permission saving. One stores all data permissions, and the other saves the permissions owned by users. The data table storing all data permissions has two key fields, the permission id, which is recorded in the form of a primary key, and the parent permission id, which saves the superior permission id of the current permission. If the current permission is the root permission, the parent permission id is -1; the other table stores the permissions of users, with two key fields, one is the user id and the other is the permission id.

[0027] First, read data from the table of all data permissions and convert it into a tree-structured data, and then cache it. When receiving the list of leaf permission ids and user id of the user, read the tree-structured data composed of all permissions from the cache, in the form of:

[0028]

[0029]

[0030] The root permission id is 1, and there are two permission information below it, with ids 2 and 3 respectively. There is another permission below id 3, with id 4;

[0031] Then start to convert the tree-structured data into hierarchical-structured data. Loop through the first layer, and the data is:

[0032]

[0033] Temporarily ignore the child nodes, create the data of the first-layer hierarchical structure with the id as the key and the parentId as the value, that is

[0034]

[0035] Create the second-layer Map data, still with the id as the key and the parent node id as the value. The created result is

[0036]

[0037] Create the third-layer Map data, and the created result is

[0038] {"4":"3"}

[0039] The final result of converting the tree into a hierarchical structure is

[0040] [{"1":"-1"},{"2":"1","3":"1"},{"4":"3"}]

[0041] If it is necessary to add permission 4 to the user with user ID 100 at this time, the method to find the permission with ID 4 from the root node is as follows:

[0042] Loop forward from the last layer of the hierarchical result. First, record the ID 4. The data in the last layer is {"4":"3"}. According to the ID 4, find the parent node ID as 3 and record 3. In the previous layer of data {"2":"1","3":"1"}, find the parent node ID corresponding to 3 as 1 and record 1. Then, in the first layer, find the parent node as -1. Since it has reached the root node, -1 is not recorded. In this way, all the nodes on the path from the leaf node to the root node, namely 4, 3, and 1, are found according to the leaf node.

[0043] The above-described embodiments only represent the specific implementation manners of the present invention. The description is relatively specific and detailed, but it should not be construed as a limitation on the scope of the patent of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present invention, several modifications and improvements can still be made, and these all belong to the protection scope of the present invention.

Claims

1. A method for finding a node in a tree structure data path, characterized in that: The following steps are involved: Step 1: After receiving the leaf node ID list to be saved, obtain the tree structure data; In step 1, the tree structure data includes: the current permission ID and the parent permission ID. The first layer is the root node, which has no upper permission and is defined as -1. The child node information data format is a list. If the current node is a leaf node, the child node information is empty. Each data in the child node information list also contains the current permission ID, parent permission ID and child node information. Step 2: convert the tree structure data into hierarchical structure data in the form of a hierarchical list; In step 2, each layer of the hierarchical structure data is composed of several Map objects, each of which contains only the permission ID of the current layer and the corresponding parent permission ID; Step 3: Recursively loop through the tree structure data, loop through the leaf node ID list and the hierarchical list, and find all the node IDs on the path from the root node to the leaf node; In step 3, recursively looping the tree structure data, looping the leaf node ID list and the hierarchical list specifically includes: In the first recursion, the unique root node data is looped out, including the permission ID and the parent permission ID. Since the current layer is the first layer and the layer result is empty, the root node ID is used as the key and the parent node definition of -1 is used as the value to create a key-value pair information as the first layer of the layer result; Then, a second recursion is performed. The incoming tree structure is converted from the previous root node to the child node information of the root node. Since it is not the root node information, there may be multiple incoming tree menus. The incoming tree menus are looped, and the id of the incoming tree menu is used as the key and the parent node id as the value. A Map object is created as the second layer of the hierarchical result. The recursion continues until the leaf node is recorded. In step 3, find all the node IDs on the path from the root node to the leaf node, including: After obtaining the hierarchical results, the outer loop passes the leaf node list, and the hierarchical results obtained before the inner loop, first record the leaf node id, and loop from the last layer, that is, the leaf node layer, to find the parent node id corresponding to the passed leaf node, and then record the parent node id, and then loop the second to last layer, according to the parent node id of the leaf node, find the parent node id of the next level up and record it, until the root node, and so on. All nodes on the path from the root node to each passed leaf node are found through this loop.

Citation Information

Patent Citations

  • Method, device and system for obtaining path between two nodes of tree structure data

    CN107679049A