------------ 本文来自 [阿P官方博客](https:
------------ 在线测试红黑树:[红黑树在线测试](https:
###一、红黑树
```markdown
自平衡二叉查找树(Binary Search Tree)
二叉查找树:
基于二叉树
左子树小于根,右子树大于根
缺点:如果选错根节点,整个树就不再平衡
特点
所有节点非红即黑
根节点一定是黑色
红节点的子节点一定是黑色
最低层的叶子节点一定是黑色
从根节点到任意叶子节点经历过的黑色节点一定相同
新增的节点颜色一定是红色节点
红黑树时间复杂度:O(logN) 与二分查找法一致
第一次查找数量 n/2
第二次查找数量 n/2/2
第三次查找数量 n/2/2/2
…...
第m次查找数量 n/(2^m)
由此我们可以得知时间复杂度=logN
```
###二、红黑树修正(遵循以下步骤进行修正)
```markdown
当前节点为红,父节点为红,并且叔父节点为红或为空
父节点、叔父节点涂黑,祖父节点涂红。对祖父节点继续操作
当前节点为红且是右子叶,父节点为红,并且叔父节点为黑
以当前节点为轴,进行左旋
当前节点为红且是左子叶,父节点为红,并且叔父节点为黑
父节点涂黑,祖父节点涂红,以父节点为轴,进行右旋
```
###三、红黑树JAVA实现类
```java
package RBTree;
public class RBTreeDemo<T extends Comparable<T>> {
Node root;
Node min;
Node max;
Boolean RED = true;
Boolean BLACK = false;
public void add(T val)
{
if (this.root == null) {
this.root = new Node<T>(val, this.BLACK, null, null, null);
return;
}
Node node = this.root;
Node current = new Node<T>(val, this.RED, null, null, null);
while (true) {
if (val.compareTo((T) node.val) > 0) {
if (node.right == null) {
node.right = current;
current.parent = node;
break;
}
node=node.right;
} else {
if (node.left == null) {
node.left = current;
current.parent = node;
break;
}
node=node.left;
}
}
fixUp(current);
}
public void fixUp(Node node)
{
if (node.parent == null) {
node.color = this.BLACK;
this.root = node;
return;
}
if (node.parent.parent == null) {
return;
}
Node uncleNode = this.getUncleNode(node);
if (node.color == this.RED && node.parent.color == this.RED) {
if (uncleNode == null || uncleNode.color == this.RED) {
node.parent.color = this.BLACK;
if (uncleNode != null) {
uncleNode.color=this.BLACK;
}
node.parent.parent.color = this.RED;
this.fixUp(node.parent.parent);
} else if (uncleNode.color == this.BLACK) {
if (node.parent.left.equals(node)) {
this.right(node);
this.fixUp(node.parent);
} else {
this.left(node);
this.fixUp(node.left);
}
}
}
}
private Node getUncleNode(Node node) {
Node uncleNode= node.parent.parent.left;
if (uncleNode == null || node.parent.equals(uncleNode)) {
uncleNode = node.parent.parent.right;
}
return uncleNode;
}
private void left(Node node) {
Node parent = node.parent;
Node left = node.left;
Node pParent = node.parent.parent;
pParent.left = node;
parent.parent = node;
parent.right = left;
left.parent = parent;
node.parent = pParent;
node.left = parent;
}
private void right(Node node) {
Node parent = node.parent;
Node right = parent.right;
Node pParent = node.parent.parent;
Node ppParent = node.parent.parent.parent;
pParent.color = this.RED;
parent.color = this.BLACK;
if (ppParent != null) {
if (ppParent.right.equals(pParent)) {
ppParent.right = parent;
} else {
ppParent.left = parent;
}
}
pParent.left = right;
pParent.parent=parent;
parent.right = pParent;
parent.parent = ppParent;
right.parent = pParent;
}
private String preOrder(Node node) {
if (node == null) {
return null;
}
String strLeft = this.preOrder(node.left);
if (strLeft == null) {
strLeft="";
}
String strRight = this.preOrder(node.right);
if (strRight == null) {
strRight="";
}
return strLeft + node.toString() + strRight;
}
@Override
public String toString() {
return preOrder(this.root);
}
private class Node<T>{
public T val;
public Boolean color;
public Node left;
public Node right;
public Node parent;
public Node(T val, Boolean color, Node left, Node right, Node parent) {
super();
this.val = val;
this.color = color;
this.left = left;
this.right = right;
this.parent = parent;
}
@Override
public String toString() {
String left = "-";
String right = "-";
if (this.left != null) {
left = this.left.val.toString();
}
if (this.right != null) {
right = this.right.val.toString();
}
return "[" + left + ", "+this.val.toString() + ", " + right +", "+ getColorName(this.color) + "]";
}
private String getColorName(Boolean color) {
return color == true? "红" : "黑";
}
@Override
public boolean equals(Object obj) {
Node obj1 = (Node)obj;
return obj1.val == this.val;
}
}
}
```