I learned some knowledge about trees and binary trees when I was learning C ++. In fact, I learned a little fuzzy at that time. This time I had this opportunity. So I would like to summarize the tree and binary tree here. The context of the article is as follows:
Basic concepts of trees
Before talking about anything else, let's talk about some concepts and definitions of the tree.
The degree of node is: the number of lower nodes of the node;
Tree degree: the degree of the maximum node;
Leaf nodes: nodes without subnodes;
Branch node: All except leaf nodes are branch nodes;
Internal node: Except the root node and the leaf node;
Parent node, child node, and sibling node are relative concepts and cannot exist independently. It can be said that node 5 is a subnode of Node 2, and node 2 and node 3 is a sibling node;
Tree traversal
View All nodes in the tree.
Based on the order in which the parent node, left child node, and right child node are traversed, they can be divided into pre-order traversal, hierarchical traversal, and post-order traversal. The common feature of traversal is that all traversal operations are performed recursively. The name of the traversal can be known Based on the access sequence of the parent node. For example, the preorder traversal is the first to traverse the parent node.
For example, there are three traversal methods:
Forward traversal: traverses all nodes of the tree in the order of the first parent node and the second child node (from left to right. The first parent node of the tree is the root node, that is, node 1; then the child node 2 of 1; because there are nodes under Node 2, follow the recursive method, we should continue to traverse subnode 5 of Node 2; node 5 without subnodes, and traverse nodes from left to right: 6 and 7 in sequence; node 3 after the subnode traversal of Node 2 is completed; 3. node 4 is traversed from left to right because there are no subnodes. node 4 is directed to node 8. node 9 and node 10 are traversed. The traversal ends, the final result is 1, 2, 5, 6, 7, 3, 4, 8, 9, and 10.
Post-order traversal: All nodes in the tree are traversed in the order of first child node, last parent node, and left to right. Continue to use, the leftmost child node of the tree is 5, so starting from 5, it is 5, 6, and 7 in sequence, and then their parent node 2; according to the principle of left-to-right, the sibling nodes of Node 2 have 3 and 4, but because 4 has child nodes 8 and 8 have child nodes 9
And 10. Therefore, traverse from 9 and 10 first. The result of the post-order traversal is 5, 6, 7, 2, 3, 9, 10, 8, 4, and 1.
Hierarchical traversal: This Traversal method is very simple and best suited to the daily logic: traverse nodes from top to bottom, from left to right, so the result is: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Conversion of trees and Binary Trees
The conversion rule is simple: the leftmost subtree of the tree is converted to the left subtree of the Binary Tree, and other sibling nodes are the child nodes of the Left subtree. For example:
2 is the leftmost subnode of the original tree. In the binary tree, it is the subnode of the root node 1. 3, 4, and 2 are the sibling nodes. In the binary tree, it is converted to a subnode of 2, from top to bottom, step-by-step conversion:
Adjust the node location:
Have you ever wondered why conversion is required? I personally think that although the tree is closer to the actual storage, the actual operation is complicated, the binary tree is closer to the computer's thinking, and the binary tree has more rules that are easier to develop and use.
Binary Tree
Binary Tree Concept
As the name suggests, a binary tree has a maximum of two forks on each branch.
Binary Tree Features
1. layer I (the tree starts from layer 0) has a maximum of 2 I-1 nodes.
2. A tree with a depth of I can have up to 2 I-1 nodes
3. If the number of leaf nodes is N0 and the node tree with the degree of 2 is N2, N0 = n2 + 1
4. The depth of the Complete Binary Tree with N nodes is x + 1 (X is the maximum integer not greater than log2n)
5. The forward and central traversal of a binary tree can uniquely determine the binary tree.
Binary tree traversal
The traversal of a binary tree is similar to that of a tree, but a middle-order traversal is added. Example
In-order traversal: traverse the tree from left to right based on the left subnode-root node-Right subnode
Full Binary Tree
A k-depth binary tree with 2k-1 nodes is called a full binary tree.
Full Binary Tree
A binary tree with a depth of K and N nodes can correspond to a full binary tree with a depth of K from 1 to n.
Search for Binary Trees
If the root node is not empty, the left child node <parent node <right node.
Note that the binary tree does not have nodes with the same value. When you insert a knot, select the Insert Location Based on the Size value. It can also be seen from its name that such sorting is easier to search for or compare a value based on the characteristics of the tree, a bit similar to the Binary Search: first compare the parent node, the result shows whether the left or right subtree is to be compared in the next step.
Static search: searches for nodes whose key value is K in the tree structure.
1. If the binary tree is empty, the search fails and the search ends.
2. If the root node value is K, the search is successful and the search ends.
3. If K is smaller than the root node value, search along the left subtree. If K is greater than the root node value, search along the right subtree.
Dynamic search: the search is used for insertion and deletion. Two pointers are obtained through dynamic search: one pointing to the node with the key value and the other pointing to the parent node of the knot.
Insert node: use dynamic search to determine the location of the new node in the following situations:
• If a node with the same key value already exists, it is no longer inserted
• If the binary tree is an empty binary tree, the new node is used as the search binary tree.
• Compare the value of the node to be inserted with the value of the inserted parent node, and determine whether it is the Left or Right child node of the parent node.
Delete a node
When you delete a node from a binary tree, consider the following three situations:
① If the P node to be deleted is a leaf node, the node will be deleted directly;
② If the node P to be deleted has only one subnode, connect the subnode directly with the parent node of the node to be deleted, and then delete the node P;
③ If the node P to be deleted has two subnodes, search for the node s with the highest key value in the middle order on the left subtree, use the value of node s to replace the value of node P, and then delete node S. node s must belong to one of the above ① and ② cases.
Optimal Binary Tree
First, let's take a look at these concepts:
Path Length: the path length from the root node to the target node
Weight: the value of the node.
Length of the weighted path: the length of the path multiplied by its own value
The cost of the tree: The sum of all node values and the path product of the tree itself
Harman tree: the tree with the lowest cost
For example, convert the following tree to the optimal binary tree:
Get
Let's not talk about how to convert it. Let's first look at the features of this tree: The closer the Regan node with a large weight is, the smaller the weight is, the farther away from the root node, in this way, the sum of the values multiplied by the path can be minimized. I personally think that the principle of the conversion step is the smaller the distance from the following points.
According to the user tree, we can introduce the user code. We will not introduce it here.
Clue Binary Tree
Why is there a clue Binary Tree, because a common binary tree has multiple null pointers: It has n nodes, but n-1 pointers are used, and n + 1 pointers are null. The clue binary tree is generated to make full use of these null pointers.
In the binary tree of a clue, the expression of a node is:
Lbit |
Lchild |
Data |
Rchild |
Rbit |
The leftmost and rightmost signs are added to point to the left subnode when the lbit is 0. When the lbit is 1, it points to the front node, and rbit works the same.
Balanced Binary Tree
The premise of a balanced binary tree is to search for a binary tree. It is to ensure that the height of the binary tree is log2n, so as to ensure that the average time for inserting, deleting, and searching a binary tree is log2n. The absolute value of the height difference between the left and right subnodes of any node of the balanced binary tree cannot exceed 1.
The differences and connections between these trees are highlighted here, and complex operations on the trees are being learned.