JAVA红黑树

JAVA红黑树

概念:每个节点都带有颜色属性的二叉查找树,颜色或红色或黑色,提高二叉树的查找性能;

特性:

性质1. 节点是红色或黑色。

  性质2. 根是黑色。

  性质3. 每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)

  性质4. 从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。

 

性能:O(log n)的时间之外,每次插入或删除需要O(log n)的空间。
实现:

JAVAQueue:用linkedlist写的队列

              

 

JAVAStack:用linkedlist写的堆栈

              

TreeNode:定义的节点类

               

TestRBT类:主要功能实现

              

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值