public void encode(char c, BitOutputStream bos) throws IOException {

        int nodeNumber = currentNumber;

        for (int i = 512; i >= currentNumber; --i) {

            // found leaf with symbol
            if (c == nodes[i].symbol && nodes[i].left == null) {

                nodeNumber = i;
                break;
            }
        }
        
        BitSet reverseEncoding = new BitSet();
        int length = 0;
        
        // found nyt node, append reverse encoding of c
        if (nodeNumber == currentNumber) {
            
            for (int i = 0; i < 8; ++i) {
                reverseEncoding.set(7 - i, (c & (1 << i)) != 0);
            }

            length = 8;
        }

        Node currentNode = nodes[nodeNumber];
        
        // ascend the tree unitl the root node is found and add encoding
        while (currentNode.parent != null) {
            if (currentNode.parent.right == currentNode) {
                reverseEncoding.set(length);
            }
            
            currentNode = currentNode.parent;
            length = length + 1;
        }
        
        // reverse the encoding
        BitSet encoding = new BitSet(length);
        
        for (int i = 0; i < length; ++i) {
            if (reverseEncoding.get(length - i - 1)) {
                encoding.set(i);
            }
        }
        
        update(nodeNumber, c);
                
        bos.write(encoding, length);
    }