完成push_heap

This commit is contained in:
邹晓航
2014-10-14 16:42:56 +08:00
parent 023dd3a212
commit 1a75d215af

View File

@@ -73,28 +73,30 @@ namespace TinySTL{
} }
//********** [make_heap] *************** //********** [make_heap] ***************
//********* [Algorithm Complexity: O(N)] **************** //********* [Algorithm Complexity: O(N)] ****************
//template<class RandomAccessIterator, class Compare> template<class RandomAccessIterator, class Compare>
////heap<61><70><EFBFBD><EFBFBD><EFBFBD> //heap<61><70><EFBFBD><EFBFBD><EFBFBD>
//static void up(RandomAccessIterator first, RandomAccessIterator last, Compare comp){//[first, last] static void up(RandomAccessIterator first, RandomAccessIterator last,
// if (first != last){ RandomAccessIterator head, Compare comp){//1.[first, last], 2.headr points the header of the heap
// auto range = last - first + 1; if (first != last){
// for (auto cur = last; rait > first; range /= 2){ int index = last - head;
// auto parent = first + (range / 2 - 1); auto parentIndex = (index - 1) / 2;
// if (comp(*parent, *cur)) for (auto cur = last; parentIndex >= 0 && cur != head; parentIndex = (index - 1) / 2){
// TinySTL::swap(*parent, *cur); auto parent = head + parentIndex;//get parent
// cur = parent; if (comp(*parent, *cur))
// } TinySTL::swap(*parent, *cur);
// } cur = parent;
//} index = cur - head;
}
}
}
template<class RandomAccessIterator, class Compare> template<class RandomAccessIterator, class Compare>
//heap<61>½<EFBFBD><C2BD> //heap<61>½<EFBFBD><C2BD>
static void down(RandomAccessIterator first, RandomAccessIterator last, static void down(RandomAccessIterator first, RandomAccessIterator last,
RandomAccessIterator head, Compare comp){//1.[first, last], 2.headr points the header of the heap RandomAccessIterator head, Compare comp){//1.[first, last], 2.headr points the header of the heap
if (first != last){ if (first != last){
const auto range = last - head + 1;
auto index = first - head; auto index = first - head;
auto leftChildIndex = index * 2 + 1; auto leftChildIndex = index * 2 + 1;
for (auto cur = first; leftChildIndex < range && cur < last; leftChildIndex = index * 2 + 1){ for (auto cur = first; leftChildIndex < (last - head + 1) && cur < last; leftChildIndex = index * 2 + 1){
auto child = head + leftChildIndex;//get the left child auto child = head + leftChildIndex;//get the left child
if ((child + 1) <= last && *(child + 1) > *child)//cur has a right child if ((child + 1) <= last && *(child + 1) > *child)//cur has a right child
child = child + 1; child = child + 1;
@@ -114,15 +116,20 @@ namespace TinySTL{
void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp){ void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp){
const auto range = last - first; const auto range = last - first;
for (auto cur = first + range / 2 - 1; cur >= first; --cur){ for (auto cur = first + range / 2 - 1; cur >= first; --cur){
down(cur, last - 1, first, comp); TinySTL::down(cur, last - 1, first, comp);
if (cur == first) return; if (cur == first) return;
} }
} }
//********* [push_heap] *************** //********* [push_heap] ***************
template <class RandomAccessIterator> template <class RandomAccessIterator>
void push_heap(RandomAccessIterator first, RandomAccessIterator last); void push_heap(RandomAccessIterator first, RandomAccessIterator last){
TinySTL::push_heap(first, last,
TinySTL::less<typename TinySTL::iterator_traits<RandomAccessIterator>::value_type>());
}
template <class RandomAccessIterator, class Compare> template <class RandomAccessIterator, class Compare>
void push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); void push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp){
TinySTL::up(first, last - 1, first, comp);
}
//********* [pop_heap] *************** //********* [pop_heap] ***************
template <class RandomAccessIterator> template <class RandomAccessIterator>
void pop_heap(RandomAccessIterator first, RandomAccessIterator last){ void pop_heap(RandomAccessIterator first, RandomAccessIterator last){
@@ -133,7 +140,7 @@ namespace TinySTL{
void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp){ void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp){
TinySTL::swap(*first, *(last - 1)); TinySTL::swap(*first, *(last - 1));
if (last - first >= 2) if (last - first >= 2)
down(first, last - 2, first, comp); TinySTL::down(first, last - 2, first, comp);
} }
//********* [sort_heap] *************** //********* [sort_heap] ***************
template <class RandomAccessIterator> template <class RandomAccessIterator>