public void BuildCodes() { HuffmanCodes.Clear(); HuffmanTreeNode Root = HuffmanTreeList.First(); BuildBinaryCodesInternal(Root, Root.BinaryCode); }
public long Decode(BitArray EncodedData, byte[] DecodedData) { long WrittenData = 0; HuffmanTreeNode CurrentNode = HuffmanTreeList.First(); foreach (bool EncodedByte in EncodedData) { CurrentNode = EncodedByte ? CurrentNode.Right ?? CurrentNode : CurrentNode.Left ?? CurrentNode; if (CurrentNode.Name != null) { DecodedData[WrittenData] = (byte)CurrentNode.Name.Value; WrittenData++; CurrentNode = HuffmanTreeList.First(); } } return(WrittenData - 1); }
private void BuildBinaryCodesInternal(HuffmanTreeNode Node, List <bool> Code) { Node.BinaryCode = new List <bool>(Code); if (Node.Name != null) { HuffmanCodes.Add(Node.Name.Value, Node.BinaryCode); } if (Node.Left != null) { var LeftList = new List <bool>(Code); LeftList.Add(false); BuildBinaryCodesInternal(Node.Left, LeftList); } if (Node.Right != null) { var RightList = new List <bool>(Code); RightList.Add(true); BuildBinaryCodesInternal(Node.Right, RightList); } }