红黑树

Operations on red-black trees take O(logN) time in the worst case.

A red-black tree is a binary tree is a binary search tree with the following coloring properties:

  1. Every node is colored either red or black.
  2. The root is black.
  3. If a node is red, its children must be black.
  4. Every path from a node to a null pointer must contain the same number of black nodes.

A consequence of the coloring rules is that the height of a read-black tree is at most 2log(N+1) .

The new item, as usual, is placed as a leaf in the tree. If we color this item black, then we are certain to violate condition 4. Thus the item must be red. If the parent is black, we are done. But if the parent is already red, then we will violate condition 3.

Bottom-Up Insertion

Let X be the newly added leaf, P be its parent, S be the sibling of the parent(if exists), and G be the grandparent.

1. Supposed S is black.

Only X and P are red in this case; G is black, because otherwise there would be two consecutive red nodes prior to the insertion, in violation of red-black rules.
红黑树_第1张图片
红黑树_第2张图片

2. Supposed S is red.

In this case, when we attempt to insert 79 in the tree in Figure 12.9.,

Top-Down Red-Black Trees

On the way down, when we see a node X that has two red children, we make X red and the two children black. In that case, initially there is one black node on the path from the subtree’s root to C. After the rotation, there must still be only one black node. But in both cases, there are three nodes, (the new root, G and S) o the path to C. Since only one may be black, and since we cannot have consecutive red nodes, it follows that we’d have to color both S and the subtree’s new root red, and G black. That’s great, but what happens if the great-grandparent is also red? In that case, we can percolate this procedure up toward the root as is done for B-trees and binary heaps until we no longer lave two consecutive red nodes, or we reach the root.

Top-Down Deletion

  1. Deletion of a red leaf is, of course, trival.
  2. If a leaf is black, however, the deletion is more complicated because removal of a black node will violate condition 4.

The solution is to ensure during the top down pass the leaf if red.

So if the leaf X that we want to delete is black. Then its parent P is red. Then X and its sibling T are black.

  1. First, suppose X has two black children. Then there are three subcases, which are shown below. If T also has two black chidren, we can flip the colors of X,T and P to maintain the invarient. Otherwise, one of T’s children is red. Depending on wich one it is, we can apply the rotation shown in the second and third cases.

红黑树_第3张图片
2. Otherwise, one of X’s children is red. In this case, we fall through to the next level, obtaining new X,T and P. If we’re lucky, X will land on the red child, and we can continue onward. If not, we know that T will be read, and X and P will be black. We can rotate T and P, making X’s new parent red; X and its grandparent will,of course, be black. At this point, we can go back to the first main case.

你可能感兴趣的:(算法,算法)