public static void Main(string[] args) { int i; PriorityQueue <PriorityClass> PQ = new PriorityQueue <PriorityClass>(10); PriorityClass[] A = new PriorityClass[5]; A[0] = new PriorityClass(1, "Susan"); A[1] = new PriorityClass(3, "Hannah"); A[2] = new PriorityClass(3, "Tony"); A[3] = new PriorityClass(7, "Pranjal"); A[4] = new PriorityClass(1, "Alan"); PQ.Add(new PriorityClass(6, "Brian")); PQ.Add(new PriorityClass(6, "Richard")); PQ.Add(new PriorityClass(4, "Sabine")); PQ.Add(new PriorityClass(7, "Wenying")); PQ.Add(new PriorityClass(0, "Patrick")); while (!PQ.Empty()) { Console.WriteLine(PQ.Front().ToString()); PQ.Remove(); } Console.ReadLine(); PQ.HeapSort(A); for (i = 0; i < A.Length; i++) { Console.WriteLine(A[i].ToString()); } Console.ReadLine(); }
// 20 marks // Build a Huffman tree based on the character frequencies greater than 0 (invoked by Huffman) private void Build(int[] F) { PriorityQueue.PriorityQueue <Node> PQ = new PriorityQueue.PriorityQueue <Node>(F.Length); for (int x = 0; x < F.Length; x++) // loop to create the leaf nodes { if (F[x] > 0) // checks to make sure there is at least one occurrence { Node temp = new Node(possibleCharacters[x], F[x], null, null); // leaf nodes have no left and right nodes PQ.Add(temp); //add leaf nodes to priority queue } } while (PQ.Size() > 2) { Node temp = new Node(); //new empty node int freq = PQ.Front().Frequency; //store left side frequency temp.Left = PQ.Front(); // store left side node PQ.Remove(); //remove left side node from priority queue temp.Frequency = freq + PQ.Front().Frequency; //store left side freq plus right side freq temp.Right = PQ.Front(); //store right side node PQ.Remove(); //remove right side node from priority queue PQ.Add(temp); //add new sub tree to priority queue } if (PQ.Size() == 2) { HT = new Node(); //initialize head node as empty int freq = PQ.Front().Frequency; //store left node frequency HT.Left = PQ.Front(); // store left node PQ.Remove(); // remove node from priority queue HT.Frequency = freq + PQ.Front().Frequency; //store both frequencies in head node HT.Right = PQ.Front(); //store right node PQ.MakeEmpty(); //remove node from priority queue (make sure its empty) } else //Special case, only one node in the priority queue { HT = new Node(); // head node is set to only have one leaf node and freq is that of the leaf node HT.Frequency = PQ.Front().Frequency; // set head node freq as leaf node freq HT.Left = PQ.Front(); // if there is only one node, it becomes the head node PQ.MakeEmpty(); //empty priority queue } }
// Method : Build // Parameters: Dictionary of characters and their frequency // Return Type : Void /* Description: Given a dictionary, we build a tree using the Priority Queue * where 2 nodes with the highest priority are removed. */ private void Build(Dictionary <char, int> charDictionary) { // Implementing priority queue class and using it's methods to mainpulate the huffman tree PriorityQueue <Node> PQ; PQ = new PriorityQueue <Node>(charDictionary.Count); // Creating a priority queue of Nodes // Based on the size of the dictionary(char,frequency) foreach (KeyValuePair <char, int> entry in charDictionary) // Adding each character as a node { Node q; q = new Node(entry.Key, entry.Value, null, null); PQ.Add(q); // Creating leaf nodes } /* To create root nodes, HT gets put back into the queue each time until size !=1*/ while (PQ.Size() != 1) { // Removing the two highest priority nodes // Less frequent == higher priority Node x = PQ.Front(); PQ.Remove(); Node y = PQ.Front(); PQ.Remove(); // Error checking if user enters nothing if (x == null || y == null) { throw new ArgumentException("Input cannot be empty, exiting program."); } // Forming a root node by adding the removed nodes frequency HT = new Node(default(char), x.Frequency + y.Frequency, x, y); PQ.Add(HT); } HT = PQ.Front(); // First node in the queue will be the tree created with all the left and right references }