Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 用于存储允许有效比较操作的值范围的数据结构_Performance_Algorithm_Data Structures - Fatal编程技术网

Performance 用于存储允许有效比较操作的值范围的数据结构

Performance 用于存储允许有效比较操作的值范围的数据结构,performance,algorithm,data-structures,Performance,Algorithm,Data Structures,我正在寻找一种允许存储非重叠整数范围的数据结构 比较数据结构中是否存在某个范围 例如,在我存储(0,9)、(10,19)、(30,29)之后, 稍后,我想检查范围(1,11)是否被覆盖,在这种情况下 算法给出一个“是”,而对于范围(15,25),算法给出一个“否”,因为给定的范围没有覆盖 非常感谢。您可能正在寻找数据结构,该结构旨在快速存储和搜索区间。由于您处理的是不重叠的整数范围,我认为一个简单的BST可以完成这项工作(如AVL或RB树,以防需要严格的O(logN)性能) 对于间隔[a-b]

我正在寻找一种允许存储非重叠整数范围的数据结构 比较数据结构中是否存在某个范围

例如,在我存储(0,9)、(10,19)、(30,29)之后, 稍后,我想检查范围(1,11)是否被覆盖,在这种情况下 算法给出一个“是”,而对于范围(15,25),算法给出一个“否”,因为给定的范围没有覆盖


非常感谢。

您可能正在寻找数据结构,该结构旨在快速存储和搜索区间。

由于您处理的是不重叠的整数范围,我认为一个简单的BST可以完成这项工作(如AVL或RB树,以防需要严格的O(logN)性能)

对于间隔[a-b] 构建树,将“a”作为键。 节点结构类似于:

struct node{
int left;
int right;
struct node*left;
struct node*right;
};
为了搜索:

bool SearchOverlap(node* root, interval I){
if(!root)
    return false;
if(I.right < root->left)
    SearchOverlap(root->left, I);
else if(I.left > root->right)
    SearchOverlap(root->right, I);
else if(I.left > root->left && I.right < root->right)
    return true;
else if(I.left < root->left && I.right < root->right)
    return SearchOverlap(root->left, new Interval(I.left, root->left));
else if(I.left > root->left && I.right > root->right)
    return SearchOverlap(root->right, new Interval(root->right, I.right));
}
bool SearchOverlap(节点*根,间隔I){
如果(!root)
返回false;
如果(I.右<根->左)
搜索重叠(根->左,I);
else if(I.left>root->right)
搜索重叠(根->右,I);
else if(I.left>root->left&&I.rightright)
返回true;
else if(I.leftleft&&I.rightright)
返回SearchOverlap(root->left,新间隔(I.left,root->left));
else if(I.left>root->left&&I.right>root->right)
返回SearchOverlap(root->right,新间隔(root->right,I.right));
}

非常感谢您的示例代码。STL不提供间隔树的实现吗?没有STL。我不知道。但是我认为Boost库提供了一个间隔图。很棒的想法,一个小错误:第二个else if应该是“I.left>root->left&&I.rightright”。如果我错了,请纠正我。@takwing:您可以使用std::map(仅适用于此问题,不适用于常规间隔树)。间隔的左端是一个键,右端是一个值。