def test_is_balanced_false(self): init = [-20, -50, -15, -60, 50, 60, 55, 85, 15, 5, -10] tree = BinaryTree(50) for i in init: tree.insert(i) print(tree) self.assertFalse(is_balanced(tree))
def test_is_balanced_true(self): init = [-30, -20, -40, 50, 70, 60, 90] tree = BinaryTree(50) for i in init: tree.insert(i) print(tree) self.assertTrue(is_balanced(tree))
class Test(unittest.TestCase): init1, init2 = [-20, -50, -15, -60, 60, 55, 85, 15, 5, -10], [55, 85] tree1, tree2 = BinaryTree(50), BinaryTree(60) def test_contains_tree_01(self): for i in self.init1: self.tree1.insert(i) for i in self.init2: self.tree2.insert(i) self.assertTrue(contains_tree(self.tree1, self.tree2)) def test_contains_tree_02(self): self.tree2.insert(95) self.assertFalse(contains_tree(self.tree1, self.tree2))
class Test(unittest.TestCase): init = [-20, -50, -15, -60, 60, 55, 85, 15, 5, -10] tree = BinaryTree(50) def test_nextNode(self): for i in self.init: self.tree.insert(i) print(self.tree) self.assertEqual(-10, next_node(self.tree.left.right).value)
class Test(unittest.TestCase): init = [-20, -50, -15, -60, 60, 55, 85, 15, 5, -10] tree = BinaryTree(50) def test_common_ancestor(self): for i in self.init: self.tree.insert(i) self.assertEqual( self.tree, common_ancestor(self.tree.left.left, self.tree.right.right))
class Test(unittest.TestCase): init = [-20, -50, -15, -60, 60, 55, 85, 15, 5, -10] tree = BinaryTree(50) def test_01_checkBST_true(self): for i in self.init: self.tree.insert(i) self.assertTrue(checkBST(self.tree)) def test_02_checkBST_false(self): self.tree.insert(50) self.assertFalse(checkBST(self.tree)) def test_03_checkBST_min_max_true(self): self.assertTrue(checkBST_min_max(self.tree)) def test_04_checkBST_min_max_false(self): self.tree.left.right.value = -25 self.assertFalse(checkBST_min_max(self.tree))
def test_count_paths(self): tree = BinaryTree(50) tree.insert(-20) tree.insert(-10) tree.insert(-10) tree.insert(-20) tree.insert(20) tree.insert(55) tree.insert(8) tree.insert(15) tree.insert(5) tree.insert(-10) print(tree) self.assertEqual(3, count_path(tree, 10))
# добавляем уровень i-1 result.append(current) parents = current # переходим на уровень i current = LinkedList() for parent in parents: # для уровня i сохраняем потомков узла i-1 if parent.item.left: current.insert_at_end(parent.item.left) if parent.item.right: current.insert_at_end(parent.item.right) return result if __name__ == '__main__': init = [-20, -50, -15, -60, 50, 60, 55, 85, 15, 5, -10] tree = BinaryTree(50) for i in init: tree.insert(i) print(tree) print('Result with modified DFS algorithm:') lists = createListsDFS(tree) for i in lists: print(i) print('Result with modified BFS algorithm:') lists = createListsBFS(tree) for i in lists: print([j.item.value for j in i])
# print('second', second) for i in second: result.insert_at_end(i) results.append(result) return head_first = first.start_node.item first.delete_at_start() prefix.insert_at_end(head_first) weave_lists(first, second, results, prefix) prefix.delete_at_end() first.insert_at_start(head_first) head_second = second.start_node.item second.delete_at_start() prefix.insert_at_end(head_second) weave_lists(first, second, results, prefix) prefix.delete_at_end() second.insert_at_start(head_second) if __name__ == '__main__': init = [1, 2, 4, 5] tree = BinaryTree(3) for i in init: tree.insert(i) res = all_sequences(tree) print(tree) for i in res: print(i)