//missing some definition here //array implementation class Heap { //from index 0 protected: int capacity; int current; ElementType* heap; int type; //min 0 or max 1, default min void PercolateUp(int i); void PercolateDown(int i); public: Heap(int num, int minOrMax = 0); ~Heap(); bool IsEmpty(void) { return (current==0); } void* Peek(void) { return heap[0].item; } void BuildHeap(KeyType keyVal, void* item = NULL); //insert one by one void BuildHeap(KeyType keyVal[], void* item[], int len); //insert all, then heapify void* Delete(KeyType keyVal); void* Delete(void); void Insert(KeyType keyVal, void* item = NULL); void Print(void); void test(void); }; //====================================== Heap Heap::Heap(int num, int minOrMax){ capacity = num; current= 0; type = minOrMax; heap = new ElementType[num]; ASSERT(heap); memset(heap, 0, sizeof(ElementType)*num); } Heap::~Heap(){ delete[] heap; } void Heap::BuildHeap( KeyType keyVal, void* item){ Insert(keyVal, item); } void Heap::BuildHeap(KeyType keyVal[], void* item[], int len) { ASSERT(len); void* ptr; while(current<capacity && current < len){ heap[current].keyVal = keyVal[current]; if(item) heap[current].item = item[current]; else{ ptr = new KeyType; ASSERT(ptr); *((KeyType*)ptr) = keyVal[current]; heap[current].item = ptr; } current ++; } for(int i = current/2-1;i>=0;i--) //len is not index, build heap PercolateDown(i); } void Heap::PercolateUp(int i){ int parent; ElementType temp; ASSERT(i>=0 && i<current); temp.keyVal = heap[i].keyVal; temp.item = heap[i].item; for(; parentHeap(i) >= 0 ; i = parent){ //parentHeap is not actual parent, should divide by 2 parent = parentHeap(i)/2; if(compareHeap(temp.keyVal, heap[parent].keyVal, type)){ heap[i].keyVal = heap[parent].keyVal; heap[i].item = heap[parent].item; } else break; } heap[i].keyVal = temp.keyVal; heap[i].item = temp.item; } void Heap::PercolateDown(int i) { int child; ElementType temp; ASSERT(i>=0 && i<current); temp.keyVal = heap[i].keyVal; temp.item = heap[i].item; for(; leftChild(i) < current; i = child){ child=leftChild(i); if(child+1<current&&compareHeap(heap[child+1].keyVal, heap[child].keyVal, type)) //check right child child++; if(compareHeap(heap[child].keyVal, temp.keyVal,type)){ heap[i].item = heap[child].item; heap[i].keyVal = heap[child].keyVal; } else break; } heap[i].item = temp.item; heap[i].keyVal = temp.keyVal; } void* Heap::Delete(KeyType keyVal){ int pos = -1; void* item = NULL; for(int i=0;i < current; i++){ if(heap[i].keyVal == keyVal){ pos = i; break; } } if(pos >= 0){ item = heap[pos].item; heap[pos].item = heap[current-1].item; heap[pos].keyVal = heap[current-1].keyVal; memset(&heap[current-1], 0, sizeof(ElementType)); PercolateDown(pos); current--; } return item; } void Heap::Insert(KeyType keyVal, void* item){ ASSERT(current < capacity); if(!item){ item = new KeyType; ASSERT(item); *((KeyType*)item) = keyVal; } heap[current].item = item; heap[current++].keyVal = keyVal; PercolateUp(current-1); } void* Heap::Delete(void){ void* item = heap[0].item; if(current){ heap[0].item = heap[current-1].item; heap[0].keyVal = heap[current-1].keyVal; memset(&heap[current], 0, sizeof(ElementType)); PercolateDown(0); current--; } return item; } void Heap::Print(void){ for(int i=0;i<current; i++){ printf("%d ", heap[i]); } printf("/n"); } void Heap::test(void){ static int L[]={13,21,16,24,31,19,68,65,26,32}; BuildHeap(L, NULL, 10); Print(); /*for(int i=0; i<10; i++){ BuildHeap(L[i]); Print(); }*/ printf("%d /n", *((int*)Peek())); printf("%d /n",*((int*)Delete(13))); Print(); printf("%d /n", *((int*)Peek())); printf("%d /n", *((int*)Delete())); printf("%d /n", *((int*)Peek())); Insert(13); printf("%d /n", *((int*)Peek())); while(!IsEmpty()){ printf("%d /n", *((int*)Delete())); } } 堆的数组实现,纯练习,多多指教; Fight on!!!