示例#1
0
文件: Huffman.cs 项目: L-Dogg/ASD2
        private void buildCodesMap(Node node, BitList code)
        {
            // ETAP 2  - tu należy zaimplementować generowanie kodów poszczególnych znaków oraz wypełnianie mapy codesMap
            if (node == null)
            {
                return;
            }
            if (node.left == null && node.right == null && code.Count == 0)
            {
                codesMap.Add(node.character, new BitList("0"));
            }
            else if (node.left == null && node.right == null)
            {
                codesMap.Add(node.character, code);
            }

            var tmp = new BitList(code);

            if (node.left != null)
            {
                code.Append(false);
                buildCodesMap(node.left, code);
            }
            if (node.right != null)
            {
                tmp.Append(true);
                buildCodesMap(node.right, tmp);
            }
        }
示例#2
0
        public BitList Compress(string content)
        {
            // ETAP 2 - wykorzystując dane w codesMap należy zakodować napis przekazany jako parametr content

            BitList compressedContent = new BitList();

            foreach (char c in content)
            {
                compressedContent.Append(codesMap[c]);
            }

            return(compressedContent);
        }
示例#3
0
        private void buildCodesMap(Node node, BitList code)
        {
            // ETAP 2  - tu należy zaimplementować generowanie kodów poszczególnych znaków oraz wypełnianie mapy codesMap

            if (node.character != default(char))
            {
                if (node == root) // Special case, there is only one character, so we should add a 0 to the code
                {
                    code.Append(false);
                }

                codesMap.Add(node.character, code);
            }
            else
            {
                // Not a leaf, continue down the path
                BitList rightSubtreeBitList = new BitList(code);
                code.Append(false);
                buildCodesMap(node.left, code);
                rightSubtreeBitList.Append(true);
                buildCodesMap(node.right, rightSubtreeBitList);
            }
        }
示例#4
0
文件: Huffman.cs 项目: L-Dogg/ASD2
        public BitList Compress(string content)
        {
            // ETAP 2 - wykorzystując dane w codesMap należy zakodować napis przekazany jako parametr content
            if (content.Length == 1)
            {
                return(new BitList("0"));
            }

            codesMap = new Dictionary <char, BitList>();
            root     = null;
            buildHuffmanTree(content);
            buildCodesMap(root, new BitList());
            BitList bl = new BitList();

            for (int i = 0; i < content.Length; i++)
            {
                bl.Append(codesMap[content[i]]);
            }
            return(bl);
        }