A method for encoding an input sequence of binary symbols using a context model having a set of context states and a respective 2 L -ary encoding tree associated with each context state, the method comprising: for a length-L string of consecutive symbols, where L is an integer greater than 1, determining the context state for a first symbol of the length-L string, and selecting the 2 L -ary encoding tree associated with that context state; appending subsequent length-L strings of consecutive symbols to the length-L string of consecutive symbols to form a sequence of strings wherein the appending comprises traversing one level of the selected 2 L -ary encoding tree based on each length-L string of consecutive symbols in the sequence of strings and including determining that an active node is a root node and in response to determining that the active node is a root node allocating a position in a buffer element of a first-in-first-out buffer; determining that the active node is an internal node and in response to determining that the active node is an internal node, determining a next active node by the subsequent length-L string of consecutive symbols; determining that the next active node is a leaf node such that one of the subsequent length-L strings completes the sequence of strings by reaching the leaf node in the selected encoding tree; encoding that sequence of strings as a codeword using the selected 2 L -ary encoding tree; storing the codeword in the buffer element of the first-in-first-out buffer, wherein the buffer element is associated with the context state of the codeword; outputting the codeword corresponding to the leaf node from the buffer in a first-in-first-out order, wherein the codeword is output in a sequential order corresponding to the order in which encoding started; updating the context state based on the symbols in that sequence of strings and the context model; and returning to select an encoding tree based on the updated context state and continue encoding the input sequence of binary symbols. for a length-L string of consecutive symbols, where L is an integer greater than 1, determining the context state for a first symbol of the length-L string, and selecting the 2 L -ary encoding tree associated with that context state; appending subsequent length-L strings of consecutive symbols to the length-L string of consecutive symbols to form a sequence of strings wherein the appending comprises traversing one level of the selected 2 L -ary encoding tree based on each length-L string of consecutive symbols in the sequence of strings and including determining that an active node is a root node and in response to determining that the active node is a root node allocating a position in a buffer element of a first-in-first-out buffer; determining that the active node is an internal node and in response to determining that the active node is an internal node, determining a next active node by the subsequent length-L string of consecutive symbols; determining that the next active node is a leaf node such that one of the subsequent length-L strings completes the sequence of strings by reaching the leaf node in the selected encoding tree; determining that an active node is a root node and in response to determining that the active node is a root node allocating a position in a buffer element of a first-in-first-out buffer; determining that the active node is an internal node and in response to determining that the active node is an internal node, determining a next active node by the subsequent length-L string of consecutive symbols; determining that the next active node is a leaf node such that one of the subsequent length-L strings completes the sequence of strings by reaching the leaf node in the selected encoding tree; encoding that sequence of strings as a codeword using the selected 2 L -ary encoding tree; storing the codeword in the buffer element of the first-in-first-out buffer, wherein the buffer element is associated with the context state of the codeword; outputting the codeword corresponding to the leaf node from the buffer in a first-in-first-out order, wherein the codeword is output in a sequential order corresponding to the order in which encoding started; updating the context state based on the symbols in that sequence of strings and the context model; and returning to select an encoding tree based on the updated context state and continue encoding the input sequence of binary symbols.