第三十三篇 玩转数据结构——红黑树(Read Black Tree)
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)的更多相关文章
- java数据结构——红黑树(R-B Tree)
红黑树相比平衡二叉树(AVL)是一种弱平衡树,且具有以下特性: 1.每个节点非红即黑; 2.根节点是黑的; 3.每个叶节点(叶节点即树尾端NULL指针或NULL节点)都是黑的; 4.如图所示,如果一个 ...
- 第二十三篇 玩转数据结构——栈(Stack)
1.. 栈的特点: 栈也是一种线性结构: 相比数组,栈所对应的操作是数组的子集: 栈只能从一端添加元素,也只能从这一端取出元素,这一端通常称之为"栈顶": 向栈中添加元 ...
- 第三十二篇 玩转数据结构——AVL树(AVL Tree)
1.. 平衡二叉树 平衡二叉树要求,对于任意一个节点,左子树和右子树的高度差不能超过1. 平衡二叉树的高度和节点数量之间的关系也是O(logn) 为二叉树标注节点高度并计算平衡因子 AVL ...
- 第三十一篇 玩转数据结构——并查集(Union Find)
1.. 并查集的应用场景 查看"网络"中节点的连接状态,这里的网络是广义上的网络 数学中的集合类的实现 2.. 并查集所支持的操作 对于一组数据,并查集主要支持两种操作:合并两 ...
- 第三十篇 玩转数据结构——字典树(Trie)
1.. Trie通常被称为"字典树"或"前缀树" Trie的形象化描述如下图: Trie的优势和适用场景 2.. 实现Trie 实现Trie的业务无 ...
- 第二十九篇 玩转数据结构——线段树(Segment Tree)
1.. 线段树引入 线段树也称为区间树 为什么要使用线段树:对于某些问题,我们只关心区间(线段) 经典的线段树问题:区间染色,有一面长度为n的墙,每次选择一段墙进行染色(染色允许覆盖),问 ...
- 树-红黑树(R-B Tree)
红黑树概念 特殊的二叉查找树,每个节点上都有存储位表示节点的颜色是红(Red)或黑(Black).时间复杂度是O(lgn),效率高. 特性: (1)每个节点或者是黑色,或者是红色. (2)根节点是黑色 ...
- 红黑树(R-B Tree)
R-B Tree简介 R-B Tree,全称是Red-Black Tree,又称为“红黑树”,它一种特殊的二叉查找树.红黑树的每个节点上都有存储位表示节点的颜色,可以是红(Red)或黑(Black). ...
- 笔试算法题(51):简介 - 红黑树(RedBlack Tree)
红黑树(Red-Black Tree) 红黑树是一种BST,但是每个节点上增加一个存储位表示该节点的颜色(R或者B):通过对任何一条从root到leaf的路径上节点着色方式的显示,红黑树确保所有路径的 ...
随机推荐
- BZOJ2780(广义后缀自动机,set启发式合并)
BZOJ2780(广义后缀自动机,set启发式合并) 题面 自己找去 HINT 就是给多个文本串,然后每次查询的时候问你这个串在多少个文本串中出现过.因为多个文本串,那么直接就往广义后缀自动机上思考啊 ...
- Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】
本文实例讲述了Java实现的上传并压缩图片功能.分享给大家供大家参考,具体如下: 先看效果: 原图:1.33M 处理后:27.4kb 关键代码: package codeGenerate.util; ...
- Codeforces Round #616 (Div. 2) C. Mind Control
题目链接:http://codeforces.com/contest/1291/problem/C 思路: 我们可以很容易想到,只有前m-1个人才能影响m的选择的大小,后面的人无法影响. 如果所有人都 ...
- 【Unity|C#】基础篇(20)——枚举器与迭代器(IEnumerable/IEnumerator)
[学习资料] <C#图解教程>(第18章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...
- 深信服SCSA
最近应公司要求报了SCSA课程,老师讲解的可谓是非常的专业以及详细,不止深信服产品及其配置,还有VPN,网络基础的原理介绍等等,产品不用说,工作中会遇到,能学即学.最好的要数网络发包原理,建立连接等底 ...
- eclipse中配置maven环境
一.配置setting.xml文件 1.首先将下载好的maven打开,打开文件夹,首先就需要对maven安装目录下有个config文件夹,在文件夹下有settings.xml文件.settings里面 ...
- 题解 CF1064A 【Make a triangle!】
题目传送门 反正数学方法我是不会 那只能模拟了一只连模拟题解都看不懂的哀怨 我的思路大体如下 1.定义3个变量a,b,c并输入 int a,b,c; cin>>a>>b> ...
- AE开发常见问题总结
一. Arcgis10.0 组件初始化失败解决方法: ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.EngineOrDe ...
- js中的闭包理解
闭包是一个比较抽象的概念,尤其是对js新手来说.书上的解释实在是比较晦涩,对我来说也是一样. 但是他也是js能力提升中无法绕过的一环,几乎每次面试必问的问题,因为在回答的时候.你的答案的深度,对术语的 ...
- vue学习指南:第十二篇(详细) - Vue的 路由 第二篇 ( 路由按需加载(懒加载))
各位朋友 因 最近工作繁忙,小编停更了一段时间,快过年了,小编祝愿 大家 事业有成 学业有成 快乐健康 2020开心过好每一天.从今天开始 我会抽时间把 Vue 的知识点补充完整,以及后期会带给大家更 ...







