Author: little brother Fu

Blog:https://bugstack.cn

Precipitation, sharing, growth, let yourself and others can have a harvest!

## 1、 Preface

`Reasonable 5 years of development, no use of data structure, you are just doing crud!`

Most of the time, most programmers suffer from slow query, low efficiency and a lot of associated SQL. The main reason is that they don’t have a good data structure in their programming. Of course, there is also part of the old business code, or some big data services are not used.

Data structure, algorithm and design pattern are the internal mental cultivation of every programmer in the process of growing up. No matter how brilliant your new skills are, no matter how multithreading is, no matter how powerful you are, you can only show that you are in good health, but good health can’t resist bullets.*Only the body and mind methods are good, and they can maneuver*。

This chapter is a combination of`HashMap`

In JDK1.8, HashMap is used`Bucket array + linked list and red black tree`

So after explaining the core principles and API functions in the previous chapter, originally this chapter wanted to go directly to the red black tree, but if you want to understand the red black tree, you need to understand its context, which is its predecessor`2-3 trees`

?。

## 2、 Interview questions

**Thank you for flying**Some simple knowledge points

- Aircraft, look at your resume to understand the data structure, can you briefly introduce the 2-3 tree?
- What are the characteristics of this tree node, compared with other tree structures you know?
- Look at this diagram. How to insert and delete nodes?

Plane, go back and wait for the news!

## 3、 What is a 2-3 tree

In my daily study and interviews with some of my partners, what I heard was: from the Chinese red black tree in HashMap, from the database index to B + tree, but I didn’t ask a lot about 2-3 trees.

### 1. Why use tree structure

From the most fundamental point of view, the use of tree structure is to improve the overall efficiency; insert, delete, search (index), especially the index operation. Compared with the linked list, the index time complexity of a balanced tree is O (logn), while the index time complexity of an array is O (n).

From the following figure, we can compare the time consumption of the two indexes;

- As can be seen from the figure above, the use of tree structure can effectively reduce the time complexity and improve the efficiency of data indexing.
- In addition, the standard tree structure is binary search tree. In addition, there are AVL tree, red black tree, 2-3 tree and so on

### 2. Degenerate list of binary search tree

In the tree data structure, the first bit is a binary search tree, that is, the abbreviation BST tree. In the process of using data insertion, ideally it is a balanced binary tree, but in fact, it may appear that the binary tree is all one-sided, making the binary tree like a list of data structures. Thus, the time complexity of the tree structure is also reduced`O(logn)`

Upgrade to`O(n)`

, as shown in the figure below;

- The data insertion process of binary search tree is that the inserted node is compared with the current tree node, smaller than on the left and larger than on the right.
- With the different insertion order of data, there will be completely different data structures. It may be a balanced binary tree, or it may degenerate into a linked list tree.
- When the tree structure degenerates into a linked list, the performance of the whole tree index also degenerates into a linked list.

**To sum up, if we want to maintain the tree characteristics and O (logn) index performance after inserting data, we need to adjust the nodes during insertion**

### 3. 2-3 tree to solve the balance problem

*What is the structure of 2-3 tree and how does it solve the problem of balance. Let’s go on with the questions.*

2-3 tree is a very clever structure. On the basis of maintaining the tree structure, it allows two elements in a node to be adjusted when the number of elements is equal to 3. In this way, to ensure the balance of the whole binary search tree.

**If you say that, you may not feel it yet. Take a look at the picture below;**

- The left side is a binary search tree, and the right side is a 2-3 balanced tree. Nodes 4 and 5 are inserted respectively to observe the change of tree structure.
- Binary search tree began to appear offset, node inverted.
- 2-3 tree adjusts the tree structure and maintains balance by storing 2-3 elements in a node.
*The so-called “keep balance” means that the link length is the same from the root node to each bottom point.*

The 2-3 tree can solve the balance problem**So, how is the data stored and adjusted? Let’s start to analyze.**

## 4、 2-3 tree usage

### 1. Definition and characteristics of tree structure

**2-3 tree, pronunciation; 2-3 tree**The characteristics are as follows;

Serial number | describe | Sketch Map |
---|---|---|

1 | 2 -, 1 data node, 2 branches | |

2 | 3 -, 2 data nodes and 3 branches | |

3 | The difference between Trident and bifurcate is that in addition to the nodes on both sides, middleware also has a node. This node is a value between 2 and 4. | |

4 | When inserting data, a temporary node with three elements appears. This will be adjusted to a binary tree. |

**In conclusion, we can conclude some properties of 2-3 trees;**

- All cotyledon nodes of 2-3 tree are in the same layer
- One node can have one or two data. If there are three nodes, the tree structure needs to be adjusted
- When one node and one data, there are two child nodes
- When one node has two data, there are three child nodes, and the middle child node is the value between the two nodes

### 2. Data insertion

Next, we simulate the change process of data degenerated into linked list in binary search tree and inserted into 2-3 tree;`1、2、3、4、5、6、7`

The insertion process is shown below;

**The above is the evolution of 2-3 trees in the whole data insertion process**Next, we will explain the changes in each step;

- α. Insert data 2 into node 1. At this time, in order to maintain balance, no new branches will be generated. Only two nodes will be stored in one node.
- β. Continue to insert data 3. At this time, this node has three data, 1, 2 and 3, which is a temporary area.
- γ. Pull up the three data nodes and intermediate nodes, and adjust them into a tree structure.
- δ. Continue to insert data 4, which will be inserted to the right of node 3 to maintain tree balance.
- ε. Continue to insert data 5, after inserting
`3、4、5`

Sharing one node, when there are three data on a node, it needs to be adjusted. - ζ. Middle node 4 up A kind of After adjustment, node 1 is on the left, node 3 is in the middle and node 5 is on the right.
- η. Continue to insert data 6 and share it with node 5 while keeping the tree balanced.
- θ. Continue to insert data 7. After inserting, node 7 will match the current node
`5 6`

share. This is a temporary storage and needs to be adjusted. After the initial adjustment, 6 nodes were extracted and stored upward, and the`2 4 6`

Sharing a node is a temporary state and needs to be adjusted continuously. - ι. Because the root node has three data
`2、4、6`

, you need to move the intermediate node up,`1、3`

and`5、7`

Then they are divided into two branches`Node 2`

、`Node 6`

Go ahead.

*Greek letters: alpha (alpha), beta (beta), gamma (gamma), δ (delta), ε (epsilon), ζ (truncated tower), η (ETA), θ (Sita), ι (Yota)*

### 3. Data deletion

With the above learning of data insertion, data deletion is actually a reverse process, and deletion mainly includes two situations;

- Delete the 3-node, that is, the node that contains two data elements. You can delete it directly without destroying the tree balance.
- If the 2-node is deleted, the tree balance will be broken. It is necessary to shorten the tree height or merge the elements to restore the tree balance.

Following the example above, we will take the data from`7、6、5、4、3、2、1`

Delete in order and observe the structural changes of 2-3 trees as follows;

- α. Delete node 7, because node 7 has only one data element, delete node 7
`5、6`

But at this time, the balance of 2-3 trees is destroyed, and the tree height needs to be shortened for adjustment. - β. Because after deleting nodes, the whole tree structure is unbalanced, so we need to shorten the tree height and adjust the elements. node
`2、4`

Merge, node`1、3`

Insert on the left and in the middle, respectively. - γ. Delete node 6, which is a 3-node（
*It can be divided into three forks*）After deleting, the tree balance will not be destroyed and will remain unchanged. - δ. If you delete node 5, the tree balance will be broken. You need to lower node 4 and merge node 3.
- ε. Delete node 4, which is still a 3-node, so there is no need to change the tree structure.
- ζ. Delete node 3. At this time, there are only 1 and 2 nodes, which need to be merged.
- η. Delete node 2, and the node is still 3-node, so there is no need to change the tree structure.

**Let’s look at a slightly more complicated 2-3 tree deletion:**

In the above figure, there is a slightly more complicated 2-3 balanced tree. The tree deletion process mainly includes:;

- To delete 4, you need to delete the node
`3、5`

Merge, point to node 2, and keep the tree balanced. - Delete 7, node
`8、9`

Merger. - Delete 14, node
`15`

Move up and restore to a 3-ary tree.

*If it’s difficult to understand deletion sometimes, you can imagine what effect the node to be deleted will have when it is inserted.*

### 4. Data index

Compared with insertion and deletion, the indexing process is relatively simple, and there is no need to adjust the data results. The basic principle is the basic principle;

- Less than the current node value, search on the left
- Greater than the current node value, search on the right side
- Until the index value is found, stop.

**The first layer is looking for:**

**The second layer is to search for:**

**Third search:**

## 5、 Summary

- To sum up, the core content of 2-3 tree is explained. Through the study of this chapter, we can understand what kind of data structure 2-3 tree is, how to insert data, delete data and index data. At the same time, we should know that this is a balanced tree structure, including 2-fork and 3-fork nodes, and the number structure is adjusted with the addition and deletion of data.
- 2-3 tree is the evolution precursor of red black tree. Through this chapter, it is easy to learn the related knowledge of red black tree, and add data to the red black tree for rendering and rotation to maintain the balance of the tree.
**Red and black trees close to balance** - The knowledge of data structure may be more brain burning, because it is necessary to think about the model structure and the process of change, so it will be difficult. But this brain burning process is also very helpful for learning, which can quickly build knowledge bulge. When the breakthrough does not understand to understand, there can be a lot of harvest.

## 6、 Series recommendation

- What do interviewers ask me
- HashMap core knowledge, disturbance function, load factor, capacity expansion list splitting, deep learning
- HashMap data insertion, search, delete, traverse, source code analysis
- Re learn java design pattern, practice internal mental skill, 22 real business scenarios of Internet
- DDD domain driven design practice, domain level decision rule tree service design