Esempio n. 1
0
def find_cross(h_lines, v_lines):
    h_queue, v_queue = build_queue(h_lines, v_lines)
    # print "h_queue", h_queue.get_keys()
    # print "v_queue", v_queue.get_keys()
    tree = BinarySearchTree()
    points = []
    while True:
        v_item = v_queue.pop()
        if v_item != None:
            print "scan", v_item.key
            delete_items = []
            h_item = h_queue.max()
            while h_item != None and h_item.key >= v_item.key:
                h_item = h_queue.pop()
                if type(h_item.value) != type(0):  # start node
                    if not tree.contains(h_item.value[0]):
                        print "insert", h_item.value[0], h_item.value[1]
                        tree.put(h_item.value[0], h_item.value[1])
                    else:  # overlapping start node
                        end = tree.search(h_item.value[0])
                        print "overlapping start", h_item.value[0]
                        if end > h_item.value[1]:
                            print "update", h_item.value[0], h_item.value[1]
                            tree.put(h_item.value[0], h_item.value[1])  # update the larger end
                        else:
                            print "drop inner section ", h_item.value[1], " within ", end
                else:  # end node
                    end = tree.search(h_item.value)
                    assert end <= h_item.key
                    if end == h_item.key:  # if it's the larger end, remove the y
                        print "catch the end", end
                        if h_item.key > v_item.key:
                            tree.hibbard_deletion(h_item.value)
                            print "remove", h_item.value, "scan", h_item.key
                        else:
                            delete_items.append(h_item.value)
                h_item = h_queue.max()
            assert v_item.value[1] >= v_item.value[0]
            nodes = tree.range_node(v_item.value[0], v_item.value[1])
            print "find node in range (", v_item.value[0], v_item.value[1], "):", nodes
            for node in nodes:
                points.append((v_item.key, node[0]))
            print "points", points
            print "tree", tree.level_order_traversal()
            print "remove", delete_items
            v_item_next = h_queue.max()
            if v_item_next != None and v_item_next.key < v_item.key:  # remove the y when v goes smaller
                for node in delete_items:
                    tree.hibbard_deletion(node)
        else:
            return points