如何构建数据结构以支持某些范围查询

时间:2015-05-25 08:43:57

标签: java python data-structures time-complexity

间隔由开始和结束定义。

  

给定一系列可能重叠的区间[比如,0-999],   构建数据结构以支持最佳的以下范围查询   时间复杂度

重叠(开始,结束) =与[start,end]重叠的所有区间的集合

内(开始,结束) = [开始,结束]内所有区间的集合

其中,

Intervals I1[start1, end1], I2[start2, end2] are said to overlap with  each other iff start1<end2 && start2<end1
Interval I1[start1, end1] is said to be within interval I2[start2,end2] iff start1>start2 && end1<end2

2 个答案:

答案 0 :(得分:0)

此问题所涉及的数据结构称为Interval tree

  

一个有序的树数据结构来保存间隔,它允许一个   有效地找到与任何给定间隔重叠的所有间隔或   点。

两种有效的方法:

用于搜索所有重叠区间的链接中提出的算法预计比传统的区间树(扩充树)更快,用于搜索操作,添加元素的速度要慢一些。

实现间隔树的各种方法:

实施解决方案时,您应该知道performance of java collections processing

答案 1 :(得分:0)

  

写了一些Scala代码来使用区间树来实现它!

class IntervalSearchTree {
     var root=new IntervalNode(null,null,null,null,null);

  class IntervalNode(var left:IntervalNode,var start:Integer,var end:Integer,var maxEnd:Integer,var right:IntervalNode);

    def add(start:Integer,end:Integer ):Unit={
      var node:IntervalNode=root
      while(node!=null){
        if(end>node.maxEnd)
          node.maxEnd=end
        if (start < node.start) {
                if (node.left == null) {
                    node.left = new IntervalNode(null, start, end, end, null);
                    return;
                }
                node = node.left;
            } else {
                if (node.right == null) {
                    node.right = new IntervalNode(null, start, end, end, null);
                    return;
                }
                node = node.right;
            }
      }
      root =  new IntervalNode(null, start, end, end, null);
    }

    def overlap(start:Integer,end:Integer):Unit={

      var intervalNode:IntervalNode=root;
      while (intervalNode != null) {
         if (intersection(start, end, intervalNode)){
           println(intervalNode.start+"-"+intervalNode.end)
         }
         if (leftSubTree(start, end, intervalNode.left)) {
                intervalNode = intervalNode.left;


            } else {

                intervalNode = intervalNode.right;

            }


      }

    }

    def within(start:Integer,end:Integer):Unit={
        var intervalNode:IntervalNode = root;
        while (intervalNode != null) {
           if (subset(start, end, intervalNode)){
           println(intervalNode.start+"-"+intervalNode.end)
         }
         if (leftSubTree(start, end, intervalNode.left)) {
                intervalNode = intervalNode.left;


            } else {

                intervalNode = intervalNode.right;

            }
        }

    }


    def subset(start:Integer,end:Integer,intervalNode:IntervalNode):Boolean={
      return start<intervalNode.start  && end >intervalNode.end ;
    }

    def intersection(start:Integer,end:Integer,intervalNode:IntervalNode):Boolean={
      return start < intervalNode.end && end > intervalNode.start;
    }

    def leftSubTree(start:Integer,end:Integer,intervalLeftSubtree:IntervalNode):Boolean={
      return intervalLeftSubtree != null && intervalLeftSubtree.maxEnd > start;
    }


    def main(args: Array[String]): Unit = {
      var intervalSearchTree=new IntervalSearchTree()
        intervalSearchTree.add(17, 19);
        intervalSearchTree.add(5, 8);
        intervalSearchTree.add(21, 24);
        intervalSearchTree.add(22, 24);
        intervalSearchTree.add(20, 26);
        intervalSearchTree.add(20, 24);
        intervalSearchTree.add(4, 8);
        intervalSearchTree.add(5, 9);
        intervalSearchTree.add(15, 18);
        intervalSearchTree.add(7, 10);
        intervalSearchTree.add(16, 22);

        //Input for testing overlaps 
        println("Overlaps");
        intervalSearchTree.overlap(23, 25);
        //Input for testing overlaps
        println("Within");
        intervalSearchTree.within(4, 25);
    }




}