prefix_code_tree
Prefix code tree data structure for the coding experiments library.
This module implements a prefix code tree (also known as a decoding tree or code tree) used for efficient decoding of prefix codes. The tree enables unique decoding of variable-length codes without separators between codewords.
- class codinglab.encoders.prefix_code_tree.TreeNode(*, value=None, children=<factory>)[source]
Bases:
Generic[ChannelChar,SourceChar]Node in a prefix code tree. Each node in the tree represents either:
An internal node (value is None): has children for possible next symbols in a code sequence
A leaf node (value is not None): represents a complete code for a source symbol
- Parameters:
value (SourceChar | None)
children (Dict[ChannelChar, TreeNode[ChannelChar, SourceChar]])
- value: SourceChar | None = None
Source symbol at this node, or None for internal nodes.
- children: Dict[ChannelChar, TreeNode[ChannelChar, SourceChar]]
Dictionary mapping channel symbols to child nodes.
- leaf()[source]
Check if this node is a leaf node.
A leaf node has no children, meaning it represents a complete code for a source symbol.
- Return type:
- Returns:
True if this node has no children (is a leaf), False otherwise (is an internal node)
- __init__(*, value=None, children=<factory>)
- Parameters:
value (SourceChar | None)
children (Dict[ChannelChar, TreeNode[ChannelChar, SourceChar]])
- Return type:
None
- class codinglab.encoders.prefix_code_tree.PrefixCodeTree(root=None)[source]
Bases:
Generic[ChannelChar,SourceChar]Prefix code tree for efficient decoding.
This tree data structure enables decoding of prefix codes by traversing from the root to leaves based on the input sequence. Each leaf corresponds to a source symbol, and the path from root to leaf defines the code for that symbol.
- Parameters:
root (TreeNode[ChannelChar, SourceChar] | None)
- root
Root node of the prefix code tree.
- insert_code(code, symbol)[source]
Insert a code sequence into the tree.
This method builds the tree by adding a path from the root corresponding to the code sequence, ending with a leaf node containing the source symbol.
- Parameters:
- Raises:
ValueError – If the code conflicts with existing codes (violates prefix property)
- Return type:
- decode(sequence, position=0)[source]
Decode a symbol from a sequence starting at the given position.
This method traverses the tree from the root, following symbols from the sequence until it reaches a leaf node. It returns the decoded symbol and the new position in the sequence (after consuming the code).
- Parameters:
- Returns:
decoded_symbol: Source symbol decoded, or None if decoding failed
new_position: Position in sequence after consuming the code
- Return type:
Tuple of (decoded_symbol, new_position) where
- Raises:
ValueError – If the sequence is incomplete or contains symbols not in the tree