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))
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_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])