0,算法对数据结构的要求 1,hash 2,heap : bin-heap, binomial-heap, fibonacci-heap, 3,tree : 各种奇形怪状的树 有时间写,先充实下内容。 // a binary-heap implementation #include <vector> #include <iostream> using std::cout; using std::endl; using std::ostream; template <typename T> void swap(T &a, T&b) { T t = a; a = b; b = t; } template <typename Element, typename KeyType> class MinHeap; template <typename Element, typename KeyType> ostream &operator<<(ostream &os, const MinHeap<Element, KeyType> &mh) { os << "heap dump :" << endl; for (int i = 0; i != mh.size(); i++) cout << *mh.elem_vec[i] << endl; os << "dump over" << endl; return os; } template <typename Element, typename KeyType> class MinHeap { std::vector<Element *> elem_vec; public: friend ostream &operator<< <Element, KeyType> (ostream &, const MinHeap<Element, KeyType> &); MinHeap() { }; //MinHeap(std::vector<Element> &e) : elem_vec(e) { }; int size() const { return elem_vec.size(); }; bool empty() { return elem_vec.empty(); }; Element &minimum() { if (elem_vec.empty()) { // throw exception //return static_cast<Element &>(Element::null); } return *elem_vec[0]; } Element &extract_min(); int insert(Element &e); int decrease_key(Element &e, KeyType &new_key); int increase_key(Element &e, KeyType &new_key); private: int left(int i) { return i * 2 + 1; } int right(int i) { return i * 2 + 2; } int parent(int i) { return (i - 1) / 2 ; } }; template <typename Element, typename KeyType> int MinHeap<Element, KeyType>::insert(Element &e) { KeyType key = e.key; e.heap_index = elem_vec.size(); elem_vec.push_back(&e); e.key = Element::maximum_key; decrease_key(e, key); return 0; } template <typename Element, typename KeyType> Element &MinHeap<Element, KeyType>::extract_min() { if (elem_vec.empty()) { // throw exception //return static_cast<Element &>(Element::null) ; } Element &e = *elem_vec[0]; KeyType key = elem_vec[elem_vec.size() - 1]->key; elem_vec[0] = elem_vec[elem_vec.size() - 1]; elem_vec[0]->key = Element::minimum_key; elem_vec[0]->heap_index = 0; elem_vec.erase(elem_vec.end() - 1); increase_key(*elem_vec[0], key); return e; } template <typename Element, typename KeyType> int MinHeap<Element, KeyType>::decrease_key(Element &e, KeyType &new_key) { int i = e.heap_index, tmp_index; elem_vec[i]->key = new_key; while (i > 0 && elem_vec[parent(i)]->key > elem_vec[i]->key) { swap(elem_vec[parent(i)]->heap_index, elem_vec[i]->heap_index); swap(elem_vec[parent(i)], elem_vec[i]); i = parent(i); } return 0; } template <typename Element, typename KeyType> int MinHeap<Element, KeyType>::increase_key(Element &e, KeyType &new_key) { // max end() -1 in pos 0 climbed down tree int i = e.heap_index, min_idx; int size = elem_vec.size(); e.key = new_key; while (left(i) < size && elem_vec[i]->key > elem_vec[left(i)]->key || right(i) < size && elem_vec[i]->key > elem_vec[left(i)]->key) { min_idx = (right(i) < size && elem_vec[left(i)]->key > elem_vec[right(i)]->key) ? right(i) : left(i); swap(elem_vec[min_idx]->heap_index, elem_vec[i]->heap_index); swap(elem_vec[min_idx], elem_vec[i]); i = min_idx; } return 0; }