A binary heap implementation.
Binary Heap Structure nature: A Complete Binary Tree
Binary heap sequence: The smallest element is on the root, and the left and right subtree is also a binary heap.
# Include
# Include
# Define ElementType intstruct binary_heap {int capacity; int size; ElementType * Elements ;}; struct binary_heap * H; int init_binary_heap (int size) {if (NULL = H) {H = malloc (sizeof (struct binary_heap); if (NULL = H) {printf ("Error: out of memory !! \ N "); return-1;} H-> Elements = malloc (sizeof (ElementType) * (size + 1); if (NULL = H-> Elements) {printf ("Error: out of memory !! \ N "); return-1;} H-> capacity = size; H-> size = 0; H-> Elements [0] =-1; // flag} return 0;} int insert_binary_heap (ElementType e) {int I; if (H-> size + 1> H-> capacity) {printf ("Error: the binary heap is already full !! \ N "); return-1 ;}for (I = ++ H-> size; H-> Elements [I] = e, h-> Elements [I] <H-> Elements [I/2]; I = I/2) {H-> Elements [I] = H-> Elements [I/2];} H-> Elements [I] = e; return 0;} ElementType delete_min_binary_heap (void) {ElementType result; int I; if (H-> size = 0) {printf ("Error: the binary heap is already empty !! \ N "); return-1 ;}// first obtain the root result of the binary heap = H-> Elements [1]; h-> Elements [1] = H-> Elements [H-> size]; // If the left branch exists and the root value is greater than the value of the left branch, then consider I = 1; while (2 * I <= H-> size) {// determine whether there are both left and right branches if (2 * I + 1 <= H-> size) {// The root is greater than the left, root is greater than right if (H-> Elements [I]> H-> Elements [2 * I] & H-> Elements [I]> H-> Elements [2 * I + 1]) {if (H-> Elements [2 * I] <H-> Elements [2 * I + 1]) {H-> Elements [I] = H-> Elements [2 * I]; H-> Elements [2 * I] = H-> Elements [H-> size]; I = 2 * I;} else {H-> Elements [I] = H-> Elements [2 * I + 1]; H-> Elements [2 * I + 1] = H-> Elements [H-> size]; I = 2 * I + 1 ;}/// the root must be greater than the left, root less than right else if (H-> Elements [I]> H-> Elements [2 * I] & H-> Elements [I] <H-> Elements [2 * I + 1]) {H-> Elements [I] = H-> Elements [2 * I]; H-> Elements [2 * I] = H-> Elements [H-> size]; I = 2 * I;} // The root is smaller than the left, root greater than right else if (H-> Elements [I] <H-> Elements [2 * I] & H-> Elements [I]> H-> Elements [2 * I + 1]) {H-> Elements [I] = H-> Elements [2 * I + 1]; H-> El Ements [2 * I + 1] = H-> Elements [H-> size]; I = 2 * I + 1;} else {break ;}} // only the left branch else {if (H-> Elements [I]> H-> Elements [2 * I]) {H-> Elements [I] = H-> Elements [2 * I]; H-> Elements [2 * I] = H-> Elements [H-> size]; I = 2 * I;} else {break;} H-> Elements [H-> size] = 0; // reset the last key H-> size --; return result;} void print_binary_heap (void) {int I; printf ("the current binary heap:"); for (I = 0; I <H-> capacity + 1; I ++) printf ("% d", H-> Elements [I]); Printf ("\ n"); return;} int main (void) {ElementType e; init_binary_heap (10); scanf ("% d", & e ); while (e! = 0) {insert_binary_heap (e); scanf ("% d", & e);} print_binary_heap (); // delete operation while (H-> size! = 0) {e = delete_min_binary_heap (); printf ("get the min value from the curret binary heap is % d \ n", e);} return 0 ;}
Running result: