第三十三篇 玩转数据结构——红黑树(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的路径上节点着色方式的显示,红黑树确保所有路径的 ...
随机推荐
- Api跨域设置
跨域设置:(服务端) webconfig文件中,system.webServer节点下添加 <!--跨域请求:三个配置信息--> <httpProtocol> <cust ...
- jQuery Moblie 问题汇总
1 使用jQuery动态添加html,没有jQuery Moblie的样式 $("body").html(listview);//以上代码只是把结构加上去了,但是却没有加上jqm ...
- 使用IntersectionObserver制作滚动动画以及其他记录
前言 最近在重做公司项目的主页,正好新来了个UI,整个都重新设计了一下,动画还挺多的.我之前没有怎么玩过这些,踩了挺多坑,最后找到了目前而言最合适的方法,现在做一个记录. 需要把原来的主页从项目中抽出 ...
- 中文 json_encode之后字符长度问题
问题描述: 将某个字符串$str 进行json编码,即json_encode($str)后变成Unicode字符存入数据库,会发现中文的长度明明没有超过设置的字符长度最大值,但是却抛出字段长度过长错误 ...
- Selenium3+python自动化016-多线程
1.进程 什么是进程? 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基 ...
- 回文串--Manacher算法(模板)
用途:在O(n)时间内,求出以每一个点为中心的回文串长度. 首先,有一个非常巧妙的转化.由于回文串长度有可能为奇数也有可能为偶数,说明回文中心不一定在一个字符上.所以要将字符串做如下处理:在每两个字母 ...
- Git和TortoiseGit
1.简介 Git是一个开源的分布式版本控制系统,用于敏捷高效的处理任何或大或小的项目.它采用了分布式版本库的方式,不必服务器端软件支持. 2.Git和Svn的区别 1.Git 是分布式的,SVN 不是 ...
- 自定义React-redux
实现mini版react-redux 1. 理解react-redux模块 1). react-redux模块整体是一个对象模块 2). 包含2个重要属性: Provider和connect 3). ...
- C语言库函数strstr、strch比较
该库函数包含在<string.h>头文件中,函数原型:extern char *strstr(char *str1, const char *str2);使用方法 char *strstr ...
- Coxeter积分计算
\begin{align*}&\int_0^{\frac{\pi}{3}}{\arccos \left( \frac{1-\cos x}{\text{2}\cos x} \right) dx} ...







