实现一个完整的红黑树(Red-Black Tree)在 Java 中是一个相对复杂的任务,因为红黑树是一种自平衡的二叉搜索树,需要维护一些性质以保证树的平衡。以下是一个简化的红黑树实现,支持插入、删除和旋转操作。为了简洁起见,代码中省略了一些细节和边界情况的处理(如空树初始化等),但在实际使用中需要确保这些细节的完善。
javaCopy Codeclass RedBlackTree { private static final boolean RED = true; private static final boolean BLACK = false; private class Node { int data;
Node parent;
Node left;
Node right; boolean color;
Node(int data) { this.data = data; this.color = RED; // New nodes are red by default
}
} private Node root; // Left rotate
private void leftRotate(Node x) { Node y = x.right;
x.right = y.left; if (y.left != null) {
y.left.parent = x;
}
y.parent = x.parent; if (x.parent == null) {
root = y;
} else if (x == x.parent.left) {
x.parent.left = y;
} else {
x.parent.right = y;
}
y.left = x;
x.parent = y;
} // Right rotate
private void rightRotate(Node y) { Node x = y.left;
y.left = x.right; if (x.right != null) {
x.right.parent = y;
}
x.parent = y.parent; if (y.parent == null) {
root = x;
} else if (y == y.parent.right) {
y.parent.right = x;
} else {
y.parent.left = x;
}
x.right = y;
y.parent = x;
} // Fix violations after insertion
private void fixViolation(Node z) { while (z != root && z.parent.color == RED) { if (z.parent == z.parent.parent.left) { Node y = z.parent.parent.right; if (y != null && y.color == RED) {
z.parent.color = BLACK;
y.color = BLACK;
z.parent.parent.color = RED;
z = z.parent.parent;
} else { if (z == z.parent.right) {
z = z.parent;
leftRotate(z);
}
z.parent.color = BLACK;
z.parent.parent.color = RED;
rightRotate(z.parent.parent);
}
} else { Node y = z.parent.parent.left; if (y != null && y.color == RED) {
z.parent.color = BLACK;
y.color = BLACK;
z.parent.parent.color = RED;
z = z.parent.parent;
} else { if (z == z.parent.left) {
z = z.parent;
rightRotate(z);
}
z.parent.color = BLACK;
z.parent.parent.color = RED;
leftRotate(z.parent.parent);
}
}
}
root.color = BLACK;
} // Insert a new node
public void insert(int data) { Node node = new Node(data); Node y = null; Node x = root; while (x != null) {
y = x; if (node.data < x.data) {
x = x.left;
} else {
x = x.right;
}
}
node.parent = y; if (y == null) {
root = node;
} else if (node.data < y.data) {
y.left = node;
} else {
y.right = node;
}
fixViolation(node);
} // Other methods like delete would be implemented here, following similar logic for maintaining tree properties
// Utility method for in-order traversal (for testing)
public void inorderTraversal(Node node) { if (node != null) {
inorderTraversal(node.left);
System.out.print(node.data + " ");
inorderTraversal(node.right);
}
} public void inorder() {
inorderTraversal(root);
} public static void main(String[] args) { RedBlackTree tree = new RedBlackTree();
tree.insert(7);
tree.insert(3);
tree.insert(18);
tree.insert(10);
tree.insert(22);
tree.insert(8);
tree.insert(11);
tree.insert(26);
System.out.println("In-order traversal of the created Red-Black Tree:");
tree.inorder();
}
}
代码说明:
-
节点类 (
Node):- 每个节点包含数据、左右子节点、父节点和颜色属性。
-
旋转操作:
-
leftRotate和rightRotate方法用于维护红黑树的平衡。
-
-
插入操作:
- 插入新节点后,通过
fixViolation方法调整树的结构以维持红黑树的性质。
- 插入新节点后,通过
-
中序遍历:
-
inorderTraversal方法用于测试,输出树的中序遍历结果。
-
注意事项:
- 删除操作:删除操作比插入操作更复杂,需要处理多种情况以维持红黑树的性质。这里没有实现删除操作,但可以参考插入操作的逻辑进行扩展。
- 边界条件:实际应用中需要处理更多的边界条件,例如空树初始化、节点查找等。
- 颜色调整:在插入和删除过程中,需要仔细调整节点的颜色以维持红黑树的性质。
这个实现提供了一个基本的红黑树框架,可以在此基础上进行扩展和优化。
http://www.zgjzs.org
http://www.shangpin.org
http://www.snjrw.org
http://wangluo.zgjzs.org
http://zhineng.zgjzs.org
http://gaosu.zgjzs.org
http://anquan.zgjzs.org
http://wangzhan.zgjzs.org
http://jiaoyu.zgjzs.org
http://guanggao.zgjzs.org
http://shuju.zgjzs.org
http://zhifu.zgjzs.org
http://yingyong.zgjzs.org
http://yonghu.zgjzs.org
http://houtai.zgjzs.org
http://fenxi.zgjzs.org
http://jiekou.zgjzs.org
http://fenxiang.zgjzs.org
http://neirong.zgjzs.org
http://zhuce.zgjzs.org
http://tongji.zgjzs.org
http://ditu.zgjzs.org
http://tuisong.zgjzs.org
http://gouwu.zgjzs.org
http://tuiguang.zgjzs.org
http://shejiao.zgjzs.org
http://zhongguo.zgjzs.org
http://wangguan.zgjzs.org
http://yule.zgjzs.org
http://pingtai.zgjzs.org
http://jichu.zgjzs.org
http://wangjie.zgjzs.org
http://zhishi.zgjzs.org
http://shangcheng.zgjzs.org
http://dianzi.zgjzs.org
http://yinyue.zgjzs.org
http://tupian.zgjzs.org
http://shipin.zgjzs.org
http://wuliu.zgjzs.org
http://gongju.zgjzs.org
http://anquan.zgjzs.org
http://sousuo.zgjzs.org
http://mima.zgjzs.org
http://jiankong.zgjzs.org
http://ceshi.zgjzs.org
http://wangyou.zgjzs.org
http://zixun.zgjzs.org
http://chajian.zgjzs.org
http://kuaidi.zgjzs.org
http://liuliang.zgjzs.org
http://zhengce.zgjzs.org
http://peizhi.zgjzs.org
http://baocun.zgjzs.org
http://yonghu.shangpin.org
http://houtai.shangpin.org
http://pingtai.shangpin.org
http://wangzhan.shangpin.org
http://zhifu.shangpin.org
http://guanggao.shangpin.org
http://yinyue.shangpin.org
http://zixun.shangpin.org
http://shejiao.shangpin.org
http://tuisong.shangpin.org
http://wangguan.shangpin.org
http://zhineng.shangpin.org
http://wangluo.shangpin.org
http://anquan.shangpin.org
http://zhongguo.shangpin.org
http://shuju.shangpin.org
http://jiekou.shangpin.org
http://zhishi.shangpin.org
http://gongneng.shangpin.org
http://tongji.shangpin.org
http://shipin.shangpin.org
http://jiaoyu.shangpin.org
http://ditu.shangpin.org
http://dianzi.shangpin.org
http://mima.shangpin.org
http://liuliang.shangpin.org
http://gouwu.shangpin.org
http://zhengce.shangpin.org
http://yule.shangpin.org
http://baocun.shangpin.org
http://jichu.shangpin.org
http://fenxi.shangpin.org
http://fenxiang.shangpin.org
http://shangcheng.shangpin.org
http://kuaidi.shangpin.org
http://peizhi.shangpin.org
http://wuliu.shangpin.org
http://ceshi.shangpin.org
http://tupian.shangpin.org
http://wangyou.shangpin.org
http://chajian.shangpin.org
http://tuiguang.shangpin.org
http://yingyong.shangpin.org
http://zhuce.shangpin.org
http://sousuo.shangpin.org
http://jiankong.shangpin.org
http://neirong.shangpin.org
http://gongju.shangpin.org
http://zonghe.shangpin.org
http://zhifu.snjrw.org
http://houtai.snjrw.org
http://yule.snjrw.org
http://wangluo.snjrw.org
http://guanggao.snjrw.org
http://shuju.snjrw.org
http://pingtai.snjrw.org
http://ditu.snjrw.org
http://wangzhan.snjrw.org
http://shejiao.snjrw.org
http://jiekou.snjrw.org
http://zhishi.snjrw.org
http://zhineng.snjrw.org
http://jiaoyu.snjrw.org
http://zhongguo.snjrw.org
http://yinyue.snjrw.org
http://yonghu.snjrw.org
http://tuiguang.snjrw.org
http://gouwu.snjrw.org
http://baocun.snjrw.org
http://fenxiang.snjrw.org
http://fenxi.snjrw.org
http://gongneng.snjrw.org
http://wangguan.snjrw.org
http://zixun.snjrw.org
http://dianzi.snjrw.org
http://tupian.snjrw.org
http://wangyou.snjrw.org
http://chajian.snjrw.org
http://wuliu.snjrw.org
http://mima.snjrw.org
http://liuliang.snjrw.org
http://zhuce.snjrw.org
http://zhengce.snjrw.org
http://peizhi.snjrw.org
http://shipin.snjrw.org
http://jichu.snjrw.org
http://tongji.snjrw.org
http://kuaidi.snjrw.org
http://yingyong.snjrw.org
http://sousuo.snjrw.org
http://jiankong.snjrw.org
http://neirong.snjrw.org
http://gongju.snjrw.org
http://tuisong.snjrw.org
http://shangcheng.snjrw.org
http://anli.snjrw.org
http://dianhua.snjrw.org
http://fuwu.snjrw.org