priority queue
For a special queue, the order of getting out elements is based on the priority (key) of the elements, and the order of getting out elements into the queue
Action: find max (min), delete (max)
Array:
Linked list:
Ordered array:
Ordered list:
Use binary search tree? NO
Use complete binary tree YES
Heap concatenation properties
Structure: a complete binary tree represented by an array:
Ordering: the key of any node is the maximum (or minimum) value of all nodes in its word tree
MaxHeap, also known as large top heap: maximum
MinHeap, also known as "small top heap": minimum
The order of node sequence on the path from root node to any node
Action: insert any element, delete the maximum element
Deletion of the maximum heap: take out the root node (maximum) element and delete one node of the heap at the same time
Creation of the maximum heap: store the existing N elements in an array according to the requirements of the maximum heap
#include <stdio.h> #include <stdlib.h> //Maximum heap #define MaxData 1000 //Sentinel, this value should be defined as greater than the value of all possible elements in the heap typedef int ElementType; typedef struct HeapNode * MaxHeap; struct HeapNode { int Capacity; //Maximum heap capacity int Size; //Number of current elements in the heap ElementType *Data; //Array used to store elements };
Build an empty maximum heap:
//Build an empty maximum heap MaxHeap InitHeap(int maxsize) { MaxHeap H = (MaxHeap)malloc(sizeof(struct HeapNode)); H->Data = (ElementType*)malloc(sizeof(struct HeapNode) * (maxsize + 1)); //The elements in the heap are stored from the subscript 1, but in order to ensure that the heap can store maxsize elements, it is necessary to allocate maxsize + 1 memory unit H->Capacity = maxsize; H->Size = 0; H->Data[0] = MaxData; //Store 0 subscript units as sentinels for (int i = 1; i < maxsize + 1; i++) H->Data[i] = 0; return H; }
Determine whether the heap is full or empty:
int IsEmpty(MaxHeap H) { return H->Size == 0; } //Determine if the heap is full int IsFull(MaxHeap H) { return H->Size == H->Capacity; }
Insert an element:
//Insert an element in the maximum heap void Insert(MaxHeap H, ElementType item) { int i; if (IsFull(H)) { printf("The heap is full\n"); return; } i = ++H->Size; //i is the position of the last element in the heap after insertion for (; H->Data[i / 2] < item; i /= 2) H->Data[i] = H->Data[i / 2]; //When the loop exits, the data of the parent node is larger than the item, and the item has found the correct location H->Data[i] = item; //Assign item to correct subscript cell }
Delete an element:
ElementType Delete(MaxHeap H) { ElementType temp, Max; int parent = 1, child; if (IsEmpty(H)) { printf("The heap is empty!\n"); return 0; } Max = H->Data[1]; //Now record the maximum value, that is, the value of the root node temp = H->Data[H->Size--]; //Replace the data of the root node with the last node element, and then reduce the heap data size by 1 //If 2 * parent > 0, then the parent is already the root node for (; 2 * parent <= H->Size; parent = child) { child = 2 * parent; //If child! = H - > size indicates that child is not the last node, the parent node has a right node, //And if the value of the right node is greater than that of the left node, then child + +; if (child != H->Size && (H->Data[child + 1] < H->Data[child])) child++; //Child points to the larger of the left and right child nodes if (temp > H->Data[child]) break; //If the child node is already less than temp, it indicates that the appropriate location has been found else H->Data[parent] = H->Data[child]; } H->Data[parent] = temp; return Max; }
Maximum heap creation:
1. In the first step, N elements are stored in the binary tree according to the input order. This step needs to satisfy the structural characteristics of the complete binary tree, regardless of its order
2. Start from the node of the last right child, and do the filtering operation similar to the element deletion. Adjust one by one until the root node
//Create a maximum heap MaxHeap CreateMaxHeap() { int dt, i = 0, j; int parent, child; ElementType X; MaxHeap H = InitHeap(20); //Create an empty maximum heap and fill it with elements while (scanf_s("%d", &dt) != EOF) { H->Data[i + 1] = dt; i++; H->Size++; } for (j = i / 2; j >= 1; j--) //Start with i / 2 and filter down one by one { //The following operations are exactly the same as deleting elements, but do not need to reduce the number of elements. X = H->Data[j]; for (parent = j; 2 * parent <= H->Size; parent = child) { child = 2 * parent; if (child != H->Size && H->Data[child] < H->Data[child + 1]) child++; if (H->Data[child] < X) break; else H->Data[parent] = H->Data[child]; } H->Data[parent] = X; } return H; }
Print elements in the heap:
//Print elements in heap void printHeap(MaxHeap H) { for (int i = 1; i <= H->Size; i++) printf("%d ", H->Data[i]); printf("\n"); }
To create a tree first:
void PreOrderCreateHeap(MaxHeap H, int index) { int dt; scanf_s("%d", &dt); if (dt == 0) { return; } H->Size++; H->Data[index] = dt; printf("please enter the left child of %d :", dt); PreOrderCreateHeap(H, 2 * index); printf("please enter the right child of %d: ", dt); PreOrderCreateHeap(H, 2 * index + 1); }
Main function:
int main() { MaxHeap H = CreateMaxHeap(); PreOrderCreateHeap(H, 1); printHeap(H); return 0; }