------------------------------------------------------ After reading Algorithm
For a long time, I have not had a good understanding of the red and black trees. I have guided me to read the algorithm and the Open Class on Coursera, so I have a good understanding of it. Now I want to write it down and share it with you.
2-3 Search Tree
Although the Binary Search Tree solves most search problems, the performance in the worst case is still poor (~ N) to ensure the balance of the search tree. We introduced 3-nodes (compared with 2-nodes in the binary search tree) and constructed 2-3 search trees accordingly. Its nature is as follows:
Allow 1 or 2 keys per node.
2-nodes: one key, two children.
3-nodes: two keys, three children.
Invalid Ric order. Inorder traversal yields keys in ascending order.
Perfect balance. Every path from root to null link has same length.
For queries of 2-3 trees, the inserted search is relatively simple. For more information, see the following figure.
Image reference from: http://algs4.cs.princeton.edu
Red/Black Binary Search Tree
The basic idea of the red/Black Binary Search Tree is to use the standard binary search tree (composed of 2-nodes) and some additional information (replace 3-node) to represent 2-3 trees.
. 1 replace 3-node
An equivalent definition of the red/black tree:
. Red links are left links
. No node is connected to two red chains at the same time.
The tree is completely black balanced, that is, the number of black links on the path connecting any blank to the root node is the same
. 2 Color Representation
. 3 rotate
.. Rotate the right link of H to the left
.. Right rotate the left link of H
.. Color conversion: it refers to the color conversion of two Red Sub-nodes at a node, and the color of the parent node is converted from black to red.
Balanced Search Tree