Beispiel #1
0
 public PriorityQueue()
 {
     mHeapImpl = new MinMaxHeap <T>();
 }
Beispiel #2
0
        static void HeapTest()
        {
            MinMaxHeap <int> heapRemoveMin = new MinMaxHeap <int>();

            heapRemoveMin.DebugCheckHeapProperty = true;
            MinMaxHeap <int> heapRemoveMax = new MinMaxHeap <int>();

            heapRemoveMax.DebugCheckHeapProperty = true;
            List <int> doubleCheckMin = new List <int>();
            List <int> doubleCheckMax = new List <int>();
            Random     r = new Random();

            // Generate the list of numbers to populate the heaps and to check against
            if (true)
            {
                for (int i = 0; i < 700; i++)
                {
                    int randInt = r.Next(-10000, 10000);
                    doubleCheckMin.Add(randInt);
                }
            }
            else
            {
                // Manually test degenerate cases
                doubleCheckMin.Add(-55);
                doubleCheckMin.Add(31);
                doubleCheckMin.Add(-93);
                //doubleCheckMin.Add(64);
            }

            for (int i = 0; i < doubleCheckMin.Count; i++)
            {
                int randInt = doubleCheckMin[i];
                // heap.Add("" + i, i);
                heapRemoveMin.Add(randInt, randInt);
                heapRemoveMax.Add(randInt, randInt);
                doubleCheckMax.Add(randInt);
            }
            doubleCheckMin.Sort(); // Default. Ascending
            doubleCheckMax.Sort(delegate(int x, int y)
            {
                if (x == y)
                {
                    return(0);
                }
                if (x > y)
                {
                    return(-1);
                }
                if (x < y)
                {
                    return(1);
                }
                return(0);
            });
            Console.WriteLine(" -- NOW REMOVE MIN --");
            int checkCount = 0;

            while (heapRemoveMin.Count > 0)
            {
                int min = heapRemoveMin.FindMin();
                if (doubleCheckMin[checkCount] != min)
                {
                    throw new Exception("WRONG!");
                }
                heapRemoveMin.RemoveMin();
                checkCount++;
                Console.WriteLine(min);
            }
            Console.WriteLine(" -- NOW REMOVE MAX --");
            checkCount = 0;
            while (heapRemoveMax.Count > 0)
            {
                //Console.WriteLine("iteration " + checkCount);
                //Console.WriteLine(heapRemoveMax.PrintTree());
                int max = heapRemoveMax.RemoveMax();
                if (doubleCheckMax[checkCount] != max)
                {
                    throw new Exception("WRONG!");
                }
                checkCount++;
                Console.WriteLine(max);
            }
        }