def sortedArrayToBST(nums): T = Tree() if len(nums) == 0: return T else: T.root = SubTreeSlove(nums, 0, len(nums) - 1) return T
def crossover(tree_1, tree_2, depth): numbNodes = helper.calculateNumberOfNodes(depth) crossPoint = random.randint(2, numbNodes - 1) #print("CrossPoint:", crossPoint, "Number nodes:", numbNodes) #print("Tree_1:", tree_1.creationList) #print("Tree_2:", tree_2.creationList) list_1 = tree_1.creationList[0:crossPoint] + tree_2.creationList[ crossPoint:int(numbNodes)] list_2 = tree_2.creationList[0:crossPoint] + tree_1.creationList[ crossPoint:int(numbNodes)] #print() #print("List_1:", list_1) #print("List_2:", list_2) newTree_1 = Tree() newTree_2 = Tree() newTree_1.createFromList(list_1) newTree_2.createFromList(list_2) return newTree_1, newTree_2
def test_Tree_CalculateValuesSubTree(tree, tree_sum, tree_avg, tree_median): t = Tree(tree) assert t.get_sum(full_tree=False, sub_tree=t.root.right) == tree_sum, "Incorrect sum" assert t.get_mean(full_tree=False, sub_tree=t.root.right) == tree_avg, "Incorrect average" assert t.get_median( full_tree=False, sub_tree=t.root.right) == tree_median, "Incorrect median"
def createPopulation(lengthPopu, dephTree): population = [] numbOperators = int(helper.calculateNumberOfNodes(dephTree - 1)) numbLeaves = int( helper.calculateNumberOfNodes(dephTree)) - numbOperators for i in range(lengthPopu): chromOperators = helper.createRandomOperationList(numbOperators) chromNumbers = helper.createRandomNumbersList(numbLeaves) tree = Tree() tree.createFromList(chromOperators + chromNumbers) population.append(tree) return population
def main(): inputArray = input().split() tree = Tree(inputArray) print("Iterative Preorder: ") iterativePreorder(tree) print("Iterative Inorder: ") iterativeInorder(tree) print("Iterative Postorder with 2 stacks: ") iterativePostorderWith2Stacks(tree) print("Iterative Postorder with 1 stack: ") iterativePostorderWith1Stack(tree)
def __init__(self, window_size, embedding_size, whole_size, step_size=0.01, Theta=None): self.window_size = window_size self.embedding_size = embedding_size self.whole_size = whole_size self.step_size = step_size # Theta if Theta is not None: self.Theta = Theta else: self.Theta = np.random.rand(self.whole_size, self.embedding_size) self.tree = Tree(self.whole_size, self.embedding_size) self.tree.growTree()
def test_Tree_RaisesStatisticsErrorOnEmptyTree(a): t = Tree(a) assert t.get_sum(full_tree=True) == 0, "Incorrect sum" with pytest.raises(statistics.StatisticsError): t.get_median(full_tree=True) t.get_mean(full_tree=True)
def test_Tree_CalculateValuesFullTree(tree, tree_sum, tree_avg, tree_median): t = Tree(tree) assert t.get_sum(full_tree=True) == tree_sum, "Incorrect sum" assert t.get_mean(full_tree=True) == tree_avg, "Incorrect average" assert t.get_median(full_tree=True) == tree_median, "Incorrect median"
def test_Tree_RaisesTypeErrorWhenInvalidRootOnInit(a): with pytest.raises(TypeError): _ = Tree(a)
def test_Tree_HandlesLoops(): a = Node(5) t = Tree(Node(3, Node(3, a), a)) assert t.get_sum(full_tree=True) == 11, "Incorrect sum"
def __init__(self): self.__binaryTree = Tree()
sum -= u.item # means we have reach node u if sum == 0 and u.left == None and u.right == None: # if node u is leave_node and subSum is 0 return True # reach node u, next to check U_child # regard u_child as new root elif u.left and zp_dfs(u.left, sum): return True elif u.right and zp_dfs(u.right, sum): return True else: return False def hasPathSum(root, sum): if root == None: # no path return False else: return zp_dfs(root, sum) sum = 22 q = Tree() test1 = [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1] q.add(test1) print(q.preorder(q.root)) q_node = q.root if hasPathSum(q_node, sum): print('True') else: print('False')
from BinaryTree import Tree from BinaryTree import Node import math from Helper import helper import random print('-----Test generate a Tree-----') tree = Tree() data = [1, 2, 3, 4, 5, 6, 7, 8, 9] tree.createFromList(data) tree.printTree() print('-----Test find a node-----') node = tree.getListNodes()[7] print('Node: ', node) nodeFound = tree.find(node) print('Parent node found: ', nodeFound) nodeFound = tree.find(Node(6)) print('Parent node found: ', nodeFound) print('----c-Test eval()-----') x = 8 print(eval('((8)*(x))')) print(eval('((9)*(math.sin((7)*(x))))')) print(eval('(((8)*(x))+((0)*(x)*(1)*(x)))')) print('-----Test number of Nodes-----') print('Deph 1:', helper.calculateNumberOfNodes(1)) print('Deph 2:', helper.calculateNumberOfNodes(2)) print('Deph 3:', helper.calculateNumberOfNodes(3)) print('Deph 4:', helper.calculateNumberOfNodes(4))
# / \ # 10 30 # / \ / \ # 5 15 25 35 # / \ # 1 28 a.left = b a.right = c b.left = d b.right = e c.left = f c.right = g d.left = h f.right = i #EX-2: RETURN A LIST OF NODE VALUES FROM A TREE IN Breadth-first traversal ORDER: # tree = Tree(a) e = tree.find_min() c = tree.find_max() d = tree.insert(Node(7)) f = tree.insert(Node(8)) # print(e) # print(c) print(d) print(f)
def main(): inputArray = input().split() tree = Tree(inputArray) levelOrderTraversal(tree)
from BinaryTree import Tree """ BinaryTree.py has been defined """ p = Tree() q = Tree() test1 = [1, None, 2] test2 = [1, 2] p.add(test1) q.add(test2) p_node = p.root q_node = q.root def isSameTree(p, q): if p is None and q is None: return True if p is None or q is None: return False if p.item == q.item: return isSameTree(p.left, q.left) and isSameTree(p.right, q.right) else: return False if isSameTree(p_node, q_node): print('True') else: print('False')
if option2 == 1: print("\n" + space + "\n| Cadastro de paciente |\n" + space + "\n") nome = str(input("Nome: ")) sexo = str(input("Sexo\n[M]Masculino\n[F]Feminino\n ")) cpf = str(input("CPF: ")) rg = str(input("RG: ")) dia = str(input("Dia Nascimento: ")) mes = str(input("Mes Nascimento: ")) ano = str(input("Ano Nascimento: ")) ctsus = int(input("Cartao SUS: ")) sintomas = str(input("Quais os sintomas: ")) paciente = Pessoa(nome, sexo, cpf, rg, dia, mes, ano, ctsus, sintomas) treePaciente = Tree(paciente) tmp = int( input( "É prioritario? (Acima de 60 anos ou febre?)\n[0] Não \n[1] Sim " )) if tmp == 1: filaPrioritaria.inserirPrioritario(treePaciente) database.inserir(treePaciente) else: filadePessoas.inserir(treePaciente) database.inserir(treePaciente) time.sleep(0.5) else: print("Selecione de quem voce vai adicionar os pais: ") eu = database.searchByIndex() print("Selecione quem é o pai: ")