再贴送两个函数,这样可以做很多事情了,哈!代码的意思和set集里的函数一样
template <class KT, class RT>
RBTreeNode<KT, RT>* RBTree<KT, RT>::lower_bound(KT fkey, RT frec) {
RBTreeNode<KT, RT> *x = root;
RBTreeNode<KT, RT> *y = x;
bool f=0;
while (x != NIL) {
if (fkey > x->key) {x = x->right;}
else if (fkey < x->key) { y = x; x = x->left; f=1;}
else {
y = x;
while (x != NULL && x->rec != frec) {y = x; x = x->nxt;}
return y;
}
}
if(f) return y;
else return NIL;
}
template <class KT, class RT>
RBTreeNode<KT, RT>* RBTree<KT, RT>::upper_bound(KT fkey, RT frec) {
RBTreeNode<KT, RT> *x = root;
RBTreeNode<KT, RT> *y = x;
bool f=0;
while (x != NIL) {
if (fkey < x->key) { x = x->left; }
else if (fkey > x->key) { y = x; x = x->right; f=1;}
else {
while (x != NULL && x->rec != frec) {y = x; x = x->nxt;}
return y;
}
}
if(f) return y;
else return NIL;
}
回复 更多评论