[Data Structure] Binary Tree - Theory

1. The concept of tree

insert image description here

When it comes to trees, we have to mention: linear structure and nonlinear structure,

Linear structure: the relationship between data and data is unique! ( One-to-one )
example: sequential list, linked list, stack, column, array, etc. are all linear structures.
Non-linear structure: data-to-data relationships are not unique! ( One-to-many or many-to-one )
Example: tree structure.

Looking at the picture above, we can roughly draw a picture ( drawing backwards ):
insert image description here
It can be concluded that there is at least one node with a branch node , and the node is not the only corresponding relationship , but a one-to-many relationship. The numbers are thus non-linear in structure .
Question: Can the node of the tree be 0 ?
Baidu's answer: The nodes of the tree are greater than or equal to 0 !
So we can draw: the node of the tree can be 0 ! n=0, it is an empty tree
But: a tree with a node of 0 is not necessarily a tree !
Therefore, we can draw two concepts of the tree:
1. The node is greater than or equal to 0
2. Satisfies the nonlinear structure

Subtree:

The tree can be divided into:
1. Root node
2. Subtree/leaf node

insert image description here

We then analyze: the tree is bifurcated (from top to bottom), that is to say, the tree is hierarchical , and the nodes between layers are not related to each other ! That is to say, the downward analysis of the tree is one-to-many , and it is branched from the only node above .
Example:
insert image description here
Analysis:
The first tree: the second layer (counting from top to bottom) There is a correlation between C and D , so it is not a tree.
The second tree: There are two upper nodes (B and C) of the nodes of the third layer E, presenting many-to-one (bottom to top), so it is not a tree.
The third tree: The third layer G node looks from bottom to top (there are two nodes A and D) showing a many-to- one structure, so it is not a tree.
Summary:
The definition of the tree satisfies:
1. The number of nodes is greater than or equal to 0.
2. Satisfies the nonlinear structure
. 3. The subtrees are independent of each other and cannot cross.
4. The tree can be recursive:
1. Root node
2. Subtree (only one node is also considered a subtree)

Explanation: Only one node can no longer be divided , that is, the recursion stops.

Tree related concepts:
insert image description here

1. Degree of a node: The number of subtrees contained in a node is called the degree.

Example: A node has a tree rooted at B, C, D, E, F, G, and there are 6 subtrees, so the degree is 6.
Second-level conclusion:
proof:
set the maximum degree of the tree as M , the number of nodes as N , and the number of edges of the tree as N-1 , (two nodes are connected by one edge, and thus N nodes can be deduced by N -1 number of edges connected), there is 1 edge of a node with degree 1, and 2
edges of a node with degree 2, and it can be deduced that there are N nodes with degree N.
Let the node with degree 0 be N0, the node with degree 1 be N1, the node with degree 2 be N2...the node with degree M be Nm, so the total number of edges: N-1=0*N0
+ 1 *N1+2*N2+...+M *Nm .

2. Leaf node or terminal node: A node with a degree of 0 is called a leaf node.

For example: B, C, H, I, K, L, M, N, P, Q nodes have no subtrees, so they are leaf nodes.

3. Non-terminal nodes or branch nodes: nodes whose degree is not 0

For example: A, D, E, F, G, J, these nodes have branches downward, called branch nodes.

4. Parent node or parent node: If a node contains child nodes, this node is called the parent node of its child nodes.
5. Child node or child node: the root node of the subtree contained in a node is called the child node of the node;

For example: node A has sub-nodes downward - B, C, D, E, F, G. Then A is called the parent node of B, C, D, E, F, and G, and then B, C, D, E, F, and G are called the child nodes (child nodes) of A.

6. Brother nodes: nodes with the same parent node are called brother nodes;

A's child nodes are B, C, D, E, F, G. The child nodes of A all have a parent node, so B, C, D, E, F, and G are called sibling nodes.

7. The degree of the tree: In a tree, the degree of the largest node is called the degree of the tree. The largest node here refers to the node with the largest degree in the tree .

The node with the largest degree in this tree is A, so the degree of A is the degree of the tree—6

8. The hierarchy of nodes: starting from the definition of the root, the root is the first layer, the child nodes of the root are the second layer, and so on.

For example: the layer number of A node is 1, the layer number of B, C, D, E, F, G nodes is 2, and the layer number of H, I, J, K, L, M, N nodes is
3 , the layers of P and Q are 4.

9. The height or depth of the tree: the maximum level of nodes in the tree

The height of the tree is the number of layers counted from top to bottom. This tree is divided into 4 layers, and the height is 4.

10. Cousin nodes: Nodes whose parents are on the same layer are cousins, where the parents are different parent nodes.

For example, the H and I nodes of the third layer do not have a parent node, so they are cousins ​​of each other.

11. Ancestors of a node: all nodes on the branch from the root to the node

For example: Q node, the node on the path to the Q node, the three nodes A, E, and j are called the ancestors of the Q node.

12. Descendants: Any node in the subtree rooted at a node is called a descendant of the node.

For example: A is the root node of this tree, all nodes are developed from A node, so A is the ancestor of all nodes, conversely, all nodes below A are called descendants of A node.

13. Forest: A collection of m (m>0) disjoint trees is called a forest.

insert image description here
Such two disjoint trees are called a forest.
A tree can become a forest by removing the root node , and the upper tree (the big tree at the top) can become a forest by removing the A node.

2. Representation method of tree

How to represent a tree?
Due to the one-to-many relationship of the nonlinear structure, it is difficult to represent a tree. Due to the existence of the boss, there are the following three representation forms.

1. Parent representation

Since each node has only one parent , we can represent a tree by parents. The specific method is implemented in the form of an array .
insert image description here
Starting from the position of the root node with subscript 0 (the subscript of the first element of the array is 0), it increases from left to right, and shows an increasing trend from top to bottom.
insert image description here

A data has two properties, the value of the stored data, the subscript position of the parent node - the root node has no parent node, so set the subscript position of the parent node to -1 , so we can store information in the form of a two-dimensional array ( The parent node stores the row index of the element, and the subscript of the element is always 0 ).

2. Child Representation

Since each node may have children , we represent a tree in the form of an array whose elements are linked lists (used to record the children of the node).
insert image description here

3. Child brother notation

Each node may have siblings and child nodes, so we can represent it in the form of a linked list .
insert image description here
This way a parent node can indirectly point to all of its children.

3. Binary tree

1. Concept

1. A binary tree refers to an ordered tree whose node degree is not greater than 2. It is the simplest and most important tree.
Recursive definition:
A binary tree is an empty tree, or a non-empty tree consisting of a root node and two disjoint left and right subtrees called the root; the left subtree and the right subtree Trees are also binary trees, which is very similar to tree recursion.
picture:
insert image description here

Features:
1. A binary tree does not have a node with a degree greater than 2.
2. The subtrees of a binary tree are divided into left and right, and the order cannot be reversed, so the binary tree is an ordered tree.
Explanation: The non-reversible here means: after being reversed, it is not the original The tree is broken, so the tree is divided into left and right, and the tree is ordered.

3. Any binary tree consists of the following components:
insert image description here

2. Special binary tree

full binary tree

Definition: The nodes of each layer are full and no more nodes can be inserted.
picture:
insert image description here

The number of nodes: Let the height/depth be h
T (N) = 2^0 + 2 to the 1st power + 2 to the 2nd power +... 2 to the h-1 power, from the first N items of the geometric sequence And we can get
T(N)=2 to the h power-1

complete binary tree

Definition: Let the height of the complete binary tree be h, then the nodes in the h-1 layer are full , and the nodes in the h layer are at least 1.
picture:
insert image description here

The range of the number of nodes: set the height as
a special case of a complete binary tree: the last layer has only 1 node, or a full binary tree.
There is only 1 node in the last layer: T(N)=2 h minus one power -1 (this is the case when the first h-1 layer is full) + 1 (the last layer is 1) full binary tree: T(N)
= 2 to the h power -1
, so the range of the node: 2 to the h power minus 1 to 2 to the n power minus one, which is a closed interval.

Notice:
insert image description here

The nodes in the last are not arranged in order, which is not called a complete binary tree.

Binary Tree Conclusion

Let the height be: h, and the number of nodes in the binary tree be N
1. The maximum number of nodes in an empty binary tree: 2 to the power of h-1
2. The maximum number of nodes in the deepest layer: 2 to the power of h minus one
3. Full binary tree Number of nodes: 2 to the h power -1
4. The range of the number of nodes in a complete binary tree: 2 to the h power of 2 to 2 to the h power -1
5. The depth of the full binary tree is: log base 2 (N+ 1)
6. The minimum depth of a complete binary tree: logN + 1, the base of the logarithm is 2
7. The child nodes of a node are: 2 M+1, 2 M+2, (M is the subscript of the node)
Substitute in the figure below to verify
insert image description here

Guess you like

Origin blog.csdn.net/Shun_Hua/article/details/129737765