private static void FillEncodeTable(HuffmanNode node, BitsWithLength[] encodeSubstitutionTable, byte bitvector = 0, byte depth = 0)
 {
     if (node.LeafLabel != null)
     {
         encodeSubstitutionTable[node.LeafLabel.Value] = new BitsWithLength(bitvector, depth);
     }
     else
     {
         if (node.Left != null)
         {
             FillEncodeTable(node.Left, encodeSubstitutionTable, (byte)((bitvector << 1) + 1), (byte)(depth + 1));
             FillEncodeTable(node.Right, encodeSubstitutionTable, (byte)((bitvector << 1) + 0), (byte)(depth + 1));
         }
     }
 }
Exemplo n.º 2
0
        public void HuffmanDepthTest()
        {
            HuffmanNode node1 = new HuffmanNode(1, 1);
            HuffmanNode node2 = new HuffmanNode(2, 1);
            HuffmanNode node3 = new HuffmanNode(5, 5);
            HuffmanNode node4 = new HuffmanNode(7, 7);
            HuffmanNode node5 = new HuffmanNode(10, 10);
            HuffmanNode node6 = new HuffmanNode(14, 14);

            List <HuffmanNode> testList = new List <HuffmanNode> {
                node1, node5, node3, node6, node2, node4
            };
            HuffmanEncoder         encoder  = new HuffmanEncoder();
            Dictionary <byte, int> testDict = new Dictionary <byte, int>();
            //testDict = encoder.EncodeToPackageMerge(testList, 4);
        }
Exemplo n.º 3
0
        //recurses through the entire tree and stacks up the respective code, then adds the respective symbol and code to the HuffmanTable when reaching a leave
        public void ConvertTreeToTable(List <bool> code, HuffmanNode node) //
        {
            if (node == null)
            {
                return;
            }
            if (node.isLeaf == true)
            {
                huffmanTable.Add(node.symbol, code);
                return;
            }

            code.Add(false);
            ConvertTreeToTable(code, node.left);
            code.RemoveAt(code.Count - 1);
            code.Add(true);
            ConvertTreeToTable(code, node.right);
        }
Exemplo n.º 4
0
 private static void FillEncodeTable(HuffmanNode node, BitsWithLength[] encodeSubstitutionTable, int bitvector = 0, int depth = 0)
 {
     if (node.LeafLabel != null)
     {
         encodeSubstitutionTable[node.LeafLabel.Value] = new BitsWithLength {
             Bits = bitvector, BitsCount = depth
         }
     }
     ;
     else
     {
         if (node.Left != null)
         {
             FillEncodeTable(node.Left, encodeSubstitutionTable, (bitvector << 1) + 1, depth + 1);
             FillEncodeTable(node.Right, encodeSubstitutionTable, (bitvector << 1) + 0, depth + 1);
         }
     }
 }
Exemplo n.º 5
0
        private static List <HuffmanNode> GetNodes(int[] frequences)
        {
            var nodesCount = frequences.Count(f => f > 0);
            var nodes      = new HuffmanNode[nodesCount];
            var index      = 0;

            for (var i = 0; i < frequences.Length; i++)
            {
                if (frequences[i] > 0)
                {
                    nodes[index++] = new HuffmanNode {
                        Frequency = frequences[i], LeafLabel = (byte)i
                    }
                }
                ;
            }

            return(nodes.Distinct().OrderBy(n => n.Frequency).ToList());
        }
Exemplo n.º 6
0
        //3b
        //builds the HuffmanTree so that the left child of each node is a leaf with the highest remaining frequency (growing to the right hand side)
        //NOITCE: the lowest non-leaf node consists of two leafs with the lowest-frequency-leaf (total) as the right child
        public HuffmanNode ReorderTreeToRightSide(HuffmanNode root)
        {
            depthList    = new List <List <HuffmanNode> >();
            maximumDepth = 0;
            GoThroughTree(root);

            for (int i = maximumDepth; i > 0; i--)
            {
                depthList[i].Sort((nodeOne, nodeTwo) => nodeOne.depth.CompareTo(nodeTwo.depth));

                while (depthList[i].Count > 0)
                {
                    HuffmanNode newNode = new HuffmanNode(depthList[i][depthList[i].Count - 2], depthList[i][depthList[i].Count - 1]);
                    newNode.depth = newNode.right.depth + 1;
                    depthList[i - 1].Add(newNode);

                    depthList[i].RemoveAt(depthList[i].Count - 1);
                    depthList[i].RemoveAt(depthList[i].Count - 1);
                }
            }

            return(depthList[0][0]);
        }
Exemplo n.º 7
0
        //3d
        public void EncodeToPackageMerge(List <HuffmanNode> inputlist, int depth)
        {
            //Sortierung nach Frequenz
            inputlist.Sort((nodeOne, nodeTwo) => nodeOne.frequency.CompareTo(nodeTwo.frequency));
            List <HuffmanNode> nodelist = new List <HuffmanNode>(inputlist);

            //Durchlaufe die Liste entsprechend der Tiefe
            for (int i = 2; i <= depth; i++)
            {
                //Bilde Paare und erstelle einen neuen Knoten, alle erstellten Knoten werden in tempList gespeichert
                List <HuffmanNode> tempList = new List <HuffmanNode>();
                for (int c = 1; c < nodelist.Count; c += 2)
                {
                    HuffmanNode mergedNode = new HuffmanNode(nodelist[c - 1].frequency + nodelist[c].frequency, nodelist[c - 1], nodelist[c]);
                    tempList.Add(mergedNode);
                }

                nodelist.Clear();             //lösche alte liste
                nodelist.AddRange(inputlist); // setzte liste auf die Anfangswerte
                nodelist.AddRange(tempList);  // füge erzeugte knoten der liste hinzu
                tempList.Clear();             //Lösche alte Knoten für einen neuen Schleifendurchlauf

                //Sortiere neue Liste nach frequency
                nodelist.Sort((nodeOne, nodeTwo) => nodeOne.frequency.CompareTo(nodeTwo.frequency));
            }

            //---------------Liste mit Knoten wurde erstellt---------------------------------------------

            var valueTable = new Dictionary <byte, int>();

            for (int i = 0; i < inputlist.Count; i++)
            {
                valueTable[inputlist[i].symbol] = inputlist[i].depth;
            }

            //---------------Liste auf 2n-2 Elemente kürzen-----------------------------------------------


            int cutter = inputlist.Count * 2 - 2;

            if (nodelist.Count > cutter)
            {
                nodelist.RemoveRange(cutter, nodelist.Count - cutter);
            }

            //---------------Key/Value Liste für die Kodierungsgewichtung erstellt------------------------

            for (int i = 0; i < nodelist.Count; i++)
            {
                if (nodelist[i].isDepthNode == true)
                {
                    nodelist.Add(nodelist[i].left);
                    nodelist.Add(nodelist[i].right);
                    nodelist.Remove(nodelist[i]);
                    i--;
                }

                else
                {
                    valueTable[nodelist[i].symbol]++;
                }
            }
            Dictionary <Byte, int> wertOutput = new Dictionary <byte, int>();

            foreach (var item in inputlist)
            {
                wertOutput.Add(item.symbol, item.frequency);
            }
            EncodeToPackageMergeList(valueTable, wertOutput);
        }