public void insert(final String pattern, final T value) throws ImagingException {
        int position = 0;
        Node<T> node = growAndGetNode(position);
        if (node.value != null) {
            throw new ImagingException("Can't add child to a leaf");
        }
        for (int patternPosition = 0; patternPosition < pattern.length(); patternPosition++) {
            final char nextChar = pattern.charAt(patternPosition);
            if (nextChar == '0') {
                position = (position << 1) + 1;
            } else {
                position = position + 1 << 1;
            }
            node = growAndGetNode(position);
            if (node.value != null) {
                throw new ImagingException("Can't add child to a leaf");
            }
        }
        node.value = value;
    }
A Huffman tree implemented as 1 array for high locality of reference.
src/main/java/org/apache/commons/imaging/formats/tiff/itu_t4/HuffmanTree.java