Minimum-maximum heap implementation

Source: Internet
Author: User

[Cpp] minimum-maximum heap implementation: [cpp]/* Minimum Maximum heap nature: 1. for any node X in the even depth, the keyword stored on X is smaller than its father but greater than its grandfather 2. for any node X in the odd depth, the keyword stored on X is greater than its father, but less than its grandfather. It can be inferred from this: 1. any node X, the value of its keyword must be between its father and his grandfather, that is to say, the value of all the keywords on the layer where X is located must be between its parent layer and its grandfather layer. the values of all even layer keywords increase progressively from the root, and the values of all odd layer keywords decrease progressively from the root. the keyword value of all odd layers is greater than that of even layers. 4. how to determine whether a node X is in the odd or even layers: whether the grandfather node of the X node is in the odd or even layers, the recursive method can be implemented, {while (I> 1) I/= 4; if (I = 1) Even layer; if (I = 0) odd layer ;} where I is the position of node X. The time complexity is O (logN) another method, according to 3. if the node value of X is smaller than the parent node value, X is on the even layer, provided that the value of X meets the maximum and minimum heap nature, at the same time, the value of X is not equal to the value of its father. 5. example 1/\ 19 15/\ 2 4 3 6/\ 16 18 9 10 7 12 13 14/\ 5 11 8 17 */# include <stdio. h> # include <string. h> # include <stdlib. h> typedef struct MinMaxStruct * MinMaxHeap; typedef int Position; typedef int ElementType; # define MinElement 0 # define MaxElement 0x00ffffstruff CT MinMaxStruct {Int Capacity; int Size; ElementType * Elements;}; static int IsEmpty (MinMaxHeap H) {return H-> Size? 0: 1;} static int IsFull (MinMaxHeap H) {return H-> Size = H-> Capacity;} static MinMaxHeap Initalize (int MaxElements) {MinMaxHeap H; int Size; size = sizeof (struct MinMaxStruct) + (MaxElements + 1) * sizeof (ElementType); H = malloc (Size); if (H = NULL) return NULL; h-> Capacity = MaxElements; H-> Size = 0; H-> Elements = (ElementType *) (H + 1); H-> Elements [0] = MinElement; return H;} static void Destroy (MinMaxHeap H) {H-> Capacity = 0; H-> Size = 0; free (H) ;}/ ** filter on: the process in which the node value is increased or decreased is called filtering. * The filtering process includes all odd layers starting from the root, after converting to the even Layer * and then going through all the even layers from the bottom * Insert: at the bottom of the tree, or filter to the even layer, or * filter down to the odd Layer * Delete the maximum value: Find the maximum value from the 2 or 3 position of the heap and put the last * data to the maximum value, then go through a complete filtering process * DecreaseKey: to reduce the value of the keyword. After the keyword location is reduced, go through a complete filtering process */static Position PercolateUp (Position I, minMaxHeap H) {Position j = I; ElementType X; H-> Elements [0] = MinElement; X = H-> Elements [I]; while (j> 1) j = j> 2;/* odd layer, first filter down, then filter, directly filter the even Layer */if (j = 0) {/* to an odd Layer */for (; I * 4 <H-> Size; I = j) {int k; j = k = 4 * I; if (H-> Size> = k + 1 & H-> Elements [j] <H-> Elements [k + 1]) j = k + 1; if (H-> Size> = k + 2 & H-> Elements [j] <H-> Elements [k + 2]) j = k + 2; if (H-> Size> = k + 3 & H-> Elements [j] <H-> Elements [k + 3]) j = k + 3; if (H-> Elements [j]> X) H-> Elements [I] = H-> Elements [j]; else brea K;} H-> Elements [I] = X;/* for parity layer swapping, replace a maximum value on the even layer with an odd Layer */j = I * 2; if (j <H-> Size) // The maximum value of j can be H-> Size-1, which is an even number, h-> Size is an odd value {if (H-> Elements [j] <H-> Elements [j + 1]) j ++ ;} else if (j> H-> Size) j = I/2; if (H-> Elements [j]> X) {H-> Elements [I] = H-> Elements [j]; I = j;} else return I;}/* filter on */(; h-> Elements [I/4]> X; I/= 4) H-> Elements [I] = H-> Elements [I/4]; h-> Elements [I] = X; return I;}/** filter down: The process of increasing the node value from small to small is called underfiltration * The underfiltration process includes all the even layers starting from the root, after converting to the odd layer, * go through all the odd layers from the bottom * Insert: at the bottom of the tree, or filter to the even layer, or * filter down to the odd Layer * Delete the minimum value: obtain the minimum value from position 1 of the heap and put the last * Data to position 1, then go through a complete filter process * IncreaseKey: to increase the keyword value, after adding the keyword location, * It goes through a complete filtering process */static Position PercolateDown (Position I, MinMaxHeap H) {int j = I; ElementType X; h-> Elements [0] = MaxElement; X = H-> Elements [I]; while (j> 1) j = j> 2;/* Even layer, first filter down, then filter, directly filter on the odd Layer */if (j = 1 ){/ * Filter down to an even Layer */for (; I * 4 <H-> Size; I = j) {int k; j = k = 4 * I; if (H-> Size> = k + 1 & H-> Elements [j]> H-> Elements [k + 1]) j = k + 1; if (H-> Size> = k + 2 & H-> Elements [j]> H-> Elements [k + 2]) j = k + 2; if (H-> Size> = k + 3 & H-> Elements [j]> H-> Elements [k + 3]) j = k + 3; if (H-> Elements [j] <X) H-> Elements [I] = H-> Elements [j]; else break ;} h-> Elements [I] = X;/* for parity layer swapping, replace a minimum value on the odd layer with an even Layer */j = I * 2; if (j <H-> Siz E) // The maximum value of j can be H-> Size-1, which is an even number, h-> Size is an odd value {if (H-> Elements [j]> H-> Elements [j + 1]) j ++ ;} else if (j> H-> Size) j = I/2; if (H-> Elements [j] <X) {H-> Elements [I] = H-> Elements [j]; I = j;} else return I;}/* filter on */(; h-> Elements [I/4] <X; I/= 4) H-> Elements [I] = H-> Elements [I/4]; h-> Elements [I] = X; return I;}/** insert: * Insert a keyword at the last position of the heap, either going through the filtering process * or going through the filtering process */static void Insert (ElementType X, MinMa XHeap H) {Position I; if (IsFull (H) return; I = ++ H-> Size; H-> Elements [I] = X; I = PercolateUp (I, H); if (I = H-> Size) PercolateDown (I, H);}/** Delete the minimum value: * Take the value at location 1, place the last element here * and go through a complete filtering process */static ElementType DeleteMin (MinMaxHeap H) {ElementType Min; if (IsEmpty (H) return MinElement; Min = H-> Elements [1]; H-> Elements [1] = H-> Elements [H-> Size --]; percolateDown (1, H); return Min;}/** find the minimum value */Static ElementType FindMin (MinMaxHeap H) {return H-> Elements [1];}/** Delete the maximum value: * after comparing the size of positions 2 and 3, put the last element here * and go through a complete filtering process */static ElementType DeleteMax (MinMaxHeap H) {int I; ElementType Max; if (IsEmpty (H) return MaxElement; if (H-> Elements [2] <H-> Elements [3]) {Max = H-> Elements [3]; I = 3 ;} else {Max = H-> Elements [2]; I = 2;} H-> Elements [I] = H-> Elements [H-> Size --]; percolateUp (I, H ); Return Max;}/** find the maximum value */static ElementType FindMax (MinMaxHeap H) {return H-> Elements [2] <H-> Elements [3]? H-> Elements [3]: H-> Elements [2];} /** decrease the value of the keyword * decrease the value of the I Position and execute the filtering process */static void DecreaseKey (Position I, ElementType Delta, MinMaxHeap H) {if (I <= H-> Size) {H-> Elements [I]-= Delta; PercolateUp (I, H );}} /** Add a keyword value * after adding the value of the I Position, execute the filter process */static void IncreaseKey (Position I, ElementType Delta, MinMaxHeap H) {if (I <= H-> Size) {H-> Elements [I] + = Delta; PercolateDown (I, H );}} /** Delete keyword * place the value of the last position to the I position * If the deleted Value is smaller than the last value, it is equivalent to increasing the value of the I position, * execute the filter process * If the Delete value is greater than the last value, it is equivalent to reducing the I Position value. * execute the filter process */static void Delete (Position I, MinMaxHeap H) {ElementType Del; if (I <= H-> Size) {Del = H-> Elements [I]; h-> Elements [I] = H-> Elements [H-> Size --]; if (Del <H-> Elements [I]) PercolateDown (I, H ); else if (Del> H-> Elements [I]) PercolateUp (I, H) ;}/ ** test routine */void MinMaxHeapTest (void) {int I; minMaxHeap H; H = Initalize (1000); if (H = NULL) return; for (I = 1; I <1000; I ++) Insert (I, H ); increaseKey (400, H); for (I = 0; I <; I ++) {printf ("% 3d -- % 3d \ n", DeleteMin (H ), deleteMax (H);} Destroy (H );}

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.