public void Add(ProductCounts productCount) { Array.Resize <ProductCounts>(ref productCounts, size + 1); size++; productCounts[size - 1] = productCount; HeapifyUp(); }
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); }
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); }