예제 #1
0
        private static void TestMyPriorityQueue()
        {
            MyPriorityQueue <int> pq = new MyPriorityQueue <int>();

            // test empty PQ
            Console.WriteLine(pq.Size() == 0);

            // test add and peek
            pq.Add(5);
            Console.WriteLine(pq.Size() == 1);
            Console.WriteLine(pq.Peek() == 5);
            pq.Add(2);
            Console.WriteLine(pq.Size() == 2);
            Console.WriteLine(pq.Peek() == 2);
            pq.Add(9);
            Console.WriteLine(pq.Size() == 3);
            Console.WriteLine(pq.Peek() == 2);
            // duplicate items not ignored
            pq.Add(3);
            Console.WriteLine(pq.Size() == 4);
            pq.Add(1);
            Console.WriteLine(pq.Size() == 5);
            pq.PrintHeap();
            // test pop
            int prev = pq.Pop();

            pq.PrintHeap();
            for (int i = 0, size = pq.Size() - 1; i < size; i++)
            {
                int curr = pq.Pop();
                Console.WriteLine(prev <= curr);
                pq.PrintHeap();
                prev = curr;
            }
        }
예제 #2
0
 public void Clear()
 {
     pq           = new MyPriorityQueue <PriorityNode>();
     nextPriority = 0;
 }