Red black tree

来源:互联网 发布:什么是淘宝直通车图片 编辑:程序博客网 时间:2024/06/05 00:45

Red black tree

Characteristics of red black tree:
(1) Each node is either black or red.
(2) The root node is black.
(3) Each leaf node (NIL) is black.
(4) If a node is red, its child nodes must be black.
(5) All the paths from one node to the descendant node of the node contain the same number of black nodes.

Add

case 1. X’s uncle node (U) is red

red_black_tree_add_1

case2. X’s uncle node (U) is black, and X is a right child (of P).

red_black_tree_add_2

case3. X’s uncle node (U) is black, and X is a left child (of P).

red_black_tree_add_3

case 1 can recursion translate into case 2 or 3.
case 2 can translate into case 3.
case 3 can go to end.

Remove

case1. X’s brother node (B) is red.

red_black_tree_remove_1

case 2. X’s brother node (B) is black, the two child nodes (N) of B are black.

red_black_tree_remove_2

case 3. X’s brother node (B) is black, the left child node of B is red, right child node is black.

red_black_tree_remove_3

case 4. X’s brother node (B) is black, the right child node of B is red.

red_black_tree_remove_4

case 1 can translate into case 2 or 3 or 4.
case 2 can recursion translate into case 3 or 4.
case 3 can translate into case 4.
case 4 can go to end.

Java code in JDK

fixAfterInsertion

private void fixAfterInsertion(Entry<K,V> x) {        x.color = RED;        while (x != null && x != root && x.parent.color == RED) {            if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {                Entry<K,V> y = rightOf(parentOf(parentOf(x)));                if (colorOf(y) == RED) {                    setColor(parentOf(x), BLACK);                    setColor(y, BLACK);                    setColor(parentOf(parentOf(x)), RED);                    x = parentOf(parentOf(x));                } else {                    if (x == rightOf(parentOf(x))) {                        x = parentOf(x);                        rotateLeft(x);                    }                    setColor(parentOf(x), BLACK);                    setColor(parentOf(parentOf(x)), RED);                    rotateRight(parentOf(parentOf(x)));                }            } else {                Entry<K,V> y = leftOf(parentOf(parentOf(x)));                if (colorOf(y) == RED) {                    setColor(parentOf(x), BLACK);                    setColor(y, BLACK);                    setColor(parentOf(parentOf(x)), RED);                    x = parentOf(parentOf(x));                } else {                    if (x == leftOf(parentOf(x))) {                        x = parentOf(x);                        rotateRight(x);                    }                    setColor(parentOf(x), BLACK);                    setColor(parentOf(parentOf(x)), RED);                    rotateLeft(parentOf(parentOf(x)));                }            }        }        root.color = BLACK;    }

fixAfterDeletion

private void fixAfterDeletion(Entry<K,V> x) {        while (x != root && colorOf(x) == BLACK) {            if (x == leftOf(parentOf(x))) {                Entry<K,V> sib = rightOf(parentOf(x));                if (colorOf(sib) == RED) {                    setColor(sib, BLACK);                    setColor(parentOf(x), RED);                    rotateLeft(parentOf(x));                    sib = rightOf(parentOf(x));                }                if (colorOf(leftOf(sib))  == BLACK &&                    colorOf(rightOf(sib)) == BLACK) {                    setColor(sib, RED);                    x = parentOf(x);                } else {                    if (colorOf(rightOf(sib)) == BLACK) {                        setColor(leftOf(sib), BLACK);                        setColor(sib, RED);                        rotateRight(sib);                        sib = rightOf(parentOf(x));                    }                    setColor(sib, colorOf(parentOf(x)));                    setColor(parentOf(x), BLACK);                    setColor(rightOf(sib), BLACK);                    rotateLeft(parentOf(x));                    x = root;                }            } else { // symmetric                Entry<K,V> sib = leftOf(parentOf(x));                if (colorOf(sib) == RED) {                    setColor(sib, BLACK);                    setColor(parentOf(x), RED);                    rotateRight(parentOf(x));                    sib = leftOf(parentOf(x));                }                if (colorOf(rightOf(sib)) == BLACK &&                    colorOf(leftOf(sib)) == BLACK) {                    setColor(sib, RED);                    x = parentOf(x);                } else {                    if (colorOf(leftOf(sib)) == BLACK) {                        setColor(rightOf(sib), BLACK);                        setColor(sib, RED);                        rotateLeft(sib);                        sib = leftOf(parentOf(x));                    }                    setColor(sib, colorOf(parentOf(x)));                    setColor(parentOf(x), BLACK);                    setColor(leftOf(sib), BLACK);                    rotateRight(parentOf(x));                    x = root;                }            }        }        setColor(x, BLACK);    }