最大堆的实现

#include <cstdlib>

#include <iostream>



using namespace std;



template<typename T>

class Heap//最大堆

{

private:

    int currentSize;

    int maxSize;

    T *heapArray;

        

public:

    Heap(int size = 10)

    {

         currentSize = 0;

         maxSize = size;   //初始化堆容量

         heapArray = new T[maxSize];  //创建堆空间

    };

    ~Heap()

    {

        if(heapArray)

            delete[] heapArray;

    };

    

    int getCurrentSize()

    {

        return currentSize;

    };

    int leftchild(int pos)

    {

        return (2 * pos + 1);

    };

    int rightchild(int pos)

    {

        return(2 * pos + 2);

    };

    int parent(int pos)

    {

        return (pos - 1) / 2;

    };

    //调整 

    void shiftDown(int pos)  //向下调整以pos为根的子树为一个堆

    {

         int largest = leftchild(pos);

         while(largest < currentSize)

         {

             //寻找较大的子节点

             if(largest < currentSize-1 && heapArray[largest] < heapArray[largest+1])

             {

                 largest++;

             }

             if(heapArray[pos] < heapArray[largest]) //交换父节点和较大的子节点

             {

                  T tmp = heapArray[pos];

                  heapArray[pos] = heapArray[largest];

                  heapArray[largest] = tmp;

                  pos = largest;      //继续向下

                  largest = leftchild(pos);

             }

             else //已经是最大堆了  

             {

                 return;

             }

         }

    };

    void shiftUp(int pos)  //向上调整为堆

    {

         int par = parent(pos);

         T tmp = heapArray[pos]; //记录上移节点

         while(pos && tmp > heapArray[par])

         {

              heapArray[pos] = heapArray[par];

              pos = par;

              par = parent(pos);

         }

         heapArray[pos] = tmp;

           

    };

    

    T & max()//因为是返回引用,要求将删除的元素放到堆尾的下一个位置

    {

        if(currentSize == 0) //空堆

        {

            cerr<<"The heap is Empty!"<<endl;

            exit(1);         //无法返回确切的T值,强制退出

        }

        T tmp = heapArray[0];

        heapArray[0] = heapArray[--currentSize];

        heapArray[currentSize] = tmp;

        if(currentSize > 1) //堆中只有一个元素就不需调整了

        {

            shiftDown(0);

        }

        return heapArray[currentSize];

    };

    

    bool insert(const T & newNode)

    {

        if(currentSize == maxSize) //堆满

        {

            cerr<<"The heap is full!"<<endl;

            return false;

        }

        heapArray[currentSize] = newNode;

        shiftUp(currentSize);    //向上调整

        currentSize++;

        return true;

    };

    

    bool remove(int pos, T &node)

    {

         if(pos < 0 || pos >= currentSize)

         {

             cerr<<"pos is error"<<endl;

             return false;

         }

         

         node = heapArray[pos];//返回值

         heapArray[pos] = heapArray[--currentSize]; //最后元素填补删除的元素 

         //当前元素大于父亲节点,向上调

         if(heapArray[pos] > heapArray[parent(pos)]) 

         {

             shiftUp(pos);

         }

         else

         {

             shiftDown(pos);

         }

         return true;

    };

    

    int find(const T &key)

    {

        

        int pos = 0;

        if(currentSize < 1 || key > heapArray[pos])

        {

            return -1;

        }      

        while(pos < currentSize)

        {

            if(key == heapArray[pos])

            {

                return pos;

            }

            pos++;            

        }

        return -1;

    };

    

    void printHeap()

    {

        for(int i = 0;i < currentSize;i++)

        {

            cout<<heapArray[i]<<" ";

        }

        cout<<endl;

    };

    

}; 

int main(int argc, char *argv[])

{

    Heap<int> *H = new Heap<int>();

    H->insert(6);

    H->printHeap();

    H->insert(5);

    H->insert(3);

    H->insert(4);

    H->insert(3);

    H->insert(2);

    H->insert(1);   

    H->printHeap();

    H->insert(7);

    H->printHeap();

    

    system("PAUSE");

    return EXIT_SUCCESS;

}

你可能感兴趣的:(实现)