Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Node
- {
- public int frekvens; //Frequency
- public char tegn; //Symbol
- public Node venstre; //Left child
- public Node høyre; //Right child
- public string s; //result string
- public Node (char c) // Node constructor containing symbol.
- {
- frekvens = 1;
- if (tegn == ' ')
- {
- tegn = '_';
- }
- else
- {
- tegn = c;
- }
- }
- public Node (int f, Node venstre, Node høyre) // Node Constructor containing frequency and children
- {
- frekvens = f;
- this.venstre = venstre;
- this.høyre = høyre;
- }
- public Node (Node node) // Node constructor containing a node
- {
- frekvens = node.frekvens;
- tegn = node.tegn;
- this.venstre = venstre;
- this.høyre = høyre;
- }
- public void ØkMed1() // Inkrement frequency by one
- {
- frekvens = frekvens + 1;
- }
- public char getVenstreTegn ()
- {
- return venstre.tegn;
- }
- public char getHøyreTegn ()
- {
- return venstre.tegn;
- }
- public int getVenstreFrekvens ()
- {
- return venstre.frekvens;
- }
- public int getHøyreFrekvens ()
- {
- return høyre.frekvens;
- }
- public int getFrekvens()
- {
- return frekvens;
- }
- public bool ErTegn(char c)
- {
- if ( c == tegn)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- //Pretty sure this does not work as intended
- public string traverser (Node n) //Traverse the tree
- {
- if (n.tegn != null) //If the node containes a symbol --> a leaf
- {
- return s;
- }
- else
- {
- if (n.getVenstreTegn() == null) //If left child does not have a symbol
- {
- s += "0";
- traverser(n);
- }
- if (n.getHøyreTegn() == null) //If right child does not have a symbol
- {
- s += "1";
- traverser(n);
- }
- }
- return s;
- }
- public string Resultat() //Used priviously to check if i got the correct huffman tree
- {
- string resultat;
- resultat = "Tegn: " + Convert.ToString(tegn) +" frekvens: " + Convert.ToString(frekvens) + "n";
- return resultat;
- }
- }
- private void btnKomprimer_Click(object sender, System.Windows.RoutedEventArgs e)
- {
- string input; //The string input I want to compress
- bool funnet; //Found
- char karakter; //character
- input = txtInput.Text; //initialize input to text input
- input = input.ToLower();
- txtOutput.Text = "";
- List<Node> noder = new List<Node>(); //The list of nodes
- //List<Node> tatt = new List<Node>();
- for (int i = 0; i < input.Length;i++) //Loops through string and sets character
- //with coresponding freqeuncy in the node list
- {
- karakter = input[i];
- funnet = false; //default
- for (int j = 0; j< noder.Count; j++)
- {
- if (noder[j].ErTegn(karakter) == false) //if the character already exists
- {
- noder[j].ØkMed1(); //inkrement frequency by one
- funnet = true;
- break;
- }
- }
- if (!funnet) //if the character does not exist
- {
- noder.Add(new Node(karakter)); //add the character to list
- }
- }
- //Sorting node list acending by frequency
- var sortertListe = noder.OrderBy(c => c.frekvens).ToList();
- noder = sortertListe;
- foreach(Node n in noder) //Prints the characters with frequency
- {
- txtOutput.Text += n.Resultat();
- }
- do
- {
- noder.Add(new Node((noder[0].frekvens + noder[1].frekvens), noder[0],noder[1]));
- //tatt.Add(new Node(noder[0]));
- //tatt.Add(new Node(noder[1]));
- //Remove the leaf nodes
- noder.RemoveAt(0);
- noder.RemoveAt(0);
- } while(noder.Count >= 2);
- foreach (Node n in noder) //Prints the root frequency
- {
- txtOutput.Text += n.Resultat();
- }
- txtOutput.Text += "n" + noder[0].traverser(noder[0]); //Result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement