content
foreword
I haven't continued to write "Dahua Data Structure" for a while. In the final analysis, it's because of laziness!
Since there are too many knowledge points about the tree, I will summarize it in three parts, and then enter the binary tree link! !
(If you forget the previous content, you can click on the right to go directly to [Dahua Data Structure] Chapter 6 Summary - Tree (Top) )
binary tree
In the last part, it was mentioned at the end that a binary tree can be formed using the child sibling notation , so what is a binary tree? Look at the picture first, then see the meaning
This is a binary tree
This is also a binary tree
Similarly, this is also a binary tree
A characteristic can be found from the figure. The maximum degree of the tree in the binary tree is 2. Let's take a look at the explanation of the binary tree.
Binary tree:
A binary tree is a finite set of n (n ≥ 0) nodes, which is either an empty set (called an empty binary tree),
Or it consists of a root node and two disjoint binary trees called the left and right subtrees of the root node.
In fact, it may be a bit confusing to look at the explanation directly, but we can start from its characteristics to help us understand
Features of binary tree:
- Each node has at most two subtrees, so there is no node with degree greater than 2 in the binary tree.
( Note that there are not only two subtrees, but at most. No subtree or one subtree is possible )
- The left subtree and right subtree are in order, and the order cannot be arbitrarily reversed.
- Even if a node in the tree has only one subtree, it is necessary to distinguish whether it is a left subtree or a right subtree. For example, the following figure shows two different binary trees
If a binary tree consists of three nodes, how many forms does the binary tree have? (It is recommended to think about it for yourself first, or draw it on paper and then look down)
。
。
。
。
。
Just when you have thought about it in your mind, the answer is announced below
There are a total of 5 types. I don't know if you answered correctly. If you answered correctly, continue to read. If you answered incorrectly or missed, you can continue to look at the picture above. It should not be difficult to understand.
Special binary tree:
In data structures, there are always some special cases, and binary trees are no exception
Oblique tree:
As shown in the figure above, a binary tree with only left subtrees at all nodes is called a left-slanted tree; a binary tree with only right subtrees at all nodes is called a right-sloped tree
Left-sloping trees and right-sloping trees are collectively referred to as oblique trees.
Its characteristic is that each layer has only one node, similar to a linear table (a linear table can be understood as a very special form of tree representation)
Full binary tree:
As shown in the figure above, it can be found that each branch node of such a binary tree has left and right subtrees (except leaves), and the leaves are all at the same level. This kind of binary tree is called a full binary tree .
The characteristics of a full binary tree are:
1. Leaves only appear on the bottom layer
2. The degree of non-leaf nodes must be 2
3. In a binary tree of the same depth, the full binary tree has the largest number of nodes and the largest number of leaves
Complete binary tree:
The above are all complete binary trees. You read that right. The third picture is also a complete binary tree. Some people are confused, isn't this a full binary tree?
The difficulty is coming. Among the two types of binary trees, "full" and "complete", the second one is more difficult to understand. Let's take a look at the more official explanation first.
A binary tree with n nodes is numbered in hierarchical order. If the node numbered i (1 ≤ i ≤ n) is in the same position as the node numbered i in the full binary tree of the same depth, then This binary tree is called a complete binary tree
The above explanation is probably dizzy for a lot of people, let me explain it
The complete binary tree is actually based on the full binary tree. Specifically, the complete binary tree is formed by cutting n nodes to the left at the lower right corner of the full binary tree! See the picture to understand
As shown in the figure above, after cutting down 1 node from the lower right corner of the full binary tree to the left , it becomes a complete binary tree.
In the same way, after cutting 2 nodes from the lower right corner of the full binary tree to the left in turn , it becomes a complete binary tree.
After cutting down 0 nodes, it is also a complete binary tree, but it is also a full binary tree at this time.
(Note: From the above figure, we can know that a full binary tree must be a complete binary tree, but a complete binary tree is not necessarily a full binary tree)
The following binary trees are not complete binary trees!
In the above figure, the nodes are not cut off in sequence
The same is true in the above figure. If you want to cut off the nodes of the third layer (the third layer from top to bottom), then you must cut down the nodes of the fourth layer to form a full binary tree before you can cut the third layer. Floor
That is, as shown in the following figure, it is a complete binary tree
Recall the complete binary tree again.
The complete binary tree is actually based on the full binary tree. Specifically, the complete binary tree is formed by cutting n nodes to the left at the lower right corner of the full binary tree!
Remember a few key points: full binary tree , start in the lower right corner , turn left in turn
Features of a complete binary tree:
1. Leaf nodes can only appear in the bottom two layers
2. The lowermost leaves must be concentrated in the left continuous position
3. The penultimate layer, if there are leaf nodes, they must be in a continuous position on the right
4. If the node degree is 1, the node has only the left child, that is, there is no situation where there is only a right subtree
5. A binary tree with the same number of nodes, the depth of a complete binary tree is the smallest
Properties of a binary tree:
1. There are at most 2^(i - 1) nodes (i ≥ 1) on the ith level of the binary tree
2. A binary tree of depth k has at most 2^k - 1 nodes (k ≥ 1)
3. For any binary tree T, if the number of terminal nodes is n0 and the number of nodes with degree 2 is n2, then n0 = n2 + 1
4. The depth of a complete binary tree with n nodes is ⌊log2n⌋ + 1 (⌊x⌋ represents the largest integer not greater than x)
5. If the nodes of a complete binary tree with n nodes (with a depth of ⌊log2n⌋ + 1) are numbered in level order (from level 1 to level ⌊log2n⌋ + 1, each level from left to right), for any node i (1 ≤ i ≤ n), we have:
- If i = 1, node i is the root of the binary tree and has no parents; if i > 1, its parents are nodes ⌊i / 2⌋
- If 2i > n, node i has no left child (node i is a leaf node); otherwise, its left child is node 2i
- If 2i + 1 > n, node i has no right child; otherwise its right child is node 2i + 1
The properties of the first 4 points are actually not difficult to understand. It is suggested that you can prove the above properties by drawing pictures and deepen your memory.
The nature of point 5 may be difficult to understand. Let's take the following figure as an example to illustrate. The figure below is a complete binary tree with a depth of 4 and a total number of 10 nodes.
5.1 If i = 1, the node i is the root of the binary tree and has no parents; if i > 1, its parents are the nodes ⌊i / 2⌋
This is obvious for the first clause of property 5, since i = 1 is the root node.
When i > 1, such as node 5, then its parent is ⌊i / 2⌋, that is, ⌊5 / 2⌋ = 2,
Node 9, its parent is ⌊i / 2⌋, which is ⌊9 / 2⌋ = 4
5.2 If 2i > n, node i has no left child (node i is a leaf node); otherwise, its left child is node 2i
For the second property of property 5, such as node 7, since 2 × 7 = 14 exceeds the total number of nodes 10, node 7 has no left child
Similarly, for node 5, since 2 × 5 = 10 equals the total number of nodes 10, its left child is node 2i, which is 2 × 5 = 10 nodes
5.3 If 2i + 1 > n, node i has no right child; otherwise its right child is node 2i + 1
For the third item of property 5, such as node 5, since 2 × 5 + 1 = 11, which is greater than the total number of nodes 10, it has no right child
Similarly, node 3, because 2 × 3 + 1 = 7, is less than the total number of nodes 10, so its right child is node 7
Summarize
This is the middle of the sixth chapter tree of "Dahua Data Structure". If there is anything you don't understand, please leave a comment below.
A person has a goal, and if he does not implement it because of laziness, the goal will soon be abandoned.
Just like writing a blog, if you keep blogging a few times a month, and suddenly you don’t keep writing because of laziness, it will be difficult to keep going.
So to avoid this, I have to act now! Don't make so many excuses for yourself! Stick to blogging, selfish and altruistic!