max heap and min heap

Reference: https://blog.csdn.net/guoweimelon/article/details/50904346

First, the definition of heap tree

A heap tree is defined as follows:

(1) The heap tree is a complete binary tree;

(2) The value of a node in the heap tree is always not greater than or less than the value of its child nodes;

(3) The subtree of each node in the heap tree is a heap tree.

The max heap is when the key value of the parent node is always greater than or equal to the key value of any child node. The min heap is when the key value of the parent node is always less than or equal to the key value of any child node. As shown in the figure below, the left is the max heap and the right is the min heap.

Second, the operation of the heap tree

Take the maximum heap as an example to explain, and the minimum heap is the same.

The original data is a[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}, which is stored sequentially. The corresponding complete binary tree is shown in the following figure:

(1) Construct the maximum heap

The basic idea of ​​constructing a heap is to first treat each leaf node as a heap, and then construct each leaf node together with its parent node into a pair containing more nodes.

Therefore, when constructing the heap, you first need to find the parent node of the last node, and construct the maximum heap from this node; until all branch nodes in front of the node are processed, the maximum heap is constructed.

Assuming that the number of nodes in the tree is n, the numbering starts with 1 as the subscript and ends at n. For node i, its parent node is i/2; the left child node is i*2, and the right child node is i*2+1. The index of the last node is n, and the index of its parent node is n/2.

 

As shown in the figure below, the last node is 7, and its parent node is 16, and the maximum heap is constructed from the node 16; after the construction is completed, it moves to the next parent node 2 until all parent nodes are constructed.

Note: When the maximum heap is constructed above, the maximum heap is constructed based on the parent node layer by layer from the bottom up, until the root node is reached to complete the construction of the entire maximum heap;

code show as below:

// Max heap
 // ============================================= ============================ 

#include <iostream>

using namespace std;

struct MaxHeap
{
    int *heap;   // The storage space of the data element, the subscript starts from 1 to store the element, and the subscript 0 stores the temporary data 
    int HeapSize;   // The number of data elements 
    int MaxSize;   // The size of the space to store the data element 
};

// Initialize the maximum heap 
void MaxHeapint(MaxHeap & H)  
{
    for (int i = H.HeapSize / 2; i > 0; i--)
    {
        H.heap[0] = H.heap[i];
        int son = i * 2;
        while (son <= H.HeapSize)
        {
            if (son < H.HeapSize && H.heap[son] < H.heap[son + 1])
                son++;
            if (H.heap[son] < H.heap[0])
                break;
            else
            {
                H.heap[son / 2] = H.heap[son];
                son *= 2;
            }
        }
        H.heap[son / 2] = H.heap[0];
    }
}

// Insert a node into the max heap
 // Idea: first add a node at the end of the heap, and then go up the heap tree. 
void MaxHeapInsert(MaxHeap &H, int x)
{
    if (H.HeapSize == H.MaxSize)
        return;
    int i = ++H.HeapSize;
    while (i != 1 && x > H.heap[i / 2])
    {
        H.heap[i] = H.heap[i / 2];
        i = i / 2;
    }
    H.heap[i] = x;
}

// Delete the top node of the maximum heap
 heap // Idea: refer the last node of the heap tree to the root node, then delete the maximum value, and then put the new root node in the appropriate position. 
void MaxHeapDelete(MaxHeap &H , int & top)
{
    int x;
    if (H.HeapSize == 0)
        return;
    top = H.heap[1];
    H.heap[0] = H.heap[H.HeapSize--];
    int i = 1, son = i * 2;
    while (son <= H.HeapSize)
    {
        if (son < H.HeapSize && H.heap[son] < H.heap[son + 1])
            son++;
        if (H.heap[0] >= H.heap[son])
            break;
        H.heap[i] = H.heap[son];
        i = son;
        sound = sound * 2 ;
    }
    H.heap[i] = H.heap[0];
    return;
}

int main(void)
{
    int top;
    int &top_1 = top;
    MaxHeap H;
    cout << " Please enter the size of the data: " << endl;
    cin >> H.HeapSize;
    H.heap = new int(H.HeapSize + 1);
    cout << " Please input the data according to the complete binary tree: " << endl;
     for ( int i = 0 ; i <= H.HeapSize; i++ )
    {
        cin >> H.heap[i];   // Here we default H.heap[0] as temporary data, set to 0 
    }
    
    MaxHeapint(H);   // Convert binary tree to max heap 
    MaxHeapInsert(H, 3 );
    MaxHeapDelete(H, top);

    for (int i = 1; i <= H.HeapSize; i++)
    {
        cout << H.heap[i] << "  ";
    }
    cout << endl << top;
    system("pause");
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325893297&siteId=291194637