关闭

最小堆的实现

标签: iostreamclassinsertdeletehpim
1663人阅读 评论(0) 收藏 举报
分类:

这是一个最小堆的模板实现,遗憾的是没有使用模板特化,否则使用起来会更方便,这个实现似乎还有些问题,在析构的时候会出现错误,但是我没有找到原因,很是郁闷。没什么好说的,代码跟书上的没什么两样……

#ifndef MINHEAP_H_

#define MINHEAP_H_

 

#include <cstddef>

#include <iostream>

 

const size_t DEFAULT_CAP = 20;    //default capacity

 

template< class Type >

class  ITEM

{

public:

   Type item;

   int key;

public:

   ITEM() {}

   ITEM( const Type& it, int k) : item(it), key(k) {}  

   ITEM<Type>& operator=( const ITEM<Type>& it) {

      item = it.item;

      key = it.key;

      return *this;

   }

};

 

 

template <class Type> class MinHeap

{

public:

   MinHeap() : m_size(0), m_capacity(DEFAULT_CAP)

   {

      m_pcontainer = new Type[m_capacity];

   }

  

   MinHeap( size_t cap) : m_size(0), m_capacity(cap)

   {

      m_capacity = (cap > DEFAULT_CAP) ? cap : DEFAULT_CAP;

      m_pcontainer = new Type[m_capacity];

   }

  

   MinHeap( Type* ary, size_t n)

   {

      m_capacity = n>m_capacity? n : m_capacity;

      m_pcontainer = new Type[m_capacity];

     

      for(int i=0; i<n; i++) {

         m_pcontainer = ary[i];  

      }

      m_size = n;

   }

  

   MinHeap( const MinHeap& hp )

   {

      m_size = hp.m_size;

      m_capacity = hp.m_capacity;

      m_pcontainer = new Type[m_capacity];

      for( int i=0; i<m_size; i++) {

         m_pcontainer[i] = hp.m_pcontainer[i];

      }

   }

  

   ~MinHeap()

   {

      delete[] m_pcontainer;

   }

  

   size_t size() const { return m_size; }

  

   size_t capacity() const { return m_capacity; }

  

   bool IsEmpty() const { return m_size == 0; }

   bool IsFull() const { return  m_capacity == m_size; }

   void MakeEmpty() { size = 0; }

  

   bool Insert( const Type& x )

   {

      if( m_size == m_capacity ) {

         std::cout << "Heap Is Full!/n" << std::endl;

         return false;

      } else {

         m_pcontainer[m_size] = x;

         m_size++;

         FilterUp(m_size-1);

         return true;

      }    

   }

  

   bool RemoveMin(Type& x)

   {

      if( m_size == 0) {

         std::cout << "Heap Is Empty!/n" << std::endl;

         return false;

      } else {

         x = m_pcontainer[0];

         m_pcontainer[0] = m_pcontainer[--m_size];

         FilterDown(0, m_size-1);

         return true;

      }    

   }

  

protected:

   void FilterDown( size_t istart, size_t iend )

   {

      size_t i = istart;    size_t j = 2*i + 1;

      Type temp = m_pcontainer[i];

      while(j<=iend) {

         if(j < iend && m_pcontainer[j].key > m_pcontainer[j+1].key) {

            j++;

         }

         if( temp.key > m_pcontainer[j].key ) {

            m_pcontainer[i] = m_pcontainer[j];

            i = j;

            j = 2*i + 1;

         } else {

            break;

         }

               

      } 

      m_pcontainer[i] = temp;    

   }

  

   void FilterUp( size_t istart )

   {

      int j = istart;

      int i =(j-1)/2;

      Type temp = m_pcontainer[j];

      while( j>0 ) {

         if( m_pcontainer[i].key > temp.key ) {

            m_pcontainer[j] = m_pcontainer[i];

            j = i;

            i = (j-1)/2;

         } else {

            break;

         }

      }

      m_pcontainer[j] = temp;    

   }

 

  

private:

   size_t m_size;              //number of contents

   size_t m_capacity;          //capacity of container

   Type* m_pcontainer;      //container

 

};

 

#endif /*MINHEAP_H_*/

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44654次
    • 积分:654
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:0篇
    • 译文:1篇
    • 评论:7条
    文章分类
    最新评论