Red-Black Tree
2015-05-21 07:27
267 查看
http://pages.cs.wisc.edu/~skrentny/cs367-common/readings/Red-Black-Trees/
http://en.wikipedia.org/wiki/Red–black_tree
1. Every node is either red or black.
2. The root is black. (This rule is sometimes omitted. Since the root can always be changed from red to black.)
3. There are no 2 adjacent red nodes.
4. Every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.
Lemma
A red-black tree with n internal nodes has the height at most 2log(n+1)
Proof of the balance
A simple example to understand balancing is, a chain of 3 nodes is not possible in Red Black Tree
Operation
insert / delete / lookup / print —— in O(log N) worst-case time
ps : Rotate - O(1)
left_rotate & right_rotate
1. insert - O(log N)
[step1] use the BST insert algorithm to add K to the tree — O(log N)
[step2] color the node containing K red
[step3] restore red-black tree properties (if necessary) — worst case, O(log N)
case 1 > K’s parent P is black, OK !
case 2 > K’s parent P is red, there will be 4+4 cases
first 4 cases, P’s sibling is NIL or black — change G to red!
<1> G.left = P, P.left = K — left rotate
<2> G.left = P, P.right = K — change color and K to root
<3> G.right = P, P.left = K — change color and K to root
<4> G.right = P, P.right = K
second 4 cases, P’s sibling is red — recolor all the way up
http://en.wikipedia.org/wiki/Red–black_tree
1. Every node is either red or black.
2. The root is black. (This rule is sometimes omitted. Since the root can always be changed from red to black.)
3. There are no 2 adjacent red nodes.
4. Every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.
Lemma
A red-black tree with n internal nodes has the height at most 2log(n+1)
Proof of the balance
A simple example to understand balancing is, a chain of 3 nodes is not possible in Red Black Tree
A chain of 3 nodes is nodes is not possible in Red-Black Trees. Following are NOT Red-Black Trees 30 30 30 / \ / \ / \ 20 NIL 20 NIL 20 NIL / \ / \ / \ 10 NIL 10 NIL 10 NIL
Operation
insert / delete / lookup / print —— in O(log N) worst-case time
ps : Rotate - O(1)
left_rotate & right_rotate
1. insert - O(log N)
[step1] use the BST insert algorithm to add K to the tree — O(log N)
[step2] color the node containing K red
[step3] restore red-black tree properties (if necessary) — worst case, O(log N)
case 1 > K’s parent P is black, OK !
case 2 > K’s parent P is red, there will be 4+4 cases
first 4 cases, P’s sibling is NIL or black — change G to red!
<1> G.left = P, P.left = K — left rotate
<2> G.left = P, P.right = K — change color and K to root
<3> G.right = P, P.left = K — change color and K to root
<4> G.right = P, P.right = K
second 4 cases, P’s sibling is red — recolor all the way up
相关文章推荐
- 动态查找---->红黑树(Red-Black Tree)
- 1135. Is It A Red-Black Tree (30)/红黑树 搜索建树
- Red-Black Tree 与 STL源码 (0)
- 红黑树Red-Black tree初步详解(Java代码实现)
- 1135. Is It A Red-Black Tree (30)-PAT甲级真题
- 红黑树(red-black tree)算法,附AVL树的比较
- 1135. Is It A Red-Black Tree (30)
- PAT甲级 1135. Is It A Red-Black Tree (30) 建树+深搜
- 1135. Is It A Red-Black Tree (30)(红黑树)
- 从2-3-4树谈到Red-Black Tree(红黑树)
- 红黑树(red-black tree)
- Red-Black tree
- 从2-3-4树谈到Red-Black Tree(红黑树)
- 从2-3-4树谈到Red-Black Tree(红黑树) http://www.cnblogs.com/guoyiqi/archive/2011/06/08/2129310.html
- skip-list vs red-black tree
- 1135. Is It A Red-Black Tree (30)
- (pat)A1135. Is It A Red-Black Tree
- 红黑树(red-black tree)算法,附AVL树的比较
- 从2-3-4树谈到Red-Black Tree(红黑树)
- 红黑树(red-black tree)