This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
/* ===== Illustrated Algorithm – Shared Visualization Library ===== */
|
||||
"use strict";
|
||||
|
||||
/* --- Utility --- */
|
||||
const $ = id => document.getElementById(id);
|
||||
const $$ = sel => document.querySelectorAll(sel);
|
||||
|
||||
/* --- StepController --- */
|
||||
class StepController {
|
||||
constructor(steps, opts = {}) {
|
||||
this.steps = steps;
|
||||
this.idx = -1;
|
||||
this.autoTimer = null;
|
||||
this.onRender = opts.onRender || (() => {});
|
||||
this.onDone = opts.onDone || (() => {});
|
||||
this.autoDelay = opts.autoDelay || 800;
|
||||
}
|
||||
get current() { return this.idx >= 0 && this.idx < this.steps.length ? this.steps[this.idx] : null; }
|
||||
get isEnd() { return this.idx >= this.steps.length - 1; }
|
||||
get total() { return this.steps.length; }
|
||||
|
||||
next() {
|
||||
if (this.idx < this.steps.length - 1) { this.idx++; this._render(); }
|
||||
else this.stopAuto();
|
||||
}
|
||||
prev() {
|
||||
if (this.idx > 0) { this.idx--; this._render(); }
|
||||
}
|
||||
jumpEnd() {
|
||||
this.idx = this.steps.length - 1;
|
||||
this._render();
|
||||
this.stopAuto();
|
||||
}
|
||||
jumpTo(i) {
|
||||
if (i >= 0 && i < this.steps.length) { this.idx = i; this._render(); }
|
||||
}
|
||||
reset() {
|
||||
this.idx = -1;
|
||||
this.stopAuto();
|
||||
this._renderPipeline();
|
||||
if (this.onRender) this.onRender(null);
|
||||
}
|
||||
startAuto() {
|
||||
this.stopAuto();
|
||||
if (this.idx < 0) this.idx = 0;
|
||||
this._render();
|
||||
this.autoTimer = setInterval(() => {
|
||||
if (this.idx < this.steps.length - 1) { this.idx++; this._render(); }
|
||||
else { this.stopAuto(); if (this.onDone) this.onDone(); }
|
||||
}, this.autoDelay);
|
||||
}
|
||||
stopAuto() {
|
||||
if (this.autoTimer) { clearInterval(this.autoTimer); this.autoTimer = null; }
|
||||
}
|
||||
_render() {
|
||||
if (this.onRender) this.onRender(this.current);
|
||||
this._renderPipeline();
|
||||
}
|
||||
_renderPipeline() {
|
||||
const el = $('pipeline');
|
||||
if (!el) return;
|
||||
el.innerHTML = '';
|
||||
for (let i = 0; i < this.steps.length; i++) {
|
||||
const d = document.createElement('div');
|
||||
d.className = 'step-dot' + (i < this.idx ? ' done' : i === this.idx ? ' active' : '');
|
||||
d.title = `Step ${i + 1}`;
|
||||
d.style.cursor = 'pointer';
|
||||
d.addEventListener('click', () => this.jumpTo(i));
|
||||
el.appendChild(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* --- Render helpers --- */
|
||||
function renderArray(container, items, opts = {}) {
|
||||
const { barMaxH = 120, barMinW = 28, barGap = 4, classes = [], tags = [], idxLabels = true } = opts;
|
||||
const maxVal = Math.max(...items.map(it => typeof it === 'object' ? it.val : it), 1);
|
||||
container.innerHTML = '';
|
||||
const row = document.createElement('div');
|
||||
row.className = 'arr-row';
|
||||
items.forEach((item, i) => {
|
||||
const val = typeof item === 'object' ? item.val : item;
|
||||
const cls = typeof item === 'object' ? (item.cls || '') : (classes[i] || '');
|
||||
const tag = typeof item === 'object' ? (item.tag || '') : (tags[i] || '');
|
||||
const h = Math.max(8, (val / maxVal) * barMaxH);
|
||||
const cell = document.createElement('div');
|
||||
cell.className = 'arr-cell';
|
||||
cell.innerHTML = `
|
||||
<span class="arr-val">${val}</span>
|
||||
<div class="arr-bar ${cls}" style="height:${h}px;width:${barMinW}px;"></div>
|
||||
${idxLabels ? `<span class="arr-idx">${i}</span>` : ''}
|
||||
${tag ? `<span class="tag ${cls === 'min-price' || cls === 'best' ? 'tag-green' : cls === 'current' ? 'tag-orange' : cls === 'checking' ? 'tag-blue' : 'tag-orange'}">${tag}</span>` : ''}
|
||||
`;
|
||||
row.appendChild(cell);
|
||||
});
|
||||
container.appendChild(row);
|
||||
}
|
||||
|
||||
function renderGrid(container, rows, cols, cellData, opts = {}) {
|
||||
/* cellData: 2D array of {val, cls} or null */
|
||||
const { cellSize = 48 } = opts;
|
||||
container.innerHTML = '';
|
||||
const grid = document.createElement('div');
|
||||
grid.className = 'grid-vis';
|
||||
grid.style.gridTemplateColumns = `repeat(${cols}, ${cellSize}px)`;
|
||||
for (let i = 0; i < rows; i++) {
|
||||
for (let j = 0; j < cols; j++) {
|
||||
const d = cellData && cellData[i] && cellData[i][j] ? cellData[i][j] : { val: '', cls: '' };
|
||||
const cell = document.createElement('div');
|
||||
cell.className = 'grid-cell ' + (d.cls || '');
|
||||
cell.textContent = d.val !== undefined && d.val !== null ? d.val : '';
|
||||
grid.appendChild(cell);
|
||||
}
|
||||
}
|
||||
container.appendChild(grid);
|
||||
}
|
||||
|
||||
function renderCode(container, code, highlightLine = -1) {
|
||||
container.innerHTML = '';
|
||||
const pre = document.createElement('pre');
|
||||
pre.className = 'code-block';
|
||||
const lines = code.split('\n');
|
||||
lines.forEach((line, i) => {
|
||||
if (i === highlightLine) {
|
||||
const span = document.createElement('span');
|
||||
span.className = 'hl';
|
||||
span.textContent = line;
|
||||
pre.appendChild(span);
|
||||
} else {
|
||||
pre.appendChild(document.createTextNode(line + '\n'));
|
||||
}
|
||||
});
|
||||
container.appendChild(pre);
|
||||
}
|
||||
|
||||
function renderLinkedList(container, nodes, opts = {}) {
|
||||
/* nodes: [{val, cls}] */
|
||||
container.innerHTML = '';
|
||||
const row = document.createElement('div');
|
||||
row.style.cssText = 'display:flex;align-items:center;gap:4px;flex-wrap:wrap;justify-content:center;';
|
||||
nodes.forEach((n, i) => {
|
||||
const box = document.createElement('div');
|
||||
box.style.cssText = `padding:6px 12px;border-radius:6px;font-family:var(--mono);font-size:.85rem;border:2px solid var(--border);background:var(--surface2);transition:all .2s;${n.cls === 'highlight' ? 'border-color:var(--orange);background:var(--orange-dim);' : n.cls === 'done' ? 'border-color:var(--green);background:var(--green-dim);' : ''}`;
|
||||
box.textContent = n.val;
|
||||
row.appendChild(box);
|
||||
if (i < nodes.length - 1) {
|
||||
const arrow = document.createElement('span');
|
||||
arrow.textContent = '→';
|
||||
arrow.style.cssText = 'color:var(--text2);font-size:1.1rem;';
|
||||
row.appendChild(arrow);
|
||||
}
|
||||
});
|
||||
container.appendChild(row);
|
||||
}
|
||||
|
||||
function renderBinaryTree(container, tree, opts = {}) {
|
||||
/* simple SVG tree rendering */
|
||||
container.innerHTML = '';
|
||||
if (!tree) return;
|
||||
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
||||
svg.setAttribute('viewBox', '0 0 500 280');
|
||||
svg.style.width = '100%';
|
||||
svg.style.maxWidth = '500px';
|
||||
const nodeEls = [];
|
||||
function draw(node, x, y, spread, depth) {
|
||||
if (!node) return;
|
||||
const leftSpread = spread / 2;
|
||||
if (node.left) {
|
||||
const lx = x - leftSpread, ly = y + 60;
|
||||
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
|
||||
line.setAttribute('x1', x); line.setAttribute('y1', y);
|
||||
line.setAttribute('x2', lx); line.setAttribute('y2', ly);
|
||||
line.setAttribute('stroke', '#2e3347'); line.setAttribute('stroke-width', '2');
|
||||
svg.appendChild(line);
|
||||
draw(node.left, lx, ly, leftSpread, depth + 1);
|
||||
}
|
||||
if (node.right) {
|
||||
const rx = x + leftSpread, ry = y + 60;
|
||||
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
|
||||
line.setAttribute('x1', x); line.setAttribute('y1', y);
|
||||
line.setAttribute('x2', rx); line.setAttribute('y2', ry);
|
||||
line.setAttribute('stroke', '#2e3347'); line.setAttribute('stroke-width', '2');
|
||||
svg.appendChild(line);
|
||||
draw(node.right, rx, ry, leftSpread, depth + 1);
|
||||
}
|
||||
const g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
|
||||
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
||||
circle.setAttribute('cx', x); circle.setAttribute('cy', y); circle.setAttribute('r', '18');
|
||||
const cls = node.cls || '';
|
||||
circle.setAttribute('fill', cls === 'highlight' ? '#f5a623' : cls === 'done' ? '#3dd68c' : '#242836');
|
||||
circle.setAttribute('stroke', cls === 'highlight' ? '#f5a623' : cls === 'done' ? '#3dd68c' : '#6c7eff');
|
||||
circle.setAttribute('stroke-width', '2');
|
||||
const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
|
||||
text.setAttribute('x', x); text.setAttribute('y', y + 5);
|
||||
text.setAttribute('text-anchor', 'middle'); text.setAttribute('fill', '#e4e6f0');
|
||||
text.setAttribute('font-size', '12'); text.setAttribute('font-family', 'var(--mono)');
|
||||
text.textContent = node.val;
|
||||
g.appendChild(circle); g.appendChild(text);
|
||||
svg.appendChild(g);
|
||||
}
|
||||
draw(tree, 250, 30, 200, 0);
|
||||
container.appendChild(svg);
|
||||
}
|
||||
|
||||
function renderStack(container, items, opts = {}) {
|
||||
container.innerHTML = '';
|
||||
const stack = document.createElement('div');
|
||||
stack.style.cssText = 'display:flex;flex-direction:column-reverse;align-items:center;gap:3px;';
|
||||
items.forEach((item, i) => {
|
||||
const d = typeof item === 'object' ? item : { val: item, cls: '' };
|
||||
const el = document.createElement('div');
|
||||
el.style.cssText = `padding:6px 20px;border-radius:4px;font-family:var(--mono);font-size:.82rem;border:1px solid var(--border);background:var(--surface2);min-width:60px;text-align:center;${d.cls === 'top' ? 'border-color:var(--orange);background:var(--orange-dim);' : ''}`;
|
||||
el.textContent = d.val;
|
||||
stack.appendChild(el);
|
||||
});
|
||||
container.appendChild(stack);
|
||||
}
|
||||
|
||||
function highlightRange(container, start, end, cls) {
|
||||
/* highlight child elements in range [start, end) with class */
|
||||
const children = container.children;
|
||||
for (let i = 0; i < children.length; i++) {
|
||||
if (i >= start && i < end) children[i].classList.add(cls);
|
||||
else children[i].classList.remove(cls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,702 @@
|
||||
[
|
||||
{
|
||||
"id": 1,
|
||||
"slug": "two-sum",
|
||||
"title": "两数之和",
|
||||
"difficulty": "简单",
|
||||
"category": "哈希"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"slug": "group-anagrams",
|
||||
"title": "字母异位词分组",
|
||||
"difficulty": "中等",
|
||||
"category": "哈希"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"slug": "longest-consecutive-sequence",
|
||||
"title": "最长连续序列",
|
||||
"difficulty": "中等",
|
||||
"category": "哈希"
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"slug": "move-zeros",
|
||||
"title": "移动零",
|
||||
"difficulty": "简单",
|
||||
"category": "双指针"
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"slug": "container-with-most-water",
|
||||
"title": "盛最多水的容器",
|
||||
"difficulty": "中等",
|
||||
"category": "双指针"
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"slug": "3sum",
|
||||
"title": "三数之和",
|
||||
"difficulty": "中等",
|
||||
"category": "双指针"
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"slug": "trapping-rain-water",
|
||||
"title": "接雨水",
|
||||
"difficulty": "困难",
|
||||
"category": "双指针"
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"slug": "longest-substring-without-repeating-chars",
|
||||
"title": "无重复字符的最长子串",
|
||||
"difficulty": "中等",
|
||||
"category": "滑动窗口"
|
||||
},
|
||||
{
|
||||
"id": 9,
|
||||
"slug": "find-all-anagrams-in-a-string",
|
||||
"title": "找到字符串中所有字母异位词",
|
||||
"difficulty": "中等",
|
||||
"category": "滑动窗口"
|
||||
},
|
||||
{
|
||||
"id": 10,
|
||||
"slug": "subarray-sum-equals-k",
|
||||
"title": "和为 K 的子数组",
|
||||
"difficulty": "中等",
|
||||
"category": "子串"
|
||||
},
|
||||
{
|
||||
"id": 11,
|
||||
"slug": "sliding-window-maximum",
|
||||
"title": "滑动窗口最大值",
|
||||
"difficulty": "困难",
|
||||
"category": "子串"
|
||||
},
|
||||
{
|
||||
"id": 12,
|
||||
"slug": "minimum-window-substring",
|
||||
"title": "最小覆盖子串",
|
||||
"difficulty": "困难",
|
||||
"category": "子串"
|
||||
},
|
||||
{
|
||||
"id": 13,
|
||||
"slug": "maximum-subarray",
|
||||
"title": "最大子数组和",
|
||||
"difficulty": "中等",
|
||||
"category": "普通数组"
|
||||
},
|
||||
{
|
||||
"id": 14,
|
||||
"slug": "merge-intervals",
|
||||
"title": "合并区间",
|
||||
"difficulty": "中等",
|
||||
"category": "普通数组"
|
||||
},
|
||||
{
|
||||
"id": 15,
|
||||
"slug": "rotate-array",
|
||||
"title": "轮转数组",
|
||||
"difficulty": "中等",
|
||||
"category": "普通数组"
|
||||
},
|
||||
{
|
||||
"id": 16,
|
||||
"slug": "product-of-array-except-self",
|
||||
"title": "除自身以外数组的乘积",
|
||||
"difficulty": "中等",
|
||||
"category": "普通数组"
|
||||
},
|
||||
{
|
||||
"id": 17,
|
||||
"slug": "first-missing-positive",
|
||||
"title": "缺失的第一个正数",
|
||||
"difficulty": "困难",
|
||||
"category": "普通数组"
|
||||
},
|
||||
{
|
||||
"id": 18,
|
||||
"slug": "set-matrix-zeroes",
|
||||
"title": "矩阵置零",
|
||||
"difficulty": "中等",
|
||||
"category": "矩阵"
|
||||
},
|
||||
{
|
||||
"id": 19,
|
||||
"slug": "spiral-matrix",
|
||||
"title": "螺旋矩阵",
|
||||
"difficulty": "中等",
|
||||
"category": "矩阵"
|
||||
},
|
||||
{
|
||||
"id": 20,
|
||||
"slug": "rotate-image",
|
||||
"title": "旋转图像",
|
||||
"difficulty": "中等",
|
||||
"category": "矩阵"
|
||||
},
|
||||
{
|
||||
"id": 21,
|
||||
"slug": "search-a-2d-matrix-ii",
|
||||
"title": "搜索二维矩阵 II",
|
||||
"difficulty": "中等",
|
||||
"category": "矩阵"
|
||||
},
|
||||
{
|
||||
"id": 22,
|
||||
"slug": "intersection-of-two-linked-lists",
|
||||
"title": "相交链表",
|
||||
"difficulty": "简单",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 23,
|
||||
"slug": "reverse-linked-list",
|
||||
"title": "反转链表",
|
||||
"difficulty": "简单",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 24,
|
||||
"slug": "palindrome-linked-list",
|
||||
"title": "回文链表",
|
||||
"difficulty": "简单",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 25,
|
||||
"slug": "linked-list-cycle",
|
||||
"title": "环形链表",
|
||||
"difficulty": "简单",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 26,
|
||||
"slug": "linked-list-cycle-ii",
|
||||
"title": "环形链表 II",
|
||||
"difficulty": "中等",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 27,
|
||||
"slug": "merge-two-sorted-lists",
|
||||
"title": "合并两个有序链表",
|
||||
"difficulty": "简单",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 28,
|
||||
"slug": "add-two-numbers",
|
||||
"title": "两数相加",
|
||||
"difficulty": "中等",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 29,
|
||||
"slug": "remove-nth-node-from-end-of-list",
|
||||
"title": "删除链表的倒数第 N 个结点",
|
||||
"difficulty": "中等",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 30,
|
||||
"slug": "swap-nodes-in-pairs",
|
||||
"title": "两两交换链表中的节点",
|
||||
"difficulty": "中等",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 31,
|
||||
"slug": "reverse-nodes-in-k-group",
|
||||
"title": "K 个一组翻转链表",
|
||||
"difficulty": "困难",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 32,
|
||||
"slug": "copy-list-with-random-pointer",
|
||||
"title": "随机链表的复制",
|
||||
"difficulty": "中等",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 33,
|
||||
"slug": "sort-list",
|
||||
"title": "排序链表",
|
||||
"difficulty": "中等",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 34,
|
||||
"slug": "merge-k-sorted-lists",
|
||||
"title": "合并 K 个升序链表",
|
||||
"difficulty": "困难",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 35,
|
||||
"slug": "lru-cache",
|
||||
"title": "LRU 缓存",
|
||||
"difficulty": "中等",
|
||||
"category": "链表"
|
||||
},
|
||||
{
|
||||
"id": 36,
|
||||
"slug": "binary-tree-inorder-traversal",
|
||||
"title": "二叉树的中序遍历",
|
||||
"difficulty": "简单",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 37,
|
||||
"slug": "maximum-depth-of-binary-tree",
|
||||
"title": "二叉树的最大深度",
|
||||
"difficulty": "简单",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 38,
|
||||
"slug": "invert-binary-tree",
|
||||
"title": "翻转二叉树",
|
||||
"difficulty": "简单",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 39,
|
||||
"slug": "symmetric-tree",
|
||||
"title": "对称二叉树",
|
||||
"difficulty": "简单",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 40,
|
||||
"slug": "diameter-of-binary-tree",
|
||||
"title": "二叉树的直径",
|
||||
"difficulty": "简单",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 41,
|
||||
"slug": "binary-tree-level-order-traversal",
|
||||
"title": "二叉树的层序遍历",
|
||||
"difficulty": "中等",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 42,
|
||||
"slug": "convert-sorted-array-to-bst",
|
||||
"title": "将有序数组转换为二叉搜索树",
|
||||
"difficulty": "简单",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 43,
|
||||
"slug": "validate-binary-search-tree",
|
||||
"title": "验证二叉搜索树",
|
||||
"difficulty": "中等",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 44,
|
||||
"slug": "kth-smallest-element-in-a-bst",
|
||||
"title": "二叉搜索树中第 K 小的元素",
|
||||
"difficulty": "中等",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 45,
|
||||
"slug": "binary-tree-right-side-view",
|
||||
"title": "二叉树的右视图",
|
||||
"difficulty": "中等",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 46,
|
||||
"slug": "flatten-binary-tree-to-linked-list",
|
||||
"title": "二叉树展开为链表",
|
||||
"difficulty": "中等",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 47,
|
||||
"slug": "construct-binary-tree-from-preorder-and-inorder",
|
||||
"title": "从前序与中序遍历序列构造二叉树",
|
||||
"difficulty": "中等",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 48,
|
||||
"slug": "path-sum-iii",
|
||||
"title": "路径总和 III",
|
||||
"difficulty": "中等",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 49,
|
||||
"slug": "lowest-common-ancestor-of-a-binary-tree",
|
||||
"title": "二叉树的最近公共祖先",
|
||||
"difficulty": "中等",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 50,
|
||||
"slug": "binary-tree-maximum-path-sum",
|
||||
"title": "二叉树中的最大路径和",
|
||||
"difficulty": "困难",
|
||||
"category": "二叉树"
|
||||
},
|
||||
{
|
||||
"id": 51,
|
||||
"slug": "number-of-islands",
|
||||
"title": "岛屿数量",
|
||||
"difficulty": "中等",
|
||||
"category": "图论"
|
||||
},
|
||||
{
|
||||
"id": 52,
|
||||
"slug": "rotting-oranges",
|
||||
"title": "腐烂的橘子",
|
||||
"difficulty": "中等",
|
||||
"category": "图论"
|
||||
},
|
||||
{
|
||||
"id": 53,
|
||||
"slug": "course-schedule",
|
||||
"title": "课程表",
|
||||
"difficulty": "中等",
|
||||
"category": "图论"
|
||||
},
|
||||
{
|
||||
"id": 54,
|
||||
"slug": "implement-trie-prefix-tree",
|
||||
"title": "实现 Trie (前缀树)",
|
||||
"difficulty": "中等",
|
||||
"category": "图论"
|
||||
},
|
||||
{
|
||||
"id": 55,
|
||||
"slug": "permutations",
|
||||
"title": "全排列",
|
||||
"difficulty": "中等",
|
||||
"category": "回溯"
|
||||
},
|
||||
{
|
||||
"id": 56,
|
||||
"slug": "subsets",
|
||||
"title": "子集",
|
||||
"difficulty": "中等",
|
||||
"category": "回溯"
|
||||
},
|
||||
{
|
||||
"id": 57,
|
||||
"slug": "letter-combinations-of-a-phone-number",
|
||||
"title": "电话号码的字母组合",
|
||||
"difficulty": "中等",
|
||||
"category": "回溯"
|
||||
},
|
||||
{
|
||||
"id": 58,
|
||||
"slug": "combination-sum",
|
||||
"title": "组合总和",
|
||||
"difficulty": "中等",
|
||||
"category": "回溯"
|
||||
},
|
||||
{
|
||||
"id": 59,
|
||||
"slug": "generate-parentheses",
|
||||
"title": "括号生成",
|
||||
"difficulty": "中等",
|
||||
"category": "回溯"
|
||||
},
|
||||
{
|
||||
"id": 60,
|
||||
"slug": "word-search",
|
||||
"title": "单词搜索",
|
||||
"difficulty": "中等",
|
||||
"category": "回溯"
|
||||
},
|
||||
{
|
||||
"id": 61,
|
||||
"slug": "palindrome-partitioning",
|
||||
"title": "分割回文串",
|
||||
"difficulty": "中等",
|
||||
"category": "回溯"
|
||||
},
|
||||
{
|
||||
"id": 62,
|
||||
"slug": "n-queens",
|
||||
"title": "N 皇后",
|
||||
"difficulty": "困难",
|
||||
"category": "回溯"
|
||||
},
|
||||
{
|
||||
"id": 63,
|
||||
"slug": "search-insert-position",
|
||||
"title": "搜索插入位置",
|
||||
"difficulty": "简单",
|
||||
"category": "二分查找"
|
||||
},
|
||||
{
|
||||
"id": 64,
|
||||
"slug": "search-a-2d-matrix",
|
||||
"title": "搜索二维矩阵",
|
||||
"difficulty": "中等",
|
||||
"category": "二分查找"
|
||||
},
|
||||
{
|
||||
"id": 65,
|
||||
"slug": "find-first-and-last-position-of-element-in-sorted-array",
|
||||
"title": "在排序数组中查找元素的第一个和最后一个位置",
|
||||
"difficulty": "中等",
|
||||
"category": "二分查找"
|
||||
},
|
||||
{
|
||||
"id": 66,
|
||||
"slug": "search-in-rotated-sorted-array",
|
||||
"title": "搜索旋转排序数组",
|
||||
"difficulty": "中等",
|
||||
"category": "二分查找"
|
||||
},
|
||||
{
|
||||
"id": 67,
|
||||
"slug": "find-minimum-in-rotated-sorted-array",
|
||||
"title": "寻找旋转排序数组中的最小值",
|
||||
"difficulty": "中等",
|
||||
"category": "二分查找"
|
||||
},
|
||||
{
|
||||
"id": 68,
|
||||
"slug": "median-of-two-sorted-arrays",
|
||||
"title": "寻找两个正序数组的中位数",
|
||||
"difficulty": "困难",
|
||||
"category": "二分查找"
|
||||
},
|
||||
{
|
||||
"id": 69,
|
||||
"slug": "valid-parentheses",
|
||||
"title": "有效的括号",
|
||||
"difficulty": "简单",
|
||||
"category": "栈"
|
||||
},
|
||||
{
|
||||
"id": 70,
|
||||
"slug": "min-stack",
|
||||
"title": "最小栈",
|
||||
"difficulty": "中等",
|
||||
"category": "栈"
|
||||
},
|
||||
{
|
||||
"id": 71,
|
||||
"slug": "decode-string",
|
||||
"title": "字符串解码",
|
||||
"difficulty": "中等",
|
||||
"category": "栈"
|
||||
},
|
||||
{
|
||||
"id": 72,
|
||||
"slug": "daily-temperatures",
|
||||
"title": "每日温度",
|
||||
"difficulty": "中等",
|
||||
"category": "栈"
|
||||
},
|
||||
{
|
||||
"id": 73,
|
||||
"slug": "largest-rectangle-in-histogram",
|
||||
"title": "柱状图中最大的矩形",
|
||||
"difficulty": "困难",
|
||||
"category": "栈"
|
||||
},
|
||||
{
|
||||
"id": 74,
|
||||
"slug": "kth-largest-element-in-an-array",
|
||||
"title": "数组中的第K个最大元素",
|
||||
"difficulty": "中等",
|
||||
"category": "堆"
|
||||
},
|
||||
{
|
||||
"id": 75,
|
||||
"slug": "top-k-frequent-elements",
|
||||
"title": "前 K 个高频元素",
|
||||
"difficulty": "中等",
|
||||
"category": "堆"
|
||||
},
|
||||
{
|
||||
"id": 76,
|
||||
"slug": "find-median-from-data-stream",
|
||||
"title": "数据流的中位数",
|
||||
"difficulty": "困难",
|
||||
"category": "堆"
|
||||
},
|
||||
{
|
||||
"id": 77,
|
||||
"slug": "best-time-to-buy-and-sell-stock",
|
||||
"title": "买卖股票的最佳时机",
|
||||
"difficulty": "简单",
|
||||
"category": "贪心"
|
||||
},
|
||||
{
|
||||
"id": 78,
|
||||
"slug": "jump-game",
|
||||
"title": "跳跃游戏",
|
||||
"difficulty": "中等",
|
||||
"category": "贪心"
|
||||
},
|
||||
{
|
||||
"id": 79,
|
||||
"slug": "jump-game-ii",
|
||||
"title": "跳跃游戏 II",
|
||||
"difficulty": "中等",
|
||||
"category": "贪心"
|
||||
},
|
||||
{
|
||||
"id": 80,
|
||||
"slug": "partition-labels",
|
||||
"title": "划分字母区间",
|
||||
"difficulty": "中等",
|
||||
"category": "贪心"
|
||||
},
|
||||
{
|
||||
"id": 81,
|
||||
"slug": "climbing-stairs",
|
||||
"title": "爬楼梯",
|
||||
"difficulty": "简单",
|
||||
"category": "动态规划"
|
||||
},
|
||||
{
|
||||
"id": 82,
|
||||
"slug": "pascals-triangle",
|
||||
"title": "杨辉三角",
|
||||
"difficulty": "简单",
|
||||
"category": "动态规划"
|
||||
},
|
||||
{
|
||||
"id": 83,
|
||||
"slug": "house-robber",
|
||||
"title": "打家劫舍",
|
||||
"difficulty": "中等",
|
||||
"category": "动态规划"
|
||||
},
|
||||
{
|
||||
"id": 84,
|
||||
"slug": "perfect-squares",
|
||||
"title": "完全平方数",
|
||||
"difficulty": "中等",
|
||||
"category": "动态规划"
|
||||
},
|
||||
{
|
||||
"id": 85,
|
||||
"slug": "coin-change",
|
||||
"title": "零钱兑换",
|
||||
"difficulty": "中等",
|
||||
"category": "动态规划"
|
||||
},
|
||||
{
|
||||
"id": 86,
|
||||
"slug": "word-break",
|
||||
"title": "单词拆分",
|
||||
"difficulty": "中等",
|
||||
"category": "动态规划"
|
||||
},
|
||||
{
|
||||
"id": 87,
|
||||
"slug": "longest-increasing-subsequence",
|
||||
"title": "最长递增子序列",
|
||||
"difficulty": "中等",
|
||||
"category": "动态规划"
|
||||
},
|
||||
{
|
||||
"id": 88,
|
||||
"slug": "maximum-product-subarray",
|
||||
"title": "乘积最大子数组",
|
||||
"difficulty": "中等",
|
||||
"category": "动态规划"
|
||||
},
|
||||
{
|
||||
"id": 89,
|
||||
"slug": "partition-equal-subset-sum",
|
||||
"title": "分割等和子集",
|
||||
"difficulty": "中等",
|
||||
"category": "动态规划"
|
||||
},
|
||||
{
|
||||
"id": 90,
|
||||
"slug": "longest-valid-parentheses",
|
||||
"title": "最长有效括号",
|
||||
"difficulty": "困难",
|
||||
"category": "动态规划"
|
||||
},
|
||||
{
|
||||
"id": 91,
|
||||
"slug": "unique-paths",
|
||||
"title": "不同路径",
|
||||
"difficulty": "中等",
|
||||
"category": "多维动态规划"
|
||||
},
|
||||
{
|
||||
"id": 92,
|
||||
"slug": "minimum-path-sum",
|
||||
"title": "最小路径和",
|
||||
"difficulty": "中等",
|
||||
"category": "多维动态规划"
|
||||
},
|
||||
{
|
||||
"id": 93,
|
||||
"slug": "longest-palindromic-substring",
|
||||
"title": "最长回文子串",
|
||||
"difficulty": "中等",
|
||||
"category": "多维动态规划"
|
||||
},
|
||||
{
|
||||
"id": 94,
|
||||
"slug": "longest-common-subsequence",
|
||||
"title": "最长公共子序列",
|
||||
"difficulty": "中等",
|
||||
"category": "多维动态规划"
|
||||
},
|
||||
{
|
||||
"id": 95,
|
||||
"slug": "edit-distance",
|
||||
"title": "编辑距离",
|
||||
"difficulty": "中等",
|
||||
"category": "多维动态规划"
|
||||
},
|
||||
{
|
||||
"id": 96,
|
||||
"slug": "single-number",
|
||||
"title": "只出现一次的数字",
|
||||
"difficulty": "简单",
|
||||
"category": "技巧"
|
||||
},
|
||||
{
|
||||
"id": 97,
|
||||
"slug": "majority-element",
|
||||
"title": "多数元素",
|
||||
"difficulty": "简单",
|
||||
"category": "技巧"
|
||||
},
|
||||
{
|
||||
"id": 98,
|
||||
"slug": "sort-colors",
|
||||
"title": "颜色分类",
|
||||
"difficulty": "中等",
|
||||
"category": "技巧"
|
||||
},
|
||||
{
|
||||
"id": 99,
|
||||
"slug": "next-permutation",
|
||||
"title": "下一个排列",
|
||||
"difficulty": "中等",
|
||||
"category": "技巧"
|
||||
},
|
||||
{
|
||||
"id": 100,
|
||||
"slug": "find-the-duplicate-number",
|
||||
"title": "寻找重复数",
|
||||
"difficulty": "中等",
|
||||
"category": "技巧"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,150 @@
|
||||
/* ===== Illustrated Algorithm – Shared Styles ===== */
|
||||
:root {
|
||||
--bg: #0f1117;
|
||||
--surface: #1a1d27;
|
||||
--surface2: #242836;
|
||||
--border: #2e3347;
|
||||
--text: #e4e6f0;
|
||||
--text2: #9499b3;
|
||||
--accent: #6c7eff;
|
||||
--accent2: #5a6adf;
|
||||
--green: #3dd68c;
|
||||
--green-dim: rgba(61,214,140,.15);
|
||||
--red: #f06060;
|
||||
--red-dim: rgba(240,96,96,.15);
|
||||
--orange: #f5a623;
|
||||
--orange-dim: rgba(245,166,35,.15);
|
||||
--blue: #4da6ff;
|
||||
--blue-dim: rgba(77,166,255,.15);
|
||||
--purple: #b47aff;
|
||||
--purple-dim: rgba(180,122,255,.15);
|
||||
--cyan: #22d3ee;
|
||||
--yellow: #facc15;
|
||||
--font: 'Inter','SF Pro Display',-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;
|
||||
--mono: 'JetBrains Mono','Fira Code','SF Mono',Menlo,Consolas,monospace;
|
||||
--radius: 8px;
|
||||
--shadow: 0 2px 12px rgba(0,0,0,.35);
|
||||
}
|
||||
*{margin:0;padding:0;box-sizing:border-box;}
|
||||
html{font-size:15px;-webkit-font-smoothing:antialiased;}
|
||||
body{background:var(--bg);color:var(--text);font-family:var(--font);line-height:1.6;padding:0 0 48px;}
|
||||
.container{max-width:1080px;margin:0 auto;padding:24px 20px;}
|
||||
h1{font-size:1.7rem;font-weight:700;margin-bottom:6px;display:flex;align-items:center;gap:10px;flex-wrap:wrap;}
|
||||
.badge{font-size:.7rem;font-weight:600;padding:2px 10px;border-radius:20px;letter-spacing:.5px;text-transform:uppercase;vertical-align:middle;}
|
||||
.badge.easy{background:var(--green-dim);color:var(--green);}
|
||||
.badge.medium{background:var(--orange-dim);color:var(--orange);}
|
||||
.badge.hard{background:var(--red-dim);color:var(--red);}
|
||||
.subtitle{color:var(--text2);font-size:.85rem;margin-bottom:18px;}
|
||||
|
||||
/* Controls */
|
||||
.controls{display:flex;align-items:center;gap:8px;flex-wrap:wrap;margin-bottom:14px;background:var(--surface);padding:10px 14px;border-radius:var(--radius);border:1px solid var(--border);}
|
||||
.controls label{font-size:.82rem;color:var(--text2);}
|
||||
.controls input[type=text]{background:var(--surface2);border:1px solid var(--border);color:var(--text);padding:6px 10px;border-radius:6px;font-family:var(--mono);font-size:.82rem;width:180px;}
|
||||
.controls select{background:var(--surface2);border:1px solid var(--border);color:var(--text);padding:5px 8px;border-radius:6px;font-size:.82rem;}
|
||||
.controls button{padding:6px 14px;border:none;border-radius:6px;font-size:.82rem;cursor:pointer;font-weight:600;transition:all .15s;}
|
||||
.controls button.primary{background:var(--accent);color:#fff;}
|
||||
.controls button.primary:hover{background:var(--accent2);}
|
||||
.controls button:not(.primary){background:var(--surface2);color:var(--text);border:1px solid var(--border);}
|
||||
.controls button:not(.primary):hover{background:var(--border);}
|
||||
|
||||
/* Pipeline */
|
||||
.pipeline{display:flex;gap:3px;margin-bottom:10px;flex-wrap:wrap;}
|
||||
.pipeline .step-dot{width:28px;height:6px;border-radius:3px;background:var(--surface2);transition:background .2s;}
|
||||
.pipeline .step-dot.done{background:var(--accent);}
|
||||
.pipeline .step-dot.active{background:var(--orange);}
|
||||
|
||||
/* Hint */
|
||||
.hint{padding:10px 14px;border-radius:var(--radius);margin-bottom:14px;font-size:.85rem;line-height:1.6;}
|
||||
.hint.info{background:var(--blue-dim);color:var(--blue);border:1px solid rgba(77,166,255,.25);}
|
||||
.hint #stepInfo{font-weight:700;}
|
||||
|
||||
/* Panels */
|
||||
.panels{display:grid;grid-template-columns:1fr;gap:14px;margin-bottom:14px;}
|
||||
.panel{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:14px 16px;}
|
||||
.panel h3{font-size:.9rem;font-weight:700;margin-bottom:10px;color:var(--text2);}
|
||||
.panel-grid{display:grid;grid-template-columns:1fr 1fr;gap:14px;}
|
||||
@media(max-width:700px){.panel-grid{grid-template-columns:1fr;}}
|
||||
|
||||
/* Vis area */
|
||||
.vis-area{min-height:120px;position:relative;}
|
||||
.vis-area svg{display:block;margin:0 auto;}
|
||||
|
||||
/* Array rendering */
|
||||
.arr-row{display:flex;align-items:flex-end;gap:4px;justify-content:center;margin:8px 0;}
|
||||
.arr-cell{display:flex;flex-direction:column;align-items:center;min-width:36px;}
|
||||
.arr-val{font-family:var(--mono);font-size:.82rem;padding:3px 0;color:var(--text);}
|
||||
.arr-bar{width:30px;border-radius:4px 4px 0 0;background:var(--accent);transition:all .25s;}
|
||||
.arr-idx{font-family:var(--mono);font-size:.68rem;color:var(--text2);margin-top:2px;}
|
||||
.arr-bar.min-price{background:var(--green);box-shadow:0 0 8px var(--green);}
|
||||
.arr-bar.current{background:var(--orange);box-shadow:0 0 8px var(--orange);}
|
||||
.arr-bar.checking{background:var(--purple);box-shadow:0 0 6px var(--purple);}
|
||||
.arr-bar.covered{background:var(--blue);opacity:.7;}
|
||||
.arr-bar.best{background:var(--green);box-shadow:0 0 8px var(--green);}
|
||||
.arr-cell .tag{font-size:.6rem;font-weight:700;margin-top:1px;}
|
||||
|
||||
/* Grid rendering */
|
||||
.grid-vis{display:inline-grid;gap:3px;margin:8px auto;}
|
||||
.grid-cell{width:48px;height:48px;display:flex;align-items:center;justify-content:center;font-family:var(--mono);font-size:.82rem;border-radius:4px;background:var(--surface2);border:1px solid var(--border);transition:all .25s;position:relative;}
|
||||
.grid-cell.highlight{background:var(--accent);color:#fff;border-color:var(--accent);}
|
||||
.grid-cell.path{background:var(--green);color:#fff;border-color:var(--green);box-shadow:0 0 6px var(--green);}
|
||||
.grid-cell.computing{background:var(--orange-dim);border-color:var(--orange);}
|
||||
.grid-cell.origin{background:var(--purple-dim);border-color:var(--purple);}
|
||||
|
||||
/* String rendering */
|
||||
.str-row{display:flex;gap:2px;justify-content:center;margin:8px 0;flex-wrap:wrap;}
|
||||
.str-cell{width:32px;height:44px;display:flex;flex-direction:column;align-items:center;justify-content:center;font-family:var(--mono);font-size:.9rem;border-radius:4px;background:var(--surface2);border:2px solid transparent;transition:all .2s;}
|
||||
.str-cell.center{border-color:var(--orange);background:var(--orange-dim);}
|
||||
.str-cell.expand{border-color:var(--purple);background:var(--purple-dim);}
|
||||
.str-cell.palindrome{background:var(--green-dim);border-color:var(--green);}
|
||||
.str-cell.best{background:var(--green);color:#fff;border-color:var(--green);}
|
||||
.str-cell .str-idx{font-size:.55rem;color:var(--text2);}
|
||||
|
||||
/* Partition colors */
|
||||
.part-0{background:var(--accent)!important;border-color:var(--accent)!important;}
|
||||
.part-1{background:var(--green)!important;border-color:var(--green)!important;}
|
||||
.part-2{background:var(--orange)!important;border-color:var(--orange)!important;}
|
||||
.part-3{background:var(--purple)!important;border-color:var(--purple)!important;}
|
||||
.part-4{background:var(--cyan)!important;border-color:var(--cyan)!important;}
|
||||
.part-5{background:var(--yellow)!important;border-color:var(--yellow)!important;color:#222!important;}
|
||||
.part-6{background:var(--red)!important;border-color:var(--red)!important;}
|
||||
.part-7{background:var(--blue)!important;border-color:var(--blue)!important;}
|
||||
|
||||
/* Jump vis */
|
||||
.jump-row{display:flex;gap:3px;align-items:center;justify-content:center;margin:8px 0;flex-wrap:wrap;}
|
||||
.jump-cell{display:flex;flex-direction:column;align-items:center;min-width:38px;padding:4px 2px;border-radius:6px;transition:all .2s;}
|
||||
.jump-cell .j-val{font-family:var(--mono);font-size:.9rem;font-weight:600;}
|
||||
.jump-cell .j-idx{font-size:.6rem;color:var(--text2);}
|
||||
.jump-cell.reachable{background:var(--blue-dim);border:1px solid var(--blue);}
|
||||
.jump-cell.current{background:var(--orange-dim);border:2px solid var(--orange);}
|
||||
.jump-cell.max-reach{background:var(--green-dim);border:2px solid var(--green);}
|
||||
.jump-cell.step-start{border-bottom:3px solid var(--orange);}
|
||||
.jump-cell .j-chip{width:28px;height:28px;border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:.75rem;font-weight:700;background:var(--surface2);border:2px solid var(--border);}
|
||||
|
||||
/* Code */
|
||||
.code-section{margin-top:16px;}
|
||||
pre.code-block{background:var(--surface2);border:1px solid var(--border);border-radius:var(--radius);padding:14px 16px;overflow-x:auto;font-family:var(--mono);font-size:.8rem;line-height:1.7;white-space:pre;color:var(--text2);}
|
||||
pre.code-block .hl{background:var(--orange-dim);display:inline-block;width:calc(100% + 24px);margin:0 -12px;padding:0 12px;border-left:3px solid var(--orange);}
|
||||
pre.code-block .kw{color:var(--purple);}
|
||||
pre.code-block .fn{color:var(--blue);}
|
||||
pre.code-block .str{color:var(--green);}
|
||||
pre.code-block .num{color:var(--orange);}
|
||||
pre.code-block .cmt{color:#555;font-style:italic;}
|
||||
|
||||
/* Result */
|
||||
.result-box{background:var(--green-dim);border:1px solid rgba(61,214,140,.3);border-radius:var(--radius);padding:12px 16px;font-size:.9rem;color:var(--green);}
|
||||
.result-box .val{font-size:1.3rem;font-weight:700;}
|
||||
|
||||
/* Footer */
|
||||
footer{text-align:center;color:var(--text2);font-size:.72rem;margin-top:32px;opacity:.5;}
|
||||
|
||||
/* Misc */
|
||||
.flex-center{display:flex;align-items:center;justify-content:center;}
|
||||
.mt-8{margin-top:8px;}
|
||||
.mb-8{margin-bottom:8px;}
|
||||
.tag-green{color:var(--green);font-weight:700;font-size:.75rem;}
|
||||
.tag-orange{color:var(--orange);font-weight:700;font-size:.75rem;}
|
||||
.tag-red{color:var(--red);font-weight:700;font-size:.75rem;}
|
||||
.tag-blue{color:var(--blue);font-weight:700;font-size:.75rem;}
|
||||
.legend{display:flex;gap:12px;flex-wrap:wrap;margin:8px 0;font-size:.75rem;color:var(--text2);}
|
||||
.legend-item{display:flex;align-items:center;gap:4px;}
|
||||
.legend-dot{width:10px;height:10px;border-radius:3px;}
|
||||
@@ -0,0 +1,197 @@
|
||||
/**
|
||||
* LeetCode Hot 100 图解 — 二叉树可视化组件
|
||||
* 所有二叉树页面通过 <script src="../shared/tree-viz.js"></script> 引入
|
||||
* 依赖: algo-viz.js (StepController, renderCode, renderArray 等)
|
||||
*/
|
||||
|
||||
/* ========== 解析数组表示法 → 树结构 ========== */
|
||||
function parseTreeArray(arr) {
|
||||
if (!arr || !arr.length) return null;
|
||||
const nodes = arr.map((v, i) =>
|
||||
(v !== null && v !== undefined) ? {val: v, idx: i, left: null, right: null} : null
|
||||
);
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
if (!nodes[i]) continue;
|
||||
const li = 2*i+1, ri = 2*i+2;
|
||||
if (li < nodes.length) nodes[i].left = nodes[li];
|
||||
if (ri < nodes.length) nodes[i].right = nodes[ri];
|
||||
}
|
||||
return nodes[0];
|
||||
}
|
||||
|
||||
/* ========== 计算树的深度 ========== */
|
||||
function treeDepth(root) {
|
||||
if (!root) return 0;
|
||||
return 1 + Math.max(treeDepth(root.left), treeDepth(root.right));
|
||||
}
|
||||
|
||||
/* ========== SVG 渲染二叉树 ========== */
|
||||
function renderTreeSVG(root, options = {}) {
|
||||
const {
|
||||
highlights = {}, // { nodeIdx: 'current'|'visited'|'active'|'found'|'matched'|'target'|'swap'|'result'|'processing'|'path'|'ancestor'|'ignored'|'left'|'right' }
|
||||
depthLabels = {}, // { nodeIdx: number } 显示深度标注
|
||||
annotations = {}, // { nodeIdx: string } 节点上方文字
|
||||
width = 720,
|
||||
nodeR = 20,
|
||||
levelH = 72,
|
||||
} = options;
|
||||
|
||||
if (!root) return '<div class="tree-container"><em>空树</em></div>';
|
||||
|
||||
const depth = treeDepth(root);
|
||||
const svgH = depth * levelH + 55;
|
||||
|
||||
// 递归布局:每个节点居中在其水平范围内
|
||||
const pos = {};
|
||||
function layout(node, xMin, xMax, y) {
|
||||
if (!node) return;
|
||||
const x = (xMin + xMax) / 2;
|
||||
pos[node.idx] = {x, y, val: node.val};
|
||||
layout(node.left, xMin, x, y + levelH);
|
||||
layout(node.right, x, xMax, y + levelH);
|
||||
}
|
||||
layout(root, 40, width - 40, 32 + nodeR);
|
||||
|
||||
// 颜色方案
|
||||
const colorMap = {
|
||||
'': {fill:'#dbeafe', stroke:'#3b82f6', text:'#1e40af'},
|
||||
'default': {fill:'#dbeafe', stroke:'#3b82f6', text:'#1e40af'},
|
||||
'active': {fill:'#c7d2fe', stroke:'#4f46e5', text:'#3730a3', glow:'3b82f6'},
|
||||
'current': {fill:'#fef3c7', stroke:'#f59e0b', text:'#92400e', glow:'f59e0b'},
|
||||
'visited': {fill:'#dcfce7', stroke:'#16a34a', text:'#166534'},
|
||||
'found': {fill:'#fef3c7', stroke:'#f59e0b', text:'#92400e', glow:'f59e0b'},
|
||||
'matched': {fill:'#bbf7d0', stroke:'#16a34a', text:'#166534', glow:'16a34a'},
|
||||
'target': {fill:'#fce7f3', stroke:'#ec4899', text:'#9d174d', glow:'ec4899'},
|
||||
'swap': {fill:'#fef3c7', stroke:'#f59e0b', text:'#92400e', glow:'f59e0b'},
|
||||
'left': {fill:'#dbeafe', stroke:'#3b82f6', text:'#1e40af'},
|
||||
'right': {fill:'#ede9fe', stroke:'#8b5cf6', text:'#5b21b6'},
|
||||
'result': {fill:'#bbf7d0', stroke:'#16a34a', text:'#166534', glow:'16a34a'},
|
||||
'processing':{fill:'#e0e7ff', stroke:'#6366f1', text:'#4338ca', glow:'3b82f6'},
|
||||
'path': {fill:'#fef9c3', stroke:'#eab308', text:'#854d0e', glow:'f59e0b'},
|
||||
'ancestor': {fill:'#fce7f3', stroke:'#ec4899', text:'#9d174d', glow:'ec4899'},
|
||||
'ignored': {fill:'#f1f5f9', stroke:'#cbd5e1', text:'#94a3b8'},
|
||||
};
|
||||
|
||||
let svg = `<svg width="${width}" height="${svgH}" viewBox="0 0 ${width} ${svgH}" xmlns="http://www.w3.org/2000/svg" style="max-width:100%;display:block;margin:0 auto;">`;
|
||||
|
||||
// Defs - 发光滤镜
|
||||
svg += '<defs>';
|
||||
['3b82f6','f59e0b','16a34a','ec4899'].forEach(id => {
|
||||
svg += `<filter id="glow-${id}" x="-50%" y="-50%" width="200%" height="200%">`;
|
||||
svg += `<feGaussianBlur stdDeviation="3" result="blur"/>`;
|
||||
svg += `<feFlood flood-color="#${id}" flood-opacity="0.3"/>`;
|
||||
svg += `<feComposite in2="blur" operator="in"/>`;
|
||||
svg += `<feMerge><feMergeNode/><feMergeNode in="SourceGraphic"/></feMerge>`;
|
||||
svg += `</filter>`;
|
||||
});
|
||||
svg += '</defs>';
|
||||
|
||||
// 边
|
||||
function drawEdges(node) {
|
||||
if (!node) return;
|
||||
const p = pos[node.idx];
|
||||
if (!p) return;
|
||||
[node.left, node.right].forEach(child => {
|
||||
if (child && pos[child.idx]) {
|
||||
const c = pos[child.idx];
|
||||
const hl = highlights[child.idx] || '';
|
||||
let ec = '#cbd5e1';
|
||||
if (['current','found','swap','path'].includes(hl)) ec = '#f59e0b';
|
||||
else if (['visited','matched','result'].includes(hl)) ec = '#16a34a';
|
||||
else if (['active','processing'].includes(hl)) ec = '#6366f1';
|
||||
else if (['target','ancestor'].includes(hl)) ec = '#ec4899';
|
||||
svg += `<line x1="${p.x}" y1="${p.y+nodeR}" x2="${c.x}" y2="${c.y-nodeR}" stroke="${ec}" stroke-width="2.5" stroke-linecap="round"/>`;
|
||||
}
|
||||
});
|
||||
drawEdges(node.left);
|
||||
drawEdges(node.right);
|
||||
}
|
||||
drawEdges(root);
|
||||
|
||||
// 节点
|
||||
for (const idx in pos) {
|
||||
const {x, y, val} = pos[idx];
|
||||
const hl = highlights[idx] || '';
|
||||
const c = colorMap[hl] || colorMap[''];
|
||||
const filterAttr = c.glow ? ` filter="url(#glow-${c.glow})"` : '';
|
||||
|
||||
svg += `<circle cx="${x}" cy="${y}" r="${nodeR}" fill="${c.fill}" stroke="${c.stroke}" stroke-width="2.5"${filterAttr}/>`;
|
||||
svg += `<text x="${x}" y="${y+5}" text-anchor="middle" fill="${c.text}" font-size="14" font-weight="700" font-family="inherit">${val}</text>`;
|
||||
|
||||
if (annotations[idx]) {
|
||||
svg += `<text x="${x}" y="${y-nodeR-8}" text-anchor="middle" fill="#64748b" font-size="11" font-family="inherit">${annotations[idx]}</text>`;
|
||||
}
|
||||
if (depthLabels[idx] !== undefined) {
|
||||
svg += `<text x="${x+nodeR+6}" y="${y+4}" text-anchor="start" fill="#94a3b8" font-size="11" font-style="italic" font-family="inherit">d=${depthLabels[idx]}</text>`;
|
||||
}
|
||||
}
|
||||
|
||||
svg += '</svg>';
|
||||
return `<div class="tree-container">${svg}</div>`;
|
||||
}
|
||||
|
||||
/* ========== 遍历索引辅助 ========== */
|
||||
function inorderIndices(root) {
|
||||
const r = [];
|
||||
(function dfs(n){ if(!n)return; dfs(n.left); r.push(n.idx); dfs(n.right); })(root);
|
||||
return r;
|
||||
}
|
||||
function preorderIndices(root) {
|
||||
const r = [];
|
||||
(function dfs(n){ if(!n)return; r.push(n.idx); dfs(n.left); dfs(n.right); })(root);
|
||||
return r;
|
||||
}
|
||||
function postorderIndices(root) {
|
||||
const r = [];
|
||||
(function dfs(n){ if(!n)return; dfs(n.left); dfs(n.right); r.push(n.idx); })(root);
|
||||
return r;
|
||||
}
|
||||
function levelOrderIndices(root) {
|
||||
if (!root) return [];
|
||||
const result = [], queue = [root];
|
||||
while (queue.length) {
|
||||
const level = [], sz = queue.length;
|
||||
for (let i = 0; i < sz; i++) {
|
||||
const nd = queue.shift();
|
||||
level.push(nd.idx);
|
||||
if (nd.left) queue.push(nd.left);
|
||||
if (nd.right) queue.push(nd.right);
|
||||
}
|
||||
result.push(level);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* ========== 收集所有节点(BFS序) ========== */
|
||||
function allNodesBFS(root) {
|
||||
if (!root) return [];
|
||||
const r = [], queue = [root];
|
||||
while (queue.length) {
|
||||
const nd = queue.shift();
|
||||
r.push(nd);
|
||||
if (nd.left) queue.push(nd.left);
|
||||
if (nd.right) queue.push(nd.right);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/* ========== 解析输入字符串 ========== */
|
||||
function parseInputTree(str) {
|
||||
try {
|
||||
const arr = JSON.parse(str.replace(/[()[\]{}]/g, m => m === '[' || m === ']' ? m : ''));
|
||||
return arr;
|
||||
} catch(e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/* ========== 全局导出 ========== */
|
||||
window.parseTreeArray = parseTreeArray;
|
||||
window.treeDepth = treeDepth;
|
||||
window.renderTreeSVG = renderTreeSVG;
|
||||
window.inorderIndices = inorderIndices;
|
||||
window.preorderIndices = preorderIndices;
|
||||
window.postorderIndices = postorderIndices;
|
||||
window.levelOrderIndices = levelOrderIndices;
|
||||
window.allNodesBFS = allNodesBFS;
|
||||
window.parseInputTree = parseInputTree;
|
||||
Reference in New Issue
Block a user