Red-black Trees

Source: Internet
Author: User

Red-black Trees?

Red-black trees is one of the many search-tree schemes that is "balanced" in order to guarantee that basic Dynamic-set opera tions take O (LGN) time in the worst case.

Red-black trees? is one of many search tree frameworks. These trees take the self-balance in order to ensure that the basic dynamic set operates in the worst case time at 0 (LGN).

?

?

A red-black tree is a binary tree that satisfies the following Red-black properties:

1. Every node is either red or black.?

2. The root is black.?

3. Every leaf (NIL) is black.?

4. If a node is red and then both it children is black.?

5. For each node, paths from the node to descendant leaves contain the same number of black nodes.?

1. All nodes are not red or black

2. The root is black.

3. The leaves (Nil) are also black

4. A node is red, and the child must be black.

5. For each node, any path from this node to the leaf contains the same amount of black.

?

The Red-black tree property from 2-5 senses is a limitation on black.

?

An example of a red-black tree.

?

As a matter of convenience in dealing with boundary conditions in Red-black

Tree code, we use a single Sentinel to represent NIL

All pointers to NIL is replaced by pointers to the Sentinel T.nil

All pointing to the empty is replaced by pointing Sentinel? The T.nil?

?

?

In the remainder of this chapter, we omit the leaves when we draw red-black trees, as shown

?

We call the number of black nodes in any simple path from, but not including, a node x down to a leaf the black-height of The node, denoted BH (x)?

?

13.2 rotations?

We change the pointer structure through rotation, which are a local operation in a search tree that preserves the binary-se Arch-tree property.

We use rotation to change the pointer structure. It is a local operation that preserves the properties of the search tree.

?

?

13.3 insertion?

We can insert a node into an n-node red-black tree in O (LGN) time.

To does so, we use a slightly modified version of the Tree-insert procedure (Section 12.3) to INSERT node? Into the tree T as if it were a ordinary binary search tree, and then we color Z red.

?

?

?

CASE1:

?

Case 2 and Case 3

?

?

13.4 deletion?

Like the other basic operations in an n-node red-black tree, deletion of a node takes time O (LGN). Deleting a node from a red-black tree? is a bit more complicated than inserting a node.?

First, we need to customize the transplant subroutine? That's tree-delete calls so the it applies to a red-black Tree:

?

?

?

?

?

?

?

Here's the red-black delete tree program? :

?

?

Finally, if node Y was black, we might has introduced one or more violations of the red-black properties, and so we Rb-delete-fixup to restore the Red-black properties. If y was red, the Red-black properties still hold when Y is removed or moved, for the following reasons:

If node y is black, we may introduce one or more violations of the red-black tree nature. If Y is red, the nature of the red and black tree can be guaranteed. The reasons are as follows:

1. No black-heights in the tree has changed.?

Black Depth No change

2. No Red nodes has been made adjacent. Because y takes Z ' s place in the tree, along with Z's color, we cannot have both adjacent red nodes at Y ' s new position in The tree. In addition, if Y is not? ' s right child, then Y ' s original right child x replaces y in the tree. If y is red, then x must are black, and so replacing y by x cannot cause the red nodes to become adjacent.?

No red nodes are adjacent. Because y replaces the z position and gets the z color, z turns out to have a red-black tree attribute, so the replacement is still there later.

In addition, if Y is not the right child of Z, the original position of y is replaced by X. If Y is red, then x must be black, so x is substituted for y? It is not possible to cause two red nodes to be adjacent to each other.

?

3. Since y could not having been the root if it was red, the root remains black.?

If y is red, y is definitely not the root, so the root remains black.

?

?

If node Y is black, three problems may arise, which the call of Rb-delete-fixup would remedy.?

First, if Y had been the root and a red child of Y becomes the new root, we had violated property 2.?

First if y is the root and a red child of Y becomes the new root, then the violation of the root is a black attribute.

?

Second, if both X and X.P is red, then we had violated property 4.?

If x and X.P are red, then we may violate the red node that cannot be adjacent.

?

Third, moving y within the tree causes any simple path that previously contained Y to has one fewer black node. Thus, property 5 are now violated by any ancestor of y in the tree.

Third, if you move y, then any route that originally contains y may be one less black than the other route, so the number of black nodes from any node to the leaf is the same.

?

We can correct the violation of property 5 by saying this node x, now occupying Y ' s original position, have an "extra" Blac K.?

We can correct the fifth property with an extra black by taking the property of x that now occupies Y's original position.

?

That's, if we add 1 to the count of black nodes on any simple path that contains x, then under this interpretation, prope Rty 5 holds.?

That is, when we calculate a simple path that includes an X-node from any one, adding 1 more will keep the attribute 5?

When we remove or move the black node y, we "push" its blackness onto node x.

When we move the black node y, we push her black to the node x.

?

? The problem is and now node X are neither red nor black, thereby violating property 1.?

The problem now is that node X is neither black nor red, violating attribute 1.

?

Instead,node x is either "doubly black" or "red-and-black," and it contributes either 2 or 1, respectively, to the Count O F Black nodes on simple paths containing x.

Node x is double black, or red-black, and it will contribute one or two, respectively, on the simple path of the calculation containing x.

? The color attribute of x would still be either RED (if X was Red-and-black) or black (if x is doubly black).?

The zodiac of X will still be red (if X is red-black) or black (if x is double black).

In other words, the extra black on a node was reflected in X's pointing to the node rather than in the color attribute.

In other words, the extra black of a node reacts to the position of x rather than the color attribute.

?

?

Case1:x ' Sibling w is red?

By converting to any of the case2,3,4?

Case2?:x ' s sibling w is black, and both of W ' s children was black?

That X's brother, and brother's children are black, let X's brother Red, X transferred to x parent node

Case3:x's brother is black, and the brother's right child is red. Converted to CASE4 by conversion

Case4:x's brother is black, and the brother's left child is red. This will solve the problem of the black node.

?

?

?

?

Red-black Trees

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.