public class RedandBlack {
private class Node{
private boolean color=false; private int value; private Node left; private Node right;
}//存储节点
private Node head; private int black; private int temp=0; private int temp1=0; private Node intro;
private int length=0; private boolean isTree=false; private Node black_Node;
public void add(int value) {
length++; intro=new Node(); intro.value=value;
if(head==null){
head=intro; intro.color=true;
}//长度为零的情况
else{
if(length==2){add(head);}//当长度为2时
if(length!=2){
if(length>3)
change_color();
add(head);
}//当长度等于2时只增加节点,大于3时变色增加节点
if(length==3&&isTree){
isTree=false;
if(head.left==null){
if(value>head.right.value){head.left=intro;head.left.value=head.value; head.value=head.right.value;head.right.value=value;
head.left.color=true;head.right.color=true;}
else if(value<head.right.value){head.left=intro; head.left.color=true;head.right.color=true;}
else{head.left=intro;head.left.value=head.value;head.value=value;head.left.color=true;head.right.color=true;}
}//当长度为3时需要要移动左为空时讨论
if(head.right==null){
if(value>head.left.value){head.right=intro;head.right.value=head.value; head.value=head.left.value;head.left.value=value;
head.right.color=true;head.left.color=true;}
else if(value<head.left.value){head.right=intro; head.right.color=true;head.left.color=true;}
else{head.right=intro;head.right.value=head.value;head.value=value;head.right.color=true;head.left.color=true;}
}//当长度为3时需要要移动右为空时讨论
} //实现长度为3时移动
if(length>3&&isTree){
isTree=false;
change_color();
isBlack(head);
temp=value;
if(black>value)
{removeRight(head);
if(black_Node.right==null&&black_Node.left!=null){
black_Node.right=intro; black_Node.right.value=black;
}
else if(black_Node.left==null&&black_Node.right!=null){
black_Node.left=intro; black_Node.left.value=black_Node.value; black_Node.value=black;
}
else{ black_Node.right=intro; black_Node.right.value=black;
}
}//对移动到节点的值即黑节点大于插入点时讨论,向右移动加黑点处增加节点情况。
if(black<value){
removeLeft(head);
if(black_Node.left==null&&black_Node.right!=null){
black_Node.left=intro; black_Node.left.value=black;
}
else if(black_Node.right==null&&black_Node.left!=null){
black_Node.right=intro; black_Node.right.value=black_Node.value; black_Node.value=black;
}
else{black_Node.left=intro; black_Node.left.value=black;
}
}//对移动到节点的值即黑节点大于插入点时讨论,向右移动加黑点处增加节点情况。
}//当长度大于3时讨论情况
}//长度不为零的情况
}//增加节点
private int add(Node head ){
if(head.value>intro.value){
if(head.left==null){
if(head.color)
head.left=intro;
else{
isTree=true;
}
}
else{add(head.left); }
}//插入点小于节点值时
else if(head.value<intro.value){
if(head.right==null){
if(head.color)
head.right=intro;
else{
isTree=true;
}
}
else{add(head.right); }
}//插入点大于节点值时
else{
}
return intro.value;
}//辅助增加节点
public void change_color(){
if(head.left!=null&&head.right!=null){
if(!head.left.color&&!head.right.color){
head.left.color=true;head.right.color=true;
}
}//对头节点左右节点变色
change_color(head);
if(head.left!=null&&head.right!=null){
if(!head.left.color&&!head.right.color){
head.left.color=true;head.right.color=true;
}
}//再次对头节点左右节点变色,不知道是否重复了
}//变色
private void change_color(Node head){
if(head.left!=null){
if(head.left.left!=null|head.left.right!=null){
change_color(head.left);
}
if(head.right!=null){
if(head.right.left!=null|head.right.right!=null){
change_color(head.right);
}
}
}
if(head.color){
if(head.left!=null){
if(head.left.left!=null&&head.left.right!=null){
if(!head.left.left.color&&!head.left.right.color){
head.left.left.color=true;head.left.right.color=true; head.left.color=false;
}
}
}
//左节点为空讨论
if(head.right!=null){
if(head.right.left!=null&&head.right.right!=null){
if(!head.right.left.color&&!head.right.right.color){
head.right.left.color=true;head.right.right.color=true;head.right.color=false;
}
}
}
//右为空讨论
}//节点为黑色变色讨论
}//辅助变色
private Node isBlack( Node head){
if((head.left==null||head.right==null)&&head.color){
black=head.value;
black_Node=head;
}
if(head.left!=null){isBlack(head.left);}
if(head.right!=null){isBlack(head.right); }
return null;
}//获得黑色插入点
private void removeLeft(Node head){
if(head==null)
return;
if(head.value<intro.value){
removeLeft(head.right);
}
if(head.value<=intro.value&&head.value>=black){
temp1=head.value; head.value=temp;temp=temp1;}
if(head.value>black){
removeLeft(head.left);
}
}//左移动
private void removeRight(Node head) {
if(head==null)
return;
if(head.value>intro.value){
removeRight(head.left);
}
if(head.value>=intro.value&&head.value<=black){
temp1=head.value; head.value=temp;temp=temp1;}
if(head.value<black){
removeRight(head.right);
}
}//右移动
public void tree_out(){
Snake snake=new Snake();
Snake snake1=new Snake();
if(head!=null)
System.out.print(head.value+"\n");
if(head.left!=null){
System.out.print(head.left.value);
System.out.print(head.left.color);
snake.add(head.left);}
if(head.right!=null){
System.out.print(head.right.value);
System.out.print(head.left.color);
snake.add(head.right);}
System.out.print("\n");
while(snake.IsEmply()||snake1.IsEmply()){
while(snake.IsEmply()){
if(((Node)snake.get()).left!=null){
System.out.print(((Node)snake.get()).left.value);
System.out.print(((Node)snake.get()).left.color);
snake1.add(((Node)snake.get()).left);}
if(((Node)snake.get()).right!=null){
System.out.print(((Node)snake.get()).right.value);
System.out.print(((Node)snake.get()).right.color);
snake1.add(((Node)snake.get()).right); }
snake.next();}
System.out.println("\n");
while(snake1.IsEmply()){
if(((Node)snake1.get()).left!=null){
System.out.print(((Node)snake1.get()).left.value);
System.out.print(((Node)snake1.get()).left.color);
snake.add(((Node)snake1.get()).left);}
else{System.out.print(0);}
if(((Node)snake1.get()).right!=null){
System.out.print(((Node)snake1.get()).right.value);
System.out.print(((Node)snake1.get()).right.color);
snake.add(((Node)snake1.get()).right); }
else{System.out.print(0);}
snake1.next();
}
System.out.println("\n");
}//遍历整个树
}//输出测试使用
}
输出测试用到的测试类snake
public class Snake {
private class Node{
private Object value;
Node next;
}
private Node head;private Node tail;private Node temp;
public void add(Object value){
temp=new Node();temp.value=value;
if(head==null){head=temp; tail=temp;}
else{head.next=temp;
head=temp;
}
}
public boolean IsEmply(){
if(tail==null){
head=null;
return false;}
return true;
}
public Object get(){
return tail.value;
}
public Node next(){
if(!IsEmply()){
return null;}
tail=tail.next;
return tail;
}
}