Head-first red-black trees

Source: Internet
Author: User

 

1. Binary Search and binary tree: balance between efficiency, space, and flexibility
Complementarity between data and linked lists:
Array: search fast (search with binary forks)-> log2n. Insertion and deletion are inconvenient. -> Overhead for moving other data. To keep data in order, there must be a large number of insert/delete operations.
Linked List: two pointers are attached to each element, pointing to the front node and the back node respectively.
1) breaks the data dependency on spatial relevance. This avoids the overhead of moving data during insertion and deletion.
2) binary search cannot be used.
In order to use binary search in the linked list, we make some changes to the element pointer: no longer pointing to its front and back elements. It points to its binary node element.

Its data structure is defined as follows: Template <class entry>
Struct binary_node
{
Entry data;
Binary_node <entry> * left;
Binary_node <entry> * right;
}

Insertion and deletion result in a binary tree imbalance.

We extend the range of the array to 1-24, and after the deletion operation, only seven elements are left:

Any imbalance, you can useTwo types of RotationOperation to complete: Left rotation, right rotation.
AdjustmentThree nodesRelationship: Sun Tzu node, parent node, and Grandpa node.
ModifyThree pointersValue:Pointer of Grandpa Node(Current root node), pointingParent nodePointer, pointingUncle NodePointer.

Template <class record>
Error_code search_tree <record>: rotate_right (binary_node <record> * & current)
{
Binary_node <record> * Reg = Current-> left;
Current-> left = reg-> right;
Reg-> right = current;
// Point to the current root node correctly
Current = reg;
Return okay;
}

Template <class record>
Rb_code search_tree <record>: rotate_left (binary_node <record> * & current)
{
Binary_node <record> * Reg = Current-> right;
Current-> right = reg-> left;
Reg-> left = current;
// Point to the current root node correctly
Current = reg;
Return okay;
}

Error_code search_tree <record>: double_rotate_left (binary_node <record> * & current)
{
Rotate_right (current );
Rotate_left (current );
Return okay;
}
Template <class record>
Error_code search_tree <record>: double_rotate_right (binary_node <record> * & current)
{
Rotate_left (current );
Rotate_right (current );
Return okay;
}

Red/black tree-Lazy balance
Red/black tree vs balanced binary tree
1) performance has almost never decreased
2) Reduced overhead
Basic Idea: Starting from the root node, layers are red and black. It only rotates when two black layers are met.
Basic operations on the red/black tree:
1) Coloring: flip_color ()
2) rotation: use the search-tree method.
Four States of the red/black tree node: Enum rb_code {okay, red_node, left_red, right_red };

Okey: a valid red/black tree.
Red_node: tree structure balance. The root node color is red.
Left_red: the tree structure is unbalanced, and the left subnode is red.
Right_red: the tree structure is unbalanced, and the right subnode is red.
The leaf node of the red/black tree: it exists in concept but does not physically exist.
Worst case:
Insert operation:
Delete operation:
Application:
In most applications, a node also contains a parent node: // Linux-2.6.17/include/Linux/rbtree. h

Struct rb_node
{
Unsigned long rb_parent_color;
# Define rb_red 0
# Define rb_black 1
Struct rb_node * rb_right;
Struct rb_node * rb_left;
} _ Attribute _ (aligned (sizeof (long ))));
/* The alignment might seem pointless, but allegedly CRIs needs it */

// STL/stl_tree.h

Struct _ rb_tree_node_base
{
Typedef _ rb_tree_color_type _ color_type;
Typedef _ rb_tree_node_base * _ base_ptr;

_ Color_type _ m_color;
_ Base_ptr _ m_parent;
_ Base_ptr _ m_left;
_ Base_ptr _ m_right;
};
Template <class _ value>
Struct _ rb_tree_node: Public _ rb_tree_node_base
{
Typedef _ rb_tree_node <_ value> * _ link_type;
_ Value _ m_value_field;
};
FDFD

Referance

Data_structures and Program Design in CPP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.