def test_giant_deserialize_preorder_inorder(giant_tree): """Writes the tree into two files and checks if they can be read back to create the same tree.""" giant_tree.save_to_disk("giant_tree") deserialized_tree = TreeNode.parse_files(preorder="giant_tree.preorder", inorder="giant_tree.inorder") assert isinstance(deserialized_tree, TreeNode) assert TreeNode.traverse(deserialized_tree) == TreeNode.traverse( giant_tree)
class TestBinaryTree(unittest.TestCase): def setUp(self): self.tree = TreeNode(1, TreeNode(2, TreeNode(3, TreeNode(4), TreeNode(5)), TreeNode(6, TreeNode(7), TreeNode(8))), TreeNode(9, TreeNode(10, TreeNode(11), TreeNode(12)), TreeNode(13, TreeNode(14), TreeNode(15)))) def test_print(self): print(self.tree.print_structure()) def test_traverse(self): self.tree.traverse(do_dfs_r)
def test_basic_deserialize_preorder_inorder(basic_tree): """Writes the tree into two files and checks if they can be read back to create the same tree.""" basic_tree.save_to_disk("basic_tree") deserialized_tree = TreeNode.parse_files(preorder="basic_tree.preorder", inorder="basic_tree.inorder") assert TreeNode.traverse(basic_tree, mode="preorder") == TreeNode.traverse( deserialized_tree, mode="preorder") assert TreeNode.traverse( basic_tree, mode="postorder") == TreeNode.traverse(deserialized_tree, mode="postorder") assert TreeNode.traverse(basic_tree, mode="inorder") == TreeNode.traverse( deserialized_tree, mode="inorder")
def test_longer_deserialize_postorder_inorder(longer_tree): """Writes the tree into two files and checks if they can be read back to create the same tree.""" longer_tree.save_to_disk("longer_tree", postorder=True, preorder=False, inorder=True) deserialized_tree = TreeNode.parse_files(postorder="longer_tree.postorder", inorder="longer_tree.inorder") assert TreeNode.traverse( longer_tree, mode="preorder") == TreeNode.traverse(deserialized_tree, mode="preorder") assert TreeNode.traverse( longer_tree, mode="postorder") == TreeNode.traverse(deserialized_tree, mode="postorder") assert TreeNode.traverse(longer_tree, mode="inorder") == TreeNode.traverse( deserialized_tree, mode="inorder")
def test_basic_serialize_postorder(basic_tree): """Tests the serialization of the basic tree with postorder traversal.""" serialized_tree = TreeNode.traverse(basic_tree, mode="postorder") assert serialized_tree == [4, 5, 2, 3, 1]
def test_basic_serialize_inorder(basic_tree): """Tests the serialization of the basic tree with inorder traversal.""" serialized_tree = TreeNode.traverse(basic_tree, mode="inorder") assert serialized_tree == [4, 2, 5, 1, 3]
def test_basic_serialize_preorder(basic_tree): """Tests the serialization of the basic tree with preorder.""" serialized_tree = TreeNode.traverse(basic_tree, mode="preorder") assert serialized_tree == [1, 2, 4, 5, 3]
def test_longer_serialize_postorder(longer_tree): """Tests the serialization of the longer tree with postorder traversal.""" serialized_tree = TreeNode.traverse(longer_tree, mode="postorder") assert serialized_tree == [ 4, 12, 10, 18, 24, 22, 15, 31, 44, 35, 66, 90, 70, 50, 25 ]
def test_longer_serialize_inorder(longer_tree): """Tests the serialization of the longer tree with inorder traversal.""" serialized_tree = TreeNode.traverse(longer_tree, mode="inorder") assert serialized_tree == [ 4, 10, 12, 15, 18, 22, 24, 25, 31, 35, 44, 50, 66, 70, 90 ]
def test_longer_serialize_preorder(longer_tree): """Tests the serialization of the longer tree with preorder.""" serialized_tree = TreeNode.traverse(longer_tree, mode="preorder") assert serialized_tree == [ 25, 15, 10, 4, 12, 22, 18, 24, 50, 35, 31, 44, 70, 66, 90 ]