def test_find_missing(self): from Question1 import createtree, readwords, tree_find words = readwords("Words.txt") bintree = createtree(words) expected = False try: actual = tree_find("missing", bintree, True).value except AttributeError: actual = False self.assertEqual(actual, expected)
def test_readwords(self): from Question1 import readwords expected = [ 'to', 'make', 'your', 'document', 'look', 'professionally', 'produced', 'word', 'provides', 'header', 'footer', 'cover', 'page', 'and', 'text', 'box', 'designs', 'that', 'complement', 'each', 'other', 'for', 'example', 'you', 'can', 'add', 'a', 'matching', 'cover', 'page', 'header', 'and', 'sidebar', 'click', 'insert', 'and', 'then', 'choose', 'the', 'elements', 'you', 'want', 'from', 'the', 'different', 'galleries' ] self.assertEqual(readwords("Words.txt"), expected)
def test_tree_print_in_order(self): from Question1 import createtree, readwords, printinorder words = readwords("Words.txt") bintree = createtree(words) expected = [ 'a', 'add', 'and', 'and', 'and', 'box', 'can', 'choose', 'click', 'complement', 'cover', 'cover', 'designs', 'different', 'document', 'each', 'elements', 'example', 'footer', 'for', 'from', 'galleries', 'header', 'header', 'insert', 'look', 'make', 'matching', 'other', 'page', 'page', 'produced', 'professionally', 'provides', 'sidebar', 'text', 'that', 'the', 'the', 'then', 'to', 'want', 'word', 'you', 'you', 'your' ] self.assertEqual(printinorder(bintree), expected)
def test_tree_count(self): from Question1 import createtree, readwords, tree_count words = readwords("Words.txt") bintree = createtree(words) expected = { 'a': 1, 'add': 1, 'and': 3, 'box': 1, 'can': 1, 'choose': 1, 'click': 1, 'complement': 1, 'cover': 2, 'designs': 1, 'different': 1, 'document': 1, 'each': 1, 'elements': 1, 'example': 1, 'footer': 1, 'for': 1, 'from': 1, 'galleries': 1, 'header': 2, 'insert': 1, 'look': 1, 'make': 1, 'matching': 1, 'other': 1, 'page': 2, 'produced': 1, 'professionally': 1, 'provides': 1, 'sidebar': 1, 'text': 1, 'that': 1, 'the': 2, 'then': 1, 'to': 1, 'want': 1, 'word': 1, 'you': 2, 'your': 1 } self.assertEqual(tree_count(bintree), expected)
def test_find_in(self): from Question1 import createtree, readwords, tree_find words = readwords("Words.txt") bintree = createtree(words) expected = "look" self.assertEqual(tree_find("look", bintree, True).value, expected)
pass return True return False def find_min_node(node): """ Find the smallest item in a tree :param node: tree defined by class Node :return: node """ # if there is a node to the left, go left else return the node if node.left is None: return node return find_min_node(node.left) if __name__ == "__main__": words = readwords("Words.txt") bintree = createtree(words) nodetodelete = tree_find("box", bintree, False) if nodetodelete is False: print("Word not found") else: print("In order before deletion \n", printinorder(bintree)) delete_node(nodetodelete) print("In order after deleting the word box \n", printinorder(bintree))