A binary tree means that any node can have a maximum of two child nodes in a tree.
The binary search tree provides element insertion and access to log (N), and its node rotation rule is that any node's key value must be greater than the key value of each node in its left child node tree, and less than the key value of each node in its right subtree. Therefore, from the tree node to the left to the end, that is, the smallest element, from the root node to the right to the end, but the largest element.
However, because of the irregular insertion value, the binary search tree may lose balance, resulting in low search efficiency. The solution is to try to make the tree shape around the "balance", for what is "balance", there is no absolute definition, the general meaning is "No one node too deep." Common balanced binary trees have avl-tree, Rb-tree, Aa-tree, which are more complex than the general binary tree above, and do some extra work to maintain the tree balance when inserting nodes and deleting nodes, but they can avoid extremely unbalanced situations that are extremely difficult to cope with, And because they always maintain some degree of balance, the average access time of the elements is relatively small.
The search tree implemented by SGI STL is Rb-tree, which adds the following conditions that must be met on the basis of a generic binary tree:
1. Each node is either red or black;
2. The root node is black;
3. If the node is red, its child nodes must be black, if the node is black, then arbitrary;
4. Any path from either node to the end of the tree (for example, from the root node to any leaf node) must contain the same number of black nodes.
According to rule 4, the new node must be red; According to Rule 3, the parent node of the new node must be black. The node color and rotation tree must be adjusted when the new node reaches its insertion point according to a generic binary tree search rule, but fails to meet the above criteria. Note After adjustment, the leaf node may be black.
For different situations, adjustment and rotation operations can be divided into the following four categories, see figure (from Houtie "STL Source Analysis"):
Usage of Rb-tree:
The Begin () function gets the leftmost (smallest) node, and end () Gets the root node (because of the technique of using an implementation, it is not really a root node);
Empty (), size (), max_size () The meaning according to the name;
Insert_unique (const value_type& x) inserts x into the rb-tree, keeping the node value unique;
Insert_equal (const value_type& x) inserts x into Rb-tree, allowing the node value to be duplicated.
Rb_tree<int, int, identity<int>, less<int>> Itree; Cout<<itree.size () <<endl; 0 Itree.insert_unique (10); __rb_tree_rebalance Itree.insert_unique (7); __rb_tree_rebalance Itree.insert_unique (8); __rb_tree_rebalance//__rb_tree_rotate_left//__rb_tree_rotate_right Itree.insert_unique (15); __rb_tree_rebalance Itree.insert_unique (5); __rb_tree_rebalance Itree.insert_unique (6); __rb_tree_rebalance//__rb_tree_rotate_left//__rb_tree_rotate_right Itree.insert_unique (11); __rb_tree_rebalance//__rb_tree_rotate_right//__rb_tree_rotate_left Itree.insert_unique (13); __rb_tree_rebalance Itree.insert_unique (12); __rb_tree_rebalance//__rb_tree_rotate_right cout<<itree.size () <<endl;
9 rb_tree<int, int, identity<int>, less<int>>::iterator it; for (it = Itree.begin (); It! = Itree.end (); it++) cout<<*it<< "; 5 6 7 8 Ten it = Itree.find (8);
Itree.insert_unique (15);
Itree.insert_unique (5);
Itree.insert_unique (6);