CN111767285A - Method for rapidly judging node relation - Google Patents
Method for rapidly judging node relation Download PDFInfo
- Publication number
- CN111767285A CN111767285A CN202010607667.3A CN202010607667A CN111767285A CN 111767285 A CN111767285 A CN 111767285A CN 202010607667 A CN202010607667 A CN 202010607667A CN 111767285 A CN111767285 A CN 111767285A
- Authority
- CN
- China
- Prior art keywords
- node
- nodes
- value
- tree
- values
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/20—Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
- G06F16/22—Indexing; Data structures therefor; Storage structures
- G06F16/2228—Indexing structures
- G06F16/2246—Trees, e.g. B+trees
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Software Systems (AREA)
- Data Mining & Analysis (AREA)
- Databases & Information Systems (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
The invention relates to the field of tree data processing, and discloses a method for quickly judging node relation, which is convenient for quickly judging the relation between any two nodes under the condition that a tree is not in a complete structure. The invention adds an index value for recording the position information of the node in the tree to the node in the tree, and can generate the index value of each node in the tree in sequence based on the length of the index value, the number of segments and the number of characters required by a single segment when the index value is added, wherein: each segment value of the root node is a default value, and the root node is represented by only one character; the value of each node before segmentation is the same as that of a father node; the segmented value of each node is a default value; and the segmented values of the nodes in the same hierarchy are sequentially increased. When the relation between the two nodes needs to be judged, the relation between the two nodes is quickly obtained by comparing the index values of the two nodes. The invention is suitable for judging the node relation in the tree data structure.
Description
Technical Field
The invention relates to the field of tree data processing, in particular to a method for rapidly judging node relation.
Background
The tree is a common data structure, and usually, information of a parent node and a child node is stored in a node, and when a tree is stored persistently, the parent node is recorded. In the persistent storage form, when the tree needs to be used, the information of all the nodes is read, the tree is reassembled according to the father node information, and finally the tree is operated according to the needs.
When the tree structure stored in a common form is used, all nodes need to be taken out, restored and reused; meanwhile, when the tree is stored in the database, if the relevant nodes of one node, such as all descendant nodes, need to be queried, the operation is very troublesome.
Disclosure of Invention
The technical problem to be solved by the invention is as follows: a method for rapidly judging node relation is provided, so that the relation between any two nodes can be rapidly judged under the condition that a tree is not in a complete structure.
In order to solve the problems, the invention adopts the technical scheme that: and adding an index value for recording the position information of the node in the tree to the node in the tree, and obtaining the relationship of the two nodes by comparing the index values of the two nodes when the relationship of the two nodes needs to be judged.
Further, the step of adding the index value to the node according to the present invention may be as follows:
randomly selecting a value range [ min, max ] of a single character in an index value, wherein the range comprises n values, and the minimum value min of the range is a default value;
traversing the tree to obtain the degree m and the depth d of the tree;
calculating the number x of characters required for a single segment of the index value according to the degree m of the tree and the n:
determining the segmentation quantity s of the index value according to the tree depth d;
calculating the length L of the index value according to the number x of characters required by a single segment of the index value and the number s of the segments;
sequentially generating index values of all nodes in the tree on the basis of the length L of the index values, the number s of the segments and the number x of characters needed by a single segment, wherein: each segment value of the root node is a default value; the value of each node before segmentation is the same as that of a father node; the segmented value of each node is a default value; and the segmented values of the nodes in the same hierarchy are sequentially increased.
Further, if the number x of characters required by a single segment of the index value is greater than 1, since a tree has only one root node, the tree can be represented by using one character. For example, if the index value has 3 segments, each segment has 2 characters, and the default value of each character is 0, the index value of the root node may be 00000, which may save one character over 000000.
Specifically, the formula for calculating the number of characters required by a single segment according to the present invention may be:
where floor () is a floor function.
Specifically, the formula for calculating the length of the index value in the present invention may be: l ═ 1+ (d-1) ×.
Further, the specific step of obtaining the relationship between the two nodes by comparing the index values of the two nodes in the present invention may include:
setting the current-level segmentation values of the two nodes and the segmentation values before the current-level segmentation as default values in sequence to obtain index values of ancestor nodes at each level of the two nodes;
if two nodes are nodes in the same level and the index values of ancestor nodes in all levels are the same, the two nodes are brother nodes;
and if the two nodes are nodes of different hierarchies, wherein the hierarchy of the node X is greater than the hierarchy of the node Y, setting the values of the node X after the hierarchy segmentation to the maximum value to obtain the upper limit of the index value of the descendant node, searching whether the node Y exists in the range (the index value of the node and the upper limit of the index value of the descendant node), and if so, determining the node Y to be the descendant node of the node X.
The invention has the beneficial effects that: when a tree is stored, an index value is added to each node in the tree, so that each node in the tree has the index value for recording the position information of the node in the tree, when the relationship between two nodes needs to be judged, the relationship between two nodes can be quickly obtained by comparing the index values of the two nodes, and the information of the tree can be obtained and the relationship between any two nodes can be judged even if the tree is not in a complete structure.
Drawings
FIG. 1 is a diagram illustrating addition of an index value to a node according to an embodiment.
Detailed Description
In order to solve the problem of judging the relationship between any two nodes under the condition that a tree is not in a complete structure, the invention provides a method for quickly judging the relationship between the nodes, which can add an index value for recording the position information of the node in the tree to the nodes in the tree when one tree is stored, and quickly obtain the relationship between the two nodes by comparing the index values of the two nodes when the relationship between the two nodes needs to be judged.
Before describing the present invention in detail, a brief description will be given of several concepts of tree data structures that may be used in the present invention.
Tree: the tree is a set with a hierarchical relationship formed by y (y ═ 0) finite nodes, and is a nonlinear data structure. Degree of tree: maximum value of degree of each node in the tree. Degree of node: the number of subtrees a node contains is called the degree of the node. Depth or height of the tree: maximum hierarchy of nodes in the tree. And (3) offspring: any node in a subtree rooted at a certain node is called a descendant of the node. Ancestor of the node: from the root to all nodes on the branch through which the node is located. And (4) parent node: if a node contains a child, then the node is called the parent of its child. The brother nodes: nodes having the same parent are referred to as siblings of each other.
When the structure of the tree is fixed, the structure and size of the index are also determined. The index value is composed of segmented character strings, each segment represents the position of a corresponding node in the hierarchy of the corresponding node and sequentially increases from left to right, the last non-default value segment in the index represents the position of the node in the hierarchy of the corresponding node, and the value of a descendant node is a default value. The specific steps of the invention for increasing the index value to the node are as follows:
1. and randomly selecting a value range [ min, max ] of a single character in an index value, wherein the range comprises n values, and the minimum value min of the range is a default value. For example, the value range of a single character is [0, 1, 2, 3], so that n is 4, and min is 0; if the value range of a single character is [1, 3, 5, 7, 9], then n is 5 and min is 1.
2. Traversing the tree results in a tree degree m and a tree depth d.
3. Calculating the number x of characters required by a single segment of the index value according to the degree m of the tree and the n, wherein the formula is as follows:where floor () is a floor function.
4. The number of segments s of the index value is determined according to the tree depth d, and in the present invention, the number of segments is equal to the tree depth, i.e., s-d.
5. Calculating the length L of the index value according to the number x of characters needed by a single segment of the index value and the number s of the segments, wherein the formula is as follows: l ═ 1+ (s-1) ×.
6. Sequentially generating index values of all nodes in the tree on the basis of the length L of the index values, the number s of the segments and the number x of characters needed by a single segment, wherein: each segment value of the root node is a default value; the value of each node before segmentation is the same as that of a father node; the segmented value of each node is a default value; and the segmented values of the nodes in the same hierarchy are sequentially increased.
The specific steps of the invention for comparing the index values of two nodes to obtain the relationship between the two nodes comprise:
1. setting the current-level segmentation values of the two nodes and the segmentation values before the current-level segmentation as default values in sequence to obtain index values of ancestor nodes at each level of the two nodes;
2. if two nodes are nodes in the same level and the index values of ancestor nodes in all levels are the same, the two nodes are brother nodes;
3. and if the two nodes are nodes of different hierarchies, wherein the hierarchy of the node X is greater than the hierarchy of the node Y, setting the values of the node X after the hierarchy segmentation to the maximum value to obtain the upper limit of the index value of the descendant node, searching whether the node Y exists in the range (the index value of the node and the upper limit of the index value of the descendant node), and if so, determining the node Y to be the descendant node of the node X.
Examples
As shown in fig. 1, a tree with a height of 3 and a tree degree of 2; the value range of the determined index character is [0, 1, 2], namely n is 3, and the default value is 0;
When generating the root node index value: the values of the 3 segments of the root node A are all default values of 0, so the index value is 000; for the first child node B of the root node a, the first segment value of the node B is 0, and the node B is the first node in the brother, so the value 1 is obtained, the node position is determined, the subsequent segment is set to the default value 0, so the node B is the index value 010. Since the present invention sequentially increases the values of the segments when generating the nodes with the same index value hierarchy, the index value 020 of the second child node C of the root node a is obtained. According to the principle of generating the node index values of the present invention, the index values of the node D, E, F can be sequentially 011, 012, 013.
When comparing the index values of two nodes:
taking node F (index value 021) as an example, the index values 020 and 000 of the ancestor node C, A can be obtained in sequence by setting the current-level segment value of node F and the segment value before the current-level segment as default values;
taking node C (index value 020) as an example, the index range of the descendant node is (020,022), since the index value 021 is in the range of (020,022], the node F with the index value 021 is the descendant node of node C:
taking node C as an example, node B is at the same level as node C, and their parents are all node A (index value 000), so they are siblings of each other.
Claims (6)
1. A method for rapidly judging node relationship is characterized in that: and adding an index value for recording the position information of the node in the tree to the node in the tree, and obtaining the relationship of the two nodes by comparing the index values of the two nodes when the relationship of the two nodes needs to be judged.
2. The method of claim 1, wherein the method for rapidly determining the node relationship comprises: the steps for adding index values to nodes are as follows:
randomly selecting a value range [ min, max ] of a single character in an index value, wherein the range comprises n values, and the minimum value min of the range is a default value;
traversing the tree to obtain the degree m and the depth d of the tree;
calculating the number x of characters required for a single segment of the index value according to the degree m of the tree and the n:
determining the segmentation quantity s of the index value according to the tree depth d;
calculating the length L of the index value according to the number x of characters required by a single segment of the index value and the number s of the segments;
sequentially generating index values of all nodes in the tree on the basis of the length L of the index values, the number s of the segments and the number x of characters needed by a single segment, wherein: each segment value of the root node is a default value; the value of each node before segmentation is the same as that of a father node; the segmented value of each node is a default value; and the segmented values of the nodes in the same hierarchy are sequentially increased.
3. The method of claim 2, wherein the method for rapidly determining the node relationship comprises: the segment in which the root node is located is represented by one character.
5. The method of claim 2, wherein the method for rapidly determining the node relationship comprises: the formula for calculating the length of the index value is as follows: l ═ 1+ (s-1) ×.
6. The method of claim 2, wherein the method for rapidly determining the node relationship comprises: the specific step of obtaining the relationship between the two nodes by comparing the index values of the two nodes comprises the following steps:
setting the current-level segmentation values of the two nodes and the segmentation values before the current-level segmentation as default values in sequence to obtain index values of ancestor nodes at each level of the two nodes;
if two nodes are nodes in the same level and the index values of ancestor nodes in all levels are the same, the two nodes are brother nodes;
and if the two nodes are nodes of different hierarchies, wherein the hierarchy of the node X is greater than the hierarchy of the node Y, setting the values of the node X after the hierarchy segmentation to the maximum value to obtain the upper limit of the index value of the descendant node, searching whether the node Y exists in the range (the index value of the node and the upper limit of the index value of the descendant node), and if so, determining the node Y to be the descendant node of the node X.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010607667.3A CN111767285A (en) | 2020-06-29 | 2020-06-29 | Method for rapidly judging node relation |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010607667.3A CN111767285A (en) | 2020-06-29 | 2020-06-29 | Method for rapidly judging node relation |
Publications (1)
Publication Number | Publication Date |
---|---|
CN111767285A true CN111767285A (en) | 2020-10-13 |
Family
ID=72724236
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202010607667.3A Pending CN111767285A (en) | 2020-06-29 | 2020-06-29 | Method for rapidly judging node relation |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN111767285A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116680252A (en) * | 2023-06-13 | 2023-09-01 | 智研汇(上海)科技有限公司 | Tree data structure and node batch moving method thereof |
-
2020
- 2020-06-29 CN CN202010607667.3A patent/CN111767285A/en active Pending
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116680252A (en) * | 2023-06-13 | 2023-09-01 | 智研汇(上海)科技有限公司 | Tree data structure and node batch moving method thereof |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US5649023A (en) | Method and apparatus for indexing a plurality of handwritten objects | |
US7739251B2 (en) | Incremental maintenance of an XML index on binary XML data | |
CN102693266B (en) | Search for method, the navigation equipment and method of generation index structure of database | |
CN108304409B (en) | Carry-based data frequency estimation method of Sketch data structure | |
CN104462582B (en) | A kind of web data similarity detection method based on structure and content secondary filtration | |
CN111581215B (en) | Array tree data storage method, fast search method and readable storage medium | |
CN109902142B (en) | Character string fuzzy matching and query method based on edit distance | |
CN113434557B (en) | Method, device, equipment and storage medium for querying range of label data | |
CN110427471B (en) | Natural language question-answering method and system based on knowledge graph | |
US20230281182A1 (en) | R-tree index merging and updating method and apparatus based on hilbert curve, and medium | |
CN104598517A (en) | Storage and inquiry technology for tree structure table based on ordinary database | |
CN111984732B (en) | Method, node and blockchain network for implementing decentralization search on blockchain | |
CN115688779B (en) | Address recognition method based on self-supervision deep learning | |
CN110134901B (en) | Multilink webpage tampering judging method based on flow analysis | |
CN111767285A (en) | Method for rapidly judging node relation | |
CN109933589B (en) | Data structure conversion method for data summarization based on ElasticSearch aggregation operation result | |
KR20020017219A (en) | Method for optimal retrieval of multi-resolution by high speed on the great capacity database | |
CN110245248B (en) | Remote sensing image keyword query method | |
CN115840751B (en) | Novel tree-shaped data coding method | |
CN102087666A (en) | Indexes based on covering relationship between nodes and key words, constructing method and query method thereof | |
US20120319875A1 (en) | Method for storing node information of huffman tree and corresponding decoding method | |
CN109299106B (en) | Data query method and device | |
CN115269745B (en) | Method, equipment and storage medium for mapping relational data to graph data | |
CN115062206B (en) | Webpage element searching method and electronic equipment | |
JP2013069042A (en) | Information processing device and information processing program |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |