|
| 1 | +#include "algorithm-visualizer.h" |
| 2 | +#include <iostream> |
| 3 | +#include <vector> |
| 4 | +#include <string> |
| 5 | + |
| 6 | +using json = nlohmann::json; |
| 7 | + |
| 8 | +struct HEAP |
| 9 | +{ |
| 10 | + std::vector<int> data; // 0-based: data[0] is root |
| 11 | + int n; // number of elements |
| 12 | + int capacity; |
| 13 | + HEAP(int cap = 32) : data(cap, 0), n(0), capacity(cap) {} |
| 14 | +}; |
| 15 | + |
| 16 | +typedef int ElemType; |
| 17 | + |
| 18 | +Array1DTracer heapTracer = Array1DTracer("堆的数组形式"); |
| 19 | +LogTracer logger = LogTracer("运行日志"); |
| 20 | +GraphTracer heapGraph = GraphTracer("最大堆").directed(false); |
| 21 | + |
| 22 | +bool HeapFull(HEAP &heap) |
| 23 | +{ |
| 24 | + return (heap.n >= heap.capacity); |
| 25 | +} |
| 26 | + |
| 27 | +// 更新图形化的堆树视图(binary heap visual) |
| 28 | +// 使用连续的数值 id (0..n-1),并把堆值作为 weight 显示 |
| 29 | +void updateHeapGraph(HEAP &heap) |
| 30 | +{ |
| 31 | + int n = heap.n; |
| 32 | + GraphTracer heapGraph_new = GraphTracer("最大堆").directed(false); |
| 33 | + heapGraph = heapGraph_new; |
| 34 | + Layout::setRoot(VerticalLayout({heapGraph, heapTracer, logger})); |
| 35 | + if (n <= 0) return; |
| 36 | + for (int i = 0; i < n; ++i) |
| 37 | + { |
| 38 | + heapGraph.addNode(heap.data[i]); |
| 39 | + } |
| 40 | + for (int i = 0; i < n; ++i) |
| 41 | + { |
| 42 | + int left = 2 * i + 1; |
| 43 | + int right = 2 * i + 2; |
| 44 | + if (left < n) |
| 45 | + { |
| 46 | + heapGraph.addEdge(heap.data[i], heap.data[left]); |
| 47 | + heapGraph.addEdge(heap.data[left], heap.data[i]); |
| 48 | + } |
| 49 | + if (right < n) |
| 50 | + { |
| 51 | + heapGraph.addEdge(heap.data[i], heap.data[right]); |
| 52 | + heapGraph.addEdge(heap.data[right], heap.data[i]); |
| 53 | + } |
| 54 | + } |
| 55 | + heapGraph.layoutTree(heap.data[0]); |
| 56 | + Tracer::delay(); |
| 57 | +} |
| 58 | + |
| 59 | +// DeleteMax (0-based indices) following image logic: parent=0, child=1 |
| 60 | +void DeleteMax(HEAP &heap) |
| 61 | +{ |
| 62 | + if (heap.n == 0) |
| 63 | + { |
| 64 | + logger.println("堆为空,无法删除元素"); |
| 65 | + Tracer::delay(); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + ElemType elem = heap.data[0]; |
| 70 | + ElemType tmp = heap.data[heap.n - 1]; |
| 71 | + // 可视化 |
| 72 | + logger.println("删除最大元素: " + std::to_string(elem) + ",并替换堆顶元素"); |
| 73 | + heapTracer.select(0); |
| 74 | + heapGraph.select(heap.data[0]); |
| 75 | + // heapGraph.select(heap.data[heap.n - 1]); |
| 76 | + Tracer::delay(); |
| 77 | + |
| 78 | + heap.n = heap.n - 1; |
| 79 | + |
| 80 | + // put last at root (will percolate down) |
| 81 | + heap.data[0] = tmp; |
| 82 | + heapTracer.deselect(0); |
| 83 | + heapTracer.patch(0, heap.data[0]); |
| 84 | + heapTracer.patch(heap.n, std::string("-")); |
| 85 | + updateHeapGraph(heap); |
| 86 | + heapGraph.select(heap.data[0]); |
| 87 | + Tracer::delay(); |
| 88 | + |
| 89 | + |
| 90 | + heapTracer.depatch(0); |
| 91 | + heapTracer.depatch(heap.n); |
| 92 | + heapGraph.deselect(heap.data[0]); |
| 93 | + Tracer::delay(); |
| 94 | + |
| 95 | + logger.println("堆顶元素被最后一个元素替换,开始下滤过程"); |
| 96 | + Tracer::delay(); |
| 97 | + |
| 98 | + int parent = 0; |
| 99 | + int child = 1; // left child of parent=0 |
| 100 | + |
| 101 | + while (child <= heap.n - 1) |
| 102 | + { |
| 103 | + // 找到更大的子结点 |
| 104 | + if (child < heap.n - 1 && heap.data[child] < heap.data[child + 1]) |
| 105 | + child = child + 1; |
| 106 | + |
| 107 | + // 可视化 |
| 108 | + heapGraph.select(heap.data[child]); |
| 109 | + heapTracer.select(child); |
| 110 | + Tracer::delay(); |
| 111 | + |
| 112 | + if (tmp >= heap.data[child]) |
| 113 | + { |
| 114 | + heapGraph.deselect(heap.data[child]); |
| 115 | + heapTracer.deselect(child); |
| 116 | + break; |
| 117 | + } |
| 118 | + |
| 119 | + // move child up |
| 120 | + heap.data[parent] = heap.data[child]; |
| 121 | + heapTracer.patch(parent, heap.data[parent]); |
| 122 | + logger.println("下滤:将 " + std::to_string(heap.data[child]) + " 移到索引 " + std::to_string(parent)); |
| 123 | + Tracer::delay(); |
| 124 | + |
| 125 | + heapTracer.depatch(parent); |
| 126 | + heapGraph.deselect(child); |
| 127 | + heapTracer.deselect(child); |
| 128 | + |
| 129 | + // move down the tree |
| 130 | + parent = child; |
| 131 | + child = 2 * parent + 1; |
| 132 | + } |
| 133 | + |
| 134 | + // place tmp at its position |
| 135 | + heap.data[parent] = tmp; |
| 136 | + heapTracer.patch(parent, heap.data[parent]); |
| 137 | + Tracer::delay(); |
| 138 | + |
| 139 | + // update graph and array visualization |
| 140 | + updateHeapGraph(heap); |
| 141 | + logger.println("删除完成: " + std::to_string(elem) + ",当前堆大小: " + std::to_string(heap.n)); |
| 142 | + Tracer::delay(); |
| 143 | +} |
| 144 | + |
| 145 | +int main() |
| 146 | +{ |
| 147 | + Layout::setRoot(VerticalLayout({heapGraph, heapTracer, logger})); |
| 148 | + |
| 149 | + HEAP heap(10); |
| 150 | + std::vector<int> init = {95, 87, 78, 65, 53, 31, 9, 45, 17, 23}; |
| 151 | + heap.n = (int)init.size(); |
| 152 | + for (int i = 0; i < heap.n; ++i) heap.data[i] = init[i]; |
| 153 | + |
| 154 | + // array tracer (index 0..capacity-1) |
| 155 | + json arr = json::array(); |
| 156 | + for (int i = 0; i < heap.capacity; ++i) |
| 157 | + { |
| 158 | + if (i < heap.n) arr.push_back(heap.data[i]); |
| 159 | + else arr.push_back(std::string("-")); |
| 160 | + } |
| 161 | + heapTracer.set(arr); |
| 162 | + |
| 163 | + updateHeapGraph(heap); |
| 164 | + heapGraph.layoutTree(heap.data[0]); |
| 165 | + |
| 166 | + Tracer::delay(); |
| 167 | + |
| 168 | + logger.println("进行一次 DeleteMax"); Tracer::delay(); |
| 169 | + DeleteMax(heap); |
| 170 | + |
| 171 | + logger.println("再执行一次 DeleteMax"); Tracer::delay(); |
| 172 | + DeleteMax(heap); |
| 173 | + |
| 174 | + logger.println("结束"); Tracer::delay(); |
| 175 | + |
| 176 | + return 0; |
| 177 | +} |
0 commit comments