def test_tree_exotic(self): node_4_6 = Node(8) node_3_2 = Node(7) node_3_3 = Node(5, None, node_4_6) node_2_1 = Node(4, None, node_3_2) node_2_2 = Node(3, node_3_3, None) node_1_1 = Node(1, node_2_1, node_2_2) tree = Tree(node_1_1) answer = [[ '|', '|', '|', '|', '|', '|', '|', '1', '|', '|', '|', '|', '|', '|', '|' ], [ '|', '|', '|', '4', '|', '|', '|', '|', '|', '|', '|', '3', '|', '|', '|' ], [ '|', '|', '|', '|', '|', '7', '|', '|', '|', '5', '|', '|', '|', '|', '|' ], [ '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '8', '|', '|', '|', '|' ]] assert tree.tree_print() == answer
def test_tree_full(self): node_4_1 = Node(8) node_4_2 = Node(9) node_4_3 = Node(10) node_4_4 = Node(11) node_4_5 = Node(12) node_4_6 = Node(13) node_4_7 = Node(14) node_4_8 = Node(15) node_3_1 = Node(4, node_4_1, node_4_2) node_3_2 = Node(5, node_4_3, node_4_4) node_3_3 = Node(6, node_4_5, node_4_6) node_3_4 = Node(7, node_4_7, node_4_8) node_2_1 = Node(2, node_3_1, node_3_2) node_2_2 = Node(3, node_3_3, node_3_4) node_1_1 = Node(1, node_2_1, node_2_2) tree = Tree(node_1_1) answer = [[ '|', '|', '|', '|', '|', '|', '|', '1', '|', '|', '|', '|', '|', '|', '|' ], [ '|', '|', '|', '2', '|', '|', '|', '|', '|', '|', '|', '3', '|', '|', '|' ], [ '|', '4', '|', '|', '|', '5', '|', '|', '|', '6', '|', '|', '|', '7', '|' ], [ '8', '|', '9', '|', '10', '|', '11', '|', '12', '|', '13', '|', '14', '|', '15' ]] assert tree.tree_print() == answer
def setUp(self): tree_1 = Tree(1, None, None) tree_2 = Tree(2, None, None) tree_3 = Tree(3, None, None) tree_4 = Tree(4, None, None) "arbol -> (((4 + 2) - (3 * 1)) / 3" tree_plus = Tree("+", tree_4, tree_2) tree_mul = Tree("*", tree_3, tree_1) tree_minus = Tree("-", tree_plus, tree_mul) tree_test = Tree("/", tree_minus, tree_3) self.tree_chromosome_test = TreeChromosome(tree_test)
class TreeTest(unittest.TestCase): def setUp(self): tree_a = Tree(1, None, None) tree_b = Tree(2, None, None) self.tree_a = tree_a self.tree_c = Tree("+", tree_a, tree_b) self.tree_plus = Tree("+", tree_a, tree_b) self.tree_minus = Tree("-", tree_a, tree_b) self.tree_mul = Tree("*", tree_a, tree_b) self.tree_div = Tree("/", tree_a, tree_b) def test_print(self): """ Prueba que la impresion del arbol sea correcta """ self.assertEqual("(1+2)", self.tree_c.__str__()) def test_evaluate(self): """ Prueba la evaluacion del arbol para las cuatro operaciones """ self.assertEqual(3, self.tree_plus.evaluate()) self.assertEqual(-1, self.tree_minus.evaluate()) self.assertEqual(2, self.tree_mul.evaluate()) self.assertEqual(0.5, self.tree_div.evaluate()) def test_get_depth(self): self.assertEqual(2, self.tree_c.get_depth()) self.assertEqual(1, self.tree_a.get_depth())
def test_tree_single(self): node_4_1 = Node(4) node_3_1 = Node(3, node_4_1, None) node_2_1 = Node(2, node_3_1, None) node_1_1 = Node(1, node_2_1, None) tree = Tree(node_1_1) answer = [[ '|', '|', '|', '|', '|', '|', '|', '1', '|', '|', '|', '|', '|', '|', '|' ], [ '|', '|', '|', '2', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|' ], [ '|', '3', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|' ], [ '4', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|' ]] assert tree.tree_print() == answer
def reproduction(self, gen, solution, functions): self.evaluate_fitness(solution) gen.evaluate_fitness(solution) best = self tree_a_nodes = [ self.value.copy_tree(), self.value.left.copy_tree(), self.value.right.copy_tree() ] tree_b_nodes = [ gen.value.copy_tree(), gen.value.left.copy_tree(), gen.value.right.copy_tree() ] for fun in functions: for a_node in tree_a_nodes: for b_node in tree_b_nodes: new_tree = TreeChromosome(Tree(fun, a_node, b_node)) new_tree.evaluate_fitness(solution) if new_tree.score < best.score: best = new_tree return best
def test_tree_one_node(self): node_a = Node(10) tree = Tree(node_a) answer = [['10']] assert tree.tree_print() == answer
def create_random_tree(self, depth=0): selector = random.randint(0, 1) if (selector == 0 or depth == self.max_depth) and depth > 1: return Tree(self.select_random_terminal(), None, None) else: return Tree(self.select_random_function(), self.create_random_tree(depth + 1), self.create_random_tree(depth + 1))
leftSum = 1000000 rightSum = 1000000 if currentNode.leftChild != None: tuple = dfs(currentNode.leftChild, sum + currentNode.leftChild.item) leftItem = tuple[0] leftSum = tuple[1] if currentNode.rightChild != None: tuple = dfs(currentNode.rightChild, sum + currentNode.rightChild.item) rightItem = tuple[0] rightSum = tuple[1] if leftSum < rightSum: return leftItem, leftSum elif leftSum > rightSum: return rightItem, rightSum else: if leftItem < rightItem: return leftItem, leftSum else: return rightItem, rightSum if __name__ == '__main__': inorder = list(map(int, input().split(" "))) # 中序遍历 postorder = list(map(int, input().split(" "))) # 后序遍历 root = Node(postorder[-1]) build(0, len(postorder) - 1, root, 0) tree = Tree(root) print(dfs(root, 0)[0])
def setUp(self): tree_a = Tree(1, None, None) tree_b = Tree(2, None, None) self.tree_a = tree_a self.tree_c = Tree("+", tree_a, tree_b) self.tree_plus = Tree("+", tree_a, tree_b) self.tree_minus = Tree("-", tree_a, tree_b) self.tree_mul = Tree("*", tree_a, tree_b) self.tree_div = Tree("/", tree_a, tree_b)