Heap
heap.h//PARENT (i)//return i/2//Left (i)//return 2 * i//right (i)//return 2 * i + 1//max-heapify (A, I)//L = Left (i)//R = Right (i)//if L <= a.heap-size and A[l] > a[i]//largest = l//else largest = i//if R <= a.heap-si Ze and a[r] > a[largest]//largest = r//if largest! = i//Exchange A[i] with a[largest]//max-heapify (A, L argest)//Build-max-heap (a)//a.heap-size = a.length//for i = A.length/2 down to 1//max-heapify (A, I)//Heapsort (a )//Build-max-heap (A)//For i = a.length-2//exchange a[1] with a[i]//a.heap-size = a.heap-size-1// Max-heapify (A, 1)//heap-extract-max//if a.heap-size < 1//error "HEAP underflow"//MAX = a[1]//a[1] = a[a.heap- size]//a.heap-size--//Max-heapify (A, 1)//return max//Heap-increase-key (A, I, KEY)//If KEY < a[i]//error "new Key is smaller than current key "//a[i] = key//when i > 1 and a[parent (i)] < a[i]//exchange A[i] with A[paren T (i)]//i = PARENT (i)//Max-heap-insert (key)//A.heap-size = A.heap-size + 1//a[a.heap-size] =-max//Heap-increase-key (A, A.heap-size, key) # Include <iostream> #include <stdint.h>using std::cout;using std::endl;class ia_heap {public:ia_heap (Int64 _t* p, uint64_t N): m_heap_size (0) {//M_array = p; M_length = n; M_array = new Int64_t[n]; memcpy (M_array, p, n * sizeof (int64_t)); } void Build_max_heap () {m_heap_size = m_length-1; for (int64_t i = m_length/2-1; I >= 0; i--) {max_heapify (i); }} void Max_heapify (int64_t i) {int64_t L = left (i); int64_t R = Right (i); int64_t largest; if (l <= m_heap_size && m_array[l] > M_array[i]) {largest = L; } else {largest = i; } if (r <= m_heap_size && M_array[r] > m_array[largest]) {largest = R; } if (LarGest! = i) {swap (I, largest); Max_heapify (largest); }} void Heap_sort () {for (uint64_t i = m_length-1; I >= 1; i--) {swap (0, i); m_heap_size--; A heap after sorting, no a heap anymore max_heapify (0); }} int64_t Heap_maximum () {return m_array[0];} int64_t Heap_extract_max (int64_t& max) {if (M_heap_size < 0) {Std::cout << err or underflow "<< Std::endl; return-1; } max = m_array[0]; M_array[0] = m_array[m_heap_size]; m_heap_size--; Max_heapify (0); return 0; } void Heap_increase_key (int64_t i, int64_t key) {if (Key < M_array[i]) {std::cout < ;< "New key is smaller than current key" << Std::endl; } M_array[i] = key; while (i > 0 && m_array[parent (i)] < M_array[i]) { Swap (I, parent (i)); i = parent (i); }} void Max_heap_insert (int64_t key) {m_heap_size++; if (m_heap_size = = m_length) {int64_t* p = new Int64_t[sizeof (int64_t) * (M_length + 1)]; memcpy (p, M_array, sizeof (int64_t) * m_length); delete [] m_array; M_array = p; m_length++; } M_array[m_heap_size] = Int64_min; Heap_increase_key (M_heap_size, key); } void Print_array () {std::cout << "Print_array" << Std::endl; for (int64_t i = 0; i < m_length; i++) {std::cout << m_array[i] << ""; } std::cout << Std::endl; } void Print_heap () {std::cout << "print_heap" << Std::endl; if (0 = = m_heap_size) {std::cout << "no heap element" << Std::endl; } for (int64_t i = 0; I <= m_heap_size; i++) {Std::cout << M_array[i] << ""; } std::cout << Std::endl; }private:uint64_t left (uint64_t i) {return 2 * i + 1; } uint64_t Right (uint64_t i) {return 2 * i + 2; } uint64_t Parent (uint64_t i) {return (i-1)/2; } void Swap (uint64_t I, uint64_t j) {int64_t tmp = m_array[i]; M_array[i] = M_array[j]; M_ARRAY[J] = tmp; } int64_t* M_array; int64_t m_heap_size; int64_t m_length;};
#include "heap.h" int main () { int64_t array[] = {4, 1, 3, 2, +, 9, ten,, 8, 7}; Ia_heap A (array, sizeof (array)/sizeof (int64_t)); A.print_array (); A.print_heap (); A.build_max_heap (); Std::cout << "A.heap_maximum:" << a.heap_maximum () << Std::endl; A.print_array (); A.print_heap (); A.heap_increase_key (8); A.print_array (); A.print_heap (); A.max_heap_insert (+); A.print_array (); A.print_heap (); int64_t Max; if (0 = = A.heap_extract_max (max)) { std::cout << max << Std::endl; } A.print_array (); A.print_heap (); while (0 = = A.heap_extract_max (max)) { std::cout << max << Std::endl; } A.heap_sort ();// A.print_array ();// a.print_heap (); GetChar (); return 0;}
Heap
Introduction to Algorithms chapter 6th heap