Algorithm Learning Note: red-black tree

Source: Internet
Author: User

I. Characteristics of red and black trees

1. The node can only be red or black.

2. The root node is black.

3. Leaf node (NIL) is black.

4. The child node of the red node must be a black node.

5. The path to the descendant nodes of any node that reaches the node contains the same number of black nodes.

Two, red black Tree basic operation

Third, insert the node

Insert the red-black tree as a normal search tree and apply the insertion node in red. Then there are three things:

1. If the current node is the root node, apply the insertion node to black.

2. If the parent node of the current node is black, no adjustment is required, still a red-black tree.

3. If the parent node of the current node is red, the red node will be blacked out after moving it to the root node, and there are still three cases:

Case 1: If the Uncle node of the current node (the sibling node of the parent node) is red

① to paint the parent node black

② The uncle's node to black.

③ The grandfather node to red

④ The grandfather node to the current node to continue operation

Case 2: If the Uncle node of the current node is black, and the current node is the right child node of its parent node

① to set the parent node as the current node

② the current node to the left of the axis

Case 3: If the Uncle node of the current node is black, and the current node is the left child node of its parent node

① to paint the parent node black

② The grandfather node to red

③ right-handed with grandfather node as axis

Iv. Delete operation

Delete the red and black tree as a normal search tree.

1. If the delete node has no child nodes, delete it directly.

2. If you delete a node with only one child node, its child nodes are substituted for its location after the node is deleted.

3. If the delete node has two child nodes, swap the deleted node with its successor node. Repeat until delete node satisfies ① or ②, delete it.

The red and black trees are adjusted after the deletion is complete.

1. If the deletion node is red, no adjustment is required, still a red-black tree.

2. If the deletion node is black, then we assume that the node in place of the initial position of the deletion node inherits the black of the deleted node, moving the extra black to the root of the tree. There are three kinds of situations:

(1) If the extra black node is red, the node is painted black to restore the red and black tree properties.

(2) If the extra black node is black, and the node is the root, you do not need to adjust, still is red black tree.

(3) If the extra black node is black, but the node is not root, there are four cases

Case 1: The sibling node of the current node is red

① the sibling node of the current node to black

② The parent node of the current node is painted red

③ The parent node of the current node to the left of the axis

Case 2: The sibling node of the current node is black, and the child nodes of the sibling nodes are black

① set the sibling node of the current node to red

② move the excess black of the current node to its parent node

Case 3: The sibling node of the current node is black, and the left child node of the sibling node is red and the right child node is black

① the left child node of the sibling node of the current node is painted black

② the sibling nodes of the current node as red

③ the current node's sibling node as the axis right-handed

Case 4: The sibling node of the current node is black, and the right child node of the sibling node is red and the left child node is red or black

① the sibling node of the current node as the color of its parent node

② The parent node of the current node to black

③ the right child node of the sibling node of the current node to black

④ The parent node of the current node to the left of the axis

⑤ to set the current node as the root node

Algorithm Learning Note: red-black tree

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.