红黑树-自己写的红黑树插入实现

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;
	
}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值