/// <summary>
        /// Computes the shortest path from u to v in the map.
        /// </summary>
        /// <param name="u"></param>
        /// <param name="v"></param>
        /// <param name="map"></param>
        /// <param name="paths"></param>
        /// <returns></returns>
        private decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map, out Dictionary <string, string> paths)
        {
            paths = new Dictionary <string, string>();
            MinPriorityQueue <decimal, Edge <string, decimal> > queue = new MinPriorityQueue <decimal, Edge <string, decimal> >();

            paths.Add(u, u);
            if (u.Equals(v))
            {
                return(0);
            }

            foreach (Edge <string, decimal> i in map.OutgoingEdges(u))
            {
                queue.Add(i.Data, i);
            }
            while (queue.Count > 0)
            {
                decimal priority            = queue.MinimumPriority;
                Edge <string, decimal> temp = queue.RemoveMinimumPriority();
                if (!paths.ContainsKey(temp.Destination))
                {
                    paths.Add(temp.Destination, temp.Source);
                    if (temp.Destination == v)
                    {
                        return(priority);
                    }
                    foreach (Edge <string, decimal> i in map.OutgoingEdges(temp.Destination))
                    {
                        queue.Add(priority + i.Data, i);
                    }
                }
            }
            return(-1);
        }
        /// <summary>
        /// Returns the shortest paths
        /// </summary>
        /// <param name="u">generic string</param>
        /// <param name="v">generic string</param>
        /// <param name="map">DirectedGraph map</param>
        /// <param name="paths">Dictionary paths</param>
        /// <returns></returns>
        private decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map, out Dictionary <string, string> paths)
        {
            paths = new Dictionary <string, string>();
            MinPriorityQueue <decimal, Edge <string, decimal> > tempQueue = new MinPriorityQueue <decimal, Edge <string, decimal> >();

            paths.Add(u, u);
            if (u.Equals(v))
            {
                return(0);
            }
            foreach (Edge <string, decimal> outGoEdge in map.OutgoingEdges(u))
            {
                tempQueue.Add(outGoEdge.Data, outGoEdge);
            }
            while (tempQueue.Count > 0) //possibly wrong
            {
                decimal p = tempQueue.MinimumPriority;
                Edge <string, decimal> edgeRemoved = tempQueue.RemoveMinimumPriority();
                if (!paths.ContainsKey(edgeRemoved.Destination))
                {
                    paths.Add(edgeRemoved.Destination, edgeRemoved.Source);
                    if (edgeRemoved.Destination.Equals(v))
                    {
                        return(p);
                    }
                    foreach (Edge <string, decimal> outGoEdge in map.OutgoingEdges(edgeRemoved.Destination))
                    {
                        tempQueue.Add(p + outGoEdge.Data, outGoEdge);
                    }
                }
            }
            return(-1);
        }
        /// <summary>
        /// Finds a shortest path from the given start node to the given end node in the given graph.
        /// </summary>
        /// <param name="u">The start node.</param>
        /// <param name="v">The end node.</param>
        /// <param name="map">The graph.</param>
        /// <param name="paths">The resulting shortest paths.</param>
        /// <returns>The length of the shortest path.</returns>
        private decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map,
                                     out Dictionary <string, string> paths)
        {
            paths = new Dictionary <string, string>();
            MinPriorityQueue <decimal, Edge <string, decimal> > pq = new MinPriorityQueue <decimal, Edge <string, decimal> >();

            paths.Add(u, u);
            if (u == v)
            {
                return(0);
            }
            foreach (Edge <string, decimal> e in map.OutgoingEdges(u))
            {
                pq.Add(e, e.Data);
            }
            while (pq.Count > 0)
            {
                decimal p = pq.MininumPriority;
                Edge <string, decimal> e = pq.RemoveMinimumPriority();
                if (!paths.ContainsKey(e.Destination))
                {
                    paths.Add(e.Destination, e.Source);
                    if (e.Destination == v)
                    {
                        return(p);
                    }
                    foreach (Edge <string, decimal> outgoing in map.OutgoingEdges(e.Destination))
                    {
                        pq.Add(outgoing, p + outgoing.Data);
                    }
                }
            }
            return(-1);
        }
Beispiel #4
0
        public void TestJMinimumPriority()
        {
            MinPriorityQueue <int, string> pq = new MinPriorityQueue <int, string>();

            pq.Add(5, "five");
            pq.Add(2, "two");
            pq.Add(6, "six");
            Assert.That(pq.MinimumPriority, Is.EqualTo(2));
        }
Beispiel #5
0
        public void TestKCountAfterRemove()
        {
            MinPriorityQueue <int, string> pq = new MinPriorityQueue <int, string>();

            pq.Add(5, "five");
            pq.Add(2, "two");
            pq.Add(6, "six");
            pq.RemoveMinimumPriority();
            Assert.That(pq.Count, Is.EqualTo(2));
        }
Beispiel #6
0
        public void TestJRemoveMinimumPriority()
        {
            MinPriorityQueue <int, string> pq = new MinPriorityQueue <int, string>();

            pq.Add(5, "five");
            pq.Add(2, "two");
            pq.Add(6, "six");
            string s = pq.RemoveMinimumPriority();

            Assert.That(s, Is.EqualTo("two"));
        }
Beispiel #7
0
    // Implementation of Dijkstra's Algorithm
    public List <Tile> FindPath(Tile start, Tile end)
    {
        MinPriorityQueue <Tile, int> queue = new MinPriorityQueue <Tile, int>();
        // from prev tile to cur tile dictionary
        Dictionary <Tile, Tile> prev = new Dictionary <Tile, Tile>();

        foreach (Tile t in tiles)
        {
            if (!t.IsWalkable && t != start)
            {
                continue;
            }

            queue.Add(t, int.MaxValue);
            prev.Add(t, null);
        }

        queue.Add(start, 0);

        while (queue.Count > 0)
        {
            int  uDist;
            Tile u = queue.Pop(out uDist);

            foreach (Tile v in u.neighborTiles)
            {
                if (!queue.Contains(v))
                {
                    continue;
                }

                int vDist = uDist + 1;
                if (vDist < queue.GetValue(v))
                {
                    queue.Add(v, vDist);
                    prev[v] = u;
                }
            }
        }

        Tile        cur  = prev[end];
        List <Tile> path = new List <Tile>();

        while (cur != null)
        {
            path.Insert(0, cur);
            cur = prev[cur];
        }

        return(path);
    }
Beispiel #8
0
        /// <summary>
        /// Finds a shortest path from the given start node to the given end node in the given graph.
        /// </summary>
        /// <param name="u">The start node.</param>
        /// <param name="v">The end node.</param>
        /// <param name="map">The graph.</param>
        /// <param name="paths">The resulting shortest paths.</param>
        /// <returns>The length of the shortest path.</returns>
        private decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map, out Dictionary <string, string> paths)
        {
            //throw new NotImplementedException();
            paths = new Dictionary <string, string>();
            paths.Add(u, u);

            if (u == v)
            {
                return(0);
            }
            MinPriorityQueue <decimal, Tuple <string, string> > q = new MinPriorityQueue <decimal, Tuple <string, string> >();

            //add all outgoing edges from u
            foreach (Tuple <string, decimal> edge in map.OutgoingEdges(u))
            {
                //add this edge to priority queue
                q.Add(new Tuple <string, string>(u, edge.Item1), edge.Item2);
            }

            while (q.Count != 0)
            {
                decimal priority = q.MininumPriority;

                Tuple <string, string> edge = q.RemoveMinimumPriority();

                string x = edge.Item1;
                string w = edge.Item2;

                if (!paths.ContainsKey(w))
                {
                    paths.Add(w, x);

                    if (w == v)
                    {
                        return(priority);
                    }
                    else
                    {
                        foreach (Tuple <string, decimal> edge2 in map.OutgoingEdges(w))
                        {
                            q.Add(new Tuple <string, string>(w, edge2.Item1), edge2.Item2 + priority);
                        }
                    }
                }
            }
            return(-1);
        }
        public void AddNum(int num)
        {
            if (maxHeap.Count == 0 || num < maxHeap.Max)
            {
                maxHeap.Add(num);
            }
            else
            {
                minHeap.Add(num);
            }

            if (maxHeap.Count < minHeap.Count)
            {
                maxHeap.Add(minHeap.DeleteMin());
            }
            else if (maxHeap.Count > minHeap.Count + 1)
            {
                minHeap.Add(maxHeap.DeleteMax());
            }
        }
Beispiel #10
0
        public void Add(Node node)
        {
            if (Contains(node))
            {
                return;
            }

            hashSet.Add(node);
            openSet.Add(node.TotalCost, node);
            CallEvent(node);
            _count++;
        }
 /// <summary>
 /// Builds a Huffman tree from the given leaves.
 /// </summary>
 /// <param name="q">A min-priority queue containing the leaves of a Huffman tree. The priority of each leaf is the number
 /// of occurrences of the byte it contains.</param>
 /// <returns>The Huffman tree.</returns>
 private BinaryTreeNode <byte> GetHuffmanTree(MinPriorityQueue <long, BinaryTreeNode <byte> > q)
 {
     while (q.Count > 1)
     {
         long p1 = q.MininumPriority;
         BinaryTreeNode <byte> t1 = q.RemoveMinimumPriority();
         long p2 = q.MininumPriority;
         BinaryTreeNode <byte> t2 = q.RemoveMinimumPriority();
         q.Add(new BinaryTreeNode <byte>(0, t1, t2), p1 + p2);
     }
     return(q.RemoveMinimumPriority());
 }
        /// <summary>
        /// Finds a shortest path from the given start node to the given end node in the given graph.
        /// </summary>
        /// <param name="u">The start node.</param>
        /// <param name="v">The end node.</param>
        /// <param name="map">The graph.</param>
        /// <param name="paths">The resulting shortest paths.</param>
        /// <returns>The length of the shortest path.</returns>
        private decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map,
                                     out Dictionary <string, string> paths)
        {
            paths = new Dictionary <string, string>();
            MinPriorityQueue <decimal, Tuple <string, string> > q = new MinPriorityQueue <decimal, Tuple <string, string> >();

            paths.Add(u, u);
            if (u == v)
            {
                return(0);
            }
            foreach (Tuple <string, decimal> t in map.OutgoingEdges(u))
            {
                string  w   = t.Item1;
                decimal len = t.Item2;
                q.Add(new Tuple <string, string>(u, w), len);
            }
            while (q.Count != 0)
            {
                decimal p = q.MininumPriority;
                Tuple <string, string> edge = q.RemoveMinimumPriority();
                string w = edge.Item1;
                string x = edge.Item2;
                if (!paths.ContainsKey(x))
                {
                    paths.Add(x, w);
                    if (x == v)
                    {
                        return(p);
                    }
                    foreach (Tuple <string, decimal> t in map.OutgoingEdges(x))
                    {
                        string  y   = t.Item1;
                        decimal len = t.Item2;
                        q.Add(new Tuple <string, string>(x, y), p + len);
                    }
                }
            }
            return(-1);
        }
Beispiel #13
0
        /// <summary>
        /// Builds a HuffmanTree
        /// </summary>
        /// <param name="minQueue"></param>
        /// <returns></returns>
        private BinaryTreeNode <byte> HuffmanTree(MinPriorityQueue <long, BinaryTreeNode <byte> > minQueue)
        {
            while (minQueue.Count > 1)
            {
                long minimumOne = minQueue.MinimumPriority;
                BinaryTreeNode <byte> minTreeOne = minQueue.RemoveMinimumPriority();
                long minimumTwo = minQueue.MinimumPriority;
                BinaryTreeNode <byte> minTreeTwo = minQueue.RemoveMinimumPriority();

                minQueue.Add(minimumOne + minimumTwo, new BinaryTreeNode <byte>(0, minTreeOne, minTreeTwo));
            }
            return(minQueue.RemoveMinimumPriority());
        }
        /// <summary>
        /// Gets a min-priority queue containing the leaves of a Huffman tree for the given frequency table.
        /// The prioiries are the frequencies of the bytes contained in the leaves.
        /// </summary>
        /// <param name="freqTable">The frequency table.</param>
        /// <returns>A min-priority queue containing the leaves of a Huffman tree.</returns>
        private MinPriorityQueue <long, BinaryTreeNode <byte> > GetLeaves(long[] freqTable)
        {
            MinPriorityQueue <long, BinaryTreeNode <byte> > q = new MinPriorityQueue <long, BinaryTreeNode <byte> >();

            for (int i = 0; i < 256; i++)
            {
                if (freqTable[i] > 0)
                {
                    q.Add(new BinaryTreeNode <byte>((byte)i, null, null), freqTable[i]);
                }
            }
            return(q);
        }
Beispiel #15
0
        /// <summary>
        /// do not iterate with a byte
        /// set priority to freq table equivalent
        /// check zero, create node
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        private MinPriorityQueue <long, BinaryTreeNode <byte> > HuffBuild(long[] x)
        {
            x = freqTableBuilder(Console.ReadLine());
            MinPriorityQueue <long, BinaryTreeNode <byte> > mpq = new
                                                                  MinPriorityQueue <long, BinaryTreeNode <byte> >();

            while (x.Length > mpq.Count)
            {
                BinaryTreeNode <byte> node = new BinaryTreeNode <byte>(Convert.ToByte(x), null, null);
                mpq.Add(x[0], node);
            }
            return(mpq);
        }
 private BinaryTreeNode <byte> makeTree(MinPriorityQueue <long, BinaryTreeNode <byte> > min)
 {
     while (min.Count > 1)
     {
         long min1 = min.MininumPriority;
         BinaryTreeNode <byte> temp1 = min.RemoveMinimumPriority();
         long min2 = min.MininumPriority;
         BinaryTreeNode <byte> temp2 = min.RemoveMinimumPriority();
         BinaryTreeNode <byte> root  = new BinaryTreeNode <byte>(0, temp1, temp2);
         min.Add(root, (min1 + min2));
     }
     return(min.RemoveMinimumPriority());
 }
Beispiel #17
0
        /// <summary>
        /// Builds the Huffman tree from the leaves.
        /// </summary>
        /// <param name="leaves"></param>
        /// <returns></returns>
        private BinaryTreeNode <byte> BuildTree(MinPriorityQueue <long, BinaryTreeNode <byte> > leaves)
        {
            while (leaves.Count > 1)
            {
                long priority1 = leaves.MinimumPriority;
                BinaryTreeNode <byte> tree1 = leaves.RemoveMinimumPriority();
                long priority2 = leaves.MinimumPriority;
                BinaryTreeNode <byte> tree2 = leaves.RemoveMinimumPriority();

                leaves.Add((priority1 + priority2), new BinaryTreeNode <byte>(0, tree1, tree2));
            }
            return(leaves.RemoveMinimumPriority());
        }
Beispiel #18
0
        /// <summary>
        /// Uses Dijkstra's Algorithm to find the shortest distance in a graph between point u and v!
        /// </summary>
        /// <param name="u"> Name of the node we are starting at </param>
        /// <param name="v">Name of the node that serves as our destination </param>
        /// <param name="map"> Map that we are sifting through to find the shortest path </param>
        /// <param name="paths"> Dictionary whose keys are a node, and the value is it's previous node </param>
        /// <returns> The shortest path in decimal form! </returns>
        private static decimal ShortestPath(string u, string v, DirectedGraph <string, decimal> map, out Dictionary <string, string> paths)
        {
            paths = new Dictionary <string, string>();
            // String value in Edge is a node that has already been visited!
            // Priority is the shortest path up until the source node (string value in Edge)
            MinPriorityQueue <decimal, Edge <string, decimal> > queue = new MinPriorityQueue <decimal, Edge <string, decimal> >();

            paths.Add(u, u);
            if (u == v)
            {
                return(0);
            }

            foreach (Edge <string, decimal> edge in map.OutgoingEdges(u))
            {
                queue.Add(edge.Data, edge);
            }

            while (queue.Count != 0)
            {
                decimal minPriority             = queue.MinimumPriority;
                Edge <string, decimal> nextEdge = queue.RemoveMinimumPriority();
                if (paths.ContainsKey(nextEdge.Destination) == false)
                {
                    paths.Add(nextEdge.Destination, nextEdge.Source);
                    if (nextEdge.Destination == v)
                    {
                        return(minPriority);
                    }
                    foreach (Edge <string, decimal> edge in map.OutgoingEdges(nextEdge.Destination))
                    {
                        //queue.Add(edge.Data, edge);
                        queue.Add(minPriority + edge.Data, edge);
                    }
                }
            }
            return(-1);
        }
        private MinPriorityQueue <long, BinaryTreeNode <byte> > makeLeaves(long[] size)
        {
            MinPriorityQueue <long, BinaryTreeNode <byte> > min = new MinPriorityQueue <long, BinaryTreeNode <byte> >();

            for (int i = 0; i < size.Length; i++)
            {
                if (size[i] != 0)
                {
                    BinaryTreeNode <byte> temp = new BinaryTreeNode <byte>((byte)i, null, null);
                    min.Add(temp, size[i]);
                }
            }
            return(min);
        }
Beispiel #20
0
        /// <summary>
        /// Builds the leaves of the Huffman tree.
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private MinPriorityQueue <long, BinaryTreeNode <byte> > BuildLeaves(long[] table)
        {
            MinPriorityQueue <long, BinaryTreeNode <byte> > leaves = new MinPriorityQueue <long, BinaryTreeNode <byte> >();

            for (int i = 0; i < table.Length; i++)
            {
                if (table[i] != 0)
                {
                    BinaryTreeNode <byte> temp = new BinaryTreeNode <byte>((byte)i, null, null);
                    leaves.Add(table[i], temp);
                }
            }
            return(leaves);
        }
        }   //END OF GerDifference METHOD

        /// <summary>
        /// Method used to find the highest frequencies of words in each file
        /// </summary>
        /// <param name="dictionary">Gives the number of occurrences of each word in each file</param>
        /// <param name="numberOfWords">Gives the number of words in each file</param>
        /// <param name="num">number of words to get</param>
        /// <returns></returns>
        public static MinPriorityQueue<float, WordFrequency> GetMostCommonWord(Dictionary<string, WordCount> dictionary, int[] numberOfWords, int number)
        {
            MinPriorityQueue<float, WordFrequency> queue = new MinPriorityQueue<float, WordFrequency>();
            foreach (KeyValuePair<string, WordCount> pair in dictionary)
            {
                WordFrequency temp = new WordFrequency(pair.Value, numberOfWords);
                queue.Add(temp[0] + temp[1], temp);
                if (queue.Count > number)
                {
                    queue.RemoveMinimumPriority();
                }   //END OF IF STATEMENT
            }   //END OF FOREACH LOOP
            return queue;
        }   //END OF GetMostCommonWord METHOD
        }                 //end ProcessFile

        /// <summary>
        /// This method removes the minimum priority object in the queue
        /// </summary>
        /// <param name="wordDictionary">This dictionary holds words and WordCounts</param>
        /// <param name="wordCount">The count of words</param>
        /// <param name="wordsToGet">The number of words to get</param>
        /// <returns>Returns the frequencies in each file of the most common words</returns>
        public static MinPriorityQueue <float, WordFrequency> GetMostCommonWords(Dictionary <string, WordCount> wordDictionary, int[] wordCount, int wordsToGet)
        {
            MinPriorityQueue <float, WordFrequency> minimumQueue = new MinPriorityQueue <float, WordFrequency>();

            foreach (WordCount w in wordDictionary.Values)
            {
                WordFrequency frequency = new WordFrequency(w, wordCount);
                minimumQueue.Add(frequency[0] + frequency[1], frequency);
                if (minimumQueue.Count > wordsToGet)
                {
                    minimumQueue.RemoveMinimumPriority();
                } //end if
            }     // foreach
            return(minimumQueue);
        }         // end GetMostCommonWords
        /// <summary>
        /// Creates a bunch of singleton tree nodes from the nonzero values in the array and adds them to
        /// a Min Priority Queue
        /// </summary>
        /// <param name="bytes"> Array of long values being used </param>
        /// <returns> Min Priority Queue full of singleton tree nodes</returns>
        private static MinPriorityQueue <long, BinaryTreeNode <byte> > BuildLeaves(long[] bytes)
        {
            MinPriorityQueue <long, BinaryTreeNode <byte> > queue = new MinPriorityQueue <long, BinaryTreeNode <byte> >();

            for (int i = 0; i < bytes.Length; i++)
            {
                if (bytes[i] != 0)
                {
                    byte b = (byte)i;
                    BinaryTreeNode <byte> singleton = new BinaryTreeNode <byte>(b, null, null);
                    queue.Add(bytes[i], singleton);
                }
            }
            return(queue);
        }
Beispiel #24
0
        /// <summary>
        /// Constructs HuffmanTreeLeaves
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private MinPriorityQueue <long, BinaryTreeNode <byte> > HuffmanTreeLeaves(long[] table)
        {
            MinPriorityQueue <long, BinaryTreeNode <byte> > mPQ = new MinPriorityQueue <long, BinaryTreeNode <byte> >();

            //MinPriorityQueue<Priority, Value>
            for (int i = 0; i < table.Length; i++)
            {
                if (table[i] != 0)
                {
                    BinaryTreeNode <byte> tempNode = new BinaryTreeNode <byte>((byte)i, null, null);
                    mPQ.Add(table[i], tempNode);
                }
            }
            return(mPQ);
        }
Beispiel #25
0
        /// <summary>
        /// returns a MinPriorityQueue whose elements contain the frequencies in each file of the most common words,
        /// and whose priorities are the combined frequencies of each of these words
        /// </summary>
        /// <param name="dictionary">dictionary to take words from</param>
        /// <param name="words">total number of words</param>
        /// <param name="number">number of words needed to get</param>
        /// <returns>min priority queue holding the frequencies</returns>
        public static MinPriorityQueue <float, WordFrequency> GetMostCommonWords(Dictionary <string, WordCount> dictionary, int[] words, int number)
        {
            MinPriorityQueue <float, WordFrequency> queue = new MinPriorityQueue <float, WordFrequency>();

            foreach (WordCount value in dictionary.Values)
            {
                WordFrequency wordFrequency = new WordFrequency(value, words);
                queue.Add(wordFrequency[0] + wordFrequency[1], wordFrequency);
                if (queue.Count > number)
                {
                    queue.RemoveMinimumPriority();
                }
            }
            return(queue);
        }
Beispiel #26
0
        /// <summary>
        /// A method to get the words with highest combined frequencies
        /// </summary>
        /// <param name="d">A Dictionary<string, WordCount> giving the number of occurrences of each word in each file.</param>
        /// <param name="numWords">An int[ ]  of size 2 giving the number of words in each file.</param>
        /// <param name="getNum">An int giving the number of words to get.</param>
        /// <returns>a MinPriorityQueue<float, WordFrequency> whose elements contain the frequencies in each file of the most common words, and whose priorities are the combined frequencies of each of these words</returns>
        public static MinPriorityQueue <float, WordFrequency> GetMostCommonWords(Dictionary <string, WordCount> d, int[] wordCount, int getNum)
        {
            MinPriorityQueue <float, WordFrequency> minQueue = new MinPriorityQueue <float, WordFrequency>();

            foreach (WordCount w in d.Values)
            {
                WordFrequency freq = new WordFrequency(w, wordCount);
                minQueue.Add(freq[0] + freq[1], freq);
                if (minQueue.Count > getNum)
                {
                    minQueue.RemoveMinimumPriority();
                }
            }
            return(minQueue);
        }
Beispiel #27
0
        /// <summary>
        /// Returns a MinPriorityQueue whose elements contain the frequencies in each file of the most common words.
        /// </summary>
        /// <param name="input">Dictionary containing a string and a WordCount object</param>
        /// <param name="words">int array storing words</param>
        /// <param name="num">int for comparing against count</param>
        /// <returns></returns>
        public static MinPriorityQueue <float, WordFrequency> GetMostCommonWord(Dictionary <string, WordCount> input, int[] words, int num)
        {
            MinPriorityQueue <float, WordFrequency> queue = new MinPriorityQueue <float, WordFrequency>();

            foreach (KeyValuePair <string, WordCount> pair in input)
            {
                WordFrequency temp = new WordFrequency(pair.Value, words);
                queue.Add(temp[0] + temp[1], temp);
                if (queue.Count > num)
                {
                    queue.RemoveMinimumPriority();
                }
            }
            return(queue);
        }
        /// <summary>
        /// Merges all of the singleton trees into a Huffman tree
        /// </summary>
        /// <param name="leaves"> Min Priority Queue containing all the singleton trees </param>
        /// <returns> Returns the resulting Huffman Tree</returns>
        private static BinaryTreeNode <byte> GetHuffmanTree(MinPriorityQueue <long, BinaryTreeNode <byte> > leaves)
        {
            while (leaves.Count > 1)
            {
                long p1 = leaves.MinimumPriority;

                BinaryTreeNode <byte> firstRemoved = leaves.RemoveMinimumPriority();

                long p2 = leaves.MinimumPriority;
                BinaryTreeNode <byte> secondRemoved = leaves.RemoveMinimumPriority();

                BinaryTreeNode <byte> newRoot = new BinaryTreeNode <byte>(0, firstRemoved, secondRemoved);
                leaves.Add(p1 + p2, newRoot);
            }
            return(leaves.RemoveMinimumPriority());
        }
Beispiel #29
0
        /// <summary>
        /// Builds the leaves of the Huffman tree
        /// </summary>
        /// <param name="longArray"></param>
        /// <returns></returns>
        private MinPriorityQueue <long, BinaryTreeNode <byte> > BuildHuffmanTreeLeaves(long[] ByteArray)
        {
            // Build a new Priority Queue
            MinPriorityQueue <long, BinaryTreeNode <byte> > q = new MinPriorityQueue <long, BinaryTreeNode <byte> >();

            for (int x = 0; x < ByteArray.Length; x++)
            {
                // Check if non-zero
                if (ByteArray[x] != 0)
                {
                    // Build new Tree Node with the indicated byte and add to the Byte Array
                    BinaryTreeNode <byte> newNode = new BinaryTreeNode <byte>((byte)x, null, null);
                    q.Add(newNode, ByteArray[x]);
                }
            }
            return(q);
        }
Beispiel #30
0
        public void TestMSort()
        {
            int[]    priorities = { 4, 2, 9, 7, 0, 8, 1, 3, 5, 6 };
            string[] values     = { "4", "2", "9", "7", "0", "8", "1", "3", "5", "6" };
            MinPriorityQueue <int, string> pq = new MinPriorityQueue <int, string>();

            for (int i = 0; i < priorities.Length; i++)
            {
                pq.Add(priorities[i], values[i]);
            }
            List <string> result = new List <string>();

            while (pq.Count > 0)
            {
                result.Add(pq.RemoveMinimumPriority());
            }
            Assert.That(result, Is.Ordered.And.EquivalentTo(values));
        }
Beispiel #31
0
 /// <summary>
 /// Builds the leaves of the Huffman tree
 /// </summary>
 /// <param name="longArray"></param>
 /// <returns></returns>
 private MinPriorityQueue<long, BinaryTreeNode<byte>> BuildHuffmanTreeLeaves(long[] ByteArray)
 {
     // Build a new Priority Queue
     MinPriorityQueue<long, BinaryTreeNode<byte>> q = new MinPriorityQueue<long, BinaryTreeNode<byte>>();
     for(int x = 0 ;x < ByteArray.Length;x++)
     {
         // Check if non-zero
         if(ByteArray[x] != 0)
         {
             // Build new Tree Node with the indicated byte and add to the Byte Array
             BinaryTreeNode<byte> newNode = new BinaryTreeNode<byte>((byte)x,null, null);
             q.Add(newNode, ByteArray[x]);
         }
     }
     return q;
 }
Beispiel #32
0
        /// <summary>
        /// Build huffman tree from given priority queue
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        private BinaryTreeNode<byte> BuildHuffmanTree(MinPriorityQueue<long,BinaryTreeNode<byte>> q)
        {
            while(q.Count>1)
            {
                // Get and remove the two smallest priorities and their associated trees
                long temp1 = q.MininumPriority;
                BinaryTreeNode<byte> tree1 = q.RemoveMinimumPriority();
                long temp2 = q.MininumPriority;
                BinaryTreeNode<byte> tree2 = q.RemoveMinimumPriority();

                // Construct a new binary tree with these trees as its children and 0 as its data (which will be unused).
                BinaryTreeNode<byte> newBinTree = new BinaryTreeNode<byte>(0,tree1,tree2);

                // Add the resulting tree to the min-priority queue with a priority equal to the sum of the priorities of its children.
                q.Add(newBinTree, temp1+temp2);
            }
            return q.RemoveMinimumPriority();
        }