1.. 图解2-3树维持绝对平衡的原理:

2.. 红黑树与2-3树是等价的

3.. 红黑树的特点

  • 简要概括如下:
  • 所有节点非黑即红;根节点为黑;NULL节点为黑;红节点孩子为黑;黑平衡

4.. 实现红黑树的业务逻辑

  • import java.util.ArrayList;
    
    public class RBTree<K extends Comparable<K>, V> {
    
        private static final boolean RED = true;
    private static final boolean BLACK = false; private class Node{
    public K key;
    public V value;
    public Node left, right;
    public boolean color; public Node(K key, V value){
    this.key = key;
    this.value = value;
    left = null;
    right = null;
    color = RED;
    }
    } private Node root;
    private int size; public RBTree(){
    root = null;
    size = 0;
    } public int getSize(){
    return size;
    } public boolean isEmpty(){
    return size == 0;
    } // 判断节点node的颜色
    private boolean isRed(Node node){
    if(node == null)
    return BLACK;
    return node.color;
    } // node x
    // / \ 左旋转 / \
    // T1 x ---------> node T3
    // / \ / \
    // T2 T3 T1 T2
    private Node leftRotate(Node node){ Node x = node.right; // 左旋转
    node.right = x.left;
    x.left = node; x.color = node.color;
    node.color = RED; return x;
    } // node x
    // / \ 右旋转 / \
    // x T2 -------> y node
    // / \ / \
    // y T1 T1 T2
    private Node rightRotate(Node node){ Node x = node.left; // 右旋转
    node.left = x.right;
    x.right = node; x.color = node.color;
    node.color = RED; return x;
    } // 颜色翻转
    private void flipColors(Node node){ node.color = RED;
    node.left.color = BLACK;
    node.right.color = BLACK;
    } // 向红黑树中添加新的元素(key, value)
    public void add(K key, V value){
    root = add(root, key, value);
    root.color = BLACK; // 最终根节点为黑色节点
    } // 向以node为根的红黑树中插入元素(key, value),递归算法
    // 返回插入新节点后红黑树的根
    private Node add(Node node, K key, V value){ if(node == null){
    size ++;
    return new Node(key, value); // 默认插入红色节点
    } if(key.compareTo(node.key) < 0)
    node.left = add(node.left, key, value);
    else if(key.compareTo(node.key) > 0)
    node.right = add(node.right, key, value);
    else // key.compareTo(node.key) == 0
    node.value = value; if (isRed(node.right) && !isRed(node.left))
    node = leftRotate(node); if (isRed(node.left) && isRed(node.left.left))
    node = rightRotate(node); if (isRed(node.left) && isRed(node.right))
    flipColors(node); return node;
    } // 返回以node为根节点的二分搜索树中,key所在的节点
    private Node getNode(Node node, K key){ if(node == null)
    return null; if(key.equals(node.key))
    return node;
    else if(key.compareTo(node.key) < 0)
    return getNode(node.left, key);
    else // if(key.compareTo(node.key) > 0)
    return getNode(node.right, key);
    } public boolean contains(K key){
    return getNode(root, key) != null;
    } public V get(K key){ Node node = getNode(root, key);
    return node == null ? null : node.value;
    } public void set(K key, V newValue){
    Node node = getNode(root, key);
    if(node == null)
    throw new IllegalArgumentException(key + " doesn't exist!"); node.value = newValue;
    } // 返回以node为根的二分搜索树的最小值所在的节点
    private Node minimum(Node node){
    if(node.left == null)
    return node;
    return minimum(node.left);
    } // 删除掉以node为根的二分搜索树中的最小节点
    // 返回删除节点后新的二分搜索树的根
    private Node removeMin(Node node){ if(node.left == null){
    Node rightNode = node.right;
    node.right = null;
    size --;
    return rightNode;
    } node.left = removeMin(node.left);
    return node;
    } // 从二分搜索树中删除键为key的节点
    public V remove(K key){ Node node = getNode(root, key);
    if(node != null){
    root = remove(root, key);
    return node.value;
    }
    return null;
    } private Node remove(Node node, K key){ if( node == null )
    return null; if( key.compareTo(node.key) < 0 ){
    node.left = remove(node.left , key);
    return node;
    }
    else if(key.compareTo(node.key) > 0 ){
    node.right = remove(node.right, key);
    return node;
    }
    else{ // key.compareTo(node.key) == 0 // 待删除节点左子树为空的情况
    if(node.left == null){
    Node rightNode = node.right;
    node.right = null;
    size --;
    return rightNode;
    } // 待删除节点右子树为空的情况
    if(node.right == null){
    Node leftNode = node.left;
    node.left = null;
    size --;
    return leftNode;
    } // 待删除节点左右子树均不为空的情况 // 找到比待删除节点大的最小节点, 即待删除节点右子树的最小节点
    // 用这个节点顶替待删除节点的位置
    Node successor = minimum(node.right);
    successor.right = removeMin(node.right);
    successor.left = node.left; node.left = node.right = null; return successor;
    }
    } public static void main(String[] args){ System.out.println("Pride and Prejudice"); ArrayList<String> words = new ArrayList<>();
    if(FileOperation.readFile("pride-and-prejudice.txt", words)) {
    System.out.println("Total words: " + words.size()); RBTree<String, Integer> map = new RBTree<>();
    for (String word : words) {
    if (map.contains(word))
    map.set(word, map.get(word) + 1);
    else
    map.add(word, 1);
    } System.out.println("Total different words: " + map.getSize());
    System.out.println("Frequency of PRIDE: " + map.get("pride"));
    System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
    } System.out.println();
    }
    }

5.. 向红黑树中添加新元素之后需要进行维护的过程示意图

第三十三篇 玩转数据结构——红黑树(Read Black Tree)的更多相关文章

  1. java数据结构——红黑树(R-B Tree)

    红黑树相比平衡二叉树(AVL)是一种弱平衡树,且具有以下特性: 1.每个节点非红即黑; 2.根节点是黑的; 3.每个叶节点(叶节点即树尾端NULL指针或NULL节点)都是黑的; 4.如图所示,如果一个 ...

  2. 第二十三篇 玩转数据结构——栈(Stack)

          1.. 栈的特点: 栈也是一种线性结构: 相比数组,栈所对应的操作是数组的子集: 栈只能从一端添加元素,也只能从这一端取出元素,这一端通常称之为"栈顶": 向栈中添加元 ...

  3. 第三十二篇 玩转数据结构——AVL树(AVL Tree)

          1.. 平衡二叉树 平衡二叉树要求,对于任意一个节点,左子树和右子树的高度差不能超过1. 平衡二叉树的高度和节点数量之间的关系也是O(logn) 为二叉树标注节点高度并计算平衡因子 AVL ...

  4. 第三十一篇 玩转数据结构——并查集(Union Find)

    1.. 并查集的应用场景 查看"网络"中节点的连接状态,这里的网络是广义上的网络 数学中的集合类的实现   2.. 并查集所支持的操作 对于一组数据,并查集主要支持两种操作:合并两 ...

  5. 第三十篇 玩转数据结构——字典树(Trie)

          1.. Trie通常被称为"字典树"或"前缀树" Trie的形象化描述如下图: Trie的优势和适用场景 2.. 实现Trie 实现Trie的业务无 ...

  6. 第二十九篇 玩转数据结构——线段树(Segment Tree)

          1.. 线段树引入 线段树也称为区间树 为什么要使用线段树:对于某些问题,我们只关心区间(线段) 经典的线段树问题:区间染色,有一面长度为n的墙,每次选择一段墙进行染色(染色允许覆盖),问 ...

  7. 树-红黑树(R-B Tree)

    红黑树概念 特殊的二叉查找树,每个节点上都有存储位表示节点的颜色是红(Red)或黑(Black).时间复杂度是O(lgn),效率高. 特性: (1)每个节点或者是黑色,或者是红色. (2)根节点是黑色 ...

  8. 红黑树(R-B Tree)

    R-B Tree简介 R-B Tree,全称是Red-Black Tree,又称为“红黑树”,它一种特殊的二叉查找树.红黑树的每个节点上都有存储位表示节点的颜色,可以是红(Red)或黑(Black). ...

  9. 笔试算法题(51):简介 - 红黑树(RedBlack Tree)

    红黑树(Red-Black Tree) 红黑树是一种BST,但是每个节点上增加一个存储位表示该节点的颜色(R或者B):通过对任何一条从root到leaf的路径上节点着色方式的显示,红黑树确保所有路径的 ...

随机推荐

  1. 《操作系统真象还原》MBR

    以下是读本书第三章的收获. 如何知道一个源程序的各符号(指令和变量)地址?简单来说,地址就是该符号偏移文件开头的距离,符号的地址是按顺序编排的,所以两个相邻的符号,其地址也是相邻的.对于指令来说,指令 ...

  2. Java中的实体类--Serializable接口、transient 关键字

    在java中,实体类是一个非常重要的概念,我们可以在实体类中封装对象.设置其属性和方法等.关于实体类,也经常涉及到适配器模式.装饰者模式等设计模式.那么在实际代码开发中,关于实体类的注意事项有哪些呢? ...

  3. 将字符串日期格式化为yyyy-mm-dd

    (CONVERT(varchar(100), CONVERT(datetime,a.con_ret_time), 23))

  4. [CF1303E] Erase Subsequences - dp

    Solution 不由分说地枚举分割点 令 \(f[i][j]\) 表示原串处理到 \(i\) ,\(s_1\) 处理到 \(j\),\(s_2\) 最多能处理到哪里 采用主动转移 任意情况, \(f ...

  5. Github+Hexo一站式部署个人博客(原创)

    写在前面 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 本文源链接:https://www.cnblogs.com/chloneda/p/hexo.ht ...

  6. java实现判断两个二叉树是否相同

    1.定义树节点类:节点值.左节点.右节点.构造器 2.先判断树是否为空的情况 3.树不为空时,判断节点所指的值是否相等,若相等,则递归判断节点的左右节点是否相同,相同则返回true /** * Def ...

  7. print不是函数

    对文章阅读有感!!! 文章地址:http://www.laruence.com/2019/03/01/4904.html print是一个语法结构(language constructs), 他并不是 ...

  8. layui table 超出自动换行

    个人博客 地址:http://www.wenhaofan.com/article/20181120180507 layui 的table的的cell默认是超出hidden的,如果希望超出长度自动换行便 ...

  9. 解决jquery.pjax加载后的异常滚动

    个人博客 地址:http://www.wenhaofan.com/article/20181106154356 在使用jquery.pjax的时候发现每次加载完成后都会将滚动条滚动至顶部,用户体验极不 ...

  10. Linux断网安装jdk1.8

    1.创建目录存放jdk包 mkdir /usr/java 2.上传jdk包 通过xftp或者其他远程工具 3.解压jdk tar zxvf jdk-8u221-linux-x64.tar.gz 4.打 ...