// This method is to encode text based on the this tree public byte[] Encode(string text) { // a local recursive function to convert a character into an array of bits based on the root huffman node BitArray convertToBitarray(HuffmanNode n, char c) { // if we found a leaf, return the code if (n.IsLeaf) { return(n.Code); } else { // Call this function again, but with either the left or the right if (n.LeftChildNode.Symbol.Contains(c)) { return(convertToBitarray(n.LeftChildNode, c)); } else if (n.RightChildNode.Symbol.Contains(c)) { return(convertToBitarray(n.RightChildNode, c)); } else { throw new Exception("something went wrong when converting the tree"); } } } // Init list for the bits BitList bits = new BitList(); // make the text into a character array, and loop tought it text.ToCharArray().ToList().ForEach(x => { // Use the recursive function from before to calculate the bits // Add the bits to the list of bits bits.AddBitArray(convertToBitarray(Root, x)); }); // We add a 1 to the list bits.Add(true); // We fill the list up with 0's, until it is divisible by 8 while (bits.Count % 8 != 0) { bits.Add(false); } // Create the array of bytes byte[] bytes = new byte[bits.Count / 8]; // Copy the array of bits to the array of bytes bits.ToBitArray().CopyTo(bytes, 0); // Return the bytes return(bytes); }
// This method is to encode text based on the this tree // Denne funktion enkoder tekst baseret på det foregående træ public byte[] Encode(string text) { // a local recursive function to convert a character into an array of bits based on the root huffman node // En lokal rekursiv funktion for at konvertere et symbol om til et array af bits baseret på Hoffman nodens rod BitArray convertToBitarray(HuffmanNode n, char c) { // if we found a leaf, return the code // Hvis det er et blad, returnerer koden if (n.IsLeaf) { return(n.Code); } else { // Call this function again, but with either the left or the right // Kald denne funktion igen, men med enten højre eller venstre if (n.LeftChildNode.Symbol.Contains(c)) { return(convertToBitarray(n.LeftChildNode, c)); } else if (n.RightChildNode.Symbol.Contains(c)) { return(convertToBitarray(n.RightChildNode, c)); } else { throw new Exception("something went wrong when converting the tree"); } } } // Init list for the bits // Ny liste for bitsene BitList bits = new BitList(); // make the text into a character array, and loop trough it // Lav teksten om til et symbol-array, og loop igennem det text.ToCharArray().ToList().ForEach(x => { // Use the recursive function from before to calculate the bits // Add the bits to the list of bits // Der bruges den rekursive funktion fra før til at udregne bitsene // Tilføj bitsene til listen over bits bits.AddBitArray(convertToBitarray(Root, x)); }); // We add a 1 to the list // Der tilføjes et 1 til listen bits.Add(true); // We fill the list up with 0's, until it is divisible by 8 // Listen fyldes op med 0'er, indtil at den kan divideres med 8 while (bits.Count % 8 != 0) { bits.Add(false); } // Create the array of bytes // Der laves et array over bytes byte[] bytes = new byte[bits.Count / 8]; // Copy the array of bits to the array of bytes // array over bits indsættes ind i array over bytes bits.ToBitArray().CopyTo(bytes, 0); // Return the bytes // Returner bytes return(bytes); }
// 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); }
// 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); }