Beispiel #1
0
 // Internal function used to add new elements to the internal nodes list
 private void Add(HuffmanTreeNode Node)
 {
     if (Storage.Count != 0)
     {
         if (Storage.ElementAt(NextIndex).Value > Node.Value)
         {
             NextIndex = Storage.Count;
         }
     }
     Storage.Add(Node);
 }
Beispiel #2
0
 // Create a new priority queue based on a predefined list of key value pairs of chars and ints
 public HuffmanPriorityQueue(List <KeyValuePair <char, int> > Data)
 {
     Storage = new List <HuffmanTreeNode>();
     for (int i = 0; i < Data.Count; ++i)
     {
         // Save index of the element with lowest frequency in the initial dataset
         if (Data.ElementAt(NextIndex).Value > Data.ElementAt(i).Value)
         {
             NextIndex = i;
         }
         // Create and add the HuffmanTreeNode created from the current data iteration
         HuffmanTreeNode NewNode = new HuffmanTreeNode(Data.ElementAt(i).Key, Data.ElementAt(i).Value);
         Storage.Add(NewNode);
     }
 }
Beispiel #3
0
 // Create a new HuffmanTreeNode from two nodes
 public HuffmanTreeNode(HuffmanTreeNode Node1, HuffmanTreeNode Node2)
 {
     Value = Node1.Value + Node2.Value;
     // Make sure that the Left node is the node with a higher frequency
     if (Node1.Value > Node2.Value)
     {
         LeftNode  = Node1;
         RightNode = Node2;
     }
     else
     {
         LeftNode  = Node2;
         RightNode = Node1;
     }
     // Make this node the parent of the two nodes
     Node1.ParentNode = this;
     Node2.ParentNode = this;
 }
Beispiel #4
0
        // Create a tree from a list of KeyValuePairs of chars and ints
        public HuffmanTree(List <KeyValuePair <char, int> > IV)
        {
            // Create a new priority queue to help us build the tree
            HuffmanPriorityQueue Queue = new HuffmanPriorityQueue(IV);

            // Repeat until one node is left in the queue
            while (Queue.Count != 1)
            {
                // Get the first two elements from the queue
                HuffmanTreeNode FirstNode  = Queue.Dequeue();
                HuffmanTreeNode SecondNode = Queue.Dequeue();
                // Merge the two nodes together
                HuffmanTreeNode NewNode = Merge(FirstNode, SecondNode);
                Queue.Enqueue(NewNode);
            }
            // "Initialize" Root with the last element of the queue and Position with Root
            Root     = Queue.Dequeue();
            Position = Root;
        }
Beispiel #5
0
 public void Enqueue(HuffmanTreeNode Node)
 {
     Add(Node);
 }
Beispiel #6
0
        // Create a new node from the KeyValuePair and pass it to Add()
        public void Enqueue(KeyValuePair <char, int> Element)
        {
            HuffmanTreeNode NewNode = new HuffmanTreeNode(Element.Key, Element.Value);

            Add(NewNode);
        }
Beispiel #7
0
 public NodeInfo(HuffmanTreeNode Node)
 {
     NodeRef = Node;
     DisplayInfo();
 }
Beispiel #8
0
 // Create a new node with the two nodes as children and return it
 HuffmanTreeNode Merge(HuffmanTreeNode FirstNode, HuffmanTreeNode SecondNode)
 {
     return(new HuffmanTreeNode(FirstNode, SecondNode));
 }