This series of articles mainly introduces the knowledge of commonly used algorithms and data structures, records the contents of "Algorithms i/ii" course, adopts "algorithm (4th edition)" This Red Cookbook as a learning material, language is java. I don't have to say much about the fame of this book. Watercress Rating 9.4, I myself also think is an excellent learning algorithm books.
With this series of articles, you can deepen your understanding of data structures and basic algorithms (individuals think more clearly than schools) and deepen their understanding of Java.
2-3 Tree Introduction
We talked about binary search tree has been very close to our goal, in many cases, performance is very good, but only in the deletion of not, once deleted operation will lose balance, then, this time, we say how to do a balance find tree, first introduce 2-3 find tree
The 2-3 tree is a special two-fork tree that allows 2 keys to appear in a node.
2 nodes: 1 key,2 children
3 nodes, 2 key,3 children
It has several features:
Conforms to the order of the sequence traversal: The left child is less than the left key, the middle child is between key, the right child is greater than the right key
Absolute balance: As long as the path from the root node to all the null nodes (we'll talk about how to maintain it later)
2-3 Tree Search
According to its characteristics, we can easily know how to find, and the two-fork tree to find almost, just a node more.
If the child is smaller than the left: go
If less than right child: go mid
If the child is greater than right: go
If equal to key, return value
If NULL, returns null
See Animation:
2-3 insertion of the tree
There are several cases of insertion:
- If the inserted node is a 2-node: Directly turn 2 nodes into 3 nodes
See Animation:
Constructing animations
Having talked so much, let's look at how to construct a 2-3 tree.
Implementation scenarios
Here first sells a xiaoguanzi, here does not write the code realization, everybody first understood, later will use the red black tree The plan realizes 2-3 trees
Common algorithms and data structures balance find tree (1)--2-3 find tree (animated)