// // MaximamHeap.h // SortingAlgorithm // // Created by MB Kyle Kwon on 10/14/12. // Copyright (c) 2012 MB Kyle Kwon. All rights reserved. // #include "HeapDefine.h" #ifndef SortingAlgorithm_MaximamHeap_h #define SortingAlgorithm_MaximamHeap_h //초기화 함수 void initMaxHeap(HeapType *h) { h->heap_size = 0; } //삽입 함수 void insertMaxHeap(HeapType *h, int item) { int i; i = ++(h->heap_size); while((i != 1) && (item > h->heap[i/2])) { h->heap[i] = h->heap[i/2]; i /= 2; } h->heap[i] = item; } //삭제 함수 int deleteMaxHeap(HeapType *h) { int parent, child; int item, temp; item = h->heap[1]; temp = h->heap[(h->heap_size)--]; parent = 1; child = 2; while(child <= h->heap_size) { if((child < h->heap_size) && (h->heap[child] < h->heap[child+1])) child++; if(temp >= h->heap[child]) break; h->heap[parent] = h->heap[child]; parent = child; child *= 2; } h->heap[parent] = temp; return item; } #endif