{"record":{"id":"40f9f53bab5f7390","repo":"donnemartin/interactive-coding-challenges","slug":"root-is-none","errorCode":null,"errorMessage":"root is None","messagePattern":"root is None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/tree_bfs/bfs_solution.ipynb","lineNumber":100,"sourceCode":"   \"outputs\": [],\n   \"source\": [\n    \"%run ../bst/bst.py\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"from collections import deque\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class BstBfs(Bst):\\n\",\n    \"\\n\",\n    \"    def bfs(self, visit_func):\\n\",\n    \"        if self.root is None:\\n\",\n    \"            raise TypeError('root is None')\\n\",\n    \"        queue = deque()\\n\",\n    \"        queue.append(self.root)\\n\",\n    \"        while queue:\\n\",\n    \"            node = queue.popleft()\\n\",\n    \"            visit_func(node)\\n\",\n    \"            if node.left is not None:\\n\",\n    \"                queue.append(node.left)\\n\",\n    \"            if node.right is not None:\\n\",\n    \"                queue.append(node.right)\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]\n  },","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/tree_bfs/bfs_solution.ipynb#L82-L118","documentation":"BstBfs.bfs raises TypeError('root is None') when the tree is empty, because the traversal requires a starting node to seed the deque. The guard clause makes the empty-tree case explicit instead of silently returning without visiting anything.","triggerScenarios":"Calling bfs(visit_func) on a BstBfs instance whose root was never set, e.g. tree = BstBfs(); tree.bfs(print).","commonSituations":"Testing edge cases with empty trees, building the BST from an empty input list, or forgetting to insert nodes before traversal in a demo notebook.","solutions":["Insert nodes before calling bfs()","Guard the call: if tree.root is not None: tree.bfs(visit_func)","Catch TypeError when deliberately testing the empty-tree path"],"exampleFix":"// before\ntree = BstBfs()\ntree.bfs(print)  # TypeError\n// after\ntree = BstBst() if False else BstBfs()\nif tree.root is not None:\n    tree.bfs(print)","handlingStrategy":"type-guard","validationCode":"assert tree.root is not None, 'cannot traverse an empty tree'","typeGuard":"def has_root(tree) -> bool:\n    return tree.root is not None","tryCatchPattern":"try:\n    tree.bfs(visit_func)\nexcept TypeError:\n    pass  # empty tree, nothing to visit","preventionTips":["Build the BST from non-empty input before traversal","Guard traversal calls in generic test harnesses"],"tags":["tree","bfs","traversal","none-guard","python"],"backgroundTag":"none-argument-guard","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}