Disclosure of Invention
The application aims at the technical problems and aims at providing a tree-shaped data structure and a node batch moving method thereof.
In order to solve the above-mentioned problems, according to an aspect of the present application, there is provided a tree-shaped data structure including a plurality of nodes including a parent node ID field, a node level field, and an ordering value field, the data type of the ordering value field being a string type of N-ary;
the tree data structure starts with a root node, and according to a preset ordering rule of ordering value fields, the ordering value fields of the nodes of each layer are respectively assigned according to a hierarchy, the ordering value fields of two adjacent nodes in the same layer are different by N/2, and two nodes in different layers are allowed to have the same ordering value field.
Optionally, in the tree data structure, the ordering value field of the first node of the same layer of nodes is a preset ordering value.
In order to solve the above problem, according to another aspect of the present application, there is provided a node batch moving method based on the tree data structure of the present application, including:
and acquiring the sequence values of the upper node and the lower node of the target position, taking the value between the sequence values of the upper node and the lower node, obtaining a new sequence value of the node to be moved, modifying the new sequence value, and completing the movement.
Optionally, a median algorithm is adopted to take the value between the sorting values of the upper node and the lower node, and the median algorithm is as follows:
dividing the difference between the sorting values of the upper node and the lower node by 2 to form a whole, obtaining a new sorting value of the node to be moved and modifying the new sorting value.
Optionally, when the difference between the ordering values of the upper node and the lower node is divided by 2 and rounded, and the obtained value is 0, modifying the character string length of the new ordering value field of the node to be moved, wherein the modifying method is as follows:
and rounding N/2 as a target character to be added, adding the target character after the sequence value field of the previous node or the next node according to the sequence rule of the sequence value field, obtaining a new sequence value of the node to be moved and modifying.
Optionally, when the ranking values of the upper node and the lower node of the target position are obtained, if the upper node of the target position does not exist, the node to be moved is considered to be moved to the first node of the current layer, at this time, a first ranking value generating algorithm is adopted to obtain a new ranking value of the node to be moved and modify the new ranking value, and the first ranking value generating algorithm is as follows:
and taking N/2 as a target value, subtracting or adding the target value to the ordering value field of the next node according to the ordering rule of the ordering value field, obtaining a new ordering value of the node to be moved, and modifying.
Optionally, when the sorting values of the upper node and the lower node of the target position are obtained, if the next node of the target position does not exist, the node to be moved is considered to be moved to the tail node of the current layer, at this time, a tail sorting value generating algorithm is adopted to obtain a new sorting value of the node to be moved and modify the new sorting value, and the tail sorting value generating algorithm is as follows:
and taking N/2 as a target value, adding or subtracting the target value to the sequence value field of the last node according to the sequence rule of the sequence value field, obtaining a new sequence value of the node to be moved, and modifying.
Optionally, when the target value is added or subtracted to the sorting value field, if the rightmost bit is greater than the maximum value of the sorting value field, the bit is advanced according to the N-ary rule, and if the rightmost bit is less than the minimum value of the sorting value field, the bit is borrowed forward according to the N-ary rule.
Optionally, before or after obtaining the new ranking value of the node to be moved and modifying, further performing:
judging whether the father node ID field of the target position is the same as the father node ID field of the node to be moved, if so, modifying the father node ID field of the node to be moved into the father node ID field of the target position.
Optionally, after obtaining a new ordering value of the node to be moved and modifying, judging whether the first node of the current layer reaches a preset rearrangement value, if so, triggering a reordering algorithm, wherein the reordering algorithm is as follows:
locking the current layer, acquiring all nodes of the current layer, and carrying out assignment on the sequence value fields of all the nodes of the current layer according to the sequence rule of the preset sequence value fields, wherein the sequence value fields of two adjacent nodes differ by N/2.
The beneficial effects are that: in a conventional tree data structure, when the position of a node changes, the subtree of the node needs to be readjusted to ensure that the entire tree structure is still correct. This process requires traversing the entire subtree and reordering each node, which can result in a significant reduction in the efficiency of the algorithm. After the tree data structure is adopted, when the node position is changed, only the sorting value of the current node and the connection (PID) of the upper node are required to be changed, and other associated nodes are not required to be changed, so that the algorithm efficiency can be remarkably improved. Particularly, under the condition of larger data volume, the time complexity of the algorithm can be greatly reduced, and the performance and efficiency of the algorithm are improved.
Description of the embodiments
The preferred embodiments of the present application will be described in detail below with reference to the attached drawings, so that the objects, features and advantages of the present application will be more clearly understood. It should be understood that the embodiments shown in the drawings are not intended to limit the scope of the application, but rather are merely illustrative of the true spirit of the application.
In the following description, for the purposes of explanation of various disclosed embodiments, certain specific details are set forth in order to provide a thorough understanding of the various disclosed embodiments. One skilled in the relevant art will recognize, however, that an embodiment may be practiced without one or more of the specific details. In other instances, well-known devices, structures, and techniques associated with the present application may not be shown or described in detail to avoid unnecessarily obscuring the description of the embodiments.
Reference throughout this specification to "one embodiment" or "an embodiment" means that a particular feature, structure, or characteristic described in connection with the embodiment is included in at least one embodiment. Thus, appearances of the phrases "in one embodiment" or "in an embodiment" in various places throughout this specification are not necessarily all referring to the same embodiment. Furthermore, the particular features, structures, or characteristics may be combined in any suitable manner in one or more embodiments.
In the following description, for the purposes of clarity of presentation of the structure and manner of operation of the present application, the description will be made with the aid of directional terms, but such terms as "forward," "rearward," "left," "right," "outward," "inner," "outward," "inward," "upper," "lower," etc. are to be construed as convenience, and are not to be limiting.
The tree data structure is an important nonlinear data structure, and can represent one-to-many relations among data table elements, so that the tree is visually defined as a hierarchical structure with branch relations. In computer science, trees are used to simulate data sets that have tree-structured properties. It is a set of n (n > 0) finite nodes that have a hierarchical relationship. It has the following characteristics: each node has zero or more child nodes; nodes without parent nodes are called root nodes; each non-root node has and has only one parent node; in addition to the root node, each child node may be divided into a plurality of disjoint sub-trees.
Referring to FIG. 3, an embodiment of the present application provides a tree data structure comprising a number of nodes, including a root node and other nodes. Each node includes a node ID field (ID), a node name field (name), a parent node ID field (pid), a node level field (level), and a rank value field (rank).
Wherein the node ID field and the parent node ID field are typically of a long integer data type, and the node name field is typically of a string data type. Of course other data types are possible.
The node level field indicates the hierarchical relationship of the current node with respect to the root node. The data type of the node level field is an integer. The node level fields are ordered starting with the root node, and the node level field of the parent node in the tree data structure is smaller than or greater than the node level field of the next node by 1. According to the habit, a mode that the node level field of the parent node is smaller than the node level field of the lower node thereof by 1 is generally adopted, but a mode that the node level field of the parent node is larger than the node level field of the lower node thereof by 1 is also possible. The hierarchy is ordered according to a preset rule.
For example, the node level field of the root node is 0, the node level field of the lower node of the root node is 1, and so on.
The rank value field indicates the position of the current node in the tree data structure, independent of the hierarchy, which is the absolute position of the current node on the entire tree. The data type of the sort value field is an N-ary character string. The N-system herein may be binary, octal, decimal, hexadecimal, etc.
Referring to fig. 4, in a computer, a comparison of the sizes of character strings is generally determined by ASCII, and when comparing the sizes of two character strings, the following procedure is followed:
1. and (5) taking out the characters to transfer the first character, and comparing the sizes of ASCII codes.
2. And if the comparison results in the step 1 are the same, respectively taking out the second characters of the two character strings for comparison.
3. And sequentially processing until the first non-identical character is found, and determining the sizes of the two character strings according to the comparison result of the character.
4. If no different character is found until the end, it is determined that the two character strings are identical.
Based on the principle, the application utilizes the character string size principle when the node is moved, thereby realizing more convenient node movement.
In one embodiment, the data type of the rank value field is a string greater than decimal.
The tree data structure of the application starts with a root node, and according to the ordering rule of the preset ordering value fields, the ordering value fields of the nodes of each layer are respectively assigned according to the hierarchy, the ordering value fields of two adjacent nodes in the same layer are different by N/2, and two nodes in different layers are allowed to have the same ordering value field.
Nodes with the same node level field are considered to be nodes of the same layer. The ordering of the ordering value field is to acquire a plurality of nodes of the same layer for the plurality of nodes of the same layer, and the plurality of nodes are sequentially assigned according to the absolute positions in the tree data structure according to the ordering rule.
The ordering rule of the ordering value field may be that the ordering rule is increased from the first node in the same layer of nodes, or may be that the ordering rule is decreased from the first node in the same layer of nodes.
According to the habit, it is preferable to employ an ordering rule that increases in order from the first node.
For example, if the node level field and the ranking value field of the root node are both 0, the node level field of all child nodes of the root node is assigned 1, and the ranking value field is assigned according to the absolute position of the root node, for example, a string of 1,2, … ….
The ordering value field of the root node can be assigned as AAAA0, the first child node in the root node position is used as the first node of the layer with the node level of 1, the ordering value field of the root node can be assigned as AAAA0, and other nodes can be assigned in sequence according to the absolute positions of the child nodes in the tree data structure.
The application adopts the mode that the sequence value fields of two adjacent nodes in the same layer differ by N/2, so that enough other character strings which can be inserted are arranged between the sequence values of the two adjacent nodes, and the movement of the nodes can be greatly facilitated.
In an embodiment, in the tree data structure, the ordering value field of the first node of the same layer of nodes is a preset ordering value.
The preset ranking value may be determined according to the number of nodes in the selected N-ary and tree data structures, and preferably takes a relatively intermediate value, so as to facilitate the forward or backward insertion of the nodes to be moved.
For example, when the data type of the ordering value field is hexadecimal and the number of nodes is small, the ordering value of the head node may be A0, AA0, AAA0 or the like, but when the number of nodes is large, it may be AAAA0 or AAAAA0 or the like.
The embodiment of the application provides a node batch moving method of a tree-shaped data structure, which is based on the tree-shaped data structure of each embodiment as an initial tree-shaped data structure and comprises the following steps:
and acquiring the sequence values of the upper node and the lower node of the target position, taking the value between the sequence values of the upper node and the lower node, obtaining a new sequence value of the node to be moved, modifying the sequence value field of the node to be moved, and completing the movement of the node to be moved.
Based on the principle of the size of the character string, the new sorting value of the node to be moved is set as a value between the sorting values of the upper node and the lower node of the target position, so that the absolute position of the node is between the upper node and the lower node without modifying the sorting values of other nodes.
Taking hexadecimal as an example, the data type of the sorting value field is that the sorting values of the upper node and the lower node of the target position are respectively AAAA0 and AAAA8, and the new sorting value of the node to be moved only needs to take any value between AAAA0 and AAAA8, for example, take AAAA1, AAAA2, AAAA3 … …, AAAA7 or the like.
In one embodiment, a median algorithm is used to take the value between the ordering values of the upper and lower nodes, where the median algorithm is:
dividing the difference between the sorting values of the upper node and the lower node by 2 to form a whole, obtaining a new sorting value of the node to be moved and modifying the new sorting value.
For example, the ranking values of the upper node and the lower node of the target position are respectively 0 and 5, the difference between the ranking values of the upper node and the lower node is divided by 2, and the obtained value is 2, and the new ranking value of the node to be moved is 2.
In an embodiment, when the difference between the ordering values of the upper node and the lower node is divided by 2 and rounded, and the obtained value is 0, the character string length of the new ordering value field of the node to be moved is modified, and the modification method is as follows:
and rounding N/2 as a target character to be added, adding the target character after the sequence value field of the previous node or the next node according to the sequence rule of the sequence value field, obtaining a new sequence value of the node to be moved and modifying.
For example, the ranking values of the upper and lower nodes of the target position are 2 and 3, respectively, the difference between the ranking values of the upper and lower nodes is divided by 2, and the obtained value is 0. At this time, the length of the character string needs to be modified, i.e., the length of the generated median increases. Taking hexadecimal as an example, 16 is divided by 2 to form a whole to obtain 8 as a target character. And as the ordering rule is that the ordering rules are increased from the first node in sequence, the target character is added after the ordering value field of the last node, so that a new ordering value of the node to be moved is obtained and is modified to be 28. The final sorting values of the three adjacent nodes are 2, 28 and 3 respectively.
If the sorting values of the upper node and the lower node of the target position are 3 and 2 respectively, the sorting rule is that the sorting values are sequentially reduced from the first node, the target character is added after the sorting value field of the next node, and a new sorting value of the node to be moved is obtained and modified to be 28. The final sorting values of the three adjacent nodes are 3, 28 and 2 respectively.
In an embodiment, when the ranking values of the upper node and the lower node of the target position are obtained, if the upper node of the target position does not exist, the node to be moved is considered to be moved to the first node of the current layer, at this time, a first ranking value generating algorithm is adopted to obtain a new ranking value of the node to be moved and modify the new ranking value, and the first ranking value generating algorithm is as follows:
n/2 is rounded as a target value, and the target value is subtracted or added to the sequencing value field of the next node according to the sequencing rule of the sequencing value field, so that a new sequencing value of the node to be moved is obtained and modified.
For example, taking hexadecimal and ordering rules as examples which are increased from the first node in sequence, dividing 16 by 2 to obtain a target value of 8;
1. the ordering value of the head node D generated at a certain level is AAAA0;
2. inserting a node C above the node D, wherein the sorting value of the node C is AAA98;
3. inserting a node B above the node C, wherein the sorting value of the node B is AAA90;
4. inserting a node A above the node B, wherein the sorting value of the node A is AAA88;
every time a node is inserted up, its rank value is subtracted by 8 from the rank value of the previous node, if it is already less than the minimum value of each bit (0), then the forward borrow is 16 in a bin.
If the ordering rule is that the ordering rule is sequentially reduced from the first node, a node C is inserted above the node D, the ordering value of the node C is AAAA8, and the like.
In an embodiment, when the ranking values of the upper node and the lower node of the target position are obtained, if the next node of the target position does not exist, the node to be moved is considered to be moved to the tail node of the current layer, at this time, a new ranking value of the node to be moved is obtained and modified by adopting an end ranking value generating algorithm, and the end ranking value generating algorithm is as follows:
and taking N/2 as a target value, adding or subtracting the target value from the sequence value field of the last node according to the sequence rule of the sequence value field, obtaining a new sequence value of the node to be moved, and modifying.
For example, taking hexadecimal and ordering rules as examples which are increased from the first node in sequence, dividing 16 by 2 to obtain a target value of 8;
1. the sorting value of the head node A generated at a certain level is AAAA0;
2. inserting a node B below the node A, wherein the sorting value of the node B is AAAA8;
3. inserting a node C below the node B, wherein the sorting value of the node C is AAAB0;
4. inserting a node D below the node C, wherein the sorting value of the node D is AAAB8;
for each node added, the sorting value of the node is added by 8 above the sorting value of the previous node, and if the sorting value is already greater than the maximum value (F) of each bit, the sorting value is carried forward in 16 scale.
If the ordering rule is decreasing from the first node, a node B is inserted below the node A, its ordering value is AAA98, and so on.
In an embodiment, before or after obtaining the new ranking value of the node to be moved and modifying, further performing:
judging whether the father node ID field of the target position is the same as the father node ID field of the node to be moved, if so, modifying the father node ID field of the node to be moved into the father node ID field of the target position.
When there is a cross-hierarchy mobile scenario, the original parent node ID field of the node to be moved is different from the parent node ID field of the target location, and thus the parent node ID field of the node to be moved needs to be modified. Modifying the parent node ID field may occur before or after the ranking value calculation.
In an embodiment, after obtaining a new ranking value of the node to be moved and modifying, determining whether the first node of the current layer reaches a preset ranking value, if so, triggering a reordering algorithm, where the reordering algorithm is as follows:
locking the current layer, acquiring all nodes of the current layer, and carrying out assignment on the sequence value fields of all the nodes of the current layer according to the sequence rule of the preset sequence value fields, wherein the sequence value fields of two adjacent nodes differ by N/2.
The preset rearrangement value of the present embodiment is preset, for example, a limit value of 0 is set, and other values near 0, such as A0, etc., may be set. On the premise of ensuring that the ordering value field of the tree data structure has the ordering according to the absolute position, the ordering value of each level cannot start from 0, if the first node becomes 0 or is close to 0 after a plurality of moving operations, the level data needs to be rearranged, and the reordering needs to be locked, namely the moving operation cannot be performed in the process.
Examples
Referring to fig. 5, the ordering value field of the tree data structure adopts hexadecimal character strings, the ordering rule is that the ordering value field is sequentially increased from the first node, a certain layer of the tree data structure is provided with nodes a to D, and the corresponding ordering values are CCC0, CCC8, CCD0 and CCD8 respectively.
The node D needs to be moved between the node a and the node B, and the following operations are required:
1. obtaining the PID of the node D (the PID can be obtained by finding the node D on a graphical interface and pressing a left mouse button to select the node D);
2. obtaining the PID of the target position, the rank value of the node A and the rank value of the node B (the PID and the rank values of the node A and the node B can be respectively obtained by dragging the node D between the node A and the node B under the condition of selecting the node D);
3. judging whether the PID of the node D is the same as the PID of the node A (or the PID of the node B), if so, indicating that the node D moves across the hierarchy, modifying the PID of the node D to the PID of the node A, and if so, not processing the PID;
4. calculating the median of the two values as CCC4 by using a median algorithm through the rank value of the node A and the rank value of the node B, and modifying the rank value of the node D as CCC4;
5. and re-rendering the node, and completing the operation.
The step 3 of determining PID, the step 4 of calculating and modifying the rank value may be vice versa.
Examples
Referring to fig. 5, the ordering value field of the tree data structure adopts hexadecimal character strings, the ordering rule is that the ordering value field is sequentially increased from the first node, a certain layer of the tree data structure is provided with nodes a to D, and the corresponding ordering values are CCC0, CCC8, CCD0 and CCD8 respectively.
Before the node D needs to be moved to the node a, the following operations are required:
1. obtaining the PID of the node D (the PID can be obtained by finding the node D on a graphical interface and pressing a left mouse button to select the node D);
2. obtaining the PID of the target position and the rank value of the node A (the PID and the rank value of the node A can be respectively obtained before dragging the node D to the node A under the condition of selecting the node D);
3. judging whether the PID of the node D is the same as the PID of the node A, if so, indicating that the node D moves across the hierarchy, modifying the PID of the node D to the PID of the node A, and if so, not processing the PID;
4. the rank value of the node D is modified to be CCB8 by adopting a rank value of the node A and adopting a first order value generation algorithm to calculate the generated value to be CCB8;
5. and re-rendering the node, and completing the operation.
The step 3 of determining PID, the step 4 of calculating and modifying the rank value may be vice versa.
Examples
Referring to fig. 5, the ordering value field of the tree data structure adopts hexadecimal character strings, the ordering rule is that the ordering value field is sequentially increased from the first node, a certain layer of the tree data structure is provided with nodes a to D, and the corresponding ordering values are CCC0, CCC8, CCD0 and CCD8 respectively.
After the node a needs to be moved to the node D, the following operations are required:
1. obtaining the PID of the node A (the PID can be obtained by finding the node A on a graphical interface and pressing a left mouse button to select the node A);
2. obtaining the PID of the target position and the rank value of the node D (the PID and the rank value of the node D can be respectively obtained after dragging the node A to the node D under the condition of selecting the node A);
3. judging whether the PID of the node A is the same as the PID of the node D, if so, indicating that the node A moves across the hierarchy, modifying the PID of the node A to the PID of the node D, and if so, not processing the PID;
4. calculating a generated value as CCE0 by adopting an end sorting value generation algorithm through the rank value of the node D, and modifying the rank value of the node A as CCE0;
5. and re-rendering the node, and completing the operation.
The step 3 of determining PID, the step 4 of calculating and modifying the rank value may be vice versa.
While the preferred embodiments of the present application have been described in detail, it will be appreciated that those skilled in the art, upon reading the above teachings, may make various changes and modifications to the application. Such equivalents are also intended to fall within the scope of the application as defined by the following claims.