Oil ouyo 2022-02-13 08:54:03 阅读数:448
R-B Tree, The full name is Red-Black Tree, also called “ Red and black trees ”, It's a balanced binary tree . Each node of the red black tree has a storage bit representing the color of the node , It can be red (Red) Or black (Black).
Java stay JDK1.8 Red black trees were introduced in , The actual reason is because when HashMap in hash When the function insertion calculation reaches the repeated position and the linked list is too long , It will cause the query to be too slow .
When the list is too long
To solve this problem , And the introduction of red and black trees , Because the query of binary tree is similar to Binary query Fast .
Introduce the red black tree
All inserted points are red by default
Color change
Of the current node Father is red , And the other child node of its grandfather node is also red （ Uncle node ）.
left-handed
At present The parent node is red , Uncle node is black When , And The current node is the right subtree . Take the parent node as the left rotation point
Right hand
At present The parent node is red , Uncle node is black When , And The current node is the left subtree .
Example
Color change
left-handed
Right hand
jdk1.8 Houdang HashMap The linked list at a certain position of exceeds 8 Time , Will be converted from a linked list to a red black tree , Speed up query .
copyright：author[Oil ouyo]，Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130854003284.html