RE求助
查看原帖
RE求助
1155764
ouxiyao楼主2024/10/8 07:20
#include<iostream>
using namespace std; 
typedef struct node;
typedef node *tree; 
struct node{
	int data;
	tree lc,rc;
};
tree bt;
int pree,nextt;
int rank1(tree &bt,int n){
	if(!bt)return 0;
	else if(bt->data>=n)return 0;
	else return 1+rank1(bt->lc,n)+rank1(bt->rc,n);
}
int rank2(tree &bt,int n){
	if(n==1)return bt->data;
	else {
		if(bt->lc==NULL)return rank2(bt->rc,n-1);
		else return rank2(bt->lc,n-1);
	}
}
void pre1(tree &bt,int n){
	if(!bt)return ;
	if(bt->data>=n)return ;
	else{
		pree = bt->data;
		pre1(bt->lc,n);
		pre1(bt->rc,n);
	}
}
void next1(tree &bt,int n){
	if(!bt)return ;
	else if(bt->data>n){
		nextt = bt->data;
		return ;
	}
	else{
		next1(bt->lc,n);
		next1(bt->rc,n);
	}
}
void charu(tree &bt,int n){
	if(bt){
		if(n<bt->data)charu(bt->lc,n);
		else charu(bt->rc,n);
	}
	else{
		bt = new node;
		bt->data = n;
		bt->lc = bt->rc = NULL;
	}
}
int main(){
	int n,zl,x;
	cin>>n;
	for(int i = 1;i<=n;i++){
		cin>>zl>>x;
		if(zl==1)cout<<rank1(bt,x)+1<<endl;
		else if(zl==2)cout<<rank2(bt,x)<<endl;
		else if(zl==3){
			pree = -2147483647;
			pre1(bt,x);
			cout<<pree<<endl;
		}
		else if(zl==4){
			nextt = 2147483647;
			next1(bt,x);
			cout<<nextt<<endl;
		}
		else charu(bt,x);
	}
	return 0;
} 
2024/10/8 07:20
加载中...