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
case2. X’s uncle node (U) is black, and X is a right child (of P).
case3. X’s uncle node (U) is black, and X is a left child (of P).
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.
case 2. X’s brother node (B) is black, the two child nodes (N) of B are black.
case 3. X’s brother node (B) is black, the left child node of B is red, right child node is black.
case 4. X’s brother node (B) is black, the right child node of B is red.
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); }
- 红黑树Red-Black-Tree
- 红黑树(Red Black Tree)
- Red-Black Tree
- red-black tree
- Red Black Tree
- 红黑树(red black tree)
- red-black tree
- Red Black Tree
- Red Black Tree
- Red-black Tree
- Red-Black Tree红黑树
- Red-Black Tree 红黑树
- 红黑树red-black-tree
- Red-Black Tree
- Red-Black Tree
- Red-Black Tree
- Red Black Tree
- red-black tree
- 使用数据工厂模式生成测试数据
- leetcode:Is Subsequence
- 简单旋转加载
- Django-MongoDB的使用
- JAVA ArrayList案例
- Red black tree
- Nginx安装、配置及使用总结
- Red Giant Trapcode Suite 12.0 含注册码
- iOS开发笔记之六十五——两种截屏方案的对比
- 【Eternallyc】qsort排序函数用法
- 函数初了解
- JavaEE----JPA中配置文件persistence.xml
- 密码验证合格程序
- Search for a Range