Exemplo n.º 1
0
def testcase4():
    print "testcase4"
    tree = RedBlackTree()
    bi_tree = BinarySearchTree()
    array = random.sample(string.uppercase[:26], 10)
    #array = ['E', 'D', 'L', 'G', 'F', 'H', 'A', 'I', 'B', 'C']
    # array = string.uppercase[:12][::-1]
    print array
    for char in array:
        tree.put(char, char)
        bi_tree.put(char, char)
        print char, tree.level_order_traversal()
        print tree.rank("A")
        
    print tree.level_order_traversal()
    print bi_tree.level_order_traversal()
    
    print tree.inorder_traversal()
        
    #print "search T:", tree.search("T")
    #print "search A:", tree.search("A")
    
    for char in array:
        print char, tree.rank(char), bi_tree.rank(char)
        
    print tree.range_size("A", "G")
    print bi_tree.range_size("D", "G")
    
    print bi_tree.range_node("D", "G")
Exemplo n.º 2
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