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. 在javascript编程语言中,数据类型boolean的相关知识

    一. 1.字符串类型: 空字符串返回false,非空字符串均返回true; 2.数值类型: 0或NaN返回false,其他数值返回true; 3.布尔类型: false返回false,true返回tr ...

  2. BZOJ#2121. 字符串游戏 [区间dp]

    // powered by c++11 // by Isaunoya #include<bits/stdc++.h> #define rep(i , x , y) for(register ...

  3. CQOI跳舞(网络流+二分答案)

    题面见 https://www.luogu.org/problemnew/show/P3153 题意简述:有n个男生,n个女生,每一首歌时两位男女配对,然后同一对男女只能跳一场,一个人只会与不喜欢的人 ...

  4. Android开发长按菜单上下文菜单

    安卓开发中长按弹出菜单的创建方法: 1.首先给View注册上下文菜单registerForContextMenu(); 2.添加上下文菜单内容onCreateContextMenu(): ---可以通 ...

  5. 4级搭建类401-Oracle 19c Non-CDB DG搭建(Linux 主备一对一 LGWR ASYNC)公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列除特定项目目前不对外发布,仅作为博客记录,其他公开.如学员在 ...

  6. Docker 镜像仓库为什么要分库分权限?

    先说一个事故案例: 场景:某大型互联网电商公司,使用一个镜像仓库管理所有Docker镜像.开发者打出的镜像上传到唯一的镜像库,测试通过后,运维环境的 Kubernetes 直接从这个库里拉取镜像,所有 ...

  7. 如何使用Mbp模块构建应用.

    上一篇文章https://www.cnblogs.com/mbpframework/p/12073102.html,介绍了一下Mbp的框架.其实这个框架写出来主要是为了学习,当然也可以经过优化运用到实 ...

  8. 风变编程笔记(一)-Python基础语法

    第0关  print()函数与变量 1. print()函数print()函数:告诉计算机,把括号的内容显示在屏幕上 # 不带引号 print(1+1) # 让计算机读懂括号里的内容,打印最终的结果 ...

  9. 备份Sql Server中的某些表

    第一步:右键需要备份表的数据库 第二步:选择=>选择特定数据库对象,在下方选择你需要备份的数据表. 第三步,点击高级,在要编写脚本的数据的类型中选择架构和数据(看个人需要),根据需要可更换生成的 ...

  10. 多个iframe,删除详情页时刷新同级iframe的table list

    说明:在使用iframe开发时,经常遇到多个iframe之间的操作. 下面就是一个需求:在一个iframe中关闭时,刷新指定的iframe: 添加需要刷新的标识reload=true //添加npi2 ...