最近复习了一下数据结构,用java实现了avl树和rb树,以下是代码,不对之处,请您斧正
//下面的是用于输入数据的实现类
//author: yxjcaesar
import java.io.*;
public class IO{
public static PrintStream p=System.out;
public static InputStream in=System.in;
public static int[] readInts(){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String s=br.readLine();
String[] ss=s.split(" ");
int[] re=new int[ss.length];
for(int i=0;i<ss.length;i++){
re[i]=Integer.parseInt(ss[i]);
}
return re;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
//下面的是RB树的实现代码
//author: yxjcaesar
import java.util.*;
public class RBTree{
Node tree=new Node(-1,Node.B);
int c=0;
int tc=0;
int c1=0;
int c2=0;
Node c3=null;
/**insert(int) 插入数据
*@author yxjcaesar
*/
public boolean insert(int s){
boolean bool=insert(this.tree,s);
tree.color=Node.B;
return bool;
}
private boolean insert(Node t,int s){
if(t.data==-1){
t.data=s;
t.color=Node.R;
t.lChild=new Node(-1,Node.B);
t.rChild=new Node(-1,Node.B);
c=2;
return true;
}else if(t.data>s){
boolean b=insert(t.lChild,s);
if(b&&c!=0){
switch(c){
case 2: tc=0;if(t.color==Node.B)c=0;else c--;break;
case 1: rbBalance(t,0);break;
}
}
return b;
}else if(t.data<s){
boolean b=insert(t.rChild,s);
if(b&&c!=0){
switch(c){
case 2:tc=1;if(t.color==Node.B)c=0;else c--;break;
case 1:rbBalance(t,1);break;
}
}
return b;
}
return false;
}
private void rbBalance(Node t,int d){
c=0;
if(t.lChild.color==Node.R&&t.rChild.color==Node.R){
t.color=Node.R;
t.lChild.color=t.rChild.color=Node.B;
c=2;
}else{
if(d==0){
switch(tc){
case 0: rRotate(t);break;
case 1: lRotate(t.lChild);rRotate(t);break;
}
}
else{
switch(tc){
case 0: rRotate(t.rChild);lRotate(t);break;
case 1: lRotate(t);break;
}
}
t.lChild.color=t.rChild.color=Node.R;
}
}
private void lRotate(Node t){
Node p=t.rChild;
int temp=p.data;
p.data=t.data;
t.data=temp;
t.rChild=p.rChild;
p.rChild=p.lChild;
p.lChild=t.lChild;
t.lChild=p;
}
private void rRotate(Node t){
Node p=t.lChild;
int temp=t.data;
t.data=p.data;
p.data=temp;
t.lChild=p.lChild;
p.lChild=p.rChild;
p.rChild=t.rChild;
t.rChild=p;
}
/**show()用来按顺序显示数据
*/
public void show(){
IO.p.println("The natural order is: ");
Node[] stack=new Node[100];
int top=0;
if(this.tree.data!=-1)
stack[top++]=this.tree;
while(top>0){
Node n1;
while((n1=stack[top-1]).data!=-1) stack[top++]=n1.lChild;
top--;
if(top>0){
Node n2=stack[--top];
IO.p.print("<"+n2.data+","+n2.color()+"> ");
stack[top++]=n2.rChild;
}
}
IO.p.println();
}
/**gShow()用凹凸法显示树
*/
public void gShow(){
IO.p.println("This tree's view is:");
Node[] n=new Node[100];
int[] is=new int[100];
int top=0;
n[top++]=this.tree;
while(top>0){
switch(is[top-1]){
case 0: IO.p.println(bank(top)+n[top-1].data);is[top-1]++;
if(n[top-1].lChild!=null){n[top]=n[top-1].lChild;top++;}break;
case 1: is[top-1]++;if(n[top-1].rChild!=null){n[top]=n[top-1].rChild;top++;}break;
case 2: is[--top]=0;
}
}
}
private String bank(int i){
String s="";
while(i>1){s+=".";i--;}
return s;
}
/**blackH()用来计算黑色高度
*/
public void blackH(){
IO.p.println("This tree black height is:"+blackH(this.tree));
}
private int blackH(Node t){
if(t.data!=-1){
int h=0;
if(t.color==Node.R) h=0; else h=1;
int l=blackH(t.lChild);
int r=blackH(t.rChild);
if(l==-1||r==-1||l!=r) return -1;
else return h+l;
}
return 0;
}
/**delete(int)用来删除数据
*/
public boolean delete(int s){
Node p=new Node(-1);
p.rChild=this.tree;
boolean b=delete(this.tree,p,1,s);
this.tree=p.rChild;
this.tree.color=Node.B;
return b;
}
private boolean delete(Node t,Node p,int d,int s){
if(t.data==-1) return false;
if(t.data>s){
boolean b=delete(t.lChild,t,0,s);
if(b&&c2==0){
if(c3.color==Node.R){c3.color=Node.B;c2=1;}
else{
leftQuit(t);
}
}
return b;
}else if(t.data<s){
boolean b=delete(t.rChild,t,1,s);
if(b&&c2==0){
if(c3.color==Node.R){c3.color=Node.B;c2=1;}
else{
rightQuit(t);
}
}
return b;
}else{
if(t.lChild.data!=-1&&t.rChild.data!=-1){
Node t1=t.lChild;
while(t1.rChild.data!=-1) t1=t1.rChild;
t.data=t1.data;
t1.data=s;
delete(t.lChild,t,0,s);
if(c2==0){
if(c3.color==Node.R){c3.color=Node.B;c2=1;}
else leftQuit(t);
}
}else{
c2=0;
c3=t.lChild;
if(t.lChild.data==-1) {c3=t.rChild;}
if(d==0) {p.lChild=c3;} else {p.rChild=c3;}
if(t.color==Node.R) {c2=1;}
}
return true;
}
}
private void leftQuit(Node t){
Node t1=t.rChild;
Node t11=t1.lChild,t12=t1.rChild;
if(t1.color==t11.color&&t11.color==t12.color&&t12.color==Node.B){
t1.color=Node.R;c3=t;c2=0;
return;
}
if(t1.color==Node.R){
lRotate(t);
Node t2=t.lChild.rChild;
Node t21=t2.lChild,t22=t2.rChild;
if(t21.color==t22.color&&t22.color==Node.B){
t2.color=Node.R;
c3=t.lChild;c3.color=Node.B;
}
else{
if(t22.color==Node.R){
t22.color=Node.B;
lRotate(t.lChild);
}else{
t21.color=Node.B;
rRotate(t.lChild.rChild);
lRotate(t.lChild);
}
}
c2=1;return;
}
if(t12.color==Node.R){
t12.color=Node.B;
lRotate(t);
}else{
t11.color=Node.B;
rRotate(t1);
lRotate(t);
}
c2=1;
}
private void rightQuit(Node t){
Node t1=t.lChild;
Node t11=t1.lChild,t12=t1.rChild;
if(t1.color==t11.color&&t11.color==t12.color&&t12.color==Node.B){
t1.color=Node.R;c3=t;c2=0;
return;
}
if(t1.color==Node.R){
rRotate(t);
Node t2=t.rChild.lChild;
Node t21=t2.lChild,t22=t2.rChild;
if(t21.color==t22.color&&t22.color==Node.B){
t2.color=Node.R;
c3=t.rChild;c3.color=Node.B;
}
else{
if(t21.color==Node.R){
t21.color=Node.B;
rRotate(t.rChild);
}else{
t22.color=Node.B;
lRotate(t.rChild.lChild);
rRotate(t.rChild);
}
}
c2=1;return;
}
if(t11.color==Node.R){
t11.color=Node.B;
rRotate(t);
}else{
t12.color=Node.B;
lRotate(t1);
rRotate(t);
}
c2=1;
}
public static void main(String[] args){
RBTree rbt=new RBTree();
IO.p.print("Input the datas one by one and split by space:");
int[] is=IO.readInts();//用来输入多个数据,用空格分开
for(int i:is){
rbt.insert(i);
}
rbt.show();
rbt.gShow();
rbt.blackH();
IO.p.print("The delete node: ");
is=IO.readInts();//输入要删除的数据,单个
while(is[0]!=0){
rbt.delete(is[0]);
rbt.show();
rbt.gShow();
rbt.blackH();
IO.p.print("The delete node: ");
is=IO.readInts();
}
}
}
class Node{
static final int R=0,B=1;
int data;
Node lChild;
Node rChild;
int color;
Node(int data){
this.data=data;
}
Node(int data,int color){
this(data);
this.color=color;
}
public String color(){
return color==0?"Red":"Black";
}
}
//下面是avl树的java实现
import java.util.*;
public class AVLTree{
int count;
int[] datas;
Node tree;
int tall;
int low;
private void lRotate(Node t){
Node p=t.rChild;
int temp=p.data;
p.data=t.data;
t.data=temp;
t.rChild=p.rChild;
p.rChild=p.lChild;
p.lChild=t.lChild;
t.lChild=p;
}
private void rRotate(Node t){
Node p=t.lChild;
int temp=t.data;
t.data=p.data;
p.data=temp;
t.lChild=p.lChild;
p.lChild=p.rChild;
p.rChild=t.rChild;
t.rChild=p;
}
//insert(int)用于插入的方法
public boolean insert(int s){
Node p=new Node();
p.rChild=this.tree;
boolean bool=insert(this.tree,p,1,s);
this.tree=p.rChild;
return bool;
}
private boolean insert(Node t,Node p,int d,int s){
if(t==null){
switch(d){
case 0:p.lChild=new Node(s);break;
case 1:p.rChild=new Node(s);break;
}
tall=1;return true;
}else{
if(t.data==s) {tall=0;return false;}
if(t.data>s&&insert(t.lChild,t,0,s)){
if(tall==1){
switch(t.bf){
case 0: tall=1;t.bf=1;break;
case 1: tall=0;leftBalance(t);
case -1:tall=0;t.bf=0;break;
}
}
return true;
}
if(t.data<s&&insert(t.rChild,t,1,s)){
if(tall==1){
switch(t.bf){
case 0: tall=1;t.bf=-1;break;
case 1: tall=0;t.bf=0;
case -1:tall=0;rightBalance(t);
}
}
return true;
}
return false;
}
}
private void leftBalance(Node t){
Node p1=t.lChild;
switch(p1.bf){
case 1:rRotate(t);t.bf=0;t.rChild.bf=0;break;
case -1: int tt=p1.rChild.bf;
lRotate(p1);rRotate(t);t.bf=0;
switch(tt){
case 0:t.lChild.bf=t.rChild.bf=0;break;
case 1:t.lChild.bf=0;t.rChild.bf=-1;break;
case -1:t.lChild.bf=1;t.rChild.bf=0;break;
}
}
}
private void rightBalance(Node t){
Node p1=t.rChild;
switch(p1.bf){
case -1:lRotate(t);t.bf=0;t.lChild.bf=0;break;
case 1: int tt=p1.lChild.bf;
rRotate(p1);lRotate(t);t.bf=0;
switch(tt){
case 0:t.lChild.bf=t.rChild.bf=0;break;
case 1:t.lChild.bf=0;t.rChild.bf=-1;break;
case -1:t.lChild.bf=1;t.rChild.bf=0;break;
}
}
}
/**gShow()用凹凸法显示树
*/
public void gShow(){
IO.p.println("This tree's view is:");
Node[] n=new Node[100];
int[] is=new int[100];
int top=0;
if(this.tree!=null)
n[top++]=this.tree;
while(top>0){
switch(is[top-1]){
case 0: IO.p.println(bank(top)+n[top-1].data);is[top-1]++;
if(n[top-1].lChild!=null){n[top]=n[top-1].lChild;top++;}break;
case 1: is[top-1]++;if(n[top-1].rChild!=null){n[top]=n[top-1].rChild;top++;}break;
case 2: is[--top]=0;
}
}
}
private String bank(int i){
String s="";
while(i>1){s+=".";i--;}
return s;
}
//delete(int)用于删除的方法
public boolean delete(int s){
Node p=new Node();p.rChild=this.tree;
boolean bool=delete(this.tree,p,1,s);
this.tree=p.rChild;
return bool;
}
private boolean delete(Node t,Node p,int d,int s){
if(t==null) {low=0;return false;}
if(t.data==s){
if(t.lChild!=null&&t.rChild!=null){
Node t1=t.lChild;
while(t1.rChild!=null){t1=t1.rChild;}
int temp=t.data;
t.data=t1.data;
t1.data=temp;
delete(t.lChild,t,0,s);
if(low==1){
switch(t.bf){
case 0:t.bf=-1;low=0;break;
case 1:t.bf=0;low=1;break;
case -1:low=1;rightBalance(t);
}
}
}else{
Node t0=null;
if(t.lChild==null)t0=t.rChild;
if(t.rChild==null)t0=t.lChild;
switch(d){
case 0:p.lChild=t0;break;
case 1:p.rChild=t0;break;
}
low=1;
}
return true;
}
if(t.data>s){
if(!delete(t.lChild,t,0,s)) return false;
if(low==1){
switch(t.bf){
case 0:t.bf=-1;low=0;break;
case 1:t.bf=0;low=1;break;
case -1:low=1;rightBalance(t);
}
}
}
if(t.data<s){
if(!delete(t.rChild,t,1,s)) return false;
if(low==1){
switch(t.bf){
case 0: t.bf=1;low=0;break;
case -1:t.bf=0;low=1;break;
case 1:low=1;leftBalance(t);
}
}
}
return false;
}
public void show(){
IO.p.print("The tree's natural order is: ");
Node[] stack=new Node[100];
int top=0;
stack[top++]=this.tree;
while(top>0){
Node n1;
while((n1=stack[top-1])!=null) stack[top++]=n1.lChild;
top--;
if(top>0){
Node n2=stack[--top];
IO.p.print("<"+n2.data+","+n2.bf+"> ");
stack[top++]=n2.rChild;
}
}
IO.p.println();
}
public static void main(String[] args){
AVLTree avl=new AVLTree();
IO.p.print("Input the datas one by one and split by space:");
int[] is=IO.readInts();
for(int i:is) avl.insert(i);
avl.show();
avl.gShow();
IO.p.print("The delete data:");
is=IO.readInts();
while(is[0]!=0){
for(int i:is) avl.delete(i);
avl.show();
avl.gShow();
IO.p.print("The delete data:");
is=IO.readInts();
}
}
}
class Node{
int data;
Node lChild;
Node rChild;
int bf;
Node(int data){
this.data=data;
}
Node(){
}
}