def nodeToBtNode(nodo): if nodo.false_branch == None: btNode = bt.Node(nodo.mostCommonValue) else: btNode = bt.Node(nodo.cat) btNode.left = nodeToBtNode(nodo.true_branch) btNode.right = nodeToBtNode(nodo.false_branch) return btNode
def to_binary_tree(self): node = binarytree.Node(self.val) if self.leftNode is not None: node.left = self.leftNode.to_binary_tree() if self.rightNode is not None: node.right = self.rightNode.to_binary_tree() return node
def wypisanie_drzewa(root): root = binarytree.Node(1) root.left = binarytree.Node(2) root.right = binarytree.Node(3) root.left.left = binarytree.Node(4) root.left.right = binarytree.Node(5) root.right.left = binarytree.Node(6) root.right.right = binarytree.Node(7) print(root)
def start_UPGMA(cluster_array, distance_matrix): C = [(cluster, 1) for cluster in cluster_array] height_array = [0] * len(cluster_array) dist_array = [x[::1] for x in distance_matrix] b_nodes = {cluster: bt.Node(0) for cluster in C} for ind in range(1, len(cluster_array)): (i, j), dist = get_min_index(dist_array) HCk, dki, dkj = get_cluster_distance(height_array, i, j, dist) C.append((str(C[j][0] + C[i][0]), C[j][1] + C[i][1])) new_clus_ind = len(C) - 1 height_array.append(HCk) dist_array.append([0] * (len(dist_array[0]))) for row in dist_array: row += [0] b_nodes[C[i]].value = (C[i][0], dki) b_nodes[C[j]].value = (C[j][0], dkj) b_nodes[C[new_clus_ind]] = bt.Node((C[new_clus_ind][0], 0)) b_nodes[C[new_clus_ind]].left = b_nodes[C[i]] b_nodes[C[new_clus_ind]].right = b_nodes[C[j]] for c in range(len(C) - 1): dist_array[new_clus_ind][c] = (C[i][1] * dist_array[i][c] + C[j][1] * dist_array[j][c]) / ( C[i][1] + C[j][1]) dist_array[c][new_clus_ind] = dist_array[new_clus_ind][c] if j > i: i, j = j, i C.pop(i) C.pop(j) height_array.pop(i) height_array.pop(j) dist_array.pop(i) for y in range(len(dist_array)): dist_array[y].pop(i) dist_array.pop(j) for y in range(len(dist_array)): dist_array[y].pop(j) bt.show(b_nodes[C[0]])
def exptree(exp): nums = list() ops = list() bottom = bt.Node("!") ops.append(bottom) for i in exp: n = bt.Node(i) if isnum(i): nums.append(n) else: while prec(i) <= prec(ops[len(ops) - 1].data): t = ops.pop() bt.mergetrees(t, nums.pop(), nums.pop()) nums.append(t) ops.append(n) while len(ops) > 1: t = ops.pop() bt.mergetrees(t, nums.pop(), nums.pop()) nums.append(t) return nums.pop()
return 0 return max(self.longestPath(root.left, root.value) + self.longestPath(root.right, root.value), \ self.longestUnivaluePath(root.left), \ self.longestUnivaluePath(root.right) ) def longestPath(self, root, value): if not root or root.value != value: return 0 return 1 + max(self.longestPath(root.left, value), self.longestPath(root.right, value)) root = binarytree.Node(5) root.left = binarytree.Node(4) root.right = binarytree.Node(5) root.left.left = binarytree.Node(1) root.left.right = binarytree.Node(1) root.right.right = binarytree.Node(5) print root root2 = binarytree.Node(1) root2.left = binarytree.Node(4) root2.right = binarytree.Node(5) root2.left.left = binarytree.Node(4) root2.left.right = binarytree.Node(4) root2.left.right.right = binarytree.Node(4) root2.right.right = binarytree.Node(5) root2.right.right.right = binarytree.Node(5)
# Binary tree Module to implement Binary Tree. print() import binarytree root = binarytree.Node(10) root.left = binarytree.Node(20) root.right = binarytree.Node(30) print(root) print() print('Inorder of Nodes', root.inorder) print('Postorder of Nodes', root.postorder) print('Preorder of Nodes', root.preorder) print() print('Size', root.size) print('Height of Tree', root.height)
printBuffer(q, i) c1 = q[:] c2 = q[:] findSum(head.left, s, c1) findSum(head.right, s, c2) def printBuffer(q, level): for i in range(level, len(q), 1): print str(q[i]) + " " print "" temp1 = binarytree.Node(4) temp2 = binarytree.Node(6) temp3 = binarytree.Node(7) temp4 = binarytree.Node(10) temp5 = binarytree.Node(2) temp5.left = temp3 temp5.right = temp4 temp3.left = temp1 temp3.right = temp2 T = binarytree.BinaryTree(temp5) #a = [1,2,3,4,5] q = [] #T = binarytree.BinaryTree(insertkeylist = a) T.printTree()
def pmf_to_SAT1(pmf, depth, ind_start, k, curr_s): clauses = [] def add_equi_clause(p, q, r, clauses): clauses.append([[not (p[0]), p[1]], q]) clauses.append([[not (p[0]), p[1]], r]) clauses.append([p, [not (q[0]), q[1]], [not (r[0]), r[1]]]) def add_rev_impl_clause(p, q, r, clauses): clauses.append([p, [not (q[0]), q[1]], [not (r[0]), r[1]]]) root = False free_leaves = [] transitions = pmf dirac = False node_desc = {} glob_ind = ind_start root_var = [] # state_leaves = 0 S = len(transitions) s_temps = [[] for x in range(S)] for s_i in range(len(transitions)): if transitions[s_i][0] == 1: root = bt.Node(-s_i) node_desc[-s_i] = ([0, True, s_i]) root_var = [True, [2, glob_ind]] s_temps[s_i].append(root_var) # root_var=[True,[3,[s_i + S*k,curr_s]]] glob_ind += 1 dirac = True break # print(dirac) if (dirac == False): root = bt.Node(glob_ind) node_desc[glob_ind] = ([0, False, 0]) root_var = [True, [2, glob_ind]] glob_ind += 1 free_leaves = [root] for d in range(1, depth + 1): next_leaf = 0 child_free = False for s_i in range(len(transitions)): if transitions[s_i][d] == 1: if (child_free == 0): free_leaves[next_leaf].left = bt.Node(-1 * s_i) node_desc[-1 * s_i] = ([d, True, s_i]) free_leaves[next_leaf].value # s <==> ( c_1 ^ ~c ) # s=[True,[3,[s_i + S*k,curr_s]]] s = [True, [2, glob_ind]] s_temps[s_i].append(s) glob_ind += 1 c = [False, [1, d - 1 + depth * (k - 1)]] c_1 = [True, [2, free_leaves[next_leaf].value]] add_equi_clause(s, c_1, c, clauses) # s_leaves[s_i].append([c,c_1]) else: free_leaves[next_leaf].right = bt.Node(-1 * s_i) node_desc[-1 * s_i] = ([d, True, s_i]) # s <==> ( c_1 ^ c ) # s=[True,[3,[s_i + S*k,curr_s]]] s = [True, [2, glob_ind]] s_temps[s_i].append(s) glob_ind += 1 c = [True, [1, d - 1 + depth * (k - 1)]] c_1 = [True, [2, free_leaves[next_leaf].value]] add_equi_clause(s, c_1, c, clauses) # s_leaves[s_i].append([c,c_1]) next_leaf += 1 child_free = not (child_free) if (next_leaf == len(free_leaves)) and (child_free == 0): break next_free_leaves = [] coin_clones = 0 if child_free == 1: free_leaves[next_leaf].right = bt.Node(glob_ind) node_desc[glob_ind] = ([d, False, coin_clones]) # c_2 <==> ( c_1 ^ c ) c_2 = [True, [2, glob_ind]] c = [True, [1, d - 1 + depth * (k - 1)]] c_1 = [True, [2, free_leaves[next_leaf].value]] add_equi_clause(c_2, c_1, c, clauses) glob_ind += 1 coin_clones += 1 next_free_leaves.append(free_leaves[next_leaf].right) next_leaf += 1 child_free = not (child_free) for nl in range(next_leaf, len(free_leaves)): free_leaves[nl].left = bt.Node(glob_ind) node_desc[glob_ind] = ([d, False, coin_clones]) # c_2 <==> ( c_1 ^ ~c ) c_2 = [True, [2, glob_ind]] c = [False, [1, d - 1 + depth * (k - 1)]] c_1 = [True, [2, free_leaves[next_leaf].value]] add_equi_clause(c_2, c_1, c, clauses) glob_ind += 1 coin_clones += 1 free_leaves[nl].right = bt.Node(glob_ind) node_desc[glob_ind] = ([d, False, coin_clones]) # c_2 <==> ( c_1 ^ c ) c_2 = [True, [2, glob_ind]] c = [True, [1, d - 1 + depth * (k - 1)]] c_1 = [True, [2, free_leaves[next_leaf].value]] add_equi_clause(c_2, c_1, c, clauses) glob_ind += 1 coin_clones += 1 next_free_leaves.append(free_leaves[next_leaf].left) next_free_leaves.append(free_leaves[next_leaf].right) free_leaves = next_free_leaves # print(node_desc) # print(dirac) # print(glob_ind) # print(pmf) # print(root) # print(clauses) # for c in clauses: # for lit in c: # if not lit[0]: # print("~",end="") # else: # print(" ",end="") # if lit[1][0] == 0: # print("s",end="") # if lit[1][0] == 1: # print("c",end="") # if lit[1][0] == 2: # print("t",end="") # print(str(lit[1][1]),end="\t") # print("") var_added = glob_ind - ind_start return clauses, var_added, root_var, s_temps # pmf=[[0,0,0,1,0,1],[0,0,1,0,1,1],[0,1,0,0,0,0]] # depth = 5 # pmf_to_SAT(pmf,depth)