红黑树的插入和创建
import java.util.Random;
public class RBTree<T extends Comparable> {
private RBTreeNode<T> root;
private static final boolean red=true;
private static final boolean black=false;
public void creatRBT(T[] arr)
{
for(int i=0;i<arr.length;i++)
{
this.insertRBT(arr[i]);
}
}
public void insertRBT(T key)
{
RBTreeNode<T> insertNode=new RBTreeNode<>();
insertNode.setKey(key);
insertNode.setColor(red);
this.insertRBT(insertNode,root,null);
}
public static boolean fixed=true;
public boolean insertRBT(RBTreeNode<T> insertNode,RBTreeNode<T> tempNode,RBTreeNode<T> preNode)
{
if(tempNode==null)
{
if(preNode==null) {insertNode.setColor(black);root=insertNode;}
else if(preNode.getKey().compareTo(insertNode.getKey())>0) {preNode.setLeftchild(insertNode);insertNode.setParent(preNode);}
else {preNode.setRightchild(insertNode); insertNode.setParent(preNode);}
fixed=false;
}
else {
if(tempNode.getKey().equals(insertNode.getKey())) { return false;}
if(tempNode.getKey().compareTo(insertNode.getKey())>0)
{
if(!this.insertRBT(insertNode, tempNode.getLeftchild(), tempNode)) return false;
if(!fixed) {
this.fixRBT(insertNode);
fixed=true;
}
}
else {
if(!this.insertRBT(insertNode, tempNode.getRightchild(), tempNode)) return false;
if(!fixed) {
this.fixRBT(insertNode);
fixed=true;
}
}
}
return true;
}
public boolean fixRBT(RBTreeNode<T> targetNode)
{
if(targetNode.getParent()!=null)
{
RBTreeNode<T> parentNode=targetNode.getParent();
if(parentNode.isColor())
{
RBTreeNode<T> gparentNode=parentNode.getParent()!=null?parentNode.getParent():null;
if(gparentNode!=null)
{
if(gparentNode.getLeftchild()!=null&&gparentNode.getLeftchild().equals(parentNode))
{
RBTreeNode<T> broNode=gparentNode.getRightchild();
if(broNode==null||!broNode.isColor())
{
if(parentNode.getLeftchild()!=null&&parentNode.getLeftchild().equals(targetNode))
{
parentNode.setColor(black);
gparentNode.setColor(red);
this.R_Rotate(gparentNode);
//fixed=true;
//break;
}
else
{
targetNode.setColor(black);
gparentNode.setColor(red);
this.L_Rotate(parentNode);
this.R_Rotate(gparentNode);
//fixed=true;
//break;
}
}
else {
parentNode.setColor(black);
broNode.setColor(black);
gparentNode.setColor(red);
targetNode=gparentNode;
this.fixRBT(targetNode);
return true;
//break;
//break;
}
}
else {
RBTreeNode<T> broNode=gparentNode.getLeftchild();
if(broNode==null||!broNode.isColor())
{
if(parentNode.getRightchild()!=null&&parentNode.getRightchild().equals(targetNode))
{
parentNode.setColor(black);
gparentNode.setColor(red);
this.L_Rotate(gparentNode);
//fixed=true;
// break;
}
else
{
targetNode.setColor(black);
gparentNode.setColor(red);
this.R_Rotate(parentNode);
this.L_Rotate(gparentNode);
//fixed=true;
//break;
}
}
else {
parentNode.setColor(black);
broNode.setColor(black);
gparentNode.setColor(red);
targetNode=gparentNode;
this.fixRBT(targetNode);
return true;
//break;
//break;
}
}
}
}
}
this.root.setColor(black);
return true;
}
public void R_Rotate(RBTreeNode<T> targetNode)
{
RBTreeNode<T> lc=targetNode.getLeftchild();
RBTreeNode<T> parentNode=targetNode.getParent();
targetNode.setLeftchild(lc.getRightchild());
if(lc.getRightchild()!=null) lc.getRightchild(). setParent(targetNode);
lc.setRightchild(targetNode);
targetNode.setParent(lc);
targetNode=lc;
if(parentNode==null) {targetNode.setParent(null); root=targetNode;}
else if(parentNode.getKey().compareTo(targetNode.getKey())>0) {targetNode.setParent(parentNode); parentNode.setLeftchild(targetNode);}
else {targetNode.setParent(parentNode); parentNode.setRightchild(targetNode);}
}
public void L_Rotate(RBTreeNode<T> targetNode)
{
RBTreeNode<T> rc=targetNode.getRightchild();
RBTreeNode<T> parentNode=targetNode.getParent();
targetNode.setRightchild(rc.getLeftchild());
if(rc.getLeftchild()!=null) rc.getLeftchild().setParent(targetNode);
rc.setLeftchild(targetNode);
targetNode.setParent(rc);
targetNode=rc;
if(parentNode==null) {targetNode.setParent(null); root=targetNode;}
else if(parentNode.getKey().compareTo(targetNode.getKey())>0) {targetNode.setParent(parentNode);parentNode.setLeftchild(targetNode);}
else {targetNode.setParent(parentNode); parentNode.setRightchild(targetNode);}
}
//中序遍历
public void inOrderVisitRBT()
{
this.inOrderVisitRBT(root);
System.out.println();
}
private void inOrderVisitRBT(RBTreeNode tempNode)
{
if(tempNode!=null)
{
//System.out.println("111");
this.inOrderVisitRBT(tempNode.getLeftchild());
System.out.print(tempNode.getKey()+" ");
this.inOrderVisitRBT(tempNode.getRightchild());
}
}
//先序遍历
public void preVisitRBT()
{
this.preVisitRBT(root);
System.out.println();
}
public void preVisitRBT(RBTreeNode tempNode)
{
if(tempNode!=null)
{
System.out.print(tempNode.getKey()+" "+tempNode.isColor()+" ");
this.preVisitRBT(tempNode.getLeftchild());
this.preVisitRBT(tempNode.getRightchild());
}
}
public static void main(String[] args)
{
Integer[] arr=new Integer[10];
for(int i=0;i<10;i++)
{
arr[i]=new Random().nextInt(100);
System.out.println("arr["+i+"]="+arr[i]);
}
RBTree<Integer> rbt=new RBTree<>();
rbt.creatRBT(arr);
rbt.inOrderVisitRBT();
rbt.preVisitRBT();
}
}
class RBTreeNode<T extends Comparable>
{
private T key;
private RBTreeNode<T> leftchild,rightchild,parent;
private boolean color;//false为黑,true为红
public T getKey() {
return key;
}
public void setKey(T key) {
this.key = key;
}
public RBTreeNode<T> getLeftchild() {
return leftchild;
}
public void setLeftchild(RBTreeNode<T> leftchild) {
this.leftchild = leftchild;
}
public RBTreeNode<T> getRightchild() {
return rightchild;
}
public void setRightchild(RBTreeNode<T> rightchild) {
this.rightchild = rightchild;
}
public RBTreeNode<T> getParent() {
return parent;
}
public void setParent(RBTreeNode<T> parent) {
this.parent = parent;
}
public boolean isColor() {
return color;
}
public void setColor(boolean color) {
this.color = color;
}
}
红黑树的插入和创建
最新推荐文章于 2020-07-30 23:46:42 发布