// add all elements to copy of heap // takes linear time since already in heap order so no keys move public HeapIterator(IndexMinPQ <Key> minPQ) { copy = new IndexMinPQ <Key>(minPQ.pq.Length - 1); for (int i = 1; i <= minPQ.N; i++) { copy.Insert(minPQ.pq[i], minPQ.keys[minPQ.pq[i]]); } }
public static void Main(string[] args) { // insert a bunch of strings string[] strings = { "it", "was", "the", "best", "of", "times", "it", "was", "the", "worst" }; var pq = new IndexMinPQ <string>(strings.Length); for (int i = 0; i < strings.Length; i++) { pq.Insert(i, strings[i]); } // delete and print each key while (!pq.IsEmpty()) { int i = pq.DelMin(); Console.WriteLine(i + " " + strings[i]); } Console.WriteLine(); // reinsert the same strings for (int i = 0; i < strings.Length; i++) { pq.Insert(i, strings[i]); } // print each key using the iterator foreach (int i in pq) { Console.WriteLine(i + " " + strings[i]); } while (!pq.IsEmpty()) { pq.DelMin(); } }