示例#1
0
        // This method decodes bytes into a string, based on the current huffman tree
        // Essentially, the opposite of the one above
        public string Decode(byte[] encoded)
        {
            // Turn the bytes into bits, and make a list of them
            BitList bits = BitList.Parse(new BitArray(encoded));

            // a recursive function to remove the ending of the bits
            // the ending is there, only to make sure that the length of the list is divisible by 8
            // this function therefore removes 0 from the end, until it hits a 1, which is then removed, and the function stops
            void clean(BitList list)
            {
                //if it is a 1
                if (list[list.Count - 1])
                {
                    //remove the 1
                    list.RemoveAt(list.Count - 1);
                }
                // if it is a 0
                else
                {
                    //remove the 0
                    list.RemoveAt(list.Count - 1);
                    //repeat
                    clean(list);
                }
            }

            //call the function just described
            clean(bits);
            //init the string that is going to hold the decoded string
            string str = "";
            //define a variable to hold the current node that we are on
            HuffmanNode currentNode = Root;

            //loop thought all the bits
            for (int i = 0; i < bits.Count; i++)
            {
                //if it is a 1, take the right node
                if (bits[i])
                {
                    currentNode = currentNode.RightChildNode;
                }
                //if it is a 0, take the left node
                else
                {
                    currentNode = currentNode.LeftChildNode;
                }
                // if the current node is a leaf
                if (currentNode.IsLeaf)
                {
                    // add the symbol to the str variable
                    str += currentNode.Symbol;
                    // go back to the root of the tree
                    currentNode = Root;
                }
            }
            ;
            // return the decoded symbols
            return(str);
        }
示例#2
0
        // this method creates a tree based on text
        // Denne funktion laver et træ baseret på tekst
        public static HuffmanTree CreateFromText(string text)
        {
            // the list that we wanna fill up with nodes
            // Listen der skal fyldes med noder
            List <HuffmanNode> nodeList = new List <HuffmanNode>();

            // all the characters from the text
            // Alle symbolerne fra teksten
            char[] characters = text.ToCharArray();
            // loop thought the characters
            // Gentag igennem symbolerne
            for (int i = 0; i < characters.Length; i++)
            {
                // the character as a string
                // Symbolerne som strenge
                string read = characters[i].ToString();
                // has the node already been created?
                // Er noden allerede blevet skabt?
                if (nodeList.Exists(x => x.Symbol == read))
                {
                    // If is yes, find the index of the Node and increase the frequency of the Node.
                    // Hvis ja, find indekset for noden og øg frekvensen for node
                    nodeList[nodeList.FindIndex(y => y.Symbol == read)].Frequency++;
                }
                else
                {
                    // If is no, create a new node and add to the List of Nodes
                    // Hvis nej, skab en ny node og tilføj den til listen over node
                    nodeList.Add(new HuffmanNode(read));
                }
            }
            // sort them, this is done based on frequency because of IComparable<HuffmanNode>.CompareTo
            // Sorter dem, dette gøres baseret på frekvens fordi at IComparable<HuffmanNode>.CompareTo
            nodeList.Sort();
            // loop thought them, until only one is left
            // Kør igennem dem alle sammen indtil der kun er en tilbage
            while (nodeList.Count > 1)
            {
                // Get the node of the first index of List, this is the one with the lowest frequency
                // Få noden for det første indeks af Listen, dette er den med den laveste frekvens
                HuffmanNode node1 = nodeList[0];
                // and delete it.
                // Fjern den
                nodeList.RemoveAt(0);
                // do the same thing again
                // Gør det samme igen
                HuffmanNode node2 = nodeList[0];
                nodeList.RemoveAt(0);
                // make a parant node with node1 and node2 and the left and right child nodes
                // Lav en parent-node med node1 og node1 og de venstre og højre child-noder
                nodeList.Add(new HuffmanNode(node1, node2));
                // and sort it again according to frequency.
                // Og sorter det igen efter frekvens
                nodeList.Sort();
            }
            // create a tree based on the remaining root node
            // Lav et træ baseret på den tilbageværende rod-node
            HuffmanTree tree = new HuffmanTree(nodeList[0]);

            // this is a recursive function to set the binary code of every leaf node
            // Dette er en rekursiv funktion for at sætte den binære værdi for hvert blad
            void setCodeToTheTree(HuffmanNode Nodes, BitArray code = null)
            {
                // if the current code is not set, set it to an empty BitArray
                // Hvis den nuværende kode ikke er sat, sættes den til et tomt BitArray
                if (code == null)
                {
                    code = new BitArray(new bool[] { });
                }
                // if the code is empty do nothing
                // Hvis koden er tom, gør intet
                if (Nodes == null)
                {
                    return;
                }
                // if there is no left node and right node, then set the code based on the current code
                // Hvis der ikke er nogen venstre node, sæt koden baseret på den nuværende kode
                if (Nodes.LeftChildNode == null && Nodes.RightChildNode == null)
                {
                    Nodes.Code = code;
                    return;
                }
                // create a bitlist for the left node
                // lav en bitliste for den venstre node
                BitList left = BitList.Parse(code);

                // add false for the left side
                // tilføj false for den venstre side
                left.Add(false);
                // call this function recursively, with the left bitlist and the left side node
                // Kald denne funktion rekursivt, med den venstre bitliste og den venstre node
                setCodeToTheTree(Nodes.LeftChildNode, left.ToBitArray());
                // create a bitlist for the right node
                // Lav en bitliste for den højre node
                BitList right = BitList.Parse(code);

                // add true for the right side
                // tilføj true for den højre side
                right.Add(true);
                // call the function recursively, with the right bitlist and the right side node
                // Kald denne funktion rekursivt, med den højre bitliste og den højre node
                setCodeToTheTree(Nodes.RightChildNode, right.ToBitArray());
            }

            // call the recursive function
            // Kald den rekursive funktion
            setCodeToTheTree(tree.Root);
            // the tree
            // Træet returneres
            return(tree);
        }
示例#3
0
        // this method creates a tree based on text
        public static HuffmanTree CreateFromText(string text)
        {
            // the list that we wanna fill up with nodes
            List <HuffmanNode> nodeList = new List <HuffmanNode>();

            // all the characters from the text
            char[] characters = text.ToCharArray();
            // loop thought the characters
            for (int i = 0; i < characters.Length; i++)
            {
                // the character as a string
                string read = characters[i].ToString();
                // has the node already been created?
                if (nodeList.Exists(x => x.Symbol == read))
                {
                    // If is yes, find the index of the Node and increase the frequency of the Node.
                    nodeList[nodeList.FindIndex(y => y.Symbol == read)].Frequency++;
                }
                else
                {
                    // If is no, create a new node and add to the List of Nodes
                    nodeList.Add(new HuffmanNode(read));
                }
            }
            // sort them, this is done based on frequency because of IComparable<HuffmanNode>.CompareTo
            nodeList.Sort();
            // loop thought them, until only one is left
            while (nodeList.Count > 1)
            {
                // Get the node of the first index of List, this is the one with the lowest frequency
                HuffmanNode node1 = nodeList[0];
                // and delete it.
                nodeList.RemoveAt(0);
                // do the same thing again
                HuffmanNode node2 = nodeList[0];
                nodeList.RemoveAt(0);
                // make a parant node with node1 and node2 and the left and right child nodes
                nodeList.Add(new HuffmanNode(node1, node2));
                // and sort it again according to frequency.
                nodeList.Sort();
            }
            // create a tree based on the remaining root node
            HuffmanTree tree = new HuffmanTree(nodeList[0]);

            // this is a recursive function to set the binary code of every leaf node
            void setCodeToTheTree(HuffmanNode Nodes, BitArray code = null)
            {
                // if the current code is not set, set it to an empty BitArray
                if (code == null)
                {
                    code = new BitArray(new bool[] { });
                }
                // if the code is empty do nothing
                if (Nodes == null)
                {
                    return;
                }
                // if there is no left node and right node, then set the code based on the current code
                if (Nodes.LeftChildNode == null && Nodes.RightChildNode == null)
                {
                    Nodes.Code = code;
                    return;
                }
                // create a bitlist for the left node
                BitList left = BitList.Parse(code);

                // add false for the left side
                left.Add(false);
                // call this function recursively, with the left bitlist and the left side node
                setCodeToTheTree(Nodes.LeftChildNode, left.ToBitArray());
                // create a bitlist for the right node
                BitList right = BitList.Parse(code);

                // add true for the right side
                right.Add(true);
                // call the function recursively, with the right bitlist and the right side node
                setCodeToTheTree(Nodes.RightChildNode, right.ToBitArray());
            }

            // call the recursive function
            setCodeToTheTree(tree.Root);
            // the tree
            return(tree);
        }
示例#4
0
        // This method decodes bytes into a string, based on the current huffman tree
        // Essentially, the opposite of the one above
        // Denne funktion afkoder bytes til en string, baseret på det nuværende Huffman-træ
        // Stort set bare det foregående baglæns
        public string Decode(byte[] encoded)
        {
            // Turn the bytes into bits, and make a list of them
            // Bytes omdannes til bits, og der laves en liste af dem
            BitList bits = BitList.Parse(new BitArray(encoded));

            // a recursive function to remove the ending of the bits
            // the ending is there, only to make sure that the length of the list is divisible by 8
            // this function therefore removes 0 from the end, until it hits a 1, which is then removed, and the function stops
            // En rekursiv funktion for at fjerne enderne på bits
            // Enderne er der kun for at sikre at længden på listen kan divideres med 8
            // Denne funktion fjerner derfor 0'er fra enden, indtil at den rammer et 1, hvilket derefter fjernes og funktionen stopper
            void clean(BitList list)
            {
                //if it is a 1
                // Hvis det er et 1
                if (list[list.Count - 1])
                {
                    // remove the 1
                    // Fjern 1
                    list.RemoveAt(list.Count - 1);
                }
                // if it is a 0
                // Hvis det er et 0
                else
                {
                    // remove the 0
                    // Fjern 0
                    list.RemoveAt(list.Count - 1);
                    // repeat
                    // Gentag
                    clean(list);
                }
            }

            // call the function just described
            // Kald funktionen fra før
            clean(bits);
            // init the string that is going to hold the decoded string
            // strengen der skal indehold den afkodede streng laves
            string str = "";
            // define a variable to hold the current node that we are on
            // Der defineres en variabel til at holde det nuværende node der kigges på
            HuffmanNode currentNode = Root;

            // loop thought all the bits
            // Kør igennem alle bits
            for (int i = 0; i < bits.Count; i++)
            {
                // if it is a 1, take the right node
                // Hvis det er et 1, tag højre node
                if (bits[i])
                {
                    currentNode = currentNode.RightChildNode;
                }
                // if it is a 0, take the left node
                // Hvis det er et 0, tag venstre node
                else
                {
                    currentNode = currentNode.LeftChildNode;
                }
                // if the current node is a leaf
                // Hvis den nuværende node er et blad
                if (currentNode.IsLeaf)
                {
                    // add the symbol to the str variable
                    // Tilføj symbolet til streng-variablen "str"
                    str += currentNode.Symbol;
                    // go back to the root of the tree
                    // Gå tilbage til træets rod
                    currentNode = Root;
                }
            }
            ;
            // return the decoded symbols
            // Returner de afkodede symboler
            return(str);
        }