二叉堆的實現

  1. #include <iostream>  
  2. #include <string>  
  3.   
  4. #define MAX_HEAP_SIZE 101  
  5.   
  6. class BinaryMinHeap {  
  7. public:  
  8.     BinaryMinHeap(int *heapArray);  
  9.     BinaryMinHeap(int size);  
  10.     ~BinaryMinHeap() {  
  11.         delete []heapArray;  
  12.     }  
  13.     void insert(int value);  
  14.     void removeMin();  
  15.     int getMin();  
  16.     void displayHeapArray();  
  17.   
  18. private:  
  19.     int *heapArray;  
  20.     int heapSize;  
  21.     int heapArraySize;  
  22.   
  23.     void siftUp(int nodeIndex);  
  24.     void siftDown(int nodeIndex);  
  25.   
  26.     int getLeftChildIndex(int nodeIndex) {  
  27.         return 2*nodeIndex+1;  
  28.     }  
  29.   
  30.     int getRightChildIndex(int nodeIndex) {  
  31.         return 2*nodeIndex+2;  
  32.     }  
  33.   
  34.     int getParentIndex(int nodeIndex) {  
  35.         return (nodeIndex-1)/2;  
  36.     }  
  37. };  



  1. #include "BinaryHeap.h"  
  2. #include <stdio.h>  
  3.   
  4. BinaryMinHeap::BinaryMinHeap(int size) {  
  5.     heapArray = new int[size];  
  6.     heapSize = 0;  
  7.     heapArraySize = size;  
  8. }  
  9.   
  10. void BinaryMinHeap::insert(int value) {  
  11.     if(heapSize == heapArraySize) {  
  12.         throw std::string("Insertion Error: The heap is full!");  
  13.     } else {  
  14.         heapSize++;  
  15.         heapArray[heapSize-1] = value;  
  16.         siftUp(heapSize-1);  
  17.     }  
  18. }  
  19.   
  20. /* 
  21.  * NON-Recursive Version 
  22.  */  
  23. void BinaryMinHeap::siftUp(int nodeIndex) {  
  24.     int i = nodeIndex;  
  25.     int parent = getParentIndex(nodeIndex);  
  26.     while(parent >= 0) {  
  27.         if(heapArray[parent] > heapArray[i]) {  
  28.             int tmp = heapArray[parent];  
  29.             heapArray[parent] =  heapArray[i];  
  30.             heapArray[i] =  tmp;  
  31.   
  32.             i = parent;  
  33.             parent = getParentIndex(parent);  
  34.         } else {  
  35.             break;  
  36.         }  
  37.     }  
  38. //  fprintf(stderr, "parent = %d\n", parent);  
  39. }  
  40.   
  41. void BinaryMinHeap::removeMin() {  
  42.     if(heapSize == 0) {  
  43.         throw std::string("Remove Error: The heap is empty!");    
  44.     } else {  
  45.         heapArray[0] =  heapArray[heapSize - 1];  
  46.         heapSize--;  
  47.         siftDown(0);  
  48.     }  
  49. }  
  50.   
  51. void BinaryMinHeap::siftDown(int nodeIndex) {  
  52.     int leftIndex = getLeftChildIndex(nodeIndex);  
  53.     int rightIndex = getRightChildIndex(nodeIndex);  
  54.     int minIndex = (heapArray[leftIndex]>heapArray[rightIndex])?rightIndex:leftIndex;  
  55.   
  56.     while(minIndex <= heapSize) {  
  57.         if(heapArray[minIndex] < heapArray[nodeIndex]) {  
  58.             int tmp = heapArray[minIndex];  
  59.             heapArray[minIndex] =  heapArray[nodeIndex];  
  60.             heapArray[nodeIndex] = tmp;  
  61.               
  62.             nodeIndex = minIndex;  
  63.             leftIndex = getLeftChildIndex(nodeIndex);  
  64.             rightIndex = getRightChildIndex(nodeIndex);  
  65.             minIndex = (heapArray[leftIndex]>heapArray[rightIndex])?rightIndex:leftIndex;  
  66.         } else {  
  67.             break;  
  68.         }  
  69.     }  
  70. }  
  71.   
  72. void BinaryMinHeap::displayHeapArray() {  
  73.     if(heapSize == 0) {  
  74.         throw std::string("Empty Heap!");  
  75.     } else {  
  76.         std::cout << "\n--------------------\n";  
  77.         std::cout << "heap size is:\t" << heapSize << std::endl;   
  78.         for(int i = 0; i < heapSize; i++) {  
  79.             std::cout << heapArray[i] << "\t";  
  80.         }  
  81.         std::cout << "\n--------------------\n";  
  82.     }  
  83. }  
  84.   
  85. int BinaryMinHeap::getMin() {  
  86.     return heapArray[0];  
  87. }  


Main.cpp:

  1. #include "BinaryHeap.h"  
  2.   
  3. using namespace std;  
  4.   
  5. int main() {  
  6.     BinaryMinHeap minHeap(10);  
  7.   
  8.     minHeap.insert(10);  
  9.     minHeap.displayHeapArray();  
  10.     minHeap.insert(23);  
  11.     minHeap.displayHeapArray();  
  12.     minHeap.insert(11);  
  13.     minHeap.displayHeapArray();  
  14.     minHeap.insert(4);  
  15.     minHeap.displayHeapArray();  
  16.     minHeap.insert(3);  
  17.     minHeap.displayHeapArray();  
  18.   
  19.     minHeap.removeMin();  
  20.     minHeap.displayHeapArray();  
  21.     minHeap.removeMin();  
  22.     minHeap.displayHeapArray();  
  23.     minHeap.removeMin();  
  24.     minHeap.displayHeapArray();  
  25.     minHeap.removeMin();  
  26.     minHeap.displayHeapArray();  
  27.     minHeap.removeMin();  
  28.     try {  
  29.         minHeap.displayHeapArray();  
  30.     } catch(std::string e) {  
  31.         cout << e << endl;  
  32.     }  
  33.     return 0;  
  34. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章