The priority queue includes binary heap, D-heap, left heap, oblique heap, and two-way queue.
1. Binary heap
A heap is a fully-filled binary tree. It is possible that the underlying layer contains elements from left to right. Such a tree is called a Complete Binary Tree.
The nature of heap sequence: in a heap, for each node X, the parent keyword of X is smaller than or equal to the keyword in X, except for the root node (it does not have a parent node ). A Complete Binary Tree can be implemented using arrays.
// Definition of the binary heap header file
If the element to be inserted is a new minimum value, it will always be pushed to the top of the heap. In this way, at a certain time point, I will be 1, and we need another insert function to let the program jump out of the while loop. This value must be equal to or less
Any value in the heap is called a tag.
#ifndef BITHEAP_H_INCLUDED#define BITHEAP_H_INCLUDED#include <stdio.h>#include <stdlib.h>typedef int ElementType;struct HeapStruct;typedef struct HeapStruct *PriorityQueue;PriorityQueue BitHeap_Init(int max_elements);void BitHeap_Insert(ElementType x, PriorityQueue H);ElementType BitHeap_Delete(PriorityQueue H);void BitHeap_Show(PriorityQueue H);int BitHeap_Free(PriorityQueue H);#endif // BITHEAP_H_INCLUDED
// Define the function in the binary heap
#include "bitheap.h"struct HeapStruct{ int Capacity; int Size; ElementType *Elements;};static void print_err(char *str);static int IsFull(PriorityQueue H);static int IsEmpty(PriorityQueue H);PriorityQueue BitHeap_Init(int max_elements){ PriorityQueue H; H = (PriorityQueue)malloc(sizeof(struct HeapStruct)); if(H == NULL) print_err("no space for H in BitHeap"); H->Elements = (ElementType *)malloc((max_elements+1) * sizeof(ElementType)); H->Elements[0] = (1<<31); if(H->Elements == NULL) print_err("no space for H->Elements in BitHeap"); H->Capacity = max_elements; H->Size = 0; return H;}void BitHeap_Insert(ElementType x, PriorityQueue H){ int i; if(IsFull(H)) { printf("the heap is full\n"); return; } for(i = ++H->Size; H->Elements[i/2] > x; i /= 2) H->Elements[i] = H->Elements[i/2]; H->Elements[i] = x;}ElementType BitHeap_Delete(PriorityQueue H){ ElementType min_val, last_val; int i, child; if(IsEmpty(H)) { printf("the bitheap is empty\n"); return (1<<31); } min_val = H->Elements[1]; last_val = H->Elements[H->Size--]; for(i = 1; 2*i < H->Size; i = child) { child = 2 * i; if(child != H->Size && (H->Elements[child+1] < H->Elements[child])) child++; if(last_val > H->Elements[child]) H->Elements[i] = H->Elements[child]; else break; } H->Elements[i] = last_val; return min_val;}void BitHeap_Show(PriorityQueue H){ int i; if(H != NULL) { for(i = 1; i <= H->Size; i++) printf("%d ", H->Elements[i]); } printf("\n");}int BitHeap_Free(PriorityQueue H){ if(H != NULL) { free(H->Elements); free(H); } return 1;}/** flowing function is used by function in bitheap.c*/static int IsFull(PriorityQueue H){ return (H->Size == H->Capacity);}static int IsEmpty(PriorityQueue H){ return (H->Size == 0);}static void print_err(char *str){ perror(str); exit(-1);}
// Test function main. C for Binary heap
#include <stdio.h>#include <stdlib.h>#include "bitheap.h"int main(){ PriorityQueue H; H = BitHeap_Init(15); BitHeap_Insert(4, H); BitHeap_Insert(2, H); BitHeap_Insert(7, H); BitHeap_Insert(3, H); BitHeap_Insert(12, H); BitHeap_Insert(8, H); BitHeap_Show(H); BitHeap_Delete(H); BitHeap_Show(H); if(BitHeap_Free(H)) printf("\nfree the bitheap is ok.\n"); return 0;}
2. The D-heap is a simple promotion of the binary heap. It is like a binary heap, but all nodes have d sons (it can be said that the binary heap is a 2-heap)
3. The priority queue also includes the left-side heap, oblique heap, and two-item queue.
For details, refer to data structure and algorithm analysis-C language description chapter 6th-priority queue (HEAP)
Priority queue-data structure (Binary heap)