text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

Heap.cpp

Go to the documentation of this file.
00001 
00017 #include "Heap.h"  // header file for class Heap
00018 
00019 Heap::Heap() : size(0)
00020 {
00021 }  // end default constructor
00022 
00023 Heap::~Heap()
00024 {
00025 }  // end destructor
00026 
00027 bool Heap::heapIsEmpty() const
00028 {
00029    return bool(size == 0);
00030 }  // end heapIsEmpty
00031 
00032 void Heap::heapInsert(const HeapItemType& newItem)
00033    throw(HeapException)
00034 // Method: Inserts the new item after the last item in the
00035 // heap and trickles it up to its proper position. The
00036 // heap is full when it contains MAX_HEAP items.
00037 {
00038    if (size >= MAX_HEAP)
00039       throw HeapException("HeapException: Heap full");
00040    // place the new item at the end of the heap
00041    items[size] = newItem;
00042 
00043    // trickle new item up to its proper position
00044    int place = size;
00045    int parent = (place - 1)/2;
00046    while ( (parent >= 0) &&
00047            (items[place].getKey() > items[parent].getKey()) )
00048    {  // swap items[place] and items[parent]
00049       HeapItemType temp = items[parent];
00050       items[parent] = items[place];
00051       items[place] = temp;
00052 
00053       place = parent;
00054       parent = (place - 1)/2;
00055    }  // end while
00056 
00057    ++size;
00058 }  // end heapInsert
00059 
00060 void Heap::heapDelete(HeapItemType& rootItem)
00061    throw(HeapException)
00062 // Method: Swaps the last item in the heap with the root
00063 // and trickles it down to its proper position.
00064 {
00065    if (heapIsEmpty())
00066       throw HeapException("HeapException: Heap empty");
00067    else
00068    {  rootItem = items[0];
00069       items[0] = items[--size];
00070       heapRebuild(0);
00071    }  // end if
00072 }  // end heapDelete
00073 
00074 void Heap::heapRebuild(int root)
00075 {
00076    // if the root is not a leaf and the root's search key
00077    // is less than the larger of the search keys in the
00078    // root's children
00079    int child = 2 * root + 1;  // index of root's left
00080                               // child, if any
00081    if ( child < size )
00082    {  // root is not a leaf, so it has a left child at child
00083       int rightChild = child + 1;  // index of right child,
00084                                    // if any
00085 
00086       // if root has a right child, find larger child
00087       if ( (rightChild < size) &&
00088            (items[rightChild].getKey() > items[child].getKey()) )
00089          child = rightChild;  // index of larger child
00090 
00091       // if the root's value is smaller than the
00092       // value in the larger child, swap values
00093       if ( items[root].getKey() < items[child].getKey() )
00094       {  HeapItemType temp = items[root];
00095          items[root] = items[child];
00096          items[child] = temp;
00097 
00098          // transform the new subtree into a heap
00099          heapRebuild(child);
00100       }  // end if
00101    }  // end if
00102 
00103    // if root is a leaf, do nothing
00104 }  // end heapRebuild
00105 // End of implementation file.

Generated on Sun Aug 27 22:03:32 2006 for AWLogo by  doxygen 1.4.6