我有一个需求,需要根据某个属性值更新图形前端的颜色。该属性值具有不同的范围,例如-30到-45,-60到-80等等。因此,我需要一种数据结构来存储这些范围(预先填充它们)。当我确定点时,我想知道该点所在的范围是O(1)时间还是O(logN)时间内的唯一范围。因此,我的查询将包含一个单独的点,并且输出应该是包含该点的唯一范围...
我在范围树和线段树之间感到困惑...我想在C++ STL map上构建树。
我在范围树和线段树之间感到困惑...我想在C++ STL map上构建树。
std::set<>
来进行O(log N)的插入、删除和查询,因为树节点需要包含额外的数据。你可以在这里阅读有关它们的内容http://syedwaqarahmad.webs.com/documents/t.cormen-_introduction_to_algorithms_3rd_edition.pdf第14.3章。http://www.boost.org/doc/libs/1_46_1/libs/icl/doc/html/index.html
如果我理解你的意思正确的话,你可以很容易地使用std::set来完成这个操作:
#include <iostream>
#include <set>
struct Interval {
int min;
int max;
};
struct ComInt {
bool operator()(const Interval& lhs, const Interval& rhs){
return lhs.max < rhs.min;
}
};
std::set<Interval, ComInt> intervals = { { -10, -5 }, { -4, 4 }, { 5, 10 } };
int main() {
int point = 3;
Interval tmp = { point, point };
auto result=intervals.find(tmp);
if (result != intervals.end()) {
std::cout << "Min:" << result->min << " - Max:" << result->max << std::endl;
} else {
std::cout << "No matching Interval found" << std::endl;
}
}
当然,你应该围绕它构建一个包装类。