标签:main min() make com size namespace OLE child insert
二叉最小堆的C++实现。
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 template <typename Comparable> 5 class binaryHeap 6 { 7 private: 8 vector<Comparable> array; 9 int currentSize; 10 void buildHeap(); 11 void nodeDown(int hole); 12 void nodeUp(int hole); 13 public: 14 explicit binaryHeap(int capacity=100); 15 explicit binaryHeap(const vector<Comparable>& items); 16 bool isEmpty()const; 17 const Comparable& findMin()const; 18 void insert(Comparable& x); 19 void deleteMin(); 20 void deleteMin(Comparable& x); 21 void makeEmpty(); 22 }; 23 24 template <typename Comparable> 25 void binaryHeap<Comparable>::buildHeap() 26 { 27 for(int i = currentSize / 2; i >= 1; --i) 28 nodeDown(i); 29 } 30 31 template <typename Comparable> 32 void binaryHeap<Comparable>::nodeDown(int hole) 33 { 34 Comparable tmp = array[hole]; 35 int child = 0; 36 for(; hole * 2 <= currentSize; hole = child){ 37 child = hole * 2; 38 if(child != currentSize && array[child] > array[child + 1]) 39 child++; 40 if(array[child] < tmp) 41 array[hole] = array[child]; 42 else 43 break; 44 } 45 array[hole] = tmp; 46 } 47 48 template <typename Comparable> 49 void binaryHeap<Comparable>::nodeUp(int hole) 50 { 51 Comparable tmp = array[hole]; 52 for(; hole > 1; hole /= 2){ 53 if(array[hole / 2] > tmp) 54 array[hole] = array[hole / 2]; 55 else 56 break; 57 } 58 array[hole] = tmp; 59 } 60 61 template <typename Comparable> 62 binaryHeap<Comparable>::binaryHeap(int capacity) 63 { 64 array.resize(capacity); 65 currentSize = 0; 66 } 67 68 template <typename Comparable> 69 binaryHeap<Comparable>::binaryHeap(const vector<Comparable>& items): 70 array(items.size() + 10), currentSize(items.size()) 71 { 72 for(int i = 0; i < items.size(); ++i) 73 array[i + 1] = items[i]; 74 buildHeap(); 75 } 76 77 template <typename Comparable> 78 bool binaryHeap<Comparable>::isEmpty()const 79 { 80 return currentSize == 0; 81 } 82 83 template <typename Comparable> 84 const Comparable& binaryHeap<Comparable>::findMin()const 85 { 86 if(!isEmpty()) 87 return array[1]; 88 } 89 90 template <typename Comparable> 91 void binaryHeap<Comparable>::insert(Comparable& x) 92 { 93 if(currentSize = array.size() - 1) 94 array.resize(currentSize * 2); 95 array[++currentSize] = x; 96 nodeUp(currentSize); 97 } 98 99 template <typename Comparable> 100 void binaryHeap<Comparable>::deleteMin() 101 { 102 array[1] = array[currentSize--]; 103 nodeDown(1); 104 } 105 106 template <typename Comparable> 107 void binaryHeap<Comparable>::deleteMin(Comparable& x) 108 { 109 x = array[1]; 110 deleteMin(); 111 } 112 113 template <typename Comparable> 114 void binaryHeap<Comparable>::makeEmpty() 115 { 116 array.clear(); 117 currentSize = 0; 118 } 119 120 int main() 121 { 122 int lt[11] = {10, 100, 77, 23, 31, 1, 9, 7, 19, 57, 83}; 123 vector<int> yj(lt, lt + 11); 124 binaryHeap<int> kk(yj); 125 cout << kk.findMin() << endl; 126 kk.deleteMin(); 127 cout << kk.findMin() << endl; 128 return 0; 129 }
标签:main min() make com size namespace OLE child insert
原文地址:https://www.cnblogs.com/kHrystal/p/9471637.html