// Created by Frank M. Carrano and Tim Henry. // Copyright (c) 2013 __Pearson Education__. All rights reserved. /** Array-based implementation of the ADT heap. @file ArrayMaxHeap.cpp */ // PARTIALLY COMPLETE #include "ArrayMaxHeap.h" #include "PrecondViolatedExcep.h" template int ArrayMaxHeap::getLeftChildIndex(const int nodeIndex) const { return (2 * nodeIndex) + 1; } // end getLeftChildIndex template void ArrayMaxHeap::heapCreate() { for (int index = itemCount / 2; index >= 0; index--) { heapRebuild(index); } // end for } // end heapCreate template ArrayMaxHeap:: ArrayMaxHeap(const ItemType someArray[], const int arraySize): itemCount(arraySize), maxItems(2 * arraySize) { // Allocate the array items = new ItemType[2 * arraySize]; // Copy given values into the array for (int i = 0; i < itemCount; i++) items[i] = someArray[i]; // Reorganize the array into a heap heapCreate(); } // end constructor template ItemType ArrayMaxHeap::peekTop() const throw(PrecondViolatedExcep) { if (isEmpty()) throw PrecondViolatedExcep("Attempted peek into an empty heap."); return items[0]; } // end peekTop