Thinking logic of computer programs (42), thinking 42
Section 40 introduces HashMap and section 41 introduces HashSet. Their common implementation mechanism is hash table. A common restriction is that there is no order. As we mentioned, they all have a corresponding class TreeMap and TreeSet that can maintain order. The basic implementation of these two classes is the sorting binary tree. In order to better understand TreeMap/TreeSet, this section describes some basic concepts and Algorithms for sorting Binary Trees.
Basic Concepts
First, the concept of a tree. In reality, a tree grows from bottom to bottom and forks. in computer programs, in general, the tree grows from top to bottom, it also forks and has a root node. Each node can have one or more child nodes. nodes without child nodes are generally called Leaf nodes.
A binary tree is a tree, but each node has a maximum of two child nodes, one left and one right, the left is called the left child, and the right is called the right child. Let's look at two examples, as shown below:
Both trees are binary trees. The root node on the left is 5. Except for the leaf node, each node has two child nodes, and the root node on the right is 7. Some nodes have two child nodes, there is only one.
The tree has a concept of height or depth, which is the maximum number of nodes passing through the root node to the leaf node. The height of the left Tree is 3, and the right is 5.
A binary tree is also a binary tree, but it does not have repeated elements and is an ordered binary tree. In what order? For each node:
- If left subtree is not empty, all nodes on the left subtree are smaller than this node.
- If the right subtree is not empty, all nodes on the right subtree are greater than the node.
Both of the preceding Binary Trees are sorted Binary Trees. For example, for the tree on the left, the root node is 5, the left side is less than 5, and the right side is greater than 5. Look at the tree on the right. The root node is 7, the left is less than 7, and the right is greater than 7. In the left subtree with 3 as the root, the value of the right subtree is greater than 3.
What are the advantages of sorting Binary Trees? How do I perform basic operations in the tree, such as searching, traversing, inserting, and deleting? Let's take a look at the basic algorithms.
Basic Algorithms
Search
Sorting a binary tree has a good advantage. It is convenient and efficient to search for an element. The basic steps are as follows:
This step is similar to binary search or semi-query in an array. If the binary tree is balanced, similar to the left-side binary tree, in this case, the comparison scope can be reduced by half and the efficiency is very high.
In addition, in the binary tree sorting, you can easily find the minimum and minimum values, that is, the leftmost node. You can find the left child from the root node. The maximum value is the rightmost node, find the right child from the root node.
Traversal
Binary Tree sorting can also be easily traversed in order. recursive algorithms can be used to traverse in order:
For example, traverse and access the following Binary Tree:
Start from the root node, but first access the left subtree of the root node to the leftmost node, so the first access is 1, 1 without the right subtree, return to the previous layer, access 3, then access the right subtree of 3, 4 without the left subtree, so access 4, then the right subtree of 4 6, and so on, the access order is ordered: 1 3 4 6 7 8 9.
Instead of recursion, you can also implement sequential traversal. The first node is the leftmost node, starting from the first node and finding the successor node in sequence. Given a node, the algorithm for finding its successor node is:
- If the node has the right child, it is the smallest node in the right subtree.
- If the node does not have the right child, the node is a parent node or an ancestor node, which is located from the current node. If it is the right child of Father's Day, the node continues to be located, the parent node is left blank until it is not the right child or the parent node is empty. The Father's Day of the first non-right child node is the successor node. If such an ancestor node cannot be found, the successor node is empty and the traversal ends.
The text description is abstract. Let's take a look at the graph. For example, the successor of each node is shown in the Green Arrow:
For each node, Let's explain in detail against the algorithm:
- The first node 1 has no right child. It is not the right child of the parent node, so its successor node is parent node 3.
- 3. There are right children, and the smallest of the right subtree is 4, so the successor node of 3 is 4.
- 4. There are right children, and there is only one node 6 in the right subtree, so the successor node of 4 is 6.
- 6. If there is no right child, go up to the parent node. It is the right child of parent node 4, 4 is the right child of parent node 3, and 3 is not the right child of parent node 7, therefore, the successor of node 6 is parent node 7 of Node 3.
- 7. There are right children, and the smallest of the right subtree is 8, so the successor node of 7 is 8.
- 8. If there is no right child, go up to the parent node. It is not the right child of parent node 9, so its successor node is parent node 9.
- 9 if there is no right child, go up to the parent node. It is the right child of parent node 7, and then go up, but 7 is already the root node, the parent node is empty, so the successor is empty.
How to build a binary sorting tree? It can be formed and maintained during element insertion and deletion.
Insert
In a binary sorting tree, the insert element first needs to find the Insert Location, that is, the parent node of the new node. How can this problem be found? Similar to the search element, start from the root node and perform the following steps:
After finding the parent node, you can insert it. If the inserted element is smaller than the parent node, It is inserted as the left child. Otherwise, it is inserted as the right child.
Let's take a look at an example. Insert the 7, 3, 4, 1, 9, 6, and 8 processes in sequence, as shown in:
Delete
It is complicated to delete a node from a binary sorting tree. There are three situations:
Let's take a look at it separately.
If the node is a leaf node, it is very simple. You can simply delete it and modify the child of the parent node to be empty.
If a node has only one child node, replace the node to be deleted with the child node, or directly establish a link between the child node and the parent node. For example, if you delete node 4 in the left-side binary tree, you can directly connect parent node 3 of node 4 to child node 6 of node 4.
If the node has two children, first find the successor of the node (according to the previous successor algorithm, the successor is the smallest node in the right subtree, and this successor must have no left child ), after finding the successor, replace the content to be deleted with the content of the successor, and then delete the successor node. The successor node has no left child, which converts the two children into leaf nodes or only one child.
For example, in a tree on the left, delete a node 3 and 3 with two children. Replace the content of 3 with 4, and then delete node 4.
Balanced sorting Binary Tree
From the preceding description, we can see that the shape of the binary sorting tree is closely related to the order of insertion and deletion. In extreme cases, the binary sorting tree may degrade into a linked list. For example, if the insertion order is: 1 3 4 6 7 8 9, the binary tree is in the following shape:
After the binary tree is degraded to a linked list, the advantages of the binary tree are lost. Even if the binary tree is not degraded to a linked list, the efficiency will be very low if the height of the binary tree is not balanced.
What is the definition of balance? There is a highly balanced definition, that is, the height difference between the left and right subtree of any node is at most one. The sort binary tree that satisfies this balanced definition is also called the AVL Tree, which is derived from its inventor G.M. adelson-Velsky and E. m. landis, in their algorithms, uses one or more rotation operations to re-balance the tree when inserting or deleting nodes.
In the implementation of TreeMap, the AVL tree is not used, but the red and black trees. Similar to the AVL Tree, the red and black trees are also a balanced sorting binary tree, the rotation operation is also used to balance the insertion and deletion of nodes, but it is not highly balanced, but roughly balanced, for any path from the root to the leaf node, the length of no path is more than twice that of other paths. The red/black tree weakens the balance requirement, but reduces the overhead required to maintain the balance. In practical applications, the statistical performance is higher than that of the AVL Tree.
Why is it a red/black tree? Because it colors each node, the color is black or red, and there are some constraints on the node coloring, meeting this constraint can ensure that the tree is roughly balanced.
For AVL and red/black trees, the details of their balance are complicated. We will not introduce them. What we need to know is that they are all sortedbinary trees, by performing a rotation operation with little overhead during insertion and deletion, the tree is highly balanced or roughly balanced, thus ensuring the search efficiency of the tree.
Summary
This section describes the basic concepts and Algorithms for sorting Binary Trees.
The sorting Binary Tree maintains the order of elements and is a highly efficient data structure. The basic storage, deletion, and search efficiency are O (h), and h is the height of the tree, in the case of tree balance, h is log2 (N) and N is the number of nodes. For example, if N is 1024, log2 (N) is 10.
The basic sorting binary tree does not guarantee the tree balance. It may degrade to a linked list. There are many algorithms that maintain the tree balance. AVL is the first tree to ensure a high balance of trees, however, the red-black tree is more widely used in practice. Although it can only ensure a rough balance, it reduces the overhead required to maintain the tree balance, and the overall statistical effect is better.
Like a hash table, a tree is also an important data structure and way of thinking in computer programs. Hash and tree are two basic ways of thinking to quickly operate data. They do not need sequence. Hash is preferred, order is required, and tree is considered. In addition to the container TreeMap/TreeSet, the index structure in the database is also based on the tree (but based on the B tree, rather than the binary tree), and the index is the key to quickly access data in a large amount of data.
It is easier to understand the basic concepts and algorithms of Binary Tree sorting and TreeMap and TreeSet. Let's discuss these two classes in the next two sections.
---------------
For more information, see the latest article. Please pay attention to the Public Account "lauma says programming" (scan the QR code below), from entry to advanced, ma and you explore the essence of Java programming and computer technology. Retain All copyrights with original intent.