红黑树简介及Java实现

Posted 蝙蝠程序猿Android

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了红黑树简介及Java实现相关的知识,希望对你有一定的参考价值。

前段时间撸了一次排序算法,有的小伙伴们希望写一个红黑树出来,所以,今天就来把撸好的红黑树献给大家吧。

简介

红黑树(Red Black Tree) 是一种自平衡二叉查找树,是在计算机科学中用到的一种数据结构,典型的用途是实现关联数组。

它是在1972年由Rudolf Bayer发明的,当时被称为平衡二叉B树(symmetric binary B-trees)。后来,在1978年被 Leo J. Guibas 和 Robert Sedgewick 修改为如今的“红黑树”。

特性

红黑树是每个节点都带有颜色属性的二叉查找树,颜色或红色或黑色。在二叉查找树强制一般要求以外,对于任何有效的红黑树我们增加了如下的额外特性:

  1. 节点是红色或黑色。

  2. 根节点是黑色。

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

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

例:

这些约束强制了红黑树的关键性质: 从根到叶子的最长的可能路径不多于最短的可能路径的两倍长。结果是这个树大致上是平衡的。因为操作比如插入、删除和查找某个值的最坏情况时间都要求与树的高度成比例,这个在高度上的理论上限允许红黑树在最坏情况下都是高效的。


实现

首先,从红黑树结构入手:

  • 红黑树由一个一个的节点组成

  • 每个节点最多可以有两个子节点,也可以有一个父节点(根节点除外)

  • 每个节点有红色或黑色两种颜色

  • 每个节点需要有关键字可以用来排序(实现代码以整型数字为例)

  • 一颗树的起点是根节点,根节点的引用不能丢。

public class RBTree {
private RBTNode mRoot; // 根结点
private static final boolean RED = false; private static final boolean BLACK = true;
public RBTree() { mRoot = null; }
public class RBTNode { boolean color; // 颜色 int key; // 关键字(键值) RBTNode left; // 左孩子 RBTNode right; // 右孩子 RBTNode parent; // 父结点
public RBTNode(int key, boolean color, RBTNode parent, RBTNode left, RBTNode right) { this.key = key; this.color = color; this.parent = parent; this.left = left; this.right = right; }
}}

其次,我们来实现红黑树的数据插入。插入数据时,必定会破坏红黑树的结构(即无法符合红黑树的所有特性)。所以在插入数据后,还需要将树修复为一颗完整的红黑树。 

节点插入代码:

 /* * 新建结点(key),并将其插入到红黑树中 * * 参数说明: key 插入结点的键值 */ public void insert(int key) { RBTNode node = new RBTNode(key, RED, null, null, null);
// 如果新建结点失败,则返回。 if (node != null) { insert(node); } }
/** * 将一个节点插入到红黑树中。仅{@link #insert(int)}使用 * * @param node 需要插入的节点,其颜色需为红色 */ private void insert(RBTNode node) { RBTNode xNode = mRoot; RBTNode yNode = null;
// 寻找合适的父节点yNode while (xNode != null) { yNode = xNode; if (node.key < xNode.key) { xNode = xNode.left; } else { xNode = xNode.right; } }
// 插入节点。 // 若树为空,则此时的yNode为空,插入的节点为根节点 node.parent = yNode; if (yNode != null) { if (node.key < yNode.key) { yNode.left = node; } else { yNode.right = node; } } else { mRoot = node; node.color = BLACK; // 根节点设置为黑色 }
// 将它重新修正为一颗二叉查找树 insertFixUp(node); }

上述代码中的注释已经比较清晰地解释了插入数据的流程。其中的insertFixUp(node)方法就是红黑树修正的方法。

红黑树修正

红黑树修正方式有三种:变色、左旋转、有旋转。适当的情况下使用适当的修正方式,即可得到完整的红黑树。

红黑树插入后需要修正的情况(来自网络大神):


插入修正代码:

 /* * 红黑树插入修正函数 * * 在向红黑树中插入节点之后(失去平衡),再调用该函数;目的是将它重新塑造成一颗红黑树。 * * 参数说明:node 插入的结点 */ private void insertFixUp(RBTNode node) { RBTNode parent, gparent; // 父节点、祖父节点
// 若“父节点存在,并且父节点的颜色是红色” while (((parent = parentOf(node)) != null) && isRed(parent)) { gparent = parentOf(parent);
// 若“父节点”是“祖父节点的左孩子” if (parent == gparent.left) { // Case 1条件:叔叔节点是红色 RBTNode uncle = gparent.right; if ((uncle != null) && isRed(uncle)) { setBlack(uncle); setBlack(parent); setRed(gparent); node = gparent; continue; }
// Case 2条件:叔叔是黑色,且当前节点是右孩子 if (parent.right == node) { RBTNode tmp; leftRotate(parent); tmp = parent; parent = node; node = tmp; }
// Case 3条件:叔叔是黑色,且当前节点是左孩子。 setBlack(parent); setRed(gparent); rightRotate(gparent); } else { // 若“z的父节点”是“z的祖父节点的右孩子” // Case 1条件:叔叔节点是红色 RBTNode uncle = gparent.left; if ((uncle != null) && isRed(uncle)) { setBlack(uncle); setBlack(parent); setRed(gparent); node = gparent; continue; }
// Case 2条件:叔叔是黑色,且当前节点是左孩子 if (parent.left == node) { RBTNode tmp; rightRotate(parent); tmp = parent; parent = node; node = tmp; }
// Case 3条件:叔叔是黑色,且当前节点是右孩子。 setBlack(parent); setRed(gparent); leftRotate(gparent); } }
// 将根节点设为黑色 setBlack(mRoot);


变色相关:

 /** * 获取父节点 若没有父节点,则返回null *  * @param node */ private boolean colorOf(RBTNode node) { return node != null ? node.color : BLACK; }
/** * 是否为红色节点 * * @param node * @return true为红色节点,false为黑色节点 */ private boolean isRed(RBTNode node) { return node != null && colorOf(node) == RED; }
/** * 是否为黑色节点 * * @param node * @return true为红色节点,false为黑色节点 */ private boolean isBlack(RBTNode node) { return colorOf(node) == BLACK; }
/** * 将节点设置为黑色节点 * * @param node */ private void setBlack(RBTNode node) { if (node != null) { node.color = BLACK; } }
/** * 将节点设置为红色节点 * * @param node */ private void setRed(RBTNode node) { if (node != null) { node.color = RED; } }


左旋转与右旋转:

 /* * 对红黑树的节点(x)进行左旋转 * * 左旋示意图(对节点x进行左旋): *  * px px  * / /  * x y  * / \ -----> / \  * lx y x ry  * / \ / \  * ly ry lx ly * * */ private void leftRotate(RBTNode x) { // 设置x的右孩子为y RBTNode y = x.right;
// 将 “y的左孩子” 设为 “x的右孩子”; // 如果y的左孩子非空,将 “x” 设为 “y的左孩子的父亲” x.right = y.left; if (y.left != null) { y.left.parent = x; }
// 将 “x的父亲” 设为 “y的父亲” y.parent = x.parent;
if (x.parent == null) { this.mRoot = y; // 如果 “x的父亲” 是空节点,则将y设为根节点 } else { if (x.parent.left == x) x.parent.left = y; // 如果 x是它父节点的左孩子,则将y设为“x的父节点的左孩子” else x.parent.right = y; // 如果 x是它父节点的左孩子,则将y设为“x的父节点的左孩子” }
// 将 “x” 设为 “y的左孩子” y.left = x; // 将 “x的父节点” 设为 “y” x.parent = y; }
/* * 对红黑树的节点(y)进行右旋转 * * 右旋示意图(对节点y进行左旋): * py py * / / * y x * / \ -----> / \ * x ry lx y * / \ / \ * lx rx rx ry * */ private void rightRotate(RBTNode y) { // 设置x是当前节点的左孩子。 RBTNode x = y.left;
// 将 “x的右孩子” 设为 “y的左孩子”; // 如果"x的右孩子"不为空的话,将 “y” 设为 “x的右孩子的父亲” y.left = x.right; if (x.right != null) x.right.parent = y;
// 将 “y的父亲” 设为 “x的父亲” x.parent = y.parent;
if (y.parent == null) { this.mRoot = x; // 如果 “y的父亲” 是空节点,则将x设为根节点 } else { if (y == y.parent.right) y.parent.right = x; // 如果 y是它父节点的右孩子,则将x设为“y的父节点的右孩子” else y.parent.left = x; // (y是它父节点的左孩子) 将x设为“x的父节点的左孩子” }
// 将 “y” 设为 “x的右孩子” x.right = y;
// 将 “y的父节点” 设为 “x” y.parent = x; }

到这里,插入模块基本完成,接下来进行删除模块。


我们需要先按照二叉树的节点删除方式来删除节点,然后再将其修复为红黑树。

二叉树删除结点找替代结点有3种情情景:

  • 若删除结点无子结点,直接删除

  • 若删除结点只有一个子结点,用子结点替换删除结点

  • 若删除结点有两个子结点,用后继结点(大于删除结点的最小结点)替换删除结点

知识补充:后继结点是大于删除结点的最小结点


刪除代码:

 /* * 删除结点 * */ public void remove(int key) { RBTNode node;
if ((node = search(mRoot, key)) != null) remove(node); }
/* * 删除结点 * * 参数说明:node 删除的结点 */ private void remove(RBTNode node) { RBTNode child, parent; boolean color;
// 被删除节点的"左右孩子都不为空"的情况。 if ((node.left != null) && (node.right != null)) { // 被删节点的后继节点。(称为"取代节点") // 用它来取代"被删节点"的位置,然后再将"被删节点"去掉。 RBTNode replace = node;
// 获取后继节点 replace = replace.right; while (replace.left != null) replace = replace.left;
// "node节点"不是根节点(只有根节点不存在父节点) if (parentOf(node) != null) { if (parentOf(node).left == node) parentOf(node).left = replace; else parentOf(node).right = replace; } else { // "node节点"是根节点,更新根节点。 this.mRoot = replace; }
// child是"取代节点"的右孩子,也是需要"调整的节点"。 // "取代节点"肯定不存在左孩子!因为它是一个后继节点。 child = replace.right; parent = parentOf(replace); // 保存"取代节点"的颜色 color = colorOf(replace);
// "被删除节点"是"它的后继节点的父节点" if (parent == node) { parent = replace; } else { // child不为空 if (child != null) setParent(child, parent); parent.left = child;
replace.right = node.right; setParent(node.right, replace); }
replace.parent = node.parent; replace.color = node.color; replace.left = node.left; node.left.parent = replace;
if (color == BLACK) removeFixUp(child, parent);
node = null; return; }
if (node.left != null) { child = node.left; } else { child = node.right; }
parent = node.parent; // 保存"取代节点"的颜色 color = node.color;
if (child != null) child.parent = parent;
// "node节点"不是根节点 if (parent != null) { if (parent.left == node) parent.left = child; else parent.right = child; } else { this.mRoot = child; }
if (color == BLACK) removeFixUp(child, parent); node = null; }
    /** * 设置节点的父节点 * * @param node * @param parent */ private void setParent(RBTNode node, RBTNode parent) { if (node != null) { node.parent = parent; } }
/* * 找结点(node)的后继结点。即,查找"红黑树中数据值大于该结点"的"最小结点"。 */ public RBTNode successor(RBTNode node) { // 如果node存在右孩子,则"node的后继结点"为 "以其右孩子为根的子树的最小结点"。 if (node.right != null) { return minimum(node.right); }
// 如果node没有右孩子。则node有以下两种可能: // (01) node是"一个左孩子",则"node的后继结点"为 "它的父结点"。 // (02) node是"一个右孩子",则查找"node的最低的父结点,并且该父结点要具有左孩子",找到的这个"最低的父结点"就是"node的后继结点"。 RBTNode parentNode = node.parent; while ((parentNode != null) && (node == parentNode.right)) { node = parentNode; parentNode = parentNode.parent; }
return parentNode; }
/* * 找结点(x)的前驱结点。即,查找"红黑树中数据值小于该结点"的"最大结点"。 */ public RBTNode predecessor(RBTNode node) { // 如果x存在左孩子,则"x的前驱结点"为 "以其左孩子为根的子树的最大结点"。 if (node.left != null) { return maximum(node.left); }
// 如果x没有左孩子。则x有以下两种可能: // (01) x是"一个右孩子",则"x的前驱结点"为 "它的父结点"。 // (01) x是"一个左孩子",则查找"x的最低的父结点,并且该父结点要具有右孩子",找到的这个"最低的父结点"就是"x的前驱结点"。 RBTNode parentNode = node.parent; while ((parentNode != null) && (node == parentNode.left)) { node = parentNode; parentNode = parentNode.parent; }
return parentNode; }

其中removeFixUp方法为删除节点后的红黑树修复方法。

修复分的情况比较复杂,直接上代码+注释。

红黑树删除修正:

 /* * 红黑树删除修正函数 * * 在从红黑树中删除插入节点之后(红黑树失去平衡),再调用该函数;目的是将它重新塑造成一颗红黑树。 * * 参数说明:node 待修正的节点 */ private void removeFixUp(RBTNode node, RBTNode parent) { RBTNode other;
while ((node == null || isBlack(node)) && (node != this.mRoot)) { if (parent.left == node) { other = parent.right; if (isRed(other)) { // Case 1: x的兄弟w是红色的 setBlack(other); setRed(parent); leftRotate(parent); other = parent.right; }
if ((other.left == null || isBlack(other.left)) && (other.right == null || isBlack(other.right))) { // Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的 setRed(other); node = parent; parent = parentOf(node); } else {
if (other.right == null || isBlack(other.right)) { // Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。 setBlack(other.left); setRed(other); rightRotate(other); other = parent.right; } // Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。 setColor(other, colorOf(parent)); setBlack(parent); setBlack(other.right); leftRotate(parent); node = this.mRoot; break; } } else {
other = parent.left; if (isRed(other)) { // Case 1: x的兄弟w是红色的 setBlack(other); setRed(parent); rightRotate(parent); other = parent.left; }
if ((other.left == null || isBlack(other.left)) && (other.right == null || isBlack(other.right))) { // Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的 setRed(other); node = parent; parent = parentOf(node); } else {
if (other.left == null || isBlack(other.left)) { // Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。 setBlack(other.right); setRed(other); leftRotate(other); other = parent.left; }
// Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。 setColor(other, colorOf(parent)); setBlack(parent); setBlack(other.left); rightRotate(parent); node = this.mRoot; break; } } }
if (node != null) setBlack(node); }
    /** * 获取父节点 若没有父节点,则返回null * * @param node */ private RBTNode parentOf(RBTNode node) { return node != null ? node.parent : null; }

至此,红黑树的基本操作已经完成,接下来我们完善它的其它方法:


前序遍历:

 private void preOrder(RBTNode node) { if (node != null) { System.out.print(node.key + " "); preOrder(node.left); preOrder(node.right); } }
/** * 前序遍历 */ public void preOrder() { preOrder(mRoot); }

中序遍历

 private void inOrder(RBTNode node) { if (node != null) { inOrder(node.left); System.out.print(node.key + " "); inOrder(node.right); } }
/** * 中序遍历 */ public void inOrder() { inOrder(mRoot); }


后序遍历:

 private void postOrder(RBTNode node) { if (node != null) { postOrder(node.left); postOrder(node.right); System.out.print(node.key + " "); } }
/** * 后序遍历 */ public void postOrder() { postOrder(mRoot); }


红黑树搜索:

 /** * 查找红黑树中或键值最接近key的节点 *  * @param node 开始查找的位置 * @param key 要查找的值 * @return 最接近key的节点 */ private RBTNode search(RBTNode node, int key) { if (node == null) { return node; }
if (node.key < key) { return search(node.right, key); } else if (node.key > key) { return search(node.left, key); } else { return node; } }
/** * 查找红黑树中或键值最接近key的节点 */ public RBTNode search(int key) { return search(mRoot, key); }


查找最小值:

/* * 查找最小结点:返回node为根结点的红黑树的最小结点。 */ private RBTNode minimum(RBTNode node) { if (node == null) { return null; }
while (node.left != null) { node = node.left; }
return node; }
/** * 查找最小节点的key值 * * @return */ public int minimum() { RBTNode node = minimum(mRoot); if (node != null) { return node.key; } return Integer.MIN_VALUE; }


查找最大值:

 /* * 查找最大结点:返回node为根结点的红黑树的最大结点。 */ private RBTNode maximum(RBTNode node) { if (node == null) { return null; }
while (node.right != null) { node = node.right; } return node; }
/** * 查找最大节点的key值 * * @return */ public int maximum() { RBTNode node = maximum(mRoot); if (node != null) { return node.key; } return Integer.MIN_VALUE; }


红黑树打印:

 /* * 打印"红黑树" * * key -- 节点的键值 direction -- 0,表示该节点是根节点; -1,表示该节点是它的父结点的左孩子; 1,表示该节点是它的父结点的右孩子。 */ private void print(RBTNode tree, int key, int direction) {
if (tree != null) {
if (direction == 0) // tree是根节点 System.out.printf("%2d(B) is root\n", tree.key); else // tree是分支节点 System.out.printf("%2d(%s) is %2d's %6s child\n", tree.key, isRed(tree) ? "R" : "B", key, direction == 1 ? "right" : "left");
print(tree.left, tree.key, -1); print(tree.right, tree.key, 1); } }
public void print() { if (mRoot != null) { print(mRoot, mRoot.key, 0); } }


红黑树释放:

 /* * 销毁红黑树 */ private void destroy(RBTNode tree) { if (tree == null) return;
if (tree.left != null) { destroy(tree.left); } if (tree.right != null) { destroy(tree.right); }
tree = null; }
public void clear() { destroy(mRoot); mRoot = null; }

大功告成。


结语

源码包括调试代码已经放在git上(点击阅读原文即可查看),排序及数据结构,有兴趣的童鞋们可以给个Star。

另,上一篇排序算法中漏掉了冒泡排序,在git项目中也一并进行了实现,有兴趣的童鞋可以一起研究~

以上是关于红黑树简介及Java实现的主要内容,如果未能解决你的问题,请参考以下文章

数据结构-红黑树

红黑树之原理分析及C语言实现

红黑树深入剖析及Java实现

红黑树深入剖析及Java实现

红黑树 实现

[C/C++]详解STL容器7--红黑树的介绍及部分模拟实现