求助,rt,上代码:
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
static Scanner sc=new Scanner(System.in);
static int bound=0;
static class tree{
int root;
int size;
int rep;
tree father;
tree Lchild;
tree Rchild;
public tree(){
this.root=0;
this.rep=1;
this.size=0;
this.father=null;
this.Lchild=null;
this.Rchild=null;
}
public void append(int val){
if(val==this.root){
this.rep++;
this.size++;
return;
}
if(this.isEmpty()){
this.root=val;
}
else{
if(val>this.root){
if(this.Rchild==null){
this.Rchild=new tree();
Rchild.father=this;
}
Rchild.append(val);
}
else{
if(this.Lchild==null){
this.Lchild=new tree();
Lchild.father=this;
}
Lchild.append(val);
}
}
this.size++;
}
public boolean isFull(){
return(this.Lchild!=null && this.Rchild!=null);
}
public boolean isEmpty(){
int m=this.size;
return(m==0);
}
public boolean isLeaf(){
return(this.Lchild==null && this.Rchild==null);
}
public int getLChildSize(){
if(this.Lchild==null){
return(0);
}
else{
return(this.Lchild.size);
}
}
public int getRChildSize(){
if(this.Rchild==null){
return(0);
}
else{
return(this.Rchild.size);
}
}
public int getNum(int pos){
if(pos>this.getLChildSize() && pos<=this.getLChildSize()+this.rep){
return(this.root);
}
else if(pos<=this.getLChildSize()){
return(Lchild.getNum(pos));
}
else{
pos-=this.rep;
pos-=this.getLChildSize();
return(Rchild.getNum(pos));
}
}
public int getPos(int num){
if(num==this.root){
if(this.Lchild==null){
return(1);
}
return(this.Lchild.size+1);
}
else if(num<this.root){
return(this.Lchild.getPos(num));
}
else{
if(this.Lchild==null){
return(this.rep+this.Rchild.getPos(num));
}
return(this.Lchild.size+this.rep+this.Rchild.getPos(num));
}
}
public int getPrev(){
if(this.Lchild==null){
if(this.father.root<this.root){
return(this.father.root);
}
else{
return(-2147483647);
}
}
else{
return(this.Lchild.getNum(this.Lchild.size));
}
}
public int getPrev(int num){
if(num==this.root){
return(this.getPrev());
}
else if(num<this.root){
try{
return(this.Lchild.getPrev(num));
}
catch(NullPointerException ex){
return(-2147483647);
}
}
else{
try{
return(this.Rchild.getPrev(num));
}
catch(NullPointerException ex){
return(-2147483647);
}
}
}
public int getNext(){
if(this.Rchild==null){
if(this.father.root>this.root){
return(this.father.root);
}
else{
return(-2147483647);
}
}
else{
return(this.Rchild.getNum(1));
}
}
public int getNext(int num){
if(num==this.root){
return(this.getNext());
}
else if(num<this.root){
try{
return(this.Lchild.getNext(num));
}
catch(NullPointerException ex){
return(-2147483647);
}
}
else{
try{
return(this.Rchild.getNext(num));
}
catch(NullPointerException ex){
return(-2147483647);
}
}
}
}
public static void main(String[] args){
tree home=new tree();
int count=sc.nextInt();
int[] comm=new int[count];
int[] param=new int[count];
for(int i=0;i<count;i++){
comm[i]=sc.nextInt();
param[i]=sc.nextInt();
}
for(int i=0;i<count;i++){
inner:switch(comm[i]){
case(1):{
System.out.println(home.getPos(param[i]));
break inner;
}
case(2):{
System.out.println(home.getNum(param[i]));
break inner;
}
case(3):{
System.out.println(home.getPrev(param[i]));
break inner;
}
case(4):{
System.out.println(home.getNext(param[i]));
break inner;
}
case(5):{
home.append(param[i]);
break inner;
}
}
}
}
}