Exemplo n.º 1
0
 public void Add(ProductCounts productCount)
 {
     Array.Resize <ProductCounts>(ref productCounts, size + 1);
     size++;
     productCounts[size - 1] = productCount;
     HeapifyUp();
 }
Exemplo n.º 2
0
        public static List <string> GetListTop2MostPQ(List <string> strings)
        {
            List <string>            result = new List <string>();
            Dictionary <string, int> counts = new Dictionary <string, int>();
            PriorityQueue            pq     = new PriorityQueue();

            //strings.Sort();
            foreach (string str in strings)
            {
                if (counts.ContainsKey(str))
                {
                    counts[str]++;
                }
                else
                {
                    counts.Add(str, 1);
                }
            }

            foreach (var key in counts.Keys)
            {
                ProductCounts productCount = new ProductCounts();
                productCount.product = key;
                counts.TryGetValue(key, out productCount.count);
                pq.Add(productCount);
            }

            result.Add(pq.Poll().product);
            result.Add(pq.Poll().product);

            return(result);
        }
Exemplo n.º 3
0
        public ProductCounts Poll()
        {
            if (productCounts.Length == 0)
            {
                throw new Exception("There are no products");
            }
            ProductCounts temp = productCounts[0];

            productCounts[0] = productCounts[size - 1];
            size--;
            HeapifyDown();
            return(temp);
        }