1.二分查找法变种(要查找的元素有多个)
实现
template < typename T>
int floor ( T arr[ ] , int n, T target) {
int l = - 1 , r = n- 1 ;
while ( l < r ) {
int mid = l + ( r- l+ 1 ) / 2 ;
if ( arr[ mid] >= target )
r = mid - 1 ;
else
l = mid;
}
assert ( l == r ) ;
if ( l + 1 < n && arr[ l+ 1 ] == target )
return l + 1 ;
return l;
}
template < typename T>
int ceil ( T arr[ ] , int n, T target) {
assert ( n >= 0 ) ;
int l = 0 , r = n;
while ( l < r ) {
int mid = l + ( r- l) / 2 ;
if ( arr[ mid] <= target )
l = mid + 1 ;
else
r = mid;
}
assert ( l == r ) ;
if ( r - 1 >= 0 && arr[ r- 1 ] == target )
return r- 1 ;
return r;
}
2.二分搜索树
2.1 定义(与堆区别)
2.2 插入
实现
void insert ( Key key, Value value) {
root = insert ( root, key, value) ;
}
Node* insert ( Node * node, Key key, Value value) {
if ( node == NULL ) {
count ++ ;
return new Node ( key, value) ;
}
if ( key == node- > key )
node- > value = value;
else if ( key < node- > key )
node- > left = insert ( node- > left , key, value) ;
else
node- > right = insert ( node- > right, key, value) ;
return node;
}
2.3 查找(contain、search)
bool contain ( Key key) {
return contain ( root, key) ;
}
bool contain ( Node* node, Key key) {
if ( node == NULL )
return false ;
if ( key == node- > key )
return true ;
else if ( key < node- > key )
return contain ( node- > left , key ) ;
else
return contain ( node- > right , key ) ;
}
Value* search ( Key key) {
return search ( root , key ) ;
}
Value* search ( Node* node, Key key) {
if ( node == NULL )
return NULL ;
if ( key == node- > key )
return & ( node- > value) ;
else if ( key < node- > key )
return search ( node- > left , key ) ;
else
return search ( node- > right, key ) ;
}
2.4 删除
删除最小值
实现
Node* removeMin ( Node* node) {
if ( node- > left == NULL ) {
Node* rightNode = node- > right;
delete node;
count -- ;
return rightNode;
}
node- > left = removeMin ( node- > left) ;
return node;
}
删除最大值
实现
Node* removeMax ( Node* node) {
if ( node- > right == NULL ) {
Node* leftNode = node- > left;
delete node;
count -- ;
return leftNode;
}
node- > right = removeMax ( node- > right) ;
return node;
}
删除任意值
实现
Node* remove ( Node* node, Key key) {
if ( node == NULL )
return NULL ;
if ( key < node- > key ) {
node- > left = remove ( node- > left , key ) ;
return node;
}
else if ( key > node- > key ) {
node- > right = remove ( node- > right, key ) ;
return node;
}
else {
if ( node- > left == NULL ) {
Node * rightNode = node- > right;
delete node;
count -- ;
return rightNode;
}
if ( node- > right == NULL ) {
Node * leftNode = node- > left;
delete node;
count-- ;
return leftNode;
}
Node * successor = new Node ( minimum ( node- > right) ) ;
count ++ ;
successor- > right = removeMin ( node- > right) ;
successor- > left = node- > left;
delete node;
count -- ;
return successor;
}
}
2.5 实现floor、celi
Key* floor ( Key key) {
if ( count == 0 || key < minimum ( ) )
return NULL ;
Node * floorNode = floor ( root, key) ;
return & ( floorNode- > key) ;
}
Node* floor ( Node* node, Key key) {
if ( node == NULL )
return NULL ;
if ( node- > key == key )
return node;
if ( node- > key > key )
return floor ( node- > left , key ) ;
Node* tempNode = floor ( node- > right , key ) ;
if ( tempNode != NULL )
return tempNode;
return node;
}
Key* ceil ( Key key) {
if ( count == 0 || key > maximum ( ) )
return NULL ;
Node * ceilNode = ceil ( root, key) ;
return & ( ceilNode- > key) ;
}
Node* ceil ( Node* node, Key key) {
if ( node == NULL )
return NULL ;
if ( node- > key == key )
return node;
if ( node- > key < key )
return ceil ( node- > right , key ) ;
Node* tempNode = ceil ( node- > left , key ) ;
if ( tempNode != NULL )
return tempNode;
return node;
}
2.6 实现predecessor、successor
Key* predecessor ( Key key) {
Node * node = search ( root, key) ;
if ( node == NULL )
return NULL ;
if ( node- > left != NULL )
return & ( maximum ( node- > left) - > key) ;
Node* preNode = predecessorFromAncestor ( root, key) ;
return preNode == NULL ? NULL : & ( preNode- > key) ;
}
Node* predecessorFromAncestor ( Node* node, Key key) {
if ( node- > key == key)
return NULL ;
if ( key < node- > key)
return predecessorFromAncestor ( node- > left, key) ;
else {
assert ( key > node- > key) ;
Node* tempNode = predecessorFromAncestor ( node- > right, key) ;
if ( tempNode)
return tempNode;
else
return node;
}
}
Key* successor ( Key key) {
Node * node = search ( root, key) ;
if ( node == NULL )
return NULL ;
if ( node- > right != NULL )
return & ( minimum ( node- > right) - > key) ;
Node* sucNode = successorFromAncestor ( root, key) ;
return sucNode == NULL ? NULL : & ( sucNode- > key) ;
}
Node* successorFromAncestor ( Node* node, Key key) {
if ( node- > key == key)
return NULL ;
if ( key > node- > key)
return successorFromAncestor ( node- > right, key) ;
else {
assert ( key < node- > key) ;
Node* tempNode = predecessorFromAncestor ( node- > left, key) ;
if ( tempNode)
return tempNode;
else
return node;
}
}