1. Why do we propose a red/black tree?
The time complexity of searching, inserting, and deleting a binary search tree is O (h), where H is the height of the tree. Assume that the number of nodes in the binary search tree is N. If the binary search tree is balanced, H = O (log n). If the binary search tree is seriously unbalanced, if the tree height H is greater than O (log n), the time complexity of the binary search tree search, insertion, and deletion operations is relatively high.
The balanced search tree is the data structure proposed to solve the problem that the time complexity of searching, inserting, and deleting operations is relatively high when the height of the Binary Search Tree is H.
So far, I have come into contact with the following balanced search trees: AVL Tree, B tree, and red/black tree. (There are many other balanced search trees)
1) AVL Tree: The self-balancing Binary Search Tree first invented. The maximum height difference between the two son trees at any node in aVL is 1.
2) B tree: A Balanced Multi-Cross Tree.
3) Red/black tree: it is a symmetric binary tree.
2. What is a red/black tree?
In short, the red-black tree is a binary search tree with an O (log n) height.
Each node in the red/black tree has five fields: Key (node value), color (node color), P (pointing to the parent), and left (pointing to the left child), right (pointing to the right child ).
The red/black tree has five properties:
Property 1. nodes are red or black. Property 2. The root node is black. Nature 3. Each leaf node (nil node, empty node) is black. (Note: The NIL node is considered a leaf node. Generally, the parent node of the root node is also a nil node, indicating the outer node relative to the inner node of the red/black tree. the two subnodes of each Red node are black. (There cannot be two consecutive red nodes in all paths from each leaf to the root.) 5. All paths from any node to each leaf contain the same number of black nodes.
Red/black tree (study notes)