-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursion_merge.html
166 lines (154 loc) · 4.75 KB
/
recursion_merge.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/prism.css">
<style>
html,
body {
box-sizing: border-box;
margin: 0;
padding: 0;
height: 100%;
font-size: 12px;
}
body {
min-height: 500px;
}
section {
display: flex;
flex-wrap: wrap;
}
.code {
margin-top: 3px;
}
pre[class*=language-] {
margin: 0;
padding: 0;
}
main {
border-top: 2px solid #ccc;
width: 100%;
height: 65%;
min-height: 200px;
}
</style>
<title>递归合并k个有序链表</title>
</head>
<body>
<section class="frames"></section>
<section style="display: none;">
<pre><code class="language-java">int factorial(int n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}</code></pre>
</section>
<main></main>
<section>
<div style='background-color:#00FF99; margin: 2px 2px 0 0; padding: 4px 6px;'>进行中</div>
<div style='background-color:#7df4f2; margin: 2px 2px 0 0; padding: 4px 6px;'>已结束</div>
</section>
<div style='margin: 2px 2px 0 0; padding: 4px 6px;'>
<span>n </span><input type="text" id='n' class="saveable" value="5">
<span>动画速度(ms) </span><input type="number" step="100" value="300" id="animate_speed" class="saveable">
<input style='font-size:12px;' type="button" value="保存" onclick="onSave('recursion_merge')">
</div>
<script src="js/p5.js"></script>
<script src="js/p5-svg.js"></script>
<script src="js/util.js"></script>
<script src="js/prism.js"></script>
<script>
const colorMap = new Map()
const colorArray = ['#1abc9c', '#e67e22', '#3498db', '#f1c40f', '#9b59b6', '#e74c3c']
let colorIndex = 0
const options = loadOptionsFromStorage('recursion_merge')
const DIAMETER = 25 // 直径 diameter
const WIDTH_ARRAY = [15, 15, 25, 80, 120, 150, 180]
const HEIGHT_ARRAY = [80, 60, 40, 20, 20, 20, 20]
const RECT_WIDTH = 30 // 矩形宽度、圆直径
const RECT_HEIGHT = 20 // 值矩形高度
const SPACING = 1 // 间隙
const INDEX_RECT_HEIGHT = 20 // 索引矩形高度
const n = options.n
const d = new Draw(options.animate_speed)
function preload() {
// const font = loadFont('JetBrainsMono-Regular.ttf')
}
function setup() {
const WIN_WIDTH = document.querySelector('main').clientWidth
const WIN_HEIGHT = document.querySelector('main').clientHeight
const FONT_SIZE = 10
createCanvas(WIN_WIDTH, WIN_HEIGHT, SVG)
textSize(FONT_SIZE)
textAlign(CENTER)
d.add({}, frame)
const array = [[1, 3], [4, 8], [2, 5], [2, 6], [3, 7]]
// const array = [[6], [1], [2], [5], [4], [3]]
const f = merge(array, tree, 0, array.length - 1)
d.updateFrameButtons()
noStroke()
}
function draw() {
d.draw(() => background('#2d2d2d'))
}
function frame({ cloned }) {
drawTree(cloned, width / 2, RECT_HEIGHT, n - 1, 0, 0)
}
function drawTree(node, x, y, deep, px, py) {
if (node) {
if (node.txt) {
if (px && py) {
stroke('white')
line(x, y, px, py)
noStroke()
}
}
drawTree(node.left, x - WIDTH_ARRAY[deep], y + HEIGHT_ARRAY[deep], deep - 1, x, y)
drawTree(node.right, x + WIDTH_ARRAY[deep], y + HEIGHT_ARRAY[deep], deep - 1, x, y)
if (node.txt) {
if (node.done) {
fill('#7df4f2')
} else {
fill('#00FF99')
}
const w = node.txt.length * 4 + 8
rect(x - w / 2, y - RECT_HEIGHT / 2, w, RECT_HEIGHT)
fill('black')
text(node.txt, x, y + 3)
}
}
}
const tree = { done: false }
function merge(lists, t, i, j) {
t.txt = `${JSON.stringify(lists.slice(i, j + 1))}`
d.add({ cloned: clone(tree) }, frame)
if (i === j) {
t.done = true
t.txt = `${JSON.stringify(lists[i])}`
d.add({ cloned: clone(tree) }, frame)
return lists[i]
}
t.left = { done: false }
t.right = { done: false }
const m = (i + j) >>> 1
const l = merge(lists, t.left, i, m)
const r = merge(lists, t.right, m + 1, j)
const f = merge2(l, r)
t.done = true
t.txt = `${JSON.stringify(f)}`
d.add({ cloned: clone(tree) }, frame)
return f
}
function merge2(l, r) {
return l.concat(r).sort((a, b) => a - b)
}
function clone(tree) {
return JSON.parse(JSON.stringify(tree))
}
</script>
</body>
</html>