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的更多相关文章

  1. The easy way to implement a Red-Black tree

    Red-Black trees are notorious for being nightmares of pointer manipulation. Instructors will show th ...

  2. 算法导论学习-RED-BLACK TREE

    1. 红黑树(RED-BLACK TREE)引言: ------------------------------------- 红黑树(RBT)可以说是binary-search tree的非严格的平 ...

  3. [Data Structure] 红黑树( Red-Black Tree ) - 笔记

    1.  红黑树属性:根到叶子的路径中,最长路径不大于最短路径的两倍. 2. 红黑树是一个二叉搜索树,并且有 a. 每个节点除了有左.右.父节点的属性外,还有颜色属性,红色或者黑色. b. ( 根属性 ...

  4. [转]SGI STL 红黑树(Red-Black Tree)源代码分析

    STL提供了许多好用的数据结构与算法,使我们不必为做许许多多的重复劳动.STL里实现了一个树结构-Red-Black Tree,它也是STL里唯一实现的一个树状数据结构,并且它是map, multim ...

  5. TreeMap Red-Black tree

    本文以Java TreeMap为例,从源代码层面,结合详细的图解,剥茧抽丝地讲解红黑树(Red-Black tree)的插入,删除以及由此产生的调整过程. 总体介绍 之所以把TreeSet和TreeM ...

  6. 红黑树(red-black tree)实现记录

    https://github.com/xieqing/red-black-tree A Red-black Tree Implementation In C There are several cho ...

  7. 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 ...

  8. PAT 甲级 1135 Is It A Red-Black Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 There is a kind of bal ...

  9. PAT-A1135. Is It A Red-Black Tree (30)

    已知先序序列,判断对应的二叉排序树是否为红黑树.序列中负数表示红色结点,正数表示黑色结点.该序列负数取绝对值后再排序得到的是中序序列.根据红黑树的性质判断它是否符合红黑树的要求.考察了根据先序序列和中 ...

随机推荐

  1. 【winform】listbox 列表

    一.Item 一个ListBox是由一个个的Item项组成的 1.向下添加Item lstResult.Items.Add("***" + PostWebRequest(cbxUr ...

  2. 教你如何在win7中的cygwin64下安装hadoop

    首先我们要准备如下环境及软件: win7(64位) cygwin - jdk-6u25-windows-x64.zip hadoop-.tar.gz 1.在win7系统上正常安装jdk,同时注意设置好 ...

  3. sparkStreaming 与fafka直接方式 进行消费者偏移量的保存如redis 里面 避免代码改变与节点重启后的数据丢失与序列化问题

    import java.util import kafka.common.TopicAndPartition import kafka.message.MessageAndMetadata impor ...

  4. poj1985和poj1849(树的直径)

    题目传送门:poj1985 树是连通无环图,树上任意两点之间的路径是唯一的.定义树上任 意两点u, v的距离为u到v路径上边权的和.树的直径MN为树上最长路 径,即点M和N是树上距离最远的两个点. 题 ...

  5. devm_kzalloc【转】

    本文转载自:https://blog.csdn.net/liuhuahan/article/details/42145507 看内核代码的时候看到这个函数不理解它的具体作用然后就上网上查,但是网上只查 ...

  6. C#方法过滤器

    方法过滤器 使用Emit和注解属性Attribute实现 使用方式 1. 自定义方法过滤器 可分别定义方法执行前过滤器, 方法执行结束过滤器, 方法异常过滤器 执行前过滤器继承 ExecutingFi ...

  7. centos7安装node

    centos7安装node 二进制文件安装 node=v10.13.0 file=node-${node}-linux-x64 wget https://nodejs.org/dist/${node} ...

  8. Yahoo Programming Contest 2019 E - Odd Subrectangles

    E - Odd Subrectangles 思路: 对于行方案固定的情况下,假设和为奇数的列为a个,和为偶数的列为b个,a+b = m 那么从奇数里面选奇数个,即C(a, 1) + C(a, 3) + ...

  9. 无头浏览器phantomJS

    selenium: 有头浏览器的代表(selenium+python也可实现静默运行 引入python的一个包,包叫:虚拟屏幕pyvirtualdisplay) PhantomJS : 无头浏览器的代 ...

  10. maven 项目编译失败

    tomcat 加载项目启动后 提示  找不到web-context配置 或者某一个类加载错误 发生在 从根目录新导入新的 未打开项目之后 install 失败  java 文件未编译成 class 解 ...