Understand the concept and nature of trees in data structures

If you want to learn the tree in the data structure well, you must understand the basic concepts of the tree, otherwise you will get more and more chaotic.

Introduction to the tree

Tree: It is a finite set of n (n>=0) nodes. When n=0, it is called an empty tree. The tree is a logical structure

And any non-empty tree should satisfy:
1) There is one and only one specific node called the root

2) When n>1, the remaining nodes can be divided into m (m>0) non-intersecting finite sets, each of which is a tree itself, called the subtree of the root node

Conclusion: There are only n-1 edges in a tree with n nodes

The following figure is an example to understand the concept of the tree

Insert picture description here

Basic concept of tree

1. Ancestor node and descendant node: if k is descendant node, ABE is the ancestor node of k

2. Parent node and child node: If k is the child node of E, and E is the parent node of k

3. Sibling node: the node that has the same parents is called sibling node, such as K and L are sibling nodes

4. Degree: The number of child nodes of a node in the tree is called the degree of the node, for example, the degree of node B is 2

5. The degree of the tree: the maximum degree in the tree is called the degree of the tree, as shown in the figure above, the degree of the tree is 3

6. Branch nodes: nodes with degree greater than 0 are called branch nodes (nodes with children), such as ABCDE

7. Leaf nodes: nodes with degree 0 are called leaf nodes (nodes without children), such as FGHIJKL

Note: Some textbooks call the first layer as the 0th layer, you need to pay attention, here the first layer is defined as the first layer

8. Node level: For example , node A is in the first layer, node B is in the second layer, node E is in the third layer, and node K is in the fourth layer.

9. The height of the node: starting from the leaf node and accumulating layer by layer, such as the height of node B is 3.

10. Depth of the node : starting from the root node and accumulating layer by layer from top to bottom (it is the opposite of the height of the node), such as the depth of node B is 2

The height (depth) of the tree is the maximum number of nodes in the tree

11. Ordered tree: If the position of B and D is exchanged, the tree in the above picture becomes another tree, then the tree in the above picture is an ordered tree

12. Unordered tree: If the position of B and D is exchanged, the tree in the above picture is still the same tree, then the tree in the above picture is an unordered tree

13. Path: The path between two nodes in the tree is composed of the sequence of nodes passed between these two nodes (the path must be from top to bottom)

14. Path length: the number of edges experienced on the path. For example, the path from A to E has two sides, so the path length is 2

15. Forest: m(m>=0) a collection of disjoint trees. Trees and forests can be converted to each other. For example, if root node A is removed from the above figure, it becomes composed of three trees. Forest, add a root node A in the figure below to become a tree

Insert picture description here

Tree nature

1. The number of nodes in the tree is equal to the degree of all nodes plus 1

2. There are at most m^(i-1) nodes on the i-th level in the tree of degree m (i>=1)

3. The m-ary tree with height h has at most (m^h)/(m-1) nodes (conclusion 3 is derived from conclusion 2)

4. The minimum height of an m-ary tree with n nodes is, rounded up logm(n(m-1)+1)

Insert picture description here
Interpretation property 1: The degree of all leaf nodes is 0, so leaf nodes are not considered, only branch nodes are considered. The degree of E is 2 (K and L respectively), the degree of B is 2 (E, F), the degree of C is 1 (G), the degree of D is 3 (H, I, J), and the degree of A is 3 (B, C, D). At this time, you will find that there is a node that is not included, that is, the root node A, so add 1. This is the origin of this conclusion

Interpretation property 2: Assume that each node has a maximum degree m. The first layer can only have one node, the maximum number of nodes in the second layer is m, the third layer is m*m, and so on, the maximum number of nodes in the i-th layer is m^(i-1)

Insert picture description here
Interpretation property 3: Just add up all the nodes in conclusion 2 to get this conclusion (derived using the geometric sequence summation formula learned in high school)
misplaced subtraction
Sn=a1+a2 +a3 +…+ an
Sn q= a1 q+a2 q+…+a(n-1) q+an q= a2 +a3 +…+an+an q

Subtract the above two formulas to get (1-q) Sn=a1-an q

Interpretation 4: Conclusion 4 is derived on the basis of conclusion 3. From conclusion 3 we can see that n=(m^h-1)/(m-1), why should we round up? Because there are decimals, it proves that there are nodes in the last layer

If you don’t understand the properties 2, 3, and 4, you can give an example, as shown in the figure below. Substituting the formula into it can deepen your understanding.

Insert picture description here
This article is referenced from "The King's Way"

Everyone is welcome to read. I have limited knowledge, and there are inevitably mistakes or omissions in the blog I wrote. I hope you guys can give me some advice. Thank you.

Guess you like

Origin blog.csdn.net/qq_41936224/article/details/108167581