java 红黑树实现
时间: 2023-11-20 10:56:36 浏览: 93
Java中的红黑树实现主要是通过对节点进行变色和旋转来维持其特性。具体实现可以参考以下步骤:
1.定义红黑树节点类,包括节点值、颜色、左右子节点等属性。
2.定义红黑树类,包括根节点、插入节点、删除节点等方法。
3.在插入节点时,根据其父节点和父节点的兄弟节点进行变色和旋转,以维持红黑树的特性。
4.在删除节点时,同样需要进行变色和旋转,以保证删除后的树仍然是一颗红黑树。
以下是一个简单的Java红黑树实现示例:
```java
public class RBNode<T extends Comparable<T>> {
boolean color;//颜色
T key;//关键字(键值)
RBNode<T> left;//左子节点
RBNode<T> right;//右子节点
RBNode<T> parent;//父节点
public RBNode(boolean color, T key, RBNode<T> parent, RBNode<T> left, RBNode<T> right) {
this.color = color;
this.key = key;
this.parent = parent;
this.left = left;
this.right = right;
}
}
public class RBTree<T extends Comparable<T>> {
private RBNode<T> root;//根节点
//插入节点
public void insert(T key) {
RBNode<T> node = new RBNode<T>(false, key, null, null, null);
if (node != null) {
insert(node);
}
}
//插入节点
private void insert(RBNode<T> node) {
RBNode<T> current = null;//当前节点
RBNode<T> x = this.root;//从根节点开始查找
//查找插入位置
while (x != null) {
current = x;
if (node.key.compareTo(x.key) < 0) {
x = x.left;
} else {
x = x.right;
}
}
node.parent = current;
if (current != null) {
if (node.key.compareTo(current.key) < 0) {
current.left = node;
} else {
current.right = node;
}
} else {
this.root = node;
}
//修正红黑树
insertFixUp(node);
}
//修正红黑树
private void insertFixUp(RBNode<T> node) {
RBNode<T> parent, gparent;//父节点和祖父节点
//需要修正的条件:父节点存在,且父节点的颜色是红色
while (((parent = parentOf(node)) != null) && isRed(parent)) {
gparent = parentOf(parent);//祖父节点
//父节点是祖父节点的左子节点
if (parent == gparent.left) {
RBNode<T> uncle = gparent.right;//叔叔节点
//case1:叔叔节点也是红色
if ((uncle != null) && isRed(uncle)) {
setBlack(parent);
setBlack(uncle);
setRed(gparent);
node = gparent;
continue;
}
//case2:叔叔节点是黑色,且当前节点是右子节点
if (node == parent.right) {
RBNode<T> tmp;
leftRotate(parent);
tmp = parent;
parent = node;
node = tmp;
}
//case3:叔叔节点是黑色,且当前节点是左子节点
setBlack(parent);
setRed(gparent);
rightRotate(gparent);
} else {//父节点是祖父节点的右子节点
RBNode<T> uncle = gparent.left;//叔叔节点
//case1:叔叔节点也是红色
if ((uncle != null) && isRed(uncle)) {
setBlack(parent);
setBlack(uncle);
setRed(gparent);
node = gparent;
continue;
}
//case2:叔叔节点是黑色,且当前节点是左子节点
if (node == parent.left) {
RBNode<T> tmp;
rightRotate(parent);
tmp = parent;
parent = node;
node = tmp;
}
//case3:叔叔节点是黑色,且当前节点是右子节点
setBlack(parent);
setRed(gparent);
leftRotate(gparent);
}
}
setBlack(this.root);//将根节点设为黑色
}
//左旋
private void leftRotate(RBNode<T> x) {
RBNode<T> y = x.right;
x.right = y.left;
if (y.left != null) {
y.left.parent = x;
}
y.parent = x.parent;
if (x.parent == null) {
this.root = y;
} else {
if (x.parent.left == x) {
x.parent.left = y;
} else {
x.parent.right = y;
}
}
y.left = x;
x.parent = y;
}
//右旋
private void rightRotate(RBNode<T> y) {
RBNode<T> x = y.left;
y.left = x.right;
if (x.right != null) {
x.right.parent = y;
}
x.parent = y.parent;
if (y.parent == null) {
this.root = x;
} else {
if (y == y.parent.right) {
y.parent.right = x;
} else {
y.parent.left = x;
}
}
x.right = y;
y.parent = x;
}
//获取节点的父节点
private RBNode<T> parentOf(RBNode<T> node) {
return node != null ? node.parent : null;
}
//判断节点是否是红色
private boolean isRed(RBNode<T> node) {
return (node != null) && node.color;
}
//设置节点为红色
private void setRed(RBNode<T> node) {
if (node != null) {
node.color = true;
}
}
//设置节点为黑色
private void setBlack(RBNode<T> node) {
if (node != null) {
node.color = false;
}
}
}
```
阅读全文