引入红黑树近似查询
This commit is contained in:
@@ -103,6 +103,8 @@ namespace ling {
|
||||
/// 查找元素
|
||||
Node *iterativeSearch(Node *x, T key) const;
|
||||
|
||||
Node *findSearch(Node *x, T key) const;
|
||||
|
||||
/// 删除元素
|
||||
void remove(Node *&root, Node *node);
|
||||
|
||||
@@ -126,9 +128,12 @@ namespace ling {
|
||||
/// 删除节点
|
||||
void remove(T key);
|
||||
|
||||
/// 查找节点
|
||||
/// 查找元素
|
||||
/// 区别于findSearch,此方法会在没有匹配时返回接近的节点
|
||||
Node *iterativeSearch(T key);
|
||||
|
||||
Node *findSearch(T key);
|
||||
|
||||
/// 根节点
|
||||
Node *getRoot() {
|
||||
return rootNode;
|
||||
@@ -332,6 +337,24 @@ namespace ling {
|
||||
|
||||
template<typename T>
|
||||
typename RedBlackTree<T>::Node *RedBlackTree<T>::iterativeSearch(RedBlackTree::Node *x, T key) const {
|
||||
Relation temp;
|
||||
Node *closest = nullptr;
|
||||
while (x != nullptr) {
|
||||
temp = equal(x->value, key);
|
||||
if (temp == EQUAL)
|
||||
return x;
|
||||
if (temp == SMALL) {
|
||||
closest = x;
|
||||
x = x->right;
|
||||
} else {
|
||||
x = x->left;
|
||||
}
|
||||
}
|
||||
return closest;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename RedBlackTree<T>::Node *RedBlackTree<T>::findSearch(RedBlackTree::Node *x, T key) const {
|
||||
while ((x != nullptr) && equal(x->value, key) != EQUAL) {
|
||||
if (equal(key, x->value) == SMALL) {
|
||||
x = x->left;
|
||||
@@ -342,6 +365,11 @@ namespace ling {
|
||||
return x;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename RedBlackTree<T>::Node *RedBlackTree<T>::findSearch(T key) {
|
||||
return findSearch(rootNode, key);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename RedBlackTree<T>::Node *RedBlackTree<T>::iterativeSearch(T key) {
|
||||
return iterativeSearch(rootNode, key);
|
||||
|
||||
Reference in New Issue
Block a user