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)
已知先序序列,判断对应的二叉排序树是否为红黑树.序列中负数表示红色结点,正数表示黑色结点.该序列负数取绝对值后再排序得到的是中序序列.根据红黑树的性质判断它是否符合红黑树的要求.考察了根据先序序列和中 ...
随机推荐
- Java爬取B站弹幕 —— Python云图Wordcloud生成弹幕词云
一 . Java爬取B站弹幕 弹幕的存储位置 如何通过B站视频AV号找到弹幕对应的xml文件号 首先爬取视频网页,将对应视频网页源码获得 就可以找到该视频的av号aid=8678034 还有弹幕序号, ...
- ERROR 1666 (HY000): Cannot execute statement: impossible to write to binary log since statement is in row format and BINLOG_FORMAT = STATEMENT.
centos7.5 binlog恢复数据失败 问题: mysql> \. /tmp/inc.sql ERROR 1050 (42S01): Table 'new_1' already exist ...
- SIP 编解码器
编解码器,编码器 - 解码器的简称,做两个基本操作 - 首先,它将模拟语音信号转换为其等效数字形式,以便可以容易地发送. 此后,它将压缩的数字信号转换回其原始模拟形式,以便可以重放. 市场上有许多编解 ...
- 【SQL Server 问题记录】A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.
本文涉及的相关问题,如果你的问题或需求有与下面所述相似之处,请阅读本文 A network-related or instance-specific error occurred while esta ...
- CBV与FBV
FBV FBV(function base views)就是在 url 中一个路径对应一个函数 urls.py urlpatterns = [ url(r'^admin/', admin.site.u ...
- QuerySet API
模型objects:这个对象是 django.db.model.manager.Manger 的对象,这个类是一个空壳类,它上面的所有方法都是从 QuerySet 这个类中拷贝过来的. >> ...
- pip安装第三方库镜像源选择
在pip安装时,有些库速度及其缓慢从而导致失败,可以通过更改镜像源的方式来安装. 我在安装的时候使用了清华的镜像源,格式如下: 想要安装什么库就在后面替换即可.
- MDK编译过程
原博文:https://blog.csdn.net/qq_33894122/article/details/83994564
- android -------- 获取手机设备信息
最近在开发中,需要用到一些系统信息,总结了一下 /** * Created by zhangqie on 2019/2/26 * Describe: 系统工具类 */ public class Equ ...
- 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)
原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ from:ht ...