{"record":{"id":"54176c845311a5ec","repo":"TheAlgorithms/Python","slug":"data-cannot-be-empty","errorCode":null,"errorMessage":"Data cannot be empty.","messagePattern":"Data cannot be empty\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/binary_tree/serialize_deserialize_binary_tree.py","lineNumber":117,"sourceCode":"    >>> root == deserialized\n    True\n    >>> root is deserialized  # two separate trees\n    False\n    >>> root.right.right.value = 6\n    >>> root == deserialized\n    False\n    >>> serialzed_data = repr(root)\n    >>> deserialized = deserialize(serialzed_data)\n    >>> root == deserialized\n    True\n    >>> deserialize(\"\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Data cannot be empty.\n    \"\"\"\n\n    if not data:\n        raise ValueError(\"Data cannot be empty.\")\n\n    # Split the serialized string by a comma to get node values\n    nodes = data.split(\",\")\n\n    def build_tree() -> TreeNode | None:\n        # Get the next value from the list\n        value = nodes.pop(0)\n\n        if value == \"null\":\n            return None\n\n        node = TreeNode(int(value))\n        node.left = build_tree()  # Recursively build left subtree\n        node.right = build_tree()  # Recursively build right subtree\n        return node\n\n    return build_tree()\n","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/binary_tree/serialize_deserialize_binary_tree.py#L99-L135","documentation":"Raised by deserialize(data) (serialize_deserialize_binary_tree.py) when data is empty (or otherwise falsy, e.g. None). The serialized form is a comma-separated preorder string like '1,2,null,null,3,null,null'; an empty string yields no tokens and the recursive build_tree would fail incoherently, so the function guards with ValueError('Data cannot be empty.') up front.","triggerScenarios":"deserialize(''); deserialize(None) after repr() of a None root or a failed read; feeding an empty file/network response into the function; whitespace-only string is NOT caught (only falsy values) and will fail later at int('').","commonSituations":"Round-tripping trees through files that may be empty; API endpoints returning empty bodies; str.strip() applied before deserialize leaving ''.","solutions":["Check the payload before calling: `if not data: return None` (an empty tree deserializes to no root)","Validate at the I/O boundary — reject empty files/responses before they reach the deserializer","Strip carefully: `data = data.strip()` then branch on emptiness rather than passing whitespace"],"exampleFix":"# before\ntree = deserialize(open(path).read())  # empty file -> ValueError\n\n# after\nraw = open(path).read().strip()\ntree = deserialize(raw) if raw else None","handlingStrategy":"validation","validationCode":"data = data.strip() if isinstance(data, str) else data\nroot = deserialize(data) if data else None","typeGuard":"def is_serialized_payload(data: object) -> bool:\n    return isinstance(data, str) and len(data) > 0 and data.split(',')[0] not in ('', 'null')","tryCatchPattern":"try:\n    root = deserialize(data)\nexcept ValueError:\n    root = None  # empty serialized tree","preventionTips":["Strip and check emptiness after every file/network read that feeds deserialize","Treat empty payload as an empty tree (None root) at the caller","Round-trip test serialize(deserialize(s)) to catch corrupted payloads early"],"tags":["serialization","empty-input","deserialization","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}