public static string Perform(string text)
        {
            var chars = text.ToCharArray();

            // Gathering alphabet frequency
            var alphabetFrequency = new Dictionary <char, int>();

            foreach (var c in chars) // First pass through the char array
            {
                if (!alphabetFrequency.ContainsKey(c))
                {
                    alphabetFrequency.Add(c, 1);
                }
                else
                {
                    alphabetFrequency[c] = alphabetFrequency[c] + 1;
                }
            }

            // Create binary heap
            var priorityQueue = new BinaryHeapPriorityQueue <TreeNode>(alphabetFrequency.Count, (i1, i2) => i2.Count.CompareTo(i1.Count));



            foreach (var entry in alphabetFrequency)
            {
                var leafNode = new LeafTreeNode(entry.Value);
                CharToNodes.Add(entry.Key, leafNode);
                priorityQueue.Insert(leafNode); // Initially PQ contains only leaf nodes
            }

            // Main part of the algorithm
            while (priorityQueue.GetSize() > 1)
            {
                var n1 = priorityQueue.Extract();
                var n2 = priorityQueue.Extract();

                var innerNode = new InnerTreeNode(n1, n2); // Creates a new node with sum of frequencies...

                priorityQueue.Insert(innerNode);           // ...and put it back
            } // Repeat the procedure untill PQ has only one element

            var root = priorityQueue.Extract();                       // Take the last element (root) from the PQ

            root.CreateCode(alphabetFrequency.Count == 1 ? "0" : ""); // Build codes for the whole hierarchy

            var sb = new StringBuilder();

            foreach (var t in chars) // Second pass through the char array
            {
                var c = CharToNodes[t].Code;
                sb.Append(c);
            }

            return(sb.ToString());
        }
예제 #2
0
        public void Extract_ValidParamsPassed_Success()
        {
            IPriorityQueue <int> priorityQueue = new BinaryHeapPriorityQueue <int>(5, (x, y) => - x.CompareTo(y));

            priorityQueue.Insert(5);
            priorityQueue.Insert(4);
            priorityQueue.Insert(3);
            priorityQueue.Insert(2);
            priorityQueue.Insert(1);

            var v1 = priorityQueue.Extract();
            var v2 = priorityQueue.Extract();
            var v3 = priorityQueue.Extract();
            var v4 = priorityQueue.Extract();
            var v5 = priorityQueue.Extract();

            Assert.AreEqual(v1, 1);
            Assert.AreEqual(v2, 2);
            Assert.AreEqual(v3, 3);
            Assert.AreEqual(v4, 4);
            Assert.AreEqual(v5, 5);
        }
예제 #3
0
        public void Extract_StringDataType_Success()
        {
            IPriorityQueue <string> priorityQueue = new BinaryHeapPriorityQueue <string>(10, (x, y) => - string.Compare(x, y, StringComparison.Ordinal));

            priorityQueue.Insert("Z");
            priorityQueue.Insert("X");
            priorityQueue.Insert("Y");
            priorityQueue.Insert("W");
            priorityQueue.Insert("V");
            priorityQueue.Insert("U");
            priorityQueue.Insert("T");
            priorityQueue.Insert("S");
            priorityQueue.Insert("R");
            priorityQueue.Insert("Q");

            var v1  = priorityQueue.Extract();
            var v2  = priorityQueue.Extract();
            var v3  = priorityQueue.Extract();
            var v4  = priorityQueue.Extract();
            var v5  = priorityQueue.Extract();
            var v6  = priorityQueue.Extract();
            var v7  = priorityQueue.Extract();
            var v8  = priorityQueue.Extract();
            var v9  = priorityQueue.Extract();
            var v10 = priorityQueue.Extract();

            Assert.AreEqual(v1, "Q");
            Assert.AreEqual(v2, "R");
            Assert.AreEqual(v3, "S");
            Assert.AreEqual(v4, "T");
            Assert.AreEqual(v5, "U");
            Assert.AreEqual(v6, "V");
            Assert.AreEqual(v7, "W");
            Assert.AreEqual(v8, "X");
            Assert.AreEqual(v9, "Y");
            Assert.AreEqual(v10, "Z");
        }
예제 #4
0
        public void Extract_StringDataTypeDifferentOrder_Success()
        {
            IPriorityQueue <string> priorityQueue = new BinaryHeapPriorityQueue <string>(10, (x, y) => string.Compare(x, y, StringComparison.Ordinal));

            priorityQueue.Insert("A");
            priorityQueue.Insert("B");
            priorityQueue.Insert("C");
            priorityQueue.Insert("D");
            priorityQueue.Insert("E");
            priorityQueue.Insert("F");
            priorityQueue.Insert("G");
            priorityQueue.Insert("H");
            priorityQueue.Insert("I");
            priorityQueue.Insert("J");

            var v1  = priorityQueue.Extract();
            var v2  = priorityQueue.Extract();
            var v3  = priorityQueue.Extract();
            var v4  = priorityQueue.Extract();
            var v5  = priorityQueue.Extract();
            var v6  = priorityQueue.Extract();
            var v7  = priorityQueue.Extract();
            var v8  = priorityQueue.Extract();
            var v9  = priorityQueue.Extract();
            var v10 = priorityQueue.Extract();

            Assert.AreEqual(v1, "J");
            Assert.AreEqual(v2, "I");
            Assert.AreEqual(v3, "H");
            Assert.AreEqual(v4, "G");
            Assert.AreEqual(v5, "F");
            Assert.AreEqual(v6, "E");
            Assert.AreEqual(v7, "D");
            Assert.AreEqual(v8, "C");
            Assert.AreEqual(v9, "B");
            Assert.AreEqual(v10, "A");
        }