Esempio n. 1
0
        // additional test
        private static void TopInts()
        {
            int[] allInts = { 12, 11, 8, 7, 9, 5, 4, 3, 2, 29, 23, 1, 24, 30, 9, 4, 88, 5, 100, 29, 23, 5, 99, 87, 22, 111 };
            //MinPQ<int> pq0 = new MinPQ<int>(allInts);
            int         M  = allInts.Length / 3;
            MinPQ <int> pq = new MinPQ <int>(M + 1);

            Console.WriteLine("Top {0} is ", M);
            foreach (var n in allInts)
            {
                pq.Insert(n);
                Console.WriteLine("Min is {0}", pq.Min);
                // remove minimum if M+1 entries on the PQ
                if (pq.Count > M)
                {
                    pq.DelMin();
                }
            }
            // print entries on PQ in reverse order
            Stack <int> stack = new Stack <int>();

            foreach (int n in pq)
            {
                stack.Push(n);
            }
            foreach (int n in stack)
            {
                Console.WriteLine(n);
            }
            Console.WriteLine("These are the top elements");
        }
Esempio n. 2
0
 // add all items to copy of heap
 // takes linear time since already in heap order so no keys move
 public HeapIEnumerator(MinPQ <Key> pq)
 {
     if (pq.comparator == null)
     {
         innerPQ = new MinPQ <Key>(pq.Count);
     }
     else
     {
         innerPQ = new MinPQ <Key>(pq.Count, pq.comparator);
     }
     for (int i = 1; i <= pq.N; i++)
     {
         innerPQ.Insert(pq.pq[i]);
     }
     copy = innerPQ;
 }
Esempio n. 3
0
        public static void MainTest(string[] args)
        {
            TextInput      StdIn = new TextInput();
            MinPQ <string> pq    = new MinPQ <string>();

            while (!StdIn.IsEmpty)
            {
                string item = StdIn.ReadString();
                if (!item.Equals("-"))
                {
                    pq.Insert(item);
                }
                else if (!pq.IsEmpty)
                {
                    Console.Write(pq.DelMin() + " ");
                }
            }
            Console.WriteLine("(" + pq.Count + " left on pq)");
        }
Esempio n. 4
0
 public void Reset()
 {
     innerPQ = copy;
 }