the implemention of redblack tree
public class redbalcktree {
private class Node{
private int val;
private int key;
boolean color; //black true
private Node left,right,p;
private int N; //the number of the all nodes of its child nodes and itself
//private int num;//the number
public Node(int val,int key){
this.val = val; this.key = key;
}
}
Node root;
private void leftrotation(Node x){
Node y = x.right;
x.right = y.left;
if(y.left != null){
y.left.p = x;
}
y.p = x.p;
if(x.p == null){
root = y;
}
else if(x == x.p.left){
x.p.left = y;
}
else{x.p.right = y;}
y.left = x;
x.p = y;
}
private void rightrotation(Node y){
Node x = y.left;
y.left = x.right;
if(x.right != null){
x.right.p = y;
}
x.p = y.p;
if(y.p == null){
root = x;
}
else if(y == y.p.left){
y.p.left = x;
}
else{y.p.right = x;}
x.right = y;
y.p = x;
}
public void insert(int val,int key){
Node y = null;
Node x = root;
Node z = new Node(val,key);
if(x == null){root = z;}
while(x != null){
y = x;
if(z.key < x.key){
x = x.left;
}
else{x = x.right;}
}
z.p = y;
if(z.key < y.key){
y.left = z;
}
else{y.right = z;}
z.left = null;
z.right = null;
z.color = false;
inseretfix(z);
}
private void inseretfix(Node z){
Node y;
while (z.p.color == false){
if(z.p == z.p.p.left){
y = z.p.p.right;
if(y.color == false){ //case 1,both z.p and y(uncle) are red
z.p.color = true;
y.color = true;
z.p.p.color = false;
z= z.p.p;
continue;
}
else if(z == z.p.right){ //case 2, z.p is red,uncle is black,z is right,transfer to case 3
z = z.p;
leftrotation(z);
}
z.p.color = true; //case 3, z.p is red,uncle is black,z is left;
z.p.p.color = false;
rightrotation(z.p.p);
}
else{ //set y(uncle) and change rotation
y = z.p.p.left;
if(y.color == false){ //case 1,both z.p and y(uncle) are red
z.p.color = true;
y.color = true;
z.p.p.color = false;
z= z.p.p;
continue;
}
else if(z == z.p.right){ //case 2, z.p is red,uncle is black,z is right,transfer to case 3
z = z.p;
rightrotation(z);
}
z.p.color = true; //case 3, z.p is red,uncle is black,z is left;
z.p.p.color = false;
leftrotation(z.p.p);
}
}
root.color = true;
}
private int get(Node x,int key){
if(x == null){
throw new NullPointerException("have't this key");
}
if(x.key>key){
return get(x.left,key);
}
else if(x.key < key){
return get(x.right,key);
}
else{
return x.val;
}
}
private void transplant(Node u,Node v){ //substitute u as v
if(u.p == null){
root = v;
}
else if(u == u.p.left){
u.p.left = v;
}
else {
u.p.right = v;
}
v.p = u.p;
}
private Node deletemin(Node x){
if(x.left == null){return x.right;}
x.left = deletemin(x.left);
return x;
}
public void delete(int key){
int val = get(root,key);
Node z = new Node(val,key);
Node x;
Node y = z;
boolean original = y.color;
if(z.left == null){
x = z.right;
transplant(z,z.right);
}
else if(z.right == null){
x = z.left;
transplant(z,z.left);
}
else{
y = deletemin(z.right); // next node after z
original = y.color;
x = y.right;
if(y.p == z){
x.p = y;
}
else{
transplant(y,y.right); // put y.right on the original y's position
y.right = z.right;
y.right.p = y;
}
transplant(z,y);
y.left = z.left;
y.left.p = y;
y.color = z.color;
}
if(original == true){
deletefix(x);
}
}
private void deletefix(Node x) {
Node other;
while ((x.color = true) && (x != root)) {
if (x.p.left == x) {
other = x.p.right;
if (other.color == false) {
// Case 1: x's brother is red
other.color = true;
x.p.color = false;
leftrotation(x.p);
other = x.p.right;
}
if ((other.left.color == true) &&
(other.right.color == true) ){
// Case 2: x's black is black and w 's both child are black
other.color = false;
x = x.p;
continue;
} else if (other.right.color == true) {
// Case 3: transfer to case 4
other.left.color = true;
other.color = false;
rightrotation(other);
other = x.p.right;
}
// Case 4: x's black is black,w's right child is red ,left anyway
other.color = x.p.color;
x.p.color = true;
other.right.color = true;
leftrotation(x.p);
x = root;
break;
}
else {
other = x.p.left;
if (other.color == false) {
// Case 1: x's brother is red
other.color = true;
x.p.color = false;
rightrotation(x.p);
other = x.p.right;
}
if ((other.left.color == true) &&
(other.right.color == true) ){
// Case 2: x's black is black and w 's both child are black
other.color = false;
x = x.p;
continue;
} else if (other.right.color == true) {
// Case 3: transfer to case 4
other.left.color = true;
other.color = false;
leftrotation(other);
other = x.p.right;
}
// Case 4: x's black is black,w's right child is red ,left anyway
other.color = x.p.color;
x.p.color = true;
other.right.color = true;
rightrotation(x.p);
x = root;
break;
}
}
}
}
the implemention of redblack tree的更多相关文章
- The easy way to implement a Red-Black tree
Red-Black trees are notorious for being nightmares of pointer manipulation. Instructors will show th ...
- 算法导论学习-RED-BLACK TREE
1. 红黑树(RED-BLACK TREE)引言: ------------------------------------- 红黑树(RBT)可以说是binary-search tree的非严格的平 ...
- [Data Structure] 红黑树( Red-Black Tree ) - 笔记
1. 红黑树属性:根到叶子的路径中,最长路径不大于最短路径的两倍. 2. 红黑树是一个二叉搜索树,并且有 a. 每个节点除了有左.右.父节点的属性外,还有颜色属性,红色或者黑色. b. ( 根属性 ...
- [转]SGI STL 红黑树(Red-Black Tree)源代码分析
STL提供了许多好用的数据结构与算法,使我们不必为做许许多多的重复劳动.STL里实现了一个树结构-Red-Black Tree,它也是STL里唯一实现的一个树状数据结构,并且它是map, multim ...
- TreeMap Red-Black tree
本文以Java TreeMap为例,从源代码层面,结合详细的图解,剥茧抽丝地讲解红黑树(Red-Black tree)的插入,删除以及由此产生的调整过程. 总体介绍 之所以把TreeSet和TreeM ...
- 红黑树(red-black tree)实现记录
https://github.com/xieqing/red-black-tree A Red-black Tree Implementation In C There are several cho ...
- A1135. Is It A Red-Black Tree
There is a kind of balanced binary search tree named red-black tree in the data structure. It has th ...
- PAT 甲级 1135 Is It A Red-Black Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 There is a kind of bal ...
- PAT-A1135. Is It A Red-Black Tree (30)
已知先序序列,判断对应的二叉排序树是否为红黑树.序列中负数表示红色结点,正数表示黑色结点.该序列负数取绝对值后再排序得到的是中序序列.根据红黑树的性质判断它是否符合红黑树的要求.考察了根据先序序列和中 ...
随机推荐
- Object类型的转为String类型
Map<String, Object> scaleMap = new HashMap(): scaleMap.put("name","张三"); S ...
- Shell 脚本格式注意事项
if 条件判断格式 if [ ! -f file.txt ];then cmd else cmd fi 注1:! 代表非.不存在文件就成功. 注2:再有参数 变量 需要 [] 阔起 1 运算书写写格式 ...
- JS设计模式(8)模板方法模式
什么是模板方法模式? 定义:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 主要解决:一些方法通用,却在每一个子类都重新 ...
- 缓存算法(FIFO 、LRU、LFU三种算法的区别)
FIFO算法 FIFO 算法是一种比较容易实现的算法.它的思想是先进先出(FIFO,队列),这是最简单.最公平的一种思想,即如果一个数据是最先进入的,那么可以认为在将来它被访问的可能性很小.空间满的时 ...
- parquet 简介
原文 Parquet 列式存储格式 面向分析型业务的列式存储格式 由 Twitter 和 Cloudera 合作开发,2015 年 5 月从 Apache 的孵化器里毕业成为 Apache 顶级项目 ...
- 【Bilinear interpolation】双线性插值详解(转)
最近在做视频拼接的项目,里面用到了图像的单应性矩阵变换,在最后的图像重映射,由于目标图像的坐标是非整数的,所以需要用到插值的方法,用的就是双线性插值,下面的博文主要是查看了前辈的博客对双 ...
- 【Luogu P2664】树上游戏
Problem Description \(lrb\) 有一棵树,树的每个节点有个颜色.给一个长度为 \(n\) 的颜色序列,定义 \(s(i,j)\) 为 \(i\) 到 \(j\) 的颜色数量.以 ...
- Git 与 GitHub 入门级
今天我们来搞一下Git 这东西虽然没啥搞头儿,但是开发当中还必须得会用,谁让你我都是苦逼的开发呢~~~~ 一.下载与安装 这玩意简单,给你赋个图片,自己研究一下~~~~ 1.官网:https://gi ...
- python检测服务器端口
import socket sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.settimeout(10) try: sk.conne ...
- Rancher2.0中邮件通知的设置
1-邮件通知的设置-中国电信189邮箱 2-2-邮件通知的设置-腾讯免费企业邮箱 **说明:网易163邮箱.QQ邮箱没有设置成功,可能是因为邮箱设置得太安全的缘故. 参考链接: 中国电信189邮箱 ...