Data Structures & Alogs HW_Part_10

Data Structures & Alogs HW_Part_10 - 37 swap(Heap,...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
37 swap(Heap, pos, j); pos = j; // Move down } } template <class Elem, class Comp> bool minheap<Elem, Comp>::insert(const Elem& val) { if (n >= size) return false; // Heap is full int curr = n++; Heap[curr] = val; // Start at end of heap // Now sift up until curr’s parent < curr while ((curr!=0) && (Comp::lt(Heap[curr], Heap[parent(curr)]))) { swap(Heap, curr, parent(curr)); curr = parent(curr); } return true; } template <class Elem, class Comp> bool minheap<Elem, Comp>::removemin(Elem& it) { if (n == 0) return false; // Heap is empty swap(Heap, 0, --n); // Swap max with last value if (n != 0) siftdown(0); // Siftdown new root val it = Heap[n]; // Return deleted value return true; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
38 Chap. 5 Binary Trees // Remove value at specified position template <class Elem, class Comp> bool minheap<Elem, Comp>::remove(int pos, Elem& it) { if ((pos < 0) || (pos >= n)) return false; // Bad pos swap(Heap, pos, --n); // Swap with last value while ((pos != 0) && (Comp::lt(Heap[pos], Heap[parent(pos)]))) swap(Heap, pos, parent(pos)); // Push up if large siftdown(pos); // Push down if small key it = Heap[n]; return true; } 5.20 Note that this summation is similar to Equation 2.5. To solve the summation
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Data Structures &amp;amp; Alogs HW_Part_10 - 37 swap(Heap,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online