def test_tree(self): """Test that the tree structure is as expected: all nodes have correct parent and children. """ tree = Tree() root = tree.add_root(1) left = root.add_left(2) right = root.add_right(3) self.assertEqual(None, root.parent) self.assertEqual(root, tree.root) self.assertEqual(left, tree.root.left) self.assertEqual(right, tree.root.right) self.assertEqual(root, left.parent) self.assertEqual(root, right.parent) self.assertEqual(None, left.left) self.assertEqual(None, left.right) self.assertEqual(None, right.left) self.assertEqual(None, right.right)
def breadth_first_test(self): """Test that the breadth first traversal returns nodes in the correct order: left to right, top to bottom; processing all nodes on a level from left to right before proceeding down a level. E.g. 1 2 3 4 5 6 7 """ tree = Tree() root = tree.add_root(1) left = root.add_left(2) right = root.add_right(3) left.add_left(4) left.add_right(5) right.add_left(6) right.add_right(7) items = [] for item in tree.breadth_first(): items.append(item.data) self.assertEqual(items, [1, 2, 3, 4, 5, 6, 7])