Binary heap implementation file C ++

Source: Internet
Author: User

writing is very inefficient. This is a lot of details. It seems that every time you write it, you have to find it again. Just write it.

// Binaryheap. CPP -- 2011-08-28-23.06 // purpose: // define methods of Class "binaryheap ". # include "stdafx. H "# include" binaryheap. H "// Private methods: // ----------------------- void binaryheap: m_percolateup (INT index) {node temp = m_heap [Index]; size_t I = index; size_t parent = (I-1)/2; while (I! = 0) {If (temp. weight <m_heap [Parent]. weight) {m_heap [I] = m_heap [Parent]; I = parent; parent = (I-1)/2;} else {m_heap [I] = temp; break;} If (0 = I) {m_heap [0] = temp;} // ----------------------- void binaryheap: m_percolatedown (INT index) {node temp = m_heap [Index]; int I = index; int child = I * 2 + 1; while (Child <m_currentsize) {If (child! = M_currentsize-1 & m_heap [Child + 1]. weight <m_heap [child]. weight) ++ child; if (temp. weight> m_heap [child]. weight) {m_heap [I] = m_heap [child]; I = Child; child = I * 2 + 1;} else {m_heap [I] = temp; break ;}} if (Child> = m_currentsize) {m_heap [I] = temp;} // ------------------- // public methods: // --------------------- binaryheap: binaryheap (INT size) {If (size <= 0) {STD: cerr <"Wrong size. "< STD: Endl; return;} m_heap = new node [size]; If (null = m_heap) {STD: cerr <"out of space. "<STD: Endl; return;} m_size = size; m_currentsize = 0;} // --------------------- // ----------------------- binaryheap ::~ Binaryheap (void) {Delete [] m_heap;} // ------------------- // define bool binaryheap: isempty (void) {If (0 = m_currentsize) return true; elsereturn false ;} // response bool binaryheap: isfull (void) {If (m_currentsize = m_size) return true; elsereturn false;} // response // ------------------- bool binaryheap :: insert (INT startindex, int endindex, int weight) {If (isfull () {STD: cerr <"heap is full already. "<STD: Endl; return false;} If (startindex> = m_size | startindex <0 | endindex> = m_size | endindex <0) {STD :: cerr <"wrong start index or end index. "<STD: Endl; return false;} m_heap [m_currentsize]. startindex = startindex; m_heap [m_currentsize]. endindex = endindex; m_heap [m_currentsize]. weight = weight; m_percolateup (m_currentsize); ++ m_currentsize; return true;} // ----------------------- // --------------------- bool binaryheap: deletemin (node * const pnode) {If (isempty () {STD: cerr <"heap is empty already. "<STD: Endl; return false;} * pnode = m_heap [0]; m_heap [0] = m_heap [-- m_currentsize]; m_percolatedown (0); Return true ;} //---------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.