00001 /* 00002 File: Heap.h 00003 00004 Function: Implements a simple binary heap. From Manber's 00005 "Introduction to Algorithms." 00006 00007 Author: Andrew Willmott 00008 00009 Copyright: (c) 2000, Andrew Willmott 00010 */ 00011 00012 #ifndef __Heap__ 00013 #define __Heap__ 00014 00015 #include "cl/PtrArray.h" 00016 00017 struct HeapEntry 00018 { 00019 Float cost; 00020 Int heapIdx; 00021 }; 00022 00023 typedef PtrArray<HeapEntry> HeapEntries; 00024 00025 class Heap 00026 { 00027 public: 00028 00029 // operations needed for a priority queue 00030 00031 Void Insert(HeapEntry *he); 00032 // add he to the heap. 00033 Void Delete(HeapEntry *he); 00034 // remove he from the heap. 00035 Void Update(HeapEntry *he); 00036 // signal that he's cost has been updated 00037 HeapEntry *RemoveMax(); 00038 // remove highest cost node. 00039 00040 Int NumItems() 00041 { return(heap.NumItems()); }; 00042 00043 Void HeapifyUp(Int i); 00044 Void HeapifyDown(Int i); 00045 00046 Int Parent(Int i) 00047 { return((i - 1) / 2); } 00048 Int Left(Int i) 00049 { return(2 * i + 1); } 00050 Int Right(Int i) 00051 { return(2 * i + 2); } 00052 00053 HeapEntries heap; 00054 }; 00055 00056 #endif