/* Priority queues, implemented as heaps * 15-122 Principles of Imperative Computation, Fall 2010 * Frank Pfenning */ typedef struct heap* heap; heap heap_new(int limit); /* create new heap of size limit */ bool heap_empty(heap H); /* is H empty? */ void heap_insert(heap H, int x); /* insert x into H */ int heap_min(heap H); /* find minimum */ int heap_delmin(heap H); /* delete minimum */ struct heap { int limit; int next; int[] heap; }; bool is_heap(heap H) //@requires H != NULL && H->limit <= \length(H->heap); { if (H == NULL) return false; if (!(1 <= H->next && H->next <= H->limit)) return false; { int i; for (i = 2; i < H->next; i++) { if (H->heap[i] < H->heap[i/2]) return false; } } return true; }