Detailed explanation of tree and binary tree [data structure]

 It is enough to read one article about learning trees and binary trees! ! ! If you like it, remember to collect it, like it! ! !

 Some basic concepts and explanations of trees:

Definition of tree : A tree refers to a collection of N finite nodes with a hierarchical structure . It is a simple nonlinear structure. When N is equal to 0, it is an empty tree .

 First, let's look at what the tree looks like:

Let's look at the picture above. It looks like the root of a tree. At the same time, it is layer by layer, which obviously has a hierarchical structure.

 What is a node?

In layman's terms, the nodes in the above figure are those circles, and a circle is a node. When the number of nodes is 0, then it is an empty tree!

Next, let's talk about the detailed concept of this tree!

1. Root node: currently there is only one node in the tree structure without a parent node (no predecessor), then we call it the root node, such as node A in the above figure

2. Parent node: In the tree structure, each node (except the root node) has only one antecedent, then this antecedent is called the parent node of this node, for example, A node is a B, C, D node parent node of

3. Child nodes: In the tree structure, each node has 0 or more consequences (child nodes), and these consequences are called child nodes of this node. For example, node B is A node's children, etc.

4. Leaf nodes: Nodes without consequences in the tree structure are called leaf nodes, such as J, K, L, M in the above figure

5. Degree: The number of child nodes of a certain node in the tree is the degree of the node. The maximum degree of a certain node in the tree is the degree of the tree. For example, the degree of node B in the above figure is 3 , the degree of the D node is 2 and so on, the degree of the tree is 3

6. Depth of the tree: The depth of the tree is actually its number of layers . For example, the depth of the tree above is 4, because there are 4 layers (counting from top to bottom)

7. Subtree: In the tree, the tree composed of the child node of a certain node as the root node is the subtree of this node

 Explanation 7: We select node A, then we follow the definition, then the following figure:

 It is a subtree of node A, and a subtree with the child node B of node A as the root node!

When we read the above content, we can see that the name of its node is not unique. A certain node may be the parent node of a child node, or it may also be the parent node of a parent node at the same time. child nodes and so on. When we are familiar with it, we can use it flexibly.


Some basic properties of trees:

1. The total number of branches = 1*N1+2*N2+3*N3...n*N n _{}; 1, 2, 3, 4....n are actually nodes degree, and N1, N2....Nn are the number of nodes with degrees 1, 2...n respectively. For example, a tree has multiple nodes, and some nodes have degree 1 , some nodes have a degree of 2, then the total number of branches is equal to the number of nodes with degree 1 times the number of nodes with degree 1 plus the number of nodes with degree 2 times the number of nodes with degree 2.

2. Summary points = N1+N2+N3.....+Nn; among them, N1, N2..... are the same as the first article, and the degrees of the nodes are 1, 2... For the number of corresponding nodes, you can refer to the example of property 1.

3. The number of nodes in the tree is equal to the sum of the node degrees of all nodes plus 1.

4. In a tree with degree m, there is at most one node in the i-th layer (i>=1)m^{i-1}

Example: For example, if the degree of a tree is 3, what is the maximum number of nodes in the third layer?

We first know that the degree is 3 through the topic, then we know that each node of this tree has at most 3 child nodes, then we can know the maximum number of nodes on the third layer of this tree by drawing:

 From the above figure, we know that the maximum number of nodes in the third layer of the tree is 9 nodes, so we can calculate it through property 4:

3^{3-1}=9, which is the same as what we drew! ! !


Binary tree concepts, properties and three traversal methods:

Definition of binary tree : Binary tree is also a tree structure, each node of binary tree has at most 2 child nodes (that is, the degree of each node <= 2)

 Notice:

1. The subtrees of the binary tree are divided into left and right, and the order cannot be reversed

2. When the number of nodes in the binary tree is 0, it is called an empty binary tree

3. When the number of nodes is greater than or equal to 1, the binary tree is composed of the root node and the disjoint left subtree and right subtree. The left subtree and the right subtree are respectively a binary tree

Let's see what a binary tree looks like:

 From the above figure, it can be analyzed that the child nodes of each node are less than or equal to 2, and the left and right subtrees of each node are divided into left and right, and the order cannot be reversed ! ! !


 Extension:

1. Full binary tree: The popular understanding is that each layer has reached the maximum number of nodes, and the leaf nodes are only divided into the bottom layer

As shown in the picture:

2. Complete binary tree: A binary tree with a depth of K is part (or all) of a certain binary tree with a depth of K , then the binary tree is a complete binary tree.

Figure 1:

 figure 2:

 Properties of binary trees:

1. The number of leaf nodes of a non-empty binary tree = the number of nodes with degree 2 + 1

2. The Kth layer of a non-empty binary tree has at most 2^{k-1}one node

3. The number of nodes of a non-empty binary tree with a depth of K = 2^{K}-1

4. The depth of a complete binary tree with N nodes is ( log_{2}N)+1

The editor does not count the specific verification. If you want to verify, you can draw a picture and think about it to understand! ! !


Binary tree storage method

Sequential storage: Use a group of continuous storage units to store the node elements of the binary tree. The general storage method is from left to right and from top to bottom.

 Chained storage: The chained storage structure of the binary tree uses a linked list to store a binary tree, and each node of the binary tree is stored with a link point of the linked list

 For example, the chain storage method in the following figure:


Next, let's talk about our key points:

Three traversals of a binary tree:

1. Preorder traversal: first traverse the root node, then traverse the left subtree, and finally traverse the right subtree

2. In-order traversal: first traverse the left subtree, then traverse the root node, and finally traverse the right subtree

3. Post-order traversal: first traverse the left subtree, then traverse the right subtree, and finally traverse the root node

 

 Preorder traversal : first traverse the root node A, then look at the left subtree, we find the root node B of the left subtree, then look at the left subtree of B, and find that B has no left subtree, then we look at the right child Tree, traverse the root node D of the right subtree of B, and then traverse G, and find that subtree D has no right subtree. At this time, the left subtree of A has been traversed, and then the right subtree C. Similarly, after we traverse Results for CEHIFJK

The final result is ABDGCEHIJK

In-order traversal : First, we look at the left subtree B of node A, and find that node B has no left subtree. At this time, we traverse B, and then look at the right subtree D, and find that node D has a left subtree. At this time Look at G, then see that G has no left subtree, then traverse G, then see that G has no right subtree, and then traverse the root node D of the subtree. At this time, the left subtree of A is traversed, and we traverse the root node A , and then look at the right subtree C of A, similarly we get HEICJFK

The final result is: BGDAHEICJFK

Post-order traversal : First, we start with the left subtree B of A. We find that B has no left subtree, and then look at the right subtree D of B. We find that D has a left subtree G, but G has no left and right subtrees, so G is traversed. Then see that D has no right subtree, so traverse D, and then traverse the root node B of the subtree. Similarly, we look at the right subtree C of A. Similarly, we get HIEJKFCA in turn

The final result is: GDBHIEJKFCA


 I have seen this, everyone remember Sanlian

Guess you like

Origin blog.csdn.net/m0_73968621/article/details/129344544