The largest heap is a useful data structure. It is a Complete Binary Tree. If a node has a son node, Its keywords are not less than those of its son node. (Minimum tree; otherwise, the node value is not greater than the full Binary Tree of the son node .)
A typical use of the Max heap is to find the largest unordered number. For example, to find the smallest first 10 billion digits in an integer of 100, one of the typical solutions is to first go to the first one hundred values, create a maximum heap, and then read the remaining values in sequence, if the value is smaller than the root node value, delete the root node, insert the value, and recreate the maximum heap. Repeat this process. Finally, we get the minimum first 100 digits (if we find the first 100 largest values, we will create a minimum heap ). The time complexity of this algorithm is nlog100. Log100 is the complexity of rebuilding the max heap (size = 100), and N is the number of digits.
Because the maximum heap is a complete binary tree, it is suitable for Array Storage. Based on some definitions of the full binary tree, you can quickly obtain the parent node or the left and right son nodes of the current node.
For the Complete Binary Tree of N nodes, sequential storage, subscripts are:
1. if I! 1, the parent node is 1/2, and 1 is the root node.
2. If 2I <= N, the left son is 2I; 2I> N, and no left son
3. If 2I + 1 <= N, the right son is 2I + 1; 2I + 1> N, without the right son.
Below are some common operation functions of the Max heap (the min heap is similar and will not be given ):
1 # define max_elements 200/* maximun heap size + 1 */2 # define heap_full (N) (n = MAX_ELEMENTS-1) 3 # define heap_empty (N )(! N) 4 typedef struct {5 Int key; 6/* Other fields */7} element; 8 Element Heap [max_elements]; 9 int n = 0; 10 11 // insert function 12 Void insert_max_heap (element item, int * n) 13 {14/* Insert item into a max heap of current size * n */15 int I; 16 if (heap_full (* n) {17 fprintf (stderr, "the heap is full. \ n "); 18 exit (1); 19} 20 I = ++ (* n); // put the value in the last 21 While (I! = 1) & (item. key> heap [I/2]. key) {22 // if the current node value is greater than its parent node value, move the parent node down to 23 heap [I] = heap [I/2]; 24 I/= 2; // find the parent node index to continue the loop 25} 26 heap [I] = item; // The position at the end of the loop is the actual position to be inserted 27} 28 // The insert function can be rewritten using the move_up function below. The inserted value should be placed at the end of the array, then stop move_up to get 29 30 // Delete the maximum 31 element delete_max_heap (int * n) 32 {33/* Delete element with the highest key from the 34 heap 35 */36 int parent, child; 37 element item, temp; 38 If (hea P_empty (* n) {39 fprintf (stderr, "the heap is empty. \ n "); 40 exit (1); 41} 42/* save value of the element with the hightest key */43 Item = heap [1]; 44/* use last element in heap to adjust heap */45 temp = heap [(* n) --]; // Delete the last value and save it temporarily. 46 parent = 1; 47 child = 2; 48 While (Child <= * n) {// the maximum length of the sub-node index is less than or equal to 49/* Find the larger child of the current parent */50 if (Child <* n) & (heap [chil D]. key