資料結構——堆的實現__資料結構

來源:互聯網
上載者:User

//普通的模板參數

template<class T>struct Less{    bool operator()(const T& left,const T& right)    {        return left < right;    }};template<class T>struct Greater{    bool operator()(const T& left,const T& right)    {        return left > right;    }};template<class T,class Compare = Less<T>>//預設為小堆class Heap{public:    Heap()    {}    Heap(const T array[],size_t size)    {        _heap.resize (size);        for(int i=0;i<size;i++)        {            //_heap.push_back (array[i]);            _heap[i] = array[i];        }        int root = (_heap.size ()-2)>>1;        for(;root>=0;root--)        {            _AdjustDown(root);        }    }    size_t Size()const    {        return _heap.size ();    }    bool Empty()const    {        return _heap.empty ();    }    void Insert(const T& data)    {        //先尾插入元素        //再向上調整        _heap.push_back (data);        //int root = (_heap.size ()-2)>>1;        //for(;root>=0;root--)        //{        //  _AdjustDown(root);        //}        _AdjustUp(_heap.size ()-1);    }    void Remove()    {        //如果堆為空白則不操作        if(_heap.size ()==0)            return ;        //如果只有根節點和其孩子則交換後直接刪除最後一個節點        if(_heap.size ()<=2)        {            std::swap (_heap[0],_heap[_heap.size ()-1]);            _heap.pop_back ();        }        //如果節點數大於2個,則先交換根項目和尾元素,再刪除尾元素,        //最後重新調整堆結構        else        {            std::swap (_heap[0],_heap[_heap.size ()-1]);            _heap.pop_back ();            //int root = (_heap.size ()-2)>>1;            //for(;root>=0;root--)            //  _AdjustDown(root);            int child = _heap.size ()-1;            _AdjustUp(child);        }    }    const T& Top()const    {        assert(!Empty());        return _heap[0];    }private:    void _AdjustDown(size_t parent)    {        size_t child = parent*2+1;        while(child<_heap.size ())        {            //Compare com;//可以用無名對象調用            //如果右孩子存在則找左右孩子中最小的節點            if(child+1<_heap.size () && Compare()(_heap[child+1],_heap[child]))                child+=1;            if(Compare()(_heap[child],_heap[parent]))            {                std::swap (_heap[parent],_heap[child]);                parent = child;                child = parent*2+1;            }            else                return ;        }    }    void _AdjustUp(size_t child)    {        while(child>=1)        {            size_t parent = (child-1)>>1;            if(child+1<_heap.size ()&&Compare()(_heap[child+1],_heap[child]))                child+=1;            if(Compare()(_heap[child],_heap[parent]))                std::swap (_heap[parent],_heap[child]);            child = parent;        }    }private:    std::vector <T> _heap;};

//模板的模板參數

template<class T>struct Less{    bool operator()(const T& left,const T& right)    {        return left < right;    }};template<class T>struct Greater{    bool operator()(const T& left,const T& right)    {        return left > right;    }};template<class T,template<class>class Compare>//可以傳入即時參數模板,但不能用無名對象調用class Heap{public:    Heap()    {}    Heap(const T array[],size_t size)    {        _heap.resize (size);        for(int i=0;i<size;i++)        {            //_heap.push_back (array[i]);            _heap[i] = array[i];        }        int root = (_heap.size ()-2)>>1;        for(;root>=0;root--)        {            _AdjustDown(root);        }    }    size_t Size()const    {        return _heap.size ();    }    bool Empty()const    {        return _heap.empty ();    }    void Insert(const T& data)    {        //先尾插入元素        //再向上調整        _heap.push_back (data);        //int root = (_heap.size ()-2)>>1;        //for(;root>=0;root--)        //{        //  _AdjustDown(root);        //}        _AdjustUp(_heap.size ()-1);    }    void Remove()    {        //如果堆為空白則不操作        if(_heap.size ()==0)            return ;        //如果只有根節點和其孩子則交換後直接刪除最後一個節點        if(_heap.size ()<=2)        {            std::swap (_heap[0],_heap[_heap.size ()-1]);            _heap.pop_back ();        }        //如果節點數大於2個,則先交換根項目和尾元素,再刪除尾元素,        //最後重新調整堆結構        else        {            std::swap (_heap[0],_heap[_heap.size ()-1]);            _heap.pop_back ();            //int root = (_heap.size ()-2)>>1;            //for(;root>=0;root--)            //  _AdjustDown(root);            int child = _heap.size ()-1;            _AdjustUp(child);        }    }    const T& Top()const    {        assert(!Empty());        return _heap[0];    }private:    void _AdjustDown(size_t parent)    {        size_t child = parent*2+1;        while(child<_heap.size ())        {            Compare<T> com;            //如果右孩子存在則找左右孩子中最小的節點            if(child+1<_heap.size () && com(_heap[child+1],_heap[child]))                child+=1;            if(com(_heap[child],_heap[parent]))            {                std::swap (_heap[parent],_heap[child]);                parent = child;                child = parent*2+1;            }            else                return ;        }    }    void _AdjustUp(size_t child)    {        while(child>=1)        {            Compare<T> com;            size_t parent = (child-1)>>1;            if(com(_heap[child],_heap[parent]))                std::swap (_heap[parent],_heap[child]);            child = parent;        }    }private:    std::vector <T> _heap;};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.