Exemplo n.º 1
0
        public void InsertTest(IHeap <int> heap, bool isMin)
        {
            var newList = RandomList().ToList();
            var tmpLst  = new List <int>(TestList);

            tmpLst.AddRange(newList);
            tmpLst.Sort();
            if (!isMin)
            {
                tmpLst.Reverse();
            }
            foreach (var num in newList)
            {
                heap.Insert(num);
            }
            var heapList = new List <int>();

            while (!heap.IsEmpty())
            {
                heapList.Add(heap.ExtractMin().Value);
            }
            Console.WriteLine("isMin:" + isMin);
            Console.WriteLine("Expect:" + string.Join(", ", tmpLst));
            Console.WriteLine("Result:" + string.Join(", ", heapList));
            Assert.IsTrue(tmpLst.SequenceEqual(heapList));
        }
Exemplo n.º 2
0
 /// <summary>
 ///     Removes and returns the object at the beginning of the queue.
 /// </summary>
 /// <returns>Object at the beginning of the queue.</returns>
 public T Dequeue()
 {
     if (_heap.IsEmpty())
     {
         throw new InvalidOperationException("PriorityObject Queue is empty");
     }
     return(_heap.ExtractMin().Value.Object);
 }
Exemplo n.º 3
0
        public override bool Add(T o)
        {
            bool added = true;

            elements.Add(o);
            while (Count > Capacity())
            {
                object dumped = elements.ExtractMin();
                if (dumped.Equals(o))
                {
                    added = false;
                }
            }
            return(added);
        }
Exemplo n.º 4
0
        public void ExtractMinTest(IHeap <int> heap, bool isMin)
        {
            var tmpLst = new List <int>(TestList);

            Console.WriteLine("isMin:" + isMin);
            var expected = isMin ? tmpLst.Min() : tmpLst.Max();
            var result   = heap.ExtractMin().Value;

            Console.WriteLine("Expect:" + expected + " Result:" + result);
            Assert.AreEqual(expected, result);
            expected = tmpLst.Count - 1;
            result   = heap.Size();
            Console.WriteLine("Expect:" + expected + " Result:" + result);
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 5
0
 protected IEnumerable <int> GetHeapList(bool isMin)
 {
     if (isMin)
     {
         while (!MinHeap.IsEmpty())
         {
             yield return(MinHeap.ExtractMin().Value);
         }
     }
     else
     {
         while (!MaxHeap.IsEmpty())
         {
             yield return(MaxHeap.ExtractMin().Value);
         }
     }
 }
Exemplo n.º 6
0
 public T Dequeue() => _heap.ExtractMin();