Skip to content

Commit 0044941

Browse files
committed
finished all algorithms
1 parent 66a24e2 commit 0044941

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3105
-0
lines changed

.vscode/settings.json

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"atomic": "cpp",
5+
"bit": "cpp",
6+
"cctype": "cpp",
7+
"chrono": "cpp",
8+
"clocale": "cpp",
9+
"cmath": "cpp",
10+
"codecvt": "cpp",
11+
"compare": "cpp",
12+
"concepts": "cpp",
13+
"cstdarg": "cpp",
14+
"cstddef": "cpp",
15+
"cstdint": "cpp",
16+
"cstdio": "cpp",
17+
"cstdlib": "cpp",
18+
"cstring": "cpp",
19+
"ctime": "cpp",
20+
"cwchar": "cpp",
21+
"cwctype": "cpp",
22+
"deque": "cpp",
23+
"forward_list": "cpp",
24+
"map": "cpp",
25+
"string": "cpp",
26+
"unordered_map": "cpp",
27+
"vector": "cpp",
28+
"exception": "cpp",
29+
"algorithm": "cpp",
30+
"functional": "cpp",
31+
"iterator": "cpp",
32+
"memory": "cpp",
33+
"memory_resource": "cpp",
34+
"numeric": "cpp",
35+
"random": "cpp",
36+
"ratio": "cpp",
37+
"string_view": "cpp",
38+
"system_error": "cpp",
39+
"tuple": "cpp",
40+
"type_traits": "cpp",
41+
"utility": "cpp",
42+
"fstream": "cpp",
43+
"initializer_list": "cpp",
44+
"iomanip": "cpp",
45+
"iosfwd": "cpp",
46+
"iostream": "cpp",
47+
"istream": "cpp",
48+
"limits": "cpp",
49+
"new": "cpp",
50+
"numbers": "cpp",
51+
"ostream": "cpp",
52+
"sstream": "cpp",
53+
"stdexcept": "cpp",
54+
"streambuf": "cpp",
55+
"typeinfo": "cpp",
56+
"valarray": "cpp",
57+
"any": "cpp",
58+
"barrier": "cpp",
59+
"bitset": "cpp",
60+
"cfenv": "cpp",
61+
"charconv": "cpp",
62+
"cinttypes": "cpp",
63+
"complex": "cpp",
64+
"condition_variable": "cpp",
65+
"coroutine": "cpp",
66+
"csetjmp": "cpp",
67+
"csignal": "cpp",
68+
"cuchar": "cpp",
69+
"list": "cpp",
70+
"set": "cpp",
71+
"unordered_set": "cpp",
72+
"optional": "cpp",
73+
"regex": "cpp",
74+
"source_location": "cpp",
75+
"future": "cpp",
76+
"latch": "cpp",
77+
"mutex": "cpp",
78+
"ranges": "cpp",
79+
"scoped_allocator": "cpp",
80+
"semaphore": "cpp",
81+
"shared_mutex": "cpp",
82+
"span": "cpp",
83+
"stop_token": "cpp",
84+
"syncstream": "cpp",
85+
"thread": "cpp",
86+
"typeindex": "cpp",
87+
"variant": "cpp"
88+
}
89+
}

Delete-test.cpp

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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

Comments
 (0)