Example #1
0
        // additional test
        static void BottomInts()
        {
            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 };
            MaxPQ <int> pq0     = new MaxPQ <int>(allInts);
            int         M       = allInts.Length / 3;
            MaxPQ <int> pq      = new MaxPQ <int>(M + 1);

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

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

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