以下是引用Devil_W在2009-9-15 23:10的发言:
本质上求rank的都不应该是你们两个说的那样
我看到过SBT树求rank的效率是最高的。
比RBT更高的效率。
对于这个题构造树的时候需要的比教的次数跟2n很接近,对于找第三个数来说没什么优势,如果是找地n个,n比较大的时候这才会有实际意义。
本质上求rank的都不应该是你们两个说的那样
我看到过SBT树求rank的效率是最高的。
比RBT更高的效率。

身体是玩命的本钱
程序代码:#include<iostream>
using namespace std;
struct BiNode{
long key;
BiNode *pre;
BiNode *next;
BiNode(long n){ key =n ;pre=NULL; next=NULL;}
};
class BiTree{
private:
static int Rank;
void BiTreeInsert(BiNode *&root,long n)
{
if( NULL == root)
{
root= new BiNode(n);
}
else
{
if( root->key > n )
BiTreeInsert(root->pre ,n);
if(root->key < n )
BiTreeInsert(root->next,n);
}
}
void BiTreeRank(BiNode * root, int r , long & key ,bool & status)
{
if( NULL == root)
{
return ;
}
else
{
BiTreeRank( root->next, r , key, status);
Rank++;
if( Rank == r)
{
key=root->key;
status =true;
}
BiTreeRank( root->pre, r, key, status);
}
}
void BiTreeShow(BiNode * root )
{
if(NULL == root)
return ;
BiTreeShow(root->pre);
cout<<root->key<<" ";
BiTreeShow(root->next);
}
void BiTreeDestroy(BiNode *root){
if( NULL == root)
return ;
BiTreeDestroy(root->pre);
BiTreeDestroy(root->next);
delete root;
}
public:
BiNode *root;
BiTree(){ root = NULL ;}
~BiTree()
{
BiTreeDestroy(root);
}
void insert(long n){ BiTreeInsert(root, n);}
long rank(int r)
{
long k;
bool sta=false;
Rank=0;
BiTreeRank(root,r, k,sta);
if( sta == true )
return k;
else
return -1;
}
void show(){ BiTreeShow( root);}
};
int BiTree::Rank=0;
int main()
{
int n;
long key;
while(cin>>n)
{
if ( 0 == n)
break;
BiTree *tree=new BiTree();
for( int i=0;i<n;i++)
{
cin>>key;
tree->insert(key);
}
//tree->show();
//cout<<endl;
int rank_key ;
rank_key=tree->rank(3);
if( rank_key == -1 )
cout<<"NO such score!"<<endl;
else
cout<<rank_key<<endl;
}
return 0;
}