def NewLeafCalculation(conds, L, local_max_value, local_max_X,
                       local_max_la_examples, local_max_lb_examples):
    with open('bounds.json', 'r') as f:
        bound_pics = json.load(f)
    for X in conds:
        la = []
        lb = []
        for ex in L.examples:
            if X.CheckCondition(ex, bound_pics):
                la.append(ex)
            else:
                lb.append(ex)
        h_la = BinTree.H(la)
        h_lb = BinTree.H(lb)
        n = len(L.examples)
        h_x = len(la) / n * h_la + len(lb) / n * h_lb
        h_l = BinTree.H(L.examples)
        IG_X_L = h_l - h_x
        if (local_max_value < IG_X_L * n):
            local_max_value = IG_X_L * n
            local_max_X = X
            local_max_la_examples = la
            local_max_lb_examples = lb
    L.IG_details = [
        local_max_value, local_max_X, local_max_la_examples,
        local_max_lb_examples
    ]
Ejemplo n.º 2
0
def main():
    '''
    tree_file = sys.argv[1]
    test_file = sys.argv[2]
    '''
    tree_file = "out"
    test_file = "mnist_test.csv"

    file = open(tree_file, 'rb')
    tree = pickle.load(file)

    print_conds(tree)  #DELETE!!!

    tests_img = ReadCsvFiles.FileToSet(test_file)

    bound_pics = []
    sharp_pics = []
    pic_id = 0
    for pic in tests_img:
        print(pic_id)
        pic.append(pic_id)
        sharped = Condition.SharpImage(pic)
        sharp_pics.append(sharped)
        bound_pics.append(Condition.CreateBounderiesImage(sharped))
        pic_id += 1

    good = 0
    for img in tests_img:
        to_print = BinTree.getLabelByTree(tree, img, [sharp_pics, bound_pics])
        print(to_print)
        if img[0] == to_print:
            good += 1

    print(good / len(tests_img) *
          100)  #DELETE! ONLY FOR PRIVATE USE! NOT FOR SUBMISSION
    def getHeight(curr: TreeNode, heightMap: dict):
        nonlocal maxHeight
        if curr is None:
            return 0
        currHeight = max(getHeight(curr.left, heightMap),
                         getHeight(curr.right, heightMap)) + 1
        heightMap.setdefault(currHeight, []).append(curr.value)
        maxHeight = max(currHeight, maxHeight)
        return currHeight

    heightMap, maxHeight = dict(), 0
    res = []
    getHeight(root, heightMap)
    print()
    for i in range(1, maxHeight + 1):
        res.append(heightMap[i])
    return res


# the above solution works only because we are measuring height from the bottom
# and for each node we are storing it's value correpsonfing to it's depth level
# postorder traversal is used for this purpose

# another m,ehotd that I thinks hould work is bfs traversal and storing the nodes for each level
# my bad I just realized it's not gonna workd cause leaves can be in at different levels but still be leaves

ip = [1, 2, 3, 4, 5]
ip = [3, "null", 2, "null", 1]
root = TreeNode.populateTreeFromList(ip)
# TreeNode.printTree_inOrder(root)
print(findLeaves(root))
Ejemplo n.º 4
0
from flask import Flask
import HeapNode
from BinTree import *

app = Flask(__name__)

instance.level_wise()
'''
create an instance for the BinTree and then we can use that
when the api calls come in
'''

anotherInstance = BinTree("anotherInstance")
'''
index()
args: Integer data
returns: the string that is generated from the level_wise() with the tree
in level wise order
'''


@app.route('/index/<int:data>')
def index(data):
    print "[Serve.py] we are at ", data
    instance.insert(data)
    return "Data has been inserted!"


@app.route('/index/show')
def show():
    print "[Serve.py] showing the tree"
Ejemplo n.º 5
0
    def depth_bottom_up(self, node: bt) -> int:
        if not node:
            return 0
        lDepth = self.depth_bottom_up(node.left)
        # check if lDepth is valid or not
        if lDepth == -1: return -1
        rDepth = self.depth_bottom_up(node.right)
        # check if rDepth is valid or not
        if rDepth == -1: return -1
        # we check after each step so the code returns as soon as one sub tree is invalid
        if abs(lDepth - rDepth) > 1:
            return -1
        return 1 + max(lDepth, rDepth)

    def isBalanced(self, node: bt) -> bool:
        if not root:
            return True
        return self.depth_bottom_up(root) != -1


#lets test oour current copde first
ip = [1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, "null", "null", 5, 5]
# ip = [1,2,2,3,3,"null","null",4,4]
root = bt.populateTreeFromList(ip)
# lets print mf
bt.printTree_inOrder(root)

print('First Type of Balanced: ', FirstApproach().isBalanced(root))
print('Second Type of Balanced, Top Down: ', SecondApproach().isBalanced(root))
print('Second Type of Balanced, Bottom Down: ', BottomUp().isBalanced(root))
Ejemplo n.º 6
0
            if node and node.right:
                node = node.right
                stack.append(node)
            else:
                node = None
        return currTreeLeaves

    if not root1 or not root2:
        return True
    root1Leaves = []

    root1Leaves = storeAndCompareLeaves(root1)
    flag = storeAndCompareLeaves(root2, root1Leaves)

    if flag: return False
    return True
    # if root1Leaves == root2Leaves:
    # 	return True
    # return False


# create trees
# ip1 = [3,5,1,6,2,9,8,"null","null",7,4]
# ip2 = [3,5,1,6,7,4,2,"null","null","null","null","null","null",9,8]
ip1, ip2 = [1], [2]

root1 = TreeNode.populateTreeFromList(ip1)
root2 = TreeNode.populateTreeFromList(ip2)

res = leafSimilar(root1, root2)
print(res)
Ejemplo n.º 7
0
        helper(root.left, currSum)
        helper(root.right, currSum)

    helper(root, 0)
    return rootToLeaf


# iterative pre ROder approach
def sumNumbers(self, root: TreeNode) -> int:
    # iterative solution
    rootToLeaf = 0
    stack = [(root, 0)]
    while stack:
        root, currSum = stack.pop()
        if root is None:
            continue
        currSum = 10 * currSum + root.val
        if not root.left and not root.right:  # we are at leaf node, add currSum to gloabal sum
            rootToLeaf += currSum
        stack.append((root.right, currSum))
        stack.append((root.left, currSum))
    return rootToLeaf


ip = [4, 5, 2, 1, "null", 0]
root = TreeNode.populateTreeFromList(ip)
TreeNode.printTree_inOrder(root)
print()
op = sumNumbers(root)
print(op)
def lcaDeepestLeaves(root: BT) -> (int, BT):
    if not root:
        return

    def dfs(root: BT):
        if not root:
            return (0, None)
        lHeight, lNode = dfs(root.left)
        rHeight, rNode = dfs(root.right)
        # both subtrees are equal height hence both consists of deepest leaves thus the root node of this subtree is
        # is the LCA
        if lHeight == rHeight:
            return (lHeight + 1, root)
        # deepest leaf is at left three thus the leafnode is LCA for curr subtree
        if lHeight > rHeight:
            return (lHeight + 1, lNode)
        # similarly for right subtree
        return (rHeight + 1, rNode)

    return dfs(root)[1]


arr = [1, 2, 3, 4, 5]

root = BT.populateTreeFromList(arr)
BT.printTree_inOrder(root)

print()
lca = lcaDeepestLeaves(root)
BT.printTree_inOrder(lca)
            dfs(node.right, node)

    dfs(root)
    # now do bfs
    level = [(target, 0)]
    res, seen = [], [target]
    while level:
        subLevel = []
        for pair in level:
            node, d = pair
            if d == K:
                res.append(node.val)
            if node and node.left and node.left not in seen:
                subLevel.append((node.left, d + 1))
                seen.append(node.left)
            if node and node.right and node.right not in seen:
                subLevel.append((node.right, d + 1))
                seen.append(node.right)
            if node in par and par[node] and par[node] not in seen:
                subLevel.append((par[node], d + 1))
                seen.append(par[node])
        level = subLevel
    return res


ip = [3, 5, 1, 6, 2, 0, 8, "null", "null", 7, 4]
root = TreeNode.populateTreeFromList(ip)
tgt = TreeNode.BinTree(5)
res = distanceK(root, tgt, 2)
print(res)
def main():
    '''
    version = sys.argv[1]
    p = sys.argv[2]
    l = sys.argv[3]
    train_set_file_name = sys.argv[4]
    output_tree_file_name = sys.argv[5]
    '''
    '''
    print("Enter Version")
    version = int(input())
    print("Enter p value")
    p = int(input())
    l = int(input("Enter l value"))
    train_set_file_name = input("Enter train set file")
    output_tree_file_name = input("Enter output file")
    '''

    start_time = time.time()
    bound_pics = []

    version = 2
    p = 20
    l = 5
    train_set_file_name = "mnist_train.csv"
    output_tree_file_name = "out"

    pictures = ReadCsvFiles.FileToSet(train_set_file_name)

    cond_nums = [1, 2, 3, 5, 6]
    # # NOT FOR SUBMISSION
    # random.shuffle(pictures)
    # pictures = pictures[:5000]

    # ---- in case condition 5 is been used, we will build the bounded pics and set in global array ----
    if (5 in cond_nums or 6 in cond_nums):
        print("Creating bounderies and sharped images...")
        print("")
        sharp_pics = []
        bound_pics1 = []
        pic_id = 0
        for pic in pictures:
            print(pic_id)
            pic.append(pic_id)
            sharped = Condition.SharpImage(pic)
            sharp_pics.append(sharped)
            bound_pics1.append(Condition.CreateBounderiesImage(sharped))
            pic_id += 1
        bound_pics = [sharp_pics, bound_pics1]

    random.shuffle(pictures)
    valid_set_size = int(p / 100 * len(pictures))
    valid_set = pictures[0:valid_set_size]
    train_set = pictures[valid_set_size:]
    conds = []

    if (version == 1):
        conds = Condition.BuildVersion1ConditionArray()
    else:
        conds1 = Condition.BuildVersion1ConditionArray()
        conds2 = Condition.BuildType2ConditionArray()
        conds3 = Condition.BuildType3ConditionArray()
        conds5 = Condition.BuildType5ConditionArray()
        conds6 = Condition.BuildType6ConditionArray()
        conds = conds1 + conds2 + conds3 + conds5 + conds6
        # conds = conds1 + conds2 + conds3

    t_trees = []
    id_leaf = 1
    # ---- Build first leaf ----
    max_digit = BinTree.common_label(train_set)
    tree = BinTree.BinTree(None, max_digit, id_leaf)
    id_leaf += 1
    tree.examples = train_set
    # ---- End build first leaf ----

    last_t = 0
    leaves = [tree]
    max_l = -1
    max_value = 0
    for i in range(l + 1):
        res_build = BuildDecisionTree(int(math.pow(2, i)), train_set, conds,
                                      bound_pics, tree, leaves, id_leaf,
                                      last_t)
        t_trees.append(res_build[0])
        tree = res_build[0]
        leaves = res_build[1]
        id_leaf = res_build[2]
        last_t = int(math.pow(2, i))

        succeeded = 0
        for img in valid_set:
            predicted_label = BinTree.getLabelByTree(tree, img, bound_pics)
            if (predicted_label == img[0]):
                succeeded += 1
        if (max_value < succeeded):
            max_value = succeeded
            max_l = i

    # Find tree with optimal L value
    # max_l = -1
    # max_value = 0
    # for i in range(len(t_trees)):
    #     succeeded = 0
    #     for img in valid_set:
    #         predicted_label = BinTree.getLabelByTree(t_trees[i],img, bound_pics)
    #         if(predicted_label == img[0]):
    #             succeeded += 1
    #     if (max_value < succeeded):
    #         max_value = succeeded
    #         max_l = i

    # ---- Build Final Tree ----
    id_leaf = 1
    # ---- Build first leaf ----
    max_digit = BinTree.common_label(train_set)
    tree = BinTree.BinTree(None, max_digit, id_leaf)
    id_leaf += 1
    tree.examples = train_set
    # ---- End build first leaf ----

    last_t = 0
    leaves = [tree]
    final_t = int(math.pow(2, max_l))
    res_final = BuildDecisionTree(final_t, pictures, conds, bound_pics, tree,
                                  leaves, id_leaf, last_t)
    final_tree = res_final[0]
    # ---- Finish Build Final Tree ----

    filehandler = open(output_tree_file_name, 'wb')
    pickle.dump(final_tree, filehandler)

    print("num: " + str(len(train_set)))
    errors = 0
    for img in pictures:
        predicted_label = BinTree.getLabelByTree(final_tree, img, bound_pics)
        if (predicted_label != img[0]):
            errors += 1

    print("error: " + str(errors))
    print("size: " + str(final_t))

    print("TIME: " + str(time.time() - start_time))  #NOT FOR SUBMISSION!!!
def BuildDecisionTree(t, train_set, conds, bound_pics, tree, leaves, id_leaf,
                      last_t):
    with open('bound.json', 'w') as f:
        json.dump(bound_pics, f)
    print("T: " + str(t))
    begin = time.time()
    for i in range(last_t, t):
        print("Working stage : " + str(i))
        # ---- Variables to save optimal X and L ---- #
        max_value = 0
        max_L = None
        max_X = None
        max_la_examples = None
        max_lb_examples = None
        # ---- End Initial Variables ---- #

        t_init = [False, False]

        for L in leaves:
            # Find Optimal X for each leaf
            local_max_value = 0
            local_max_X = None
            local_max_la_examples = None
            local_max_lb_examples = None
            jobs = []
            if len(L.IG_details) == 0:  # If leaf did not calculated before
                if not t_init[0]:
                    t_init[0] = True
                    T1 = mp.Process(target=NewLeafCalculation,
                                    args=(conds, L, local_max_value,
                                          local_max_X, local_max_la_examples,
                                          local_max_lb_examples))
                    jobs.append(T1)
                    T1.start()
                    print("T1 started... ")
                else:
                    t_init[1] = True
                    T2 = mp.Process(target=NewLeafCalculation,
                                    args=(conds, L, local_max_value,
                                          local_max_X, local_max_la_examples,
                                          local_max_lb_examples))
                    jobs.append(T2)
                    T2.start()
                    print("T2 started... ")

                # NewLeafCalculation(conds, bound_pics, L, local_max_value, local_max_X, local_max_la_examples,
                #                    local_max_lb_examples)
                # for X in conds:
                #     la = []
                #     lb = []
                #     for ex in L.examples:
                #         if X.CheckCondition(ex, bound_pics):
                #             la.append(ex)
                #         else:
                #             lb.append(ex)
                #     h_la = BinTree.H(la)
                #     h_lb = BinTree.H(lb)
                #     n = len(L.examples)
                #     h_x = len(la)/n * h_la + len(lb)/n * h_lb
                #     h_l = BinTree.H(L.examples)
                #     IG_X_L = h_l - h_x
                #     if(local_max_value < IG_X_L*n):
                #         local_max_value = IG_X_L*n
                #         local_max_X = X
                #         local_max_la_examples = la
                #         local_max_lb_examples = lb
                # L.IG_details = [local_max_value, local_max_X, local_max_la_examples, local_max_lb_examples]
        for job in jobs:
            job.join()
        for L in leaves:
            if max_value < L.IG_details[0]:
                max_value = L.IG_details[0]
                max_L = L
                max_X = L.IG_details[1]
                max_la_examples = L.IG_details[2]
                max_lb_examples = L.IG_details[3]

        # ---- Build Chosen leaf ----
        leaf_la = BinTree.BinTree(None, BinTree.common_label(max_la_examples),
                                  id_leaf)
        id_leaf += 1
        leaf_la.examples = max_la_examples
        leaf_lb = BinTree.BinTree(None, BinTree.common_label(max_lb_examples),
                                  id_leaf)
        id_leaf += 1
        leaf_lb.examples = max_lb_examples
        max_L.left = leaf_lb
        max_L.right = leaf_la
        max_L.label = None
        max_L.cond = max_X
        leaves.remove(max_L)
        leaves.append(leaf_la)
        leaves.append(leaf_lb)
        # ---- End Build Chosen Leaf ----

        total_calc_time = time.time() - begin
        print("Total time for tree calculation: " + str(total_calc_time))
        print("")
    return [tree, leaves, id_leaf]