示例#1
0
def constructBinaryTree(l):
    counter = 0
    y = BinaryTree.Node(l)
    thislevel = [y]
    A = BinaryTree.BT(y)
    while len(thislevel) > 0:
        nextlevel = []
        for i in range(len(thislevel)):
            if type(thislevel[i].value) == list and len(thislevel[i].value) == 2:
                k = thislevel[i]
                k.LC = BinaryTree.Node(thislevel[i].value[0])
                k.RC = BinaryTree.Node(thislevel[i].value[1])
                A.size += 2
                k.value = "z"+str(counter)
                counter += 1
                k.LC.parent = k
                k.RC.parent = k
                nextlevel.append(k.LC)
                nextlevel.append(k.RC)
        thislevel = nextlevel
        
    return A
示例#2
0
def findSubtreeAndOpt(lines):
    # still under construction
    # exhaust a small tree reconstruction
    i = 0
    while i < len(lines):
        line_header = get_line_header(lines)
        # constructing the Binary Tree
        N = BinaryTree.Node(lines[i][0])
        BT1 = BinaryTree.BT(N)
        findSubTree(lines, line_header, BT1, N)
        # generate dictionary of singly-used-variables
        singly_used_variables = single_used_variables(lines)
        # finding all the elements, with a limit
        elements = BT1.findTouchableElementsLimit(5, singly_used_variables)
        # original lines involved with the elements
        struct = BT1.touchableElementsStructureLimit(5, singly_used_variables)
        original_line = gendistinct.lineToCode(struct, len(elements))
        # substitute to be compatible with the lines
        substitution(original_line, lines)
        # find out all the variables that only used a single time if no such variables, skip
        one_time_variables = []
        for e in elements:
            if singly_used_variables[e] == 0:
                one_time_variables.append(e)
        if len(one_time_variables) == 0:
            i += 1
            continue

        # generate all possible structures of the binary tree
        alltrees = gendistinct.genStruct(len(elements))
        trees = []
        # looping through all the possible structures, try to optimise it
        for tree in alltrees:
            T = gendistinct.convertToTrees(tree, elements)
            for t in T:
                trees.append(t)
        # eliminate isomorphic trees
        gendistinct.convertDistinct(trees, elements)
        # find the best tree
        # criteria for best tree:
        best_tree = None
        best_XOR = 100
        for j in range(len(trees)):
            trees[j] = gendistinct.lineToCode(trees[j], len(elements))
            substitution(trees[j], lines)
            # if the tree is the original tree, skip it
            flag = 0
            for k in range(len(trees[j])):
                if set(trees[j][k][1:]) == set(original_line[k][1:]):
                    continue
                else:
                    flag = 1
                    break
            if flag == 0:
                continue

            XOR = XOR_count(trees[j], lines)
            saves = saving(trees[j], one_time_variables, lines)
            XOR = XOR - saves
            if best_XOR > XOR:
                best_XOR = XOR
                best_tree = trees[j]
        if best_XOR < 0:
            saves = saving(trees[j], one_time_variables, lines)
            replaceLines(original_line, best_tree, lines, one_time_variables)
            i -= 1
        i += 1