堆(优先队列)priority queue
特殊的队列,取出元素的顺序是依照元素的优先权(关键字)大小,而出元素进入队列的先后顺序
操作:查找最大值(最小值),删除(最大值)
数组:
链表:
有序数组:
有序链表:
采用二叉搜索树? NO
采用完全二叉树 YES
堆的连个特性
结构性:用数组表示的完全二叉树:
有序性:任一结点的关键字是其字树所有结点的最大值(或最小值)
最大堆(MaxHeap)也称大顶堆:最大值
最小堆(MinHeap)也称“小顶堆”:最小值
从根节点到任意结点路径上结点序列的有序性
操作:插入任意一个元素,删除最 大值元素
最大堆的删除:取出根节点(最大值)元素,同时删除堆的一个结点
最大堆的建立:将已存在的N个元素按最大堆的要求存放在一个以为数组中
1 #include <stdio.h> 2 #include <stdlib.h> 3 //最小堆 4 5 #define ElementType int 6 #define MaxSize 15 7 #define MinData -1 8 9 typedef struct HeapNode * MinHeap; 10 struct HeapNode { 11 int Capacity; 12 int Size; 13 ElementType *element; 14 }; 15 16 MinHeap InitHeap(int maxsize) 17 { 18 MinHeap H = (MinHeap)malloc(sizeof(struct HeapNode)); 19 H->element = (ElementType*)malloc(sizeof(struct HeapNode) * (maxsize + 1)); 20 H->Capacity = maxsize; 21 H->Size = 0; 22 H->element[0] = MinData; 23 for (int i = 1; i < maxsize + 1; i++) 24 H->element[i] = 0; 25 return H; 26 } 27 28 void CreateHeap(MinHeap H, int position) 29 { 30 int val; 31 scanf_s("%d", &val); 32 H->Size++; 33 if (val == 0 || position > MaxSize) 34 { 35 H->Size--; 36 return; 37 } 38 H->element[position] = val; 39 //printf("please enter %d left child:\n", val); 40 CreateHeap(H, position * 2); 41 //printf("please enter %d right child:\n", val); 42 CreateHeap(H, position * 2 + 1); 43 } 44 45 int IsEmpty(MinHeap H) 46 { 47 return H->Size == 0; 48 } 49 50 int IsFull(MinHeap H) 51 { 52 return H->Size == MaxSize; 53 } 54 55 void Insert(MinHeap H, ElementType item) 56 { 57 int i; 58 if (IsFull(H)) 59 { 60 printf("The heap is full\n"); 61 return; 62 } 63 i = ++H->Size; 64 for (; H->element[i / 2] > item; i /= 2) 65 H->element[i] = H->element[i / 2]; 66 H->element[i] = item; 67 } 68 69 ElementType Delete(MinHeap H) 70 { 71 ElementType temp, Min; 72 int parent = 1, child; 73 if (IsEmpty(H)) 74 { 75 printf("The heap is empty!\n"); 76 return 0; 77 } 78 Min = H->element[1]; 79 temp = H->element[H->Size--]; 80 for (; 2 * parent <= H->Size; parent = child) 81 { 82 child = 2 * parent; 83 if (child != H->Size && (H->element[child + 1] < H->element[child])) 84 child++; 85 if (temp <= H->element[child]) break; 86 else 87 H->element[parent] = H->element[child]; 88 } 89 H->element[parent] = temp; 90 return Min; 91 } 92 93 void printHeap(MinHeap H, int position, int depth) 94 { 95 while(depth--) 96 printf(" "); 97 printf("%d\n", H->element[position]); 98 } 99 100 void InOrderTraversal(MinHeap H, int position, int depth) 101 { 102 if (!IsEmpty(H) && position <= H->Size) 103 { 104 InOrderTraversal(H, position * 2 + 1, depth + 1); 105 printHeap(H, position, depth); 106 InOrderTraversal(H, position * 2, depth + 1); 107 } 108 } 109 110 111 112 int main() 113 { 114 MinHeap H = InitHeap(15); 115 CreateHeap(H, 1); 116 InOrderTraversal(H, 1, 0); 117 printf("\nAfter inserting:\n"); 118 Insert(H, 14); 119 InOrderTraversal(H, 1, 0); 120 printf("\nAfter deleting:\n"); 121 Delete(H); 122 InOrderTraversal(H, 1, 0); 123 124 125 126 127 return 0; 128 }
原文地址:https://www.cnblogs.com/hi3254014978/p/9527060.html
时间: 2024-11-05 23:28:46