資料結構›Ch7 搜尋與排序
第 37 題/共 76 題
◀ DS 37/76
37. Min Heap、Debugging
#DS-07-037易Min HeapDebugging
  1. [10%] Min Heap There are TWO BUGS in the following min heap program. Note that the data stored in a min heap should not contain identical values. For each bug, please provide:
  • The line number where the bug occurs, and
  • The corrected solution in C.
1  #include <stdio.h>
2  #include <stdlib.h>
3  #define MAX_SIZE 100  // Maximum size of the heap
4  // Structure to represent a Min Heap
5  struct MinHeap {
6      int arr[MAX_SIZE];  // Array to store heap elements
7      int size;           // Current number of elements in the heap
8  };
9  // Function to initialize the heap
10 void initHeap(struct MinHeap* heap) {
11     heap->size = 0;
12 }
13 // Function to get the index of the parent node
14 int parent(int i) {
15     return (i - 1) / 2;
16 }
17 // Function to get the index of the left child
18 int leftChild(int i) {
19     return 2 * i + 1;
20 }
21 // Function to get the index of the right child
22 int rightChild(int i) {
23     return 2 * i + 2;
24 }
25 // Function to swap two elements
26 void swap(int* a, int* b) {
27     int temp = *a;
28     *a = *b;
29     *b = temp;
30 }
31 // Function to insert a new element into the heap
32 void insert(struct MinHeap* heap, int key) {
33     if (heap->size == MAX_SIZE) {
34         printf("Heap overflow! Cannot insert.\n");
35         return;
36     }
37     // Insert the new key at the end
38     int i = heap->size;
39     heap->arr[i] = key;
40     heap->size++;
41     while (i != 0 && heap->arr[parent(i)] < heap->arr[i]) {
42         swap(&heap->arr[i], &heap->arr[parent(i)]);
43         i = parent(i);
44     }
45 }
46 // Function to heapify (maintain min heap property) from index i
47 void heapify(struct MinHeap* heap, int i) {
48     int l = leftChild(i);
49     int r = rightChild(i);
50     int smallest = i;
51     if (l < heap->size && heap->arr[l] < heap->arr[smallest]) {
52         smallest = l;
53     }
54     if (r < heap->size && heap->arr[r] < heap->arr[smallest]) {
55         smallest = r;
56     }
57     if (smallest != i) {
58         swap(&heap->arr[i], &heap->arr[smallest]);
59         heapify(heap, smallest);
60     }
61 }
62 // Function to delete and return the minimum element (root)
63 int deleteMin(struct MinHeap* heap) {
64     if (heap->size <= 0) {
65         printf("Heap underflow! No elements to delete.\n");
66         return -1;
67     }
68     if (heap->size == 1) {
69         heap->size--;
70         return heap->arr[0];
71     }
72     // Store the minimum value and remove it
73     int root = heap->arr[0];
74     heap->arr[0] = heap->arr[1];
75     heap->size--;
76     heapify(heap, 0);
77     return root;
78 }
79 // Main function to test the Min Heap
80 int main() {
81     struct MinHeap heap;
82     initHeap(&heap);
83     insert(&heap, 20);
84     insert(&heap, 5);
85     printf("Deleted min: %d\n", deleteMin(&heap));
86     return 0;
87 }
📄 交大115
跳轉到第題
▤完整推導請見《WH 資工筆記 · 資料結構》Ch7 搜尋與排序
本章題號 · 21–40 / 76